Skip to content

Commit e22fff4

Browse files
authored
migrate fullchain IT worker array no bad actors (#158)
2 parents a803fbd + 2f18557 commit e22fff4

File tree

6 files changed

+130
-10
lines changed

6 files changed

+130
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Migrate integration test files to Typescript & Hardhat:
66
- 000_fullchain.js (#156, #157)
7+
- 00X_fullchain-Xworkers.js (#158)
78
- Remove `smock` from unit tests:
89
- IexecEscrow.v8 (#154, #155)
910
- IexecPocoDelegate (#149, #151)

test/000_fullchain.test.ts

Lines changed: 129 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@ import {
1616
} from '../utils/poco-tools';
1717
import { IexecWrapper } from './utils/IexecWrapper';
1818

19-
// +---------+-------------+-------------+-------------+----------+-----+----------------+
20-
// | | Sponsorship | Replication | Beneficiary | Callback | BoT | Type |
21-
// +---------+-------------+-------------+-------------+----------+-----+----------------+
22-
// | [1] | ✔ | ✔ | ✔ | ✔ | ✔ | Standard |
23-
// | [2] | x | ✔ | ✔ | ✔ | ✔ | Standard |
24-
// | [3] | ✔ | x | ✔ | ✔ | ✔ | Standard,TEE |
25-
// | [4] | x | x | ✔ | ✔ | ✔ | Standard,TEE |
26-
// | [5] | x | x | x | x | x | Standard,TEE |
27-
// +---------+-------------+-------------+----------+-----+-------------+----------------+
19+
// +---------+-------------+-------------+-------------+----------+-----+---------------------------------+
20+
// | | Sponsorship | Replication | Beneficiary | Callback | BoT | Type |
21+
// +---------+-------------+-------------+-------------+----------+-----+---------------------------------+
22+
// | [1] | ✔ | ✔ | ✔ | ✔ | ✔ | Standard |
23+
// | [2] | x | ✔ | ✔ | ✔ | ✔ | Standard |
24+
// | [3] | ✔ | x | ✔ | ✔ | ✔ | Standard,TEE |
25+
// | [4] | x | x | ✔ | ✔ | ✔ | Standard,TEE |
26+
// | [5] | x | x | x | x | x | Standard,TEE |
27+
// | [6.x] | x | ✔ | x | x | x | Standard,TEE, X good workers |
28+
// +---------+-------------+-------------+-------------+----------+-----+---------------------------------+
2829

2930
const standardDealTag = '0x0000000000000000000000000000000000000000000000000000000000000000';
3031
const teeDealTag = '0x0000000000000000000000000000000000000000000000000000000000000001';
3132
const appPrice = 1000;
3233
const datasetPrice = 1_000_000;
3334
const workerpoolPrice = 1_000_000_000;
3435
const callbackAddress = ethers.Wallet.createRandom().address;
35-
const { results } = buildUtf8ResultAndDigest('result');
36+
const { results, resultDigest } = buildUtf8ResultAndDigest('result');
3637
const { resultsCallback, callbackResultDigest } = buildResultCallbackAndDigest(123);
3738

3839
let proxyAddress: string;
@@ -48,6 +49,10 @@ let [
4849
scheduler,
4950
anyone,
5051
worker1,
52+
worker2,
53+
worker3,
54+
worker4,
55+
worker5,
5156
]: SignerWithAddress[] = [];
5257
let ordersActors: OrdersActors;
5358
let ordersAssets: OrdersAssets;
@@ -72,6 +77,10 @@ describe('Integration tests', function () {
7277
scheduler,
7378
anyone,
7479
worker1,
80+
worker2,
81+
worker3,
82+
worker4,
83+
worker5,
7584
} = accounts);
7685
iexecWrapper = new IexecWrapper(proxyAddress, accounts);
7786
({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets());
@@ -198,6 +207,103 @@ describe('Integration tests', function () {
198207
it('[4] No sponsorship, beneficiary, callback, BoT, no replication', async function () {});
199208

200209
it('[5] No sponsorship, no beneficiary, no callback, no BoT, no replication', async function () {});
210+
211+
describe('Integration tests array of worker', function () {
212+
for (let workerNumber = 1; workerNumber < 6; workerNumber++) {
213+
it(`[6.${workerNumber}] No sponsorship, no beneficiary, no callback, no BoT, up to ${workerNumber} workers`, async function () {
214+
const volume = 1;
215+
const disposableWorkers = [worker1, worker2, worker3, worker4, worker5];
216+
const workers = disposableWorkers.slice(0, workerNumber);
217+
const acounts = [requester, scheduler, appProvider, datasetProvider, ...workers];
218+
// Create deal.
219+
const orders = buildOrders({
220+
assets: ordersAssets,
221+
prices: ordersPrices,
222+
requester: requester.address,
223+
tag: standardDealTag,
224+
beneficiary: beneficiary.address,
225+
volume,
226+
trust: workerNumber ** 2 - 1,
227+
});
228+
const { dealId, dealPrice, schedulerStakePerDeal } =
229+
await iexecWrapper.signAndMatchOrders(...orders.toArray());
230+
const taskPrice = appPrice + datasetPrice + workerpoolPrice;
231+
const schedulerStakePerTask = schedulerStakePerDeal / volume;
232+
const workerRewardPerTask = await iexecWrapper.computeWorkerRewardPerTask(
233+
dealId,
234+
PocoMode.CLASSIC,
235+
);
236+
const schedulerRewardPerTask = workerpoolPrice - workerRewardPerTask;
237+
// Check initial balances.
238+
let accountsInitBalances = [
239+
{
240+
address: proxyAddress,
241+
frozen: (await iexecPoco.frozenOf(proxyAddress)).toNumber(),
242+
},
243+
];
244+
for (const account of acounts) {
245+
let address = account.address;
246+
let frozen = (await iexecPoco.frozenOf(account.address)).toNumber();
247+
accountsInitBalances.push({ address, frozen });
248+
}
249+
for (let i = 0; i < workerNumber; i++) {
250+
expect(await iexecPoco.viewScore(workers[i].address)).to.be.equal(0);
251+
}
252+
const taskId = await iexecWrapper.initializeTask(dealId, 0);
253+
// Finalize each task and check balance changes.
254+
const workerStake = await iexecPoco
255+
.viewDeal(dealId)
256+
.then((deal) => deal.workerStake.toNumber());
257+
258+
for (let i = 0; i < workerNumber; i++) {
259+
await iexecWrapper.contributeToTask(dealId, 0, resultDigest, workers[i]);
260+
}
261+
for (let i = 0; i < workerNumber; i++) {
262+
await iexecPoco
263+
.connect(workers[i])
264+
.reveal(taskId, resultDigest)
265+
.then((tx) => tx.wait());
266+
}
267+
const finalizeTx = await iexecPoco
268+
.connect(scheduler)
269+
.finalize(taskId, results, '0x');
270+
expect(finalizeTx).to.changeTokenBalances(
271+
iexecPoco,
272+
[proxyAddress, requester, scheduler, appProvider, datasetProvider],
273+
[
274+
-(dealPrice + schedulerStakePerDeal),
275+
0,
276+
schedulerStakePerTask + schedulerRewardPerTask,
277+
appPrice,
278+
datasetPrice,
279+
],
280+
);
281+
for (let i = 0; i < workerNumber; i++) {
282+
expect(finalizeTx).to.changeTokenBalances(
283+
iexecPoco,
284+
[workers[i]],
285+
[workerStake + workerRewardPerTask / workerNumber],
286+
);
287+
}
288+
expect((await iexecPoco.viewTask(taskId)).status).to.equal(
289+
TaskStatusEnum.COMPLETED,
290+
);
291+
const expectedFrozenChanges = [0, -taskPrice, -schedulerStakePerTask, 0, 0];
292+
for (let i = 0; i < workerNumber; i++) {
293+
expectedFrozenChanges.push(0);
294+
}
295+
await changesInFrozen({
296+
accountsInitBalances,
297+
frozenChanges: expectedFrozenChanges,
298+
});
299+
for (let i = 0; i < workerNumber; i++) {
300+
if (workerNumber == 1) {
301+
expect(await iexecPoco.viewScore(workers[i].address)).to.be.equal(0);
302+
}
303+
}
304+
});
305+
}
306+
});
201307
});
202308

203309
async function checkBalancesAndFrozens(args: {
@@ -214,3 +320,16 @@ async function checkBalancesAndFrozens(args: {
214320
expect(await iexecPoco.frozenOf(account.signer.address)).to.equal(account.frozen, message);
215321
}
216322
}
323+
324+
async function changesInFrozen(args: {
325+
accountsInitBalances: { address: string; frozen: number }[];
326+
frozenChanges: number[];
327+
}) {
328+
for (let i = 0; i < args.accountsInitBalances.length; i++) {
329+
const message = `Failed with account at index ${i}`;
330+
expect(await iexecPoco.frozenOf(args.accountsInitBalances[i].address)).to.equal(
331+
args.accountsInitBalances[i].frozen + args.frozenChanges[i],
332+
message,
333+
);
334+
}
335+
}

0 commit comments

Comments
 (0)