Skip to content

Commit d7473b2

Browse files
committed
bug fix: scheduler stake rounding
1 parent c9c7a2e commit d7473b2

File tree

6 files changed

+53
-57
lines changed

6 files changed

+53
-57
lines changed

contracts/IexecAPI.sol

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@ import "rlc-token/contracts/RLC.sol";
77

88
contract IexecAPI is OwnableOZ, IexecHubAccessor, IexecCallbackInterface
99
{
10-
11-
12-
event WorkOrder(address woid);
13-
14-
event WithdrawRLCFromIexecAPI(address to,uint256 amount);
15-
16-
event ApproveIexecHub(address iexecHub,uint256 amount);
17-
event DepositRLCOnIexecHub(address iexecHub,uint256 amount);
18-
event WithdrawRLCFromIexecHub(address iexecHub,uint256 amount);
19-
20-
21-
22-
10+
event WorkOrder (address woid);
11+
event WithdrawRLCFromIexecAPI(address to, uint256 amount);
12+
event ApproveIexecHub (address iexecHub, uint256 amount);
13+
event DepositRLCOnIexecHub (address iexecHub, uint256 amount);
14+
event WithdrawRLCFromIexecHub(address iexecHub, uint256 amount);
2315

2416
// Constructor
2517
function IexecAPI(address _iexecHubAddress)
2618
IexecHubAccessor(_iexecHubAddress)
27-
public
19+
public
2820
{
2921

3022
}
@@ -39,8 +31,8 @@ contract IexecAPI is OwnableOZ, IexecHubAccessor, IexecCallbackInterface
3931
address _beneficiary)
4032
public
4133
{
42-
address woid=iexecHubInterface.buyForWorkOrder(_marketorderIdx,_workerpool,_app,_dataset,_params,_callback,_beneficiary);
43-
emit WorkOrder(woid);
34+
address woid = iexecHubInterface.buyFortWorkOrder(_marketorderIdx, _workerpool, _app, _dataset, _params, _callback, _beneficiary);
35+
emit WorkOrder(woid);
4436
}
4537

4638
function workOrderCallback(
@@ -82,7 +74,7 @@ contract IexecAPI is OwnableOZ, IexecHubAccessor, IexecCallbackInterface
8274
{
8375
require(iexecHubInterface.withdraw(amount));
8476
require(withdrawRLCFromIexecAPI(amount));
85-
emit WithdrawRLCFromIexecHub(iexecHubAddress,amount);
77+
emit WithdrawRLCFromIexecHub(iexecHubAddress, amount);
8678
return true;
8779
}
8880

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
pragma solidity ^0.4.21;
22

3-
contract IexecCallbackInterface {
4-
5-
function workOrderCallback(
6-
address _woid,
7-
string _stdout,
8-
string _stderr,
9-
string _uri) public returns (bool);
3+
contract IexecCallbackInterface
4+
{
5+
6+
function workOrderCallback(
7+
address _woid,
8+
string _stdout,
9+
string _stderr,
10+
string _uri) public returns (bool);
1011

1112
event WorkOrderCallback(address woid, string stdout, string stderr, string uri);
1213
}

contracts/IexecHub.sol

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,15 @@ contract IexecHub
300300
require(workerpool.claimFailedConsensus(_woid));
301301
workorder.claim(); // revert on error
302302

303-
uint value;
304-
address workerpoolOwner;
305-
value = marketplace.getMarketOrderValue(workorder.m_marketorderIdx());//revert if not exist
306-
workerpoolOwner = marketplace.getMarketOrderWorkerpoolOwner(workorder.m_marketorderIdx());//revert if not exist
303+
uint256 value = marketplace.getMarketOrderValue(workorder.m_marketorderIdx()); //revert if not exist
304+
address workerpoolOwner = marketplace.getMarketOrderWorkerpoolOwner(workorder.m_marketorderIdx()); //revert if not exist
305+
uint256 workerpoolStake = value.percentage(marketplace.ASK_STAKE_RATIO());
307306

308-
require(unlock(workorder.m_requester(), value.add(workorder.m_emitcost()))); // UNLOCK THE FUNDS FOR REINBURSEMENT
309-
require(seize (workerpoolOwner, value.percentage(marketplace.ASK_STAKE_RATIO()) ));
307+
require(unlock (workorder.m_requester(), value.add(workorder.m_emitcost()))); // UNLOCK THE FUNDS FOR REINBURSEMENT
308+
require(seize (workerpoolOwner, workerpoolStake));
310309
//put workerpoolOwner stake seize into iexecHub address for bonus for scheduler on next well finalized Task
311-
require(reward (this, value.percentage(marketplace.ASK_STAKE_RATIO()) ));
312-
require(lock (this, value.percentage(marketplace.ASK_STAKE_RATIO()) ));
310+
require(reward (this, workerpoolStake));
311+
require(lock (this, workerpoolStake));
313312

314313

315314
emit WorkOrderClaimed(_woid, workorder.m_workerpool());
@@ -354,24 +353,25 @@ contract IexecHub
354353
* reward = value: was locked at market making
355354
* emitcost: was locked at when emiting the workorder
356355
*/
357-
uint256 value;
358-
address workerpoolOwner;
359-
value = marketplace.getMarketOrderValue(workorder.m_marketorderIdx());//revert if not exist
360-
workerpoolOwner = marketplace.getMarketOrderWorkerpoolOwner(workorder.m_marketorderIdx());//revert if not exist
356+
uint256 value = marketplace.getMarketOrderValue(workorder.m_marketorderIdx()); //revert if not exist
357+
address workerpoolOwner = marketplace.getMarketOrderWorkerpoolOwner(workorder.m_marketorderIdx()); //revert if not exist
358+
uint256 workerpoolStake = value.percentage(marketplace.ASK_STAKE_RATIO());
361359

362360
require(seize (workorder.m_requester(), value.add(workorder.m_emitcost()))); // seize funds for payment (market value + emitcost)
363-
require(unlock(workerpoolOwner, value.percentage(marketplace.ASK_STAKE_RATIO()))); // unlock scheduler stake
361+
require(unlock(workerpoolOwner, workerpoolStake)); // unlock scheduler stake
364362

365363
// write results
366364
workorder.setResult(_stdout, _stderr, _uri); // revert on error
367365

368366
// Rien ne se perd, rien ne se crée, tout se transfere
369367
// distribute bonus to scheduler. jackpot bonus come from scheduler stake loose on IexecHub contract
370-
uint256 locked;
371-
(,locked) = checkBalance(this);
372-
if(locked > 0){
373-
require(seize(this, locked.min(locked.percentage(STAKE_BONUS_RATIO).max(STAKE_BONUS_MIN_THRESHOLD))));
374-
require(reward(workerpoolOwner,locked.min(locked.percentage(STAKE_BONUS_RATIO).max(STAKE_BONUS_MIN_THRESHOLD))));
368+
uint256 kitty;
369+
(,kitty) = checkBalance(this); // kitty is locked on `this` wallet
370+
if(kitty > 0)
371+
{
372+
uint256 kittyFraction = kitty.min(kitty.percentage(STAKE_BONUS_RATIO).max(STAKE_BONUS_MIN_THRESHOLD));
373+
require(seize(this, kittyFraction));
374+
require(reward(workerpoolOwner, kittyFraction));
375375
}
376376

377377
emit WorkOrderCompleted(_woid, workorder.m_workerpool());
@@ -387,7 +387,8 @@ contract IexecHub
387387
return m_categories[_catId].workClockTimeRef;
388388
}
389389

390-
function existingCategory(uint256 _catId) public view returns (bool categoryExist){
390+
function existingCategory(uint256 _catId) public view returns (bool categoryExist)
391+
{
391392
return m_categories[_catId].catid > 0;
392393
}
393394

@@ -441,7 +442,7 @@ contract IexecHub
441442
function unregisterFromPool(address _worker) public returns (bool unsubscribed)
442443
// msg.sender = workerPool
443444
{
444-
require(removeWorker(msg.sender,_worker));
445+
require(removeWorker(msg.sender, _worker));
445446
// Trigger event notice
446447
emit WorkerPoolUnsubscription(msg.sender, _worker);
447448
return true;
@@ -450,7 +451,7 @@ contract IexecHub
450451
function evictWorker(address _worker) public returns (bool unsubscribed)
451452
// msg.sender = workerpool
452453
{
453-
require(removeWorker(msg.sender,_worker));
454+
require(removeWorker(msg.sender, _worker));
454455
// Trigger event notice
455456
emit WorkerPoolEviction(msg.sender, _worker);
456457
return true;

contracts/Marketplace.sol

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ contract Marketplace is IexecHubAccessor
6060
{
6161
require(WorkerPool(_workerpool).m_owner() == msg.sender);
6262

63-
require(iexecHubInterface.lockForOrder(msg.sender, _value.mul(_volume).percentage(ASK_STAKE_RATIO)));
63+
require(iexecHubInterface.lockForOrder(msg.sender, _value.percentage(ASK_STAKE_RATIO).mul(_volume))); // mul must be done after percentage to avoid rounding errors
6464
marketorder.workerpool = _workerpool;
6565
marketorder.workerpoolOwner = msg.sender;
6666
}
@@ -79,7 +79,7 @@ contract Marketplace is IexecHubAccessor
7979
if (marketorder.direction == IexecLib.MarketOrderDirectionEnum.ASK)
8080
{
8181
require(marketorder.workerpoolOwner == msg.sender);
82-
require(iexecHubInterface.unlockForOrder(marketorder.workerpoolOwner, marketorder.value.mul(marketorder.remaining).percentage(ASK_STAKE_RATIO)));
82+
require(iexecHubInterface.unlockForOrder(marketorder.workerpoolOwner, marketorder.value.percentage(ASK_STAKE_RATIO).mul(marketorder.remaining))); // mul must be done after percentage to avoid rounding errors
8383
}
8484
else
8585
{
@@ -116,7 +116,8 @@ contract Marketplace is IexecHubAccessor
116116
return true;
117117
}
118118

119-
function existingMarketOrder(uint256 _marketorderIdx) public view returns (bool marketOrderExist){
119+
function existingMarketOrder(uint256 _marketorderIdx) public view returns (bool marketOrderExist)
120+
{
120121
return m_orderBook[_marketorderIdx].category > 0;
121122
}
122123

contracts/WorkerPool.sol

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor
160160
m_workers [index ] = lastWorker;
161161
m_workerIndex[lastWorker] = index;
162162
delete m_workers[m_workers.length.sub(1)];
163-
m_workers.length=m_workers.length.sub(1);
163+
m_workers.length = m_workers.length.sub(1);
164164
return true;
165165
}
166166

@@ -197,11 +197,12 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor
197197
return m_consensus[_woid].contributors[index];
198198
}
199199

200-
function existingContribution(address _woid,address _worker) public view returns (bool contributionExist){
200+
function existingContribution(address _woid, address _worker) public view returns (bool contributionExist)
201+
{
201202
return m_contributions[_woid][_worker].status != IexecLib.ContributionStatusEnum.UNSET;
202203
}
203204

204-
function getContribution(address _woid,address _worker) public view returns
205+
function getContribution(address _woid, address _worker) public view returns
205206
(
206207
IexecLib.ContributionStatusEnum status,
207208
bytes32 resultHash,
@@ -210,7 +211,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor
210211
uint256 score,
211212
uint256 weight)
212213
{
213-
require(existingContribution(_woid,_worker));//no silent value returned
214+
require(existingContribution(_woid, _worker)); //no silent value returned
214215
IexecLib.Contribution storage contribution = m_contributions[_woid][_worker];
215216
return (
216217
contribution.status,
@@ -250,7 +251,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor
250251
w = consensus.contributors[i];
251252
if (m_contributions[_woid][w].status != IexecLib.ContributionStatusEnum.AUTHORIZED)
252253
{
253-
require(iexecHubInterface.unlockForWork(_woid, w, consensus.stakeAmount));
254+
require(iexecHubInterface.unlockForWork(_woid, w, consensus.stakeAmount));
254255
}
255256
}
256257
return true;
@@ -315,7 +316,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor
315316
function revealConsensus(address _woid, bytes32 _consensus) public onlyOwner /*onlySheduler*/ returns (bool)
316317
{
317318
IexecLib.Consensus storage consensus = m_consensus[_woid];
318-
require(now <= consensus.consensusTimout);
319+
require(now <= consensus.consensusTimout);
319320
require(iexecHubInterface.startRevealingPhase(_woid));
320321

321322
consensus.winnerCount = 0;
@@ -442,7 +443,7 @@ contract WorkerPool is OwnableOZ, IexecHubAccessor, MarketplaceAccessor
442443
}
443444
}
444445
// totalReward now contains the scheduler share
445-
require(iexecHubInterface.rewardForWork(_woid,_consensus.workerpoolOwner, totalReward, false));
446+
require(iexecHubInterface.rewardForWork(_woid, _consensus.workerpoolOwner, totalReward, false));
446447

447448
return true;
448449
}

test/byFunctions/Marketplace/createMarketOrder.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ contract('IexecHub', function(accounts) {
564564
assert.strictEqual(workerpoolOwner, scheduleProvider, "check workerpoolOwner");
565565

566566
checkBalance = await aIexecHubInstance.checkBalance.call(scheduleProvider);
567-
assert.strictEqual(checkBalance[0].toNumber(), 140, "check stake of the scheduleProvider");
568-
assert.strictEqual(checkBalance[1].toNumber(), 60, "check stake locked of the scheduleProvider. 25 * 4 * 0.3 = 30 . previous 30 +30 =60");
567+
assert.strictEqual(checkBalance[0].toNumber(), 142, "check stake of the scheduleProvider");
568+
assert.strictEqual(checkBalance[1].toNumber(), 58, "check stake locked of the scheduleProvider. floor(25 * 0.3) * 4 = 28 . previous 30 + 28 = 58");
569569

570570

571571

0 commit comments

Comments
 (0)