Skip to content

Commit

Permalink
locking worker reputation (score) at contribution time
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Mar 7, 2018
1 parent cfc163b commit 025027c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions contracts/IexecLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ library IexecLib
bytes32 resultHash;
bytes32 resultSign; // change from salt to tx.origin based signature
address enclaveChallenge;
uint256 score;
uint256 weight;
}

Expand Down
11 changes: 5 additions & 6 deletions contracts/Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ contract Marketplace is IexecHubAccessor
}
// marketorderIdx => user => workerpool => quantity
m_assetBook[_marketorderIdx][marketorder.requester][_workerpool] = m_assetBook[_marketorderIdx][marketorder.requester][_workerpool].add(_quantity);
// TODO: create event
MarketOrderBidAnswered(_marketorderIdx,marketorder.requester,_workerpool,_quantity);
MarketOrderBidAnswered(_marketorderIdx, marketorder.requester, _workerpool, _quantity);
return _quantity;
}
function answerAskOrder(uint _marketorderIdx, uint256 _quantity) public returns (uint256)
Expand All @@ -150,7 +149,7 @@ contract Marketplace is IexecHubAccessor
require(iexecHubInterface.lockDepositForOrder(msg.sender, marketorder.value.mul(_quantity)));
// marketorderIdx => user => workerpool => quantity
m_assetBook[_marketorderIdx][msg.sender][marketorder.workerpool] = m_assetBook[_marketorderIdx][msg.sender][marketorder.workerpool].add(_quantity);
MarketOrderAskAnswered(_marketorderIdx, msg.sender, marketorder.workerpool,_quantity);
MarketOrderAskAnswered(_marketorderIdx, msg.sender, marketorder.workerpool, _quantity);
return _quantity;
}
*/
Expand All @@ -165,10 +164,10 @@ contract Marketplace is IexecHubAccessor
public onlyIexecHub returns (bool)
{
IexecLib.MarketOrder storage marketorder = m_orderBook[_marketorderIdx];
require(marketorder.direction == IexecLib.MarketOrderDirectionEnum.ASK);
require(marketorder.direction == IexecLib.MarketOrderDirectionEnum.ASK);
require(marketorder.remaining > 0);
require(marketorder.workerpool == _workerpool);
/* require(marketorder.marketDeadline > now); // assetDeadline > marketdeadline > now */
require(marketorder.remaining > 0);
require(marketorder.workerpool == _workerpool);

marketorder.remaining = marketorder.remaining.sub(1);
if (marketorder.remaining == 0)
Expand Down
29 changes: 16 additions & 13 deletions contracts/WorkerPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
* Members
*/
WorkerPoolStatusEnum public m_workerPoolStatus;
string public m_name;
string public m_description;
uint256 public m_stakeRatioPolicy; // % of reward to stake
uint256 public m_schedulerRewardRatioPolicy; // % of reward given to scheduler
uint256 public m_subscriptionLockStakePolicy; // Stake locked when in workerpool - Constant set by constructor, do not update
Expand Down Expand Up @@ -77,7 +77,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
// Constructor
function WorkerPool(
address _iexecHubAddress,
string _name,
string _description,
uint256 _subscriptionLockStakePolicy,
uint256 _subscriptionMinimumStakePolicy,
uint256 _subscriptionMinimumScorePolicy,
Expand All @@ -91,7 +91,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
require(tx.origin != msg.sender);
transferOwnership(tx.origin); // owner → tx.origin

m_name = _name;
m_description = _description;
m_stakeRatioPolicy = 30; // % of the work order price to stake
m_schedulerRewardRatioPolicy = 1; // % of the work reward going to scheduler vs workers reward
m_subscriptionLockStakePolicy = _subscriptionLockStakePolicy; // only at creation. cannot be change to respect lock/unlock of worker stake
Expand Down Expand Up @@ -165,7 +165,8 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
return m_workers.length;
}

function subscribeToPool() public returns (bool){
function subscribeToPool() public returns (bool)
{
//tx.origin = worker
require(iexecHubInterface.subscribeToPool());
uint index = m_workers.push(tx.origin);
Expand Down Expand Up @@ -320,6 +321,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
contribution.status = IexecLib.ContributionStatusEnum.CONTRIBUTED;
contribution.resultHash = _resultHash;
contribution.resultSign = _resultSign;
(,contribution.score) = iexecHubInterface.getWorkerStatus(msg.sender);
consensus.contributors.push(msg.sender);

require(iexecHubInterface.lockForWork(_woid, msg.sender, consensus.stakeAmount));
Expand Down Expand Up @@ -407,6 +409,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
{
uint256 i;
address w;
IexecLib.Contribution storage c;
/**
* Reward distribution:
* totalReward is to be distributed amoung the winners relative to their
Expand All @@ -418,7 +421,6 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
* → https://ethereum.stackexchange.com/questions/8086/logarithm-math-operation-in-solidity#8110
*/
uint256 workerBonus;
uint256 workerScore;
uint256 workerWeight;
uint256 totalWeight;
uint256 workerReward;
Expand All @@ -427,13 +429,13 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
for (i = 0; i<contributors.length; ++i)
{
w = contributors[i];
if (m_contributions[_woid][w].status == IexecLib.ContributionStatusEnum.PROVED)
c = m_contributions[_woid][w];
if (c.status == IexecLib.ContributionStatusEnum.PROVED)
{
workerBonus = (m_contributions[_woid][w].enclaveChallenge != address(0)) ? 3 : 1; // TODO: bonus sgx = 3 ?
(,workerScore) = iexecHubInterface.getWorkerStatus(w);
workerWeight = 1 + workerScore.mul(workerBonus).log2();
totalWeight = totalWeight.add(workerWeight);
m_contributions[_woid][w].weight = workerWeight; // store so we don't have to recompute
workerBonus = (c.enclaveChallenge != address(0)) ? 3 : 1; // TODO: bonus sgx = 3 ?
workerWeight = 1 + c.score.mul(workerBonus).log2();
totalWeight = totalWeight.add(workerWeight);
c.weight = workerWeight; // store so we don't have to recompute
}
else // ContributionStatusEnum.REJECT or ContributionStatusEnum.CONTRIBUTED (not revealed)
{
Expand All @@ -448,9 +450,10 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor // Owned
for (i = 0; i<contributors.length; ++i)
{
w = contributors[i];
if (m_contributions[_woid][w].status == IexecLib.ContributionStatusEnum.PROVED)
c = m_contributions[_woid][w];
if (c.status == IexecLib.ContributionStatusEnum.PROVED)
{
workerReward = workersReward.mulByFraction(m_contributions[_woid][w].weight, totalWeight);
workerReward = workersReward.mulByFraction(c.weight, totalWeight);
totalReward = totalReward.sub(workerReward);
require(iexecHubInterface.unlockForWork(_woid, w, _consensus.stakeAmount));
require(iexecHubInterface.rewardForWork(_woid, w, workerReward, true));
Expand Down

0 comments on commit 025027c

Please sign in to comment.