Skip to content

Commit 5ef0221

Browse files
authored
Migrate 000_fullchain-ABILegacy.js file to Hardhat/TS (#174)
2 parents ae5ead6 + 7d63e4d commit 5ef0221

File tree

3 files changed

+211
-20
lines changed

3 files changed

+211
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- 201_fullchain-bot-dualPool.js (#171, #172)
1919
- Fix balance checks in integration tests (#165)
2020
- 300_fullchain-reopen.js (#170, #173)
21+
- 000_fullchain-ABILegacy.js (#174)
2122
- Remove `smock` from unit tests:
2223
- IexecEscrow.v8 (#154, #155)
2324
- IexecPocoDelegate (#149, #151)

test/300_fullchain-reopen.test.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@ describe('Integration tests', function () {
8787
};
8888
}
8989

90-
/*
91-
This test simulates the full lifecycle of a task in iExec:
92-
- Creates a deal with specific orders and initializes a task.
93-
- Tests worker contributions:
94-
- The first group of workers contributes, triggering the reveal phase.
95-
- Task is reopened after the reveal deadline passes.
96-
- Ensures that workers who already contributed cannot contribute again.
97-
- The second group of workers contributes and reveals successfully.
98-
- Finalizes the task, distributing rewards among workers and the scheduler.
99-
- Validates token balance changes for all participants.
100-
- Verifies that winning workers receive a positive score, while losing workers do not.
101-
*/
90+
/**
91+
* This test checks the full lifecycle of a task with reveal deadline reached
92+
* and reopen operation taking place:
93+
* - Create a deal with specific orders and initialize a task.
94+
* - Test worker contributions:
95+
* - The first group of workers contributes, triggering the reveal phase.
96+
* - The task is reopened after the reveal deadline is reached.
97+
* - Ensure that workers who already contributed cannot contribute again.
98+
* - The second group of workers contributes and reveals successfully.
99+
* - Finalize the task, distributing rewards among workers and the scheduler.
100+
* - Validate token balance changes for all participants.
101+
* - Verify that winning workers receive a positive score, while losing workers do not.
102+
*/
102103
it(`[1] Task lifecycle with contributions and reopening`, async function () {
103104
const volume = 1;
104105
const workers = [worker1, worker2, worker3, worker4];
@@ -122,8 +123,8 @@ describe('Integration tests', function () {
122123
const schedulerStakePerTask = schedulerStakePerDeal / volume;
123124
const accountsInitialFrozens = await iexecWrapper.getInitialFrozens(accounts);
124125

125-
for (let i = 0; i < 4; i++) {
126-
expect(await iexecPoco.viewScore(workers[i].address)).to.be.equal(0);
126+
for (const worker of workers) {
127+
expect(await iexecPoco.viewScore(worker.address)).to.be.equal(0);
127128
}
128129
const taskId = await iexecWrapper.initializeTask(dealId, 0);
129130
const workerStakePerTask = await iexecPoco
@@ -134,13 +135,13 @@ describe('Integration tests', function () {
134135
}
135136
const task = await iexecPoco.viewTask(taskId);
136137
expect(task.status).to.equal(TaskStatusEnum.REVEALING);
138+
// Time travel post reveal deadline and reopen task.
137139
await setNextBlockTimestamp(task.revealDeadline).then(() => mine());
138140
await expect(iexecPocoAsScheduler.reopen(taskId))
139141
.to.emit(iexecPoco, 'TaskReopen')
140142
.withArgs(taskId);
141143
expect((await iexecPoco.viewTask(taskId)).status).to.equal(TaskStatusEnum.ACTIVE);
142-
143-
// test that the already contributed workers can't contribute anymore
144+
// Check that the already contributed workers can't contribute anymore
144145
for (const contributor of firstContributors) {
145146
const { resultHash, resultSeal } = buildResultHashAndResultSeal(
146147
taskId,
@@ -153,6 +154,7 @@ describe('Integration tests', function () {
153154
AddressZero,
154155
scheduler,
155156
);
157+
await iexecWrapper.depositInIexecAccount(contributor, workerStakePerTask); // Not a balance related revert.
156158
await expect(
157159
iexecPoco
158160
.connect(contributor)
@@ -166,7 +168,7 @@ describe('Integration tests', function () {
166168
),
167169
).to.revertedWithoutReason();
168170
}
169-
171+
// Contribute and reveal with new workers.
170172
for (const contributor of secondContributors) {
171173
await iexecWrapper.contributeToTask(dealId, 0, resultDigest, contributor);
172174
}
@@ -178,8 +180,9 @@ describe('Integration tests', function () {
178180
}
179181
const finalizeTx = await iexecPocoAsScheduler.finalize(taskId, results, '0x');
180182
await finalizeTx.wait();
183+
// Bad workers lose their stake and add it to the pool price
181184
const totalWorkerPoolReward =
182-
workerpoolPrice + workerStakePerTask * firstContributors.length; // bad workers lose their stake and add it to the pool price
185+
workerpoolPrice + workerStakePerTask * firstContributors.length;
183186

184187
const workersRewardPerTask = await iexecWrapper.computeWorkersRewardForCurrentTask(
185188
totalWorkerPoolReward,
@@ -219,11 +222,11 @@ describe('Integration tests', function () {
219222
];
220223
await iexecWrapper.checkFrozenChanges(accountsInitialFrozens, expectedFrozenChanges);
221224

222-
// checks on losing worker
225+
// Check losing workers scores.
223226
for (const contributor of firstContributors) {
224227
expect(await iexecPoco.viewScore(contributor.address)).to.be.equal(0);
225228
}
226-
// checks on winning workers
229+
// Check winning workers scores.
227230
for (const contributor of secondContributors) {
228231
expect(await iexecPoco.viewScore(contributor.address)).to.be.equal(1);
229232
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
// SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH <[email protected]>
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { AddressZero } from '@ethersproject/constants';
5+
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
6+
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
7+
import { ethers, expect } from 'hardhat';
8+
import { loadHardhatFixtureDeployment } from '../../../scripts/hardhat-fixture-deployer';
9+
import {
10+
IexecInterfaceNative,
11+
IexecInterfaceNativeABILegacy,
12+
IexecInterfaceNativeABILegacy__factory,
13+
IexecInterfaceNative__factory,
14+
} from '../../../typechain';
15+
import { OrdersActors, OrdersAssets, OrdersPrices, buildOrders } from '../../../utils/createOrders';
16+
import {
17+
ContributionStatusEnum,
18+
TaskStatusEnum,
19+
buildResultHashAndResultSeal,
20+
buildUtf8ResultAndDigest,
21+
getIexecAccounts,
22+
} from '../../../utils/poco-tools';
23+
import { IexecWrapper } from '../../utils/IexecWrapper';
24+
25+
const standardDealTag = '0x0000000000000000000000000000000000000000000000000000000000000000';
26+
const volume = 1;
27+
const trust = 1;
28+
const categoryId = 1;
29+
const appPrice = 1000;
30+
const datasetPrice = 1_000_000;
31+
const workerpoolPrice = 1_000_000_000;
32+
const callbackAddress = ethers.Wallet.createRandom().address;
33+
const dealParams = 'params';
34+
const { resultDigest } = buildUtf8ResultAndDigest('result');
35+
const taskIndex = 0;
36+
37+
let proxyAddress: string;
38+
let iexecPoco: IexecInterfaceNative;
39+
let iexecPocoABILegacy: IexecInterfaceNativeABILegacy;
40+
let iexecWrapper: IexecWrapper;
41+
let [appAddress, workerpoolAddress, datasetAddress]: string[] = [];
42+
let [
43+
requester,
44+
beneficiary,
45+
appProvider,
46+
datasetProvider,
47+
scheduler,
48+
anyone,
49+
worker1,
50+
]: SignerWithAddress[] = [];
51+
let ordersActors: OrdersActors;
52+
let ordersAssets: OrdersAssets;
53+
let ordersPrices: OrdersPrices;
54+
let [dealId, taskId, resultHash, resultSeal]: string[] = [];
55+
56+
describe('IexecAccessorsABILegacy', function () {
57+
beforeEach('Deploy', async () => {
58+
// Deploy all contracts
59+
proxyAddress = await loadHardhatFixtureDeployment();
60+
// Initialize test environment
61+
await loadFixture(initFixture);
62+
});
63+
64+
async function initFixture() {
65+
const accounts = await getIexecAccounts();
66+
({ requester, beneficiary, appProvider, datasetProvider, scheduler, anyone, worker1 } =
67+
accounts);
68+
iexecWrapper = new IexecWrapper(proxyAddress, accounts);
69+
({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets());
70+
iexecPoco = IexecInterfaceNative__factory.connect(proxyAddress, anyone);
71+
iexecPocoABILegacy = IexecInterfaceNativeABILegacy__factory.connect(proxyAddress, anyone);
72+
ordersActors = {
73+
appOwner: appProvider,
74+
datasetOwner: datasetProvider,
75+
workerpoolOwner: scheduler,
76+
requester: requester,
77+
};
78+
ordersAssets = {
79+
app: appAddress,
80+
dataset: datasetAddress,
81+
workerpool: workerpoolAddress,
82+
};
83+
ordersPrices = {
84+
app: appPrice,
85+
dataset: datasetPrice,
86+
workerpool: workerpoolPrice,
87+
};
88+
// Create deal and finalize a task.
89+
const orders = buildOrders({
90+
assets: ordersAssets,
91+
prices: ordersPrices,
92+
requester: requester.address,
93+
tag: standardDealTag,
94+
beneficiary: beneficiary.address,
95+
callback: callbackAddress,
96+
volume,
97+
trust,
98+
category: categoryId,
99+
params: dealParams,
100+
});
101+
({ dealId } = await iexecWrapper.signAndMatchOrders(...orders.toArray()));
102+
taskId = await iexecWrapper.initializeTask(dealId, taskIndex);
103+
({ resultHash, resultSeal } = buildResultHashAndResultSeal(taskId, resultDigest, worker1));
104+
await iexecWrapper.contributeToTask(dealId, taskIndex, resultDigest, worker1);
105+
}
106+
107+
it('[ABILegacy] Should return deal part1', async function () {
108+
const dealPart1 = await iexecPocoABILegacy.viewDealABILegacy_pt1(dealId);
109+
expect(dealPart1.length).to.equal(9);
110+
expect(dealPart1[0]).to.equal(appAddress);
111+
expect(dealPart1[1]).to.equal(appProvider.address);
112+
expect(dealPart1[2]).to.equal(appPrice);
113+
expect(dealPart1[3]).to.equal(datasetAddress);
114+
expect(dealPart1[4]).to.equal(datasetProvider.address);
115+
expect(dealPart1[5]).to.equal(datasetPrice);
116+
expect(dealPart1[6]).to.equal(workerpoolAddress);
117+
expect(dealPart1[7]).to.equal(scheduler.address);
118+
expect(dealPart1[8]).to.equal(workerpoolPrice);
119+
});
120+
121+
it('[ABILegacy] Should return deal part2', async function () {
122+
const dealPart2 = await iexecPocoABILegacy.viewDealABILegacy_pt2(dealId);
123+
expect(dealPart2.length).to.equal(6);
124+
expect(dealPart2[0]).to.equal(trust);
125+
expect(dealPart2[1]).to.equal(standardDealTag);
126+
expect(dealPart2[2]).to.equal(requester.address);
127+
expect(dealPart2[3]).to.equal(beneficiary.address);
128+
expect(dealPart2[4]).to.equal(callbackAddress);
129+
expect(dealPart2[5]).to.equal(dealParams);
130+
});
131+
132+
it('[ABILegacy] Should return deal config', async function () {
133+
const dealConfig = await iexecPocoABILegacy.viewConfigABILegacy(dealId);
134+
expect(dealConfig.length).to.equal(6);
135+
expect(dealConfig[0]).to.equal(categoryId);
136+
expect(dealConfig[1]).to.be.greaterThan(0); // startTime
137+
expect(dealConfig[2]).to.equal(taskIndex); // botFirst
138+
expect(dealConfig[3]).to.equal(volume); // botSize
139+
expect(dealConfig[4]).to.be.greaterThan(0); // workerStake
140+
expect(dealConfig[5]).to.be.greaterThan(0); // schedulerRewardRatio
141+
});
142+
143+
it('[ABILegacy] Should return account', async function () {
144+
const balanceAmount = 3;
145+
await iexecWrapper.depositInIexecAccount(requester, balanceAmount);
146+
const account = await iexecPocoABILegacy.viewAccountABILegacy(requester.address);
147+
expect(account.length).to.equal(2);
148+
expect(account[0]).to.equal(balanceAmount); // Balance
149+
expect(account[1]).to.be.greaterThan(0); // Frozen
150+
});
151+
152+
it('[ABILegacy] Should return task', async function () {
153+
const task = await iexecPocoABILegacy.viewTaskABILegacy(taskId);
154+
expect(task.length).to.equal(12);
155+
expect(task[0]).to.equal(TaskStatusEnum.REVEALING);
156+
expect(task[1]).to.equal(dealId);
157+
expect(task[2]).to.equal(taskIndex);
158+
expect(task[3]).to.be.greaterThan(0); // timeref
159+
expect(task[4]).to.be.greaterThan(0); // contributionDeadline
160+
expect(task[5]).to.be.greaterThan(0); // revealDeadline
161+
expect(task[6]).to.be.greaterThan(0); // finalDeadline
162+
expect(task[7]).to.equal(resultHash); // consensusValue
163+
expect(task[8]).to.equal(0); // revealCounter
164+
expect(task[9]).to.equal(1); // winnerCounter
165+
expect(task[10][0]).to.equal(worker1.address); // contributors
166+
expect(task[11]).to.equal('0x');
167+
});
168+
169+
it('[ABILegacy] Should return contribution', async function () {
170+
const contribution = await iexecPocoABILegacy.viewContributionABILegacy(
171+
taskId,
172+
worker1.address,
173+
);
174+
expect(contribution.length).to.equal(4);
175+
expect(contribution[0]).to.equal(ContributionStatusEnum.CONTRIBUTED);
176+
expect(contribution[1]).to.equal(resultHash);
177+
expect(contribution[2]).to.equal(resultSeal);
178+
expect(contribution[3]).to.equal(AddressZero); // enclaveChallenge
179+
});
180+
181+
it('[ABILegacy] Should return category', async function () {
182+
const category = await iexecPocoABILegacy.viewCategoryABILegacy(1);
183+
expect(category[0]).to.equal('S');
184+
expect(category[1]).to.equal('{}');
185+
expect(category[2]).to.equal(1200);
186+
});
187+
});

0 commit comments

Comments
 (0)