From cb59a612e557d69a5e7f4869258c57387dec0d99 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Wed, 23 Oct 2024 15:09:27 +0200 Subject: [PATCH 1/9] Move sponsoring test from IexecPocoDelegate.test.ts --- test/byContract/IexecPoco/IexecPoco1.test.ts | 47 +++++++++++++++---- ...test.ts => IexecPocoDelegate.test.ts.skip} | 0 2 files changed, 38 insertions(+), 9 deletions(-) rename test/byContract/IexecPoco/{IexecPocoDelegate.test.ts => IexecPocoDelegate.test.ts.skip} (100%) diff --git a/test/byContract/IexecPoco/IexecPoco1.test.ts b/test/byContract/IexecPoco/IexecPoco1.test.ts index 1d91ea497..93ce7a2bd 100644 --- a/test/byContract/IexecPoco/IexecPoco1.test.ts +++ b/test/byContract/IexecPoco/IexecPoco1.test.ts @@ -14,6 +14,7 @@ import { IexecInterfaceNative, IexecInterfaceNative__factory, IexecLibOrders_v5, + IexecPocoAccessors, IexecPocoAccessors__factory, OwnableMock, OwnableMock__factory, @@ -62,6 +63,9 @@ describe('IexecPoco1', () => { let proxyAddress: string; let [iexecPoco, iexecPocoAsRequester]: IexecInterfaceNative[] = []; let iexecPocoAsSponsor: IexecPoco1; // Sponsor function not available yet in IexecInterfaceNative. + // TODO use iexecPoco when IexecInterfaceNative is up-to-date + // and contains the function `computeDealVolume`. + let iexecPocoAccessors: IexecPocoAccessors; let iexecPocoContract: Contract; let iexecWrapper: IexecWrapper; let [appAddress, datasetAddress, workerpoolAddress]: string[] = []; @@ -109,6 +113,7 @@ describe('IexecPoco1', () => { iexecPoco = IexecInterfaceNative__factory.connect(proxyAddress, anyone); iexecPocoAsRequester = iexecPoco.connect(requester); iexecPocoAsSponsor = IexecPoco1__factory.connect(proxyAddress, sponsor); + iexecPocoAccessors = IexecPocoAccessors__factory.connect(proxyAddress, anyone); iexecPocoContract = iexecPoco as Contract; ordersActors = { appOwner: appProvider, @@ -389,9 +394,7 @@ describe('IexecPoco1', () => { iexecWrapper.hashOrders(fullConfigOrders); const dealId = getDealId(iexecWrapper.getDomain(), fullConfigOrders.requester); expect( - await IexecPocoAccessors__factory.connect(proxyAddress, anyone).computeDealVolume( - ...fullConfigOrders.toArray(), - ), + await iexecPocoAccessors.computeDealVolume(...fullConfigOrders.toArray()), ).to.equal(botVolume); expect(await iexecPoco.callStatic.matchOrders(...fullConfigOrders.toArray())).to.equal( @@ -992,13 +995,22 @@ describe('IexecPoco1', () => { describe('Sponsor match orders', () => { it('Should sponsor match orders', async () => { + const domain = iexecWrapper.getDomain(); + const { appOrder, datasetOrder, workerpoolOrder, requestOrder } = orders.toObject(); + // override volumes and set different value for each order + // to make sure the smallest volume is considered. + const expectedVolume = 2; + appOrder.volume = 2; // smallest unconsumed volume among all orders + datasetOrder.volume = 3; + workerpoolOrder.volume = 4; + requestOrder.volume = 5; // Compute prices, stakes, rewards, ... const dealPrice = (appPrice + datasetPrice + workerpoolPrice) * // task price - volume; + expectedVolume; const schedulerStake = await iexecWrapper.computeSchedulerDealStake( workerpoolPrice, - volume, + expectedVolume, ); // Deposit required amounts. await iexecWrapper.depositInIexecAccount(sponsor, dealPrice); @@ -1006,8 +1018,14 @@ describe('IexecPoco1', () => { // Save frozen balances before match. const sponsorFrozenBefore = (await iexecPoco.frozenOf(sponsor.address)).toNumber(); // Sign and match orders. - await signOrders(iexecWrapper.getDomain(), orders, ordersActors); - const dealId = getDealId(iexecWrapper.getDomain(), orders.requester); + await signOrders(domain, orders, ordersActors); + const dealId = getDealId(domain, orders.requester); + expect( + await iexecPocoAsSponsor.callStatic.sponsorMatchOrders(...orders.toArray()), + ).to.equal(dealId); + expect( + await iexecPocoAccessors.callStatic.computeDealVolume(...orders.toArray()), + ).to.equal(expectedVolume); const tx = iexecPocoAsSponsor.sponsorMatchOrders(...orders.toArray()); // Check balances and frozen. await expect(tx).to.changeTokenBalances( @@ -1020,7 +1038,18 @@ describe('IexecPoco1', () => { sponsorFrozenBefore + dealPrice, ); // Check events. - await expect(tx).to.emit(iexecPoco, 'OrdersMatched'); + await expect(tx) + .to.emit(iexecPoco, 'OrdersMatched') + .withArgs( + dealId, + hashOrder(domain, appOrder), + hashOrder(domain, datasetOrder), + hashOrder(domain, workerpoolOrder), + hashOrder(domain, requestOrder), + expectedVolume, + ) + .to.emit(iexecPoco, 'DealSponsored') + .withArgs(dealId, sponsor.address); // Check deal const deal = await iexecPoco.viewDeal(dealId); expect(deal.sponsor).to.equal(sponsor.address); @@ -1048,7 +1077,7 @@ describe('IexecPoco1', () => { /** * Helper function to deposit requester and scheduler stakes with - * default prices for tests that do not rely on price changes. + * default prices for tests that do not rely on custom prices. */ async function depositForRequesterAndSchedulerWithDefaultPrices(volume: number) { const dealPrice = (appPrice + datasetPrice + workerpoolPrice) * volume; diff --git a/test/byContract/IexecPoco/IexecPocoDelegate.test.ts b/test/byContract/IexecPoco/IexecPocoDelegate.test.ts.skip similarity index 100% rename from test/byContract/IexecPoco/IexecPocoDelegate.test.ts rename to test/byContract/IexecPoco/IexecPocoDelegate.test.ts.skip From 29998c84bf6287b37b782541b573a1dc67dd99e6 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:26:12 +0200 Subject: [PATCH 2/9] Add viewTask tests to accessors test file --- CHANGELOG.md | 4 + .../testing/IexecPocoCompositeDelegate.sol | 2 + .../IexecAccessors/IexecAccessors.test.ts | 96 +++++++++++++++++-- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c336db5d5..076358057 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog ## vNEXT + +- Remove `smock` from unit tests: + - IexecPocoDelegate (#149) + - IexecPocoBoost (#148) - Migrate unit test files to Typescript & Hardhat: - ERC1154 (#145, #146) - IexecEscrowToken (#141, #143) diff --git a/contracts/tools/testing/IexecPocoCompositeDelegate.sol b/contracts/tools/testing/IexecPocoCompositeDelegate.sol index 9fd3868d2..65c805529 100644 --- a/contracts/tools/testing/IexecPocoCompositeDelegate.sol +++ b/contracts/tools/testing/IexecPocoCompositeDelegate.sol @@ -6,6 +6,8 @@ pragma solidity ^0.8.0; import {IexecPocoAccessorsDelegate} from "../../modules/delegates/IexecPocoAccessorsDelegate.sol"; import {IexecPoco1Delegate} from "../../modules/delegates/IexecPoco1Delegate.sol"; +// TODO remove + /** * @notice This contract is dedicated to unit testing. */ diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index 0bc0d91e7..9678fa646 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -6,16 +6,21 @@ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; import { deployments, ethers, expect } from 'hardhat'; import { loadHardhatFixtureDeployment } from '../../../scripts/hardhat-fixture-deployer'; import { IexecInterfaceNative, IexecInterfaceNative__factory } from '../../../typechain'; -import { getIexecAccounts } from '../../../utils/poco-tools'; +import { OrdersAssets, OrdersPrices, buildOrders } from '../../../utils/createOrders'; +import { TaskStatusEnum, getIexecAccounts } from '../../../utils/poco-tools'; +import { IexecWrapper } from '../../utils/IexecWrapper'; /** * Test state view functions. */ -describe('IexecAccessors', async () => { - let proxyAddress: string; - let iexecPocoAsAnyone: IexecInterfaceNative; - let anyone: SignerWithAddress; +let proxyAddress: string; +let iexecPocoAsAnyone: IexecInterfaceNative; +let iexecWrapper: IexecWrapper; +let [appAddress, datasetAddress, workerpoolAddress]: string[] = []; +let [requester, anyone]: SignerWithAddress[] = []; + +describe('IexecAccessors', async () => { beforeEach('Deploy', async () => { // Deploy all contracts proxyAddress = await loadHardhatFixtureDeployment(); @@ -25,81 +30,160 @@ describe('IexecAccessors', async () => { async function initFixture() { const accounts = await getIexecAccounts(); - ({ anyone } = accounts); + ({ requester, anyone } = accounts); iexecPocoAsAnyone = IexecInterfaceNative__factory.connect(proxyAddress, anyone); } it('name', async function () { expect(await iexecPocoAsAnyone.name()).to.equal('Staked RLC'); }); + it('symbol', async function () { expect(await iexecPocoAsAnyone.symbol()).to.equal('SRLC'); }); + it('decimals', async function () { expect(await iexecPocoAsAnyone.decimals()).to.equal(9n); }); + it('totalSupply', async function () { expect(await iexecPocoAsAnyone.totalSupply()).to.equal(0n); }); + // TODO test the case where token() == 0x0 in native mode. it('token', async function () { expect(await iexecPocoAsAnyone.token()).to.equal( '0x5FbDB2315678afecb367f032d93F642f64180aa3', ); }); + + it('viewTask', async function () { + const { dealId, taskId, taskIndex, startTime, timeRef } = await createTask(); + const contributionDeadlineRatio = ( + await iexecPocoAsAnyone.contribution_deadline_ratio() + ).toNumber(); + const finalDeadlineRatio = (await iexecPocoAsAnyone.final_deadline_ratio()).toNumber(); + + const task = await iexecPocoAsAnyone.viewTask(taskId); + expect(task.status).to.equal(TaskStatusEnum.UNSET); + expect(task.dealid).to.equal(dealId); + expect(task.idx).to.equal(taskIndex); + expect(task.timeref).to.equal(timeRef); + expect(task.contributionDeadline).to.equal(startTime + timeRef * contributionDeadlineRatio); + expect(task.revealDeadline).to.equal(0); + expect(task.finalDeadline).to.equal(startTime + timeRef * finalDeadlineRatio); + expect(task.consensusValue).to.be.empty('string'); + expect(task.revealCounter).to.equal(0); + expect(task.winnerCounter).to.equal(0); + expect(task.contributors[0]).to.be.empty('string'); + expect(task.resultDigest).to.be.empty('string'); + expect(task.results).to.be.empty('string'); + expect(task.resultsTimestamp).to.equal(0); + expect(task.resultsCallback).to.be.empty('string'); + }); + it('countCategory', async function () { expect(await iexecPocoAsAnyone.countCategory()).to.equal(5); }); + it('appRegistry', async function () { expect(await iexecPocoAsAnyone.appregistry()).to.equal( (await deployments.get('AppRegistry')).address, ); }); + it('datasetRegistry', async function () { expect(await iexecPocoAsAnyone.datasetregistry()).to.equal( (await deployments.get('DatasetRegistry')).address, ); }); + it('workerpoolRegistry', async function () { expect(await iexecPocoAsAnyone.workerpoolregistry()).to.equal( (await deployments.get('WorkerpoolRegistry')).address, ); }); + it('teeBroker', async function () { expect(await iexecPocoAsAnyone.teebroker()).to.equal(ethers.constants.AddressZero); }); + it('callbackGas', async function () { expect(await iexecPocoAsAnyone.callbackgas()).to.equal(100_000n); }); + it('contributionDeadlineRatio', async function () { expect(await iexecPocoAsAnyone.contribution_deadline_ratio()).to.equal(7); }); + it('revealDeadlineRatio', async function () { expect(await iexecPocoAsAnyone.reveal_deadline_ratio()).to.equal(2n); }); + it('finalDeadlineRatio', async function () { expect(await iexecPocoAsAnyone.final_deadline_ratio()).to.equal(10n); }); + it('workerpoolStakeRatio', async function () { expect(await iexecPocoAsAnyone.workerpool_stake_ratio()).to.equal(30n); }); + it('kittyRatio', async function () { expect(await iexecPocoAsAnyone.kitty_ratio()).to.equal(10n); }); + it('kittyMin', async function () { expect(await iexecPocoAsAnyone.kitty_min()).to.equal(1_000_000_000n); }); + it('kittyAddress', async function () { expect(await iexecPocoAsAnyone.kitty_address()).to.equal( '0x99c2268479b93fDe36232351229815DF80837e23', ); }); + it('groupMemberPurpose', async function () { expect(await iexecPocoAsAnyone.groupmember_purpose()).to.equal(4n); }); + it('eip712domainSeparator', async function () { expect(await iexecPocoAsAnyone.eip712domain_separator()).to.equal( '0xfc2178d8b8300e657cb9f8b5a4d1957174cf1392e294f3575b82a9cea1da1c4b', ); }); }); + +/** + * Build orders, create a deal, and initialize to create a task. + */ +async function createTask() { + const appPrice = 1000; + const datasetPrice = 1_000_000; + const workerpoolPrice = 1_000_000_000; + const callback = ethers.Wallet.createRandom().address; + const ordersAssets: OrdersAssets = { + app: appAddress, + dataset: datasetAddress, + workerpool: workerpoolAddress, + }; + const ordersPrices: OrdersPrices = { + app: appPrice, + dataset: datasetPrice, + workerpool: workerpoolPrice, + }; + const orders = buildOrders({ + assets: ordersAssets, + prices: ordersPrices, + requester: requester.address, + callback: callback, + }); + const { dealId, taskId, taskIndex, startTime } = await iexecWrapper.signAndMatchOrders( + ...orders.toArray(), + ); + const deal = await iexecPocoAsAnyone.viewDeal(dealId); + const timeRef = ( + await iexecPocoAsAnyone.viewCategory(deal.category) + ).workClockTimeRef.toNumber(); + await iexecPocoAsAnyone.initialize(dealId, taskIndex).then((tx) => tx.wait()); + return { dealId, taskId, taskIndex, startTime, timeRef }; +} From 734f18f19f602550b9f594e8e230e76d9907c915 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:53:08 +0200 Subject: [PATCH 3/9] Fix tests --- test/byContract/IexecAccessors/IexecAccessors.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index 9678fa646..217c4cfc8 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -31,6 +31,7 @@ describe('IexecAccessors', async () => { async function initFixture() { const accounts = await getIexecAccounts(); ({ requester, anyone } = accounts); + iexecWrapper = new IexecWrapper(proxyAddress, accounts); iexecPocoAsAnyone = IexecInterfaceNative__factory.connect(proxyAddress, anyone); } From d3f01bc6844ec3a948db8cf2361a0593c82f3699 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:25:53 +0200 Subject: [PATCH 4/9] Fix view task test --- .../IexecAccessors/IexecAccessors.test.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index 217c4cfc8..bd83c3fe1 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: 2020-2024 IEXEC BLOCKCHAIN TECH // SPDX-License-Identifier: Apache-2.0 +import { HashZero } from '@ethersproject/constants'; import { loadFixture } from '@nomicfoundation/hardhat-network-helpers'; import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; import { deployments, ethers, expect } from 'hardhat'; @@ -32,6 +33,7 @@ describe('IexecAccessors', async () => { const accounts = await getIexecAccounts(); ({ requester, anyone } = accounts); iexecWrapper = new IexecWrapper(proxyAddress, accounts); + ({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets()); iexecPocoAsAnyone = IexecInterfaceNative__factory.connect(proxyAddress, anyone); } @@ -66,21 +68,21 @@ describe('IexecAccessors', async () => { const finalDeadlineRatio = (await iexecPocoAsAnyone.final_deadline_ratio()).toNumber(); const task = await iexecPocoAsAnyone.viewTask(taskId); - expect(task.status).to.equal(TaskStatusEnum.UNSET); + expect(task.status).to.equal(TaskStatusEnum.ACTIVE); expect(task.dealid).to.equal(dealId); expect(task.idx).to.equal(taskIndex); expect(task.timeref).to.equal(timeRef); expect(task.contributionDeadline).to.equal(startTime + timeRef * contributionDeadlineRatio); expect(task.revealDeadline).to.equal(0); expect(task.finalDeadline).to.equal(startTime + timeRef * finalDeadlineRatio); - expect(task.consensusValue).to.be.empty('string'); + expect(task.consensusValue).to.equal(HashZero); expect(task.revealCounter).to.equal(0); expect(task.winnerCounter).to.equal(0); - expect(task.contributors[0]).to.be.empty('string'); - expect(task.resultDigest).to.be.empty('string'); - expect(task.results).to.be.empty('string'); + expect(task.contributors.length).to.equal(0); + expect(task.resultDigest).to.equal(HashZero); + expect(task.results).to.equal('0x'); expect(task.resultsTimestamp).to.equal(0); - expect(task.resultsCallback).to.be.empty('string'); + expect(task.resultsCallback).to.equal('0x'); }); it('countCategory', async function () { From ce74d1ce42f27e81f1407f6486bd5c5ba581aef7 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Fri, 25 Oct 2024 09:12:10 +0200 Subject: [PATCH 5/9] Clean --- .../tools/testing/IexecPocoCompositeDelegate.sol | 14 -------------- .../IexecAccessors/IexecAccessors.test.ts | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 contracts/tools/testing/IexecPocoCompositeDelegate.sol diff --git a/contracts/tools/testing/IexecPocoCompositeDelegate.sol b/contracts/tools/testing/IexecPocoCompositeDelegate.sol deleted file mode 100644 index 65c805529..000000000 --- a/contracts/tools/testing/IexecPocoCompositeDelegate.sol +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2024 IEXEC BLOCKCHAIN TECH -// SPDX-License-Identifier: Apache-2.0 - -pragma solidity ^0.8.0; - -import {IexecPocoAccessorsDelegate} from "../../modules/delegates/IexecPocoAccessorsDelegate.sol"; -import {IexecPoco1Delegate} from "../../modules/delegates/IexecPoco1Delegate.sol"; - -// TODO remove - -/** - * @notice This contract is dedicated to unit testing. - */ -contract IexecPocoCompositeDelegate is IexecPoco1Delegate, IexecPocoAccessorsDelegate {} diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index bd83c3fe1..9c60e07a9 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -157,7 +157,7 @@ describe('IexecAccessors', async () => { }); /** - * Build orders, create a deal, and initialize to create a task. + * Build orders, create a deal, and initialize task. */ async function createTask() { const appPrice = 1000; From bd746ecb9cca9f98a407051b45ab3ae3202123d4 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Fri, 25 Oct 2024 10:20:03 +0200 Subject: [PATCH 6/9] Use ethers.provider instead of anyone signer --- test/byContract/IexecPoco/IexecPoco1.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/byContract/IexecPoco/IexecPoco1.test.ts b/test/byContract/IexecPoco/IexecPoco1.test.ts index 93ce7a2bd..caebe25ee 100644 --- a/test/byContract/IexecPoco/IexecPoco1.test.ts +++ b/test/byContract/IexecPoco/IexecPoco1.test.ts @@ -113,7 +113,7 @@ describe('IexecPoco1', () => { iexecPoco = IexecInterfaceNative__factory.connect(proxyAddress, anyone); iexecPocoAsRequester = iexecPoco.connect(requester); iexecPocoAsSponsor = IexecPoco1__factory.connect(proxyAddress, sponsor); - iexecPocoAccessors = IexecPocoAccessors__factory.connect(proxyAddress, anyone); + iexecPocoAccessors = IexecPocoAccessors__factory.connect(proxyAddress, ethers.provider); iexecPocoContract = iexecPoco as Contract; ordersActors = { appOwner: appProvider, From 92bad68dab51fce83bd41f290da763c1d4964571 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Fri, 25 Oct 2024 11:32:30 +0200 Subject: [PATCH 7/9] Remove useless await --- test/byContract/IexecAccessors/IexecAccessors.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index 2e8bbe873..97a2b515f 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -213,10 +213,8 @@ describe('IexecAccessors', async () => { .finalize(taskId, results, resultsCallback) .then((tx) => tx.wait()); const task = await iexecPocoAsAnyone.viewTask(taskId); - await expect(task.status).to.equal(TaskStatusEnum.COMPLETED); - await expect(await iexecPocoAsAnyone.callStatic.resultFor(taskId)).to.equal( - resultsCallback, - ); + expect(task.status).to.equal(TaskStatusEnum.COMPLETED); + expect(await iexecPocoAsAnyone.callStatic.resultFor(taskId)).to.equal(resultsCallback); }); it('Should not get result when task is not completed', async function () { const volume = 3; @@ -271,7 +269,7 @@ describe('IexecAccessors', async () => { const verifyTaskStatusAndResult = async (taskId: string, expectedStatus: number) => { const task = await iexecPocoAsAnyone.viewTask(taskId); - await expect(task.status).to.equal(expectedStatus); + expect(task.status).to.equal(expectedStatus); await expect(iexecPocoAsAnyone.resultFor(taskId)).to.be.revertedWith('task-pending'); }; }); From f7d5e89ed178d6994a805a20e9c4907ae03689c1 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:04:46 +0200 Subject: [PATCH 8/9] Move helper functions outside of describe --- .../IexecAccessors/IexecAccessors.test.ts | 98 ++++++++----------- 1 file changed, 42 insertions(+), 56 deletions(-) diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index 97a2b515f..8c3ab5619 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -27,11 +27,15 @@ import { IexecWrapper } from '../../utils/IexecWrapper'; * Test state view functions. */ +const { results, resultDigest } = buildUtf8ResultAndDigest('result'); +const { resultsCallback, callbackResultDigest } = buildResultCallbackAndDigest(123); + let proxyAddress: string; let iexecPocoAsAnyone: IexecInterfaceNative; let iexecWrapper: IexecWrapper; let [appAddress, datasetAddress, workerpoolAddress]: string[] = []; -let [requester, anyone]: SignerWithAddress[] = []; +let [requester, scheduler, worker1, anyone]: SignerWithAddress[] = []; +let ordersAssets: OrdersAssets; describe('IexecAccessors', async () => { beforeEach('Deploy', async () => { @@ -47,6 +51,11 @@ describe('IexecAccessors', async () => { iexecWrapper = new IexecWrapper(proxyAddress, accounts); ({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets()); iexecPocoAsAnyone = IexecInterfaceNative__factory.connect(proxyAddress, anyone); + ordersAssets = { + app: appAddress, + dataset: datasetAddress, + workerpool: workerpoolAddress, + }; } it('name', async function () { @@ -168,25 +177,6 @@ describe('IexecAccessors', async () => { }); describe('resultFor', () => { - let iexecWrapper: IexecWrapper; - let [appAddress, datasetAddress, workerpoolAddress]: string[] = []; - let [requester, scheduler, worker1]: SignerWithAddress[] = []; - let ordersAssets: OrdersAssets; - const { results, resultDigest } = buildUtf8ResultAndDigest('result'); - const { resultsCallback, callbackResultDigest } = buildResultCallbackAndDigest(123); - - beforeEach(async () => { - const accounts = await getIexecAccounts(); - iexecWrapper = new IexecWrapper(proxyAddress, accounts); - ({ requester, scheduler, worker1 } = accounts); - ({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets()); - ordersAssets = { - app: appAddress, - dataset: datasetAddress, - workerpool: workerpoolAddress, - }; - }); - it('Should get result of task', async function () { const oracleConsumerInstance = await new TestClient__factory() .connect(anyone) @@ -236,42 +226,6 @@ describe('IexecAccessors', async () => { await verifyTaskStatusAndResult(activeTaskId, TaskStatusEnum.ACTIVE); await verifyTaskStatusAndResult(revealingTaskId, TaskStatusEnum.REVEALING); }); - - // Helper function to initialize and contribute to a task - const initializeTask = async (dealId: string, taskIndex: number) => { - await iexecPocoAsAnyone.initialize(dealId, taskIndex).then((tx) => tx.wait()); - return getTaskId(dealId, taskIndex); - }; - - const contributeTask = async (dealId: string, taskIndex: number, resultDigest: string) => { - const taskId = getTaskId(dealId, taskIndex); - const workerTaskStake = await iexecPocoAsAnyone - .viewDeal(dealId) - .then((deal) => deal.workerStake.toNumber()); - const { resultHash, resultSeal } = buildResultHashAndResultSeal( - taskId, - resultDigest, - worker1, - ); - const schedulerSignature = await buildAndSignContributionAuthorizationMessage( - worker1.address, - taskId, - AddressZero, - scheduler, - ); - await iexecWrapper.depositInIexecAccount(worker1, workerTaskStake); - await iexecPocoAsAnyone - .connect(worker1) - .contribute(taskId, resultHash, resultSeal, AddressZero, '0x', schedulerSignature) - .then((tx) => tx.wait()); - return taskId; - }; - - const verifyTaskStatusAndResult = async (taskId: string, expectedStatus: number) => { - const task = await iexecPocoAsAnyone.viewTask(taskId); - expect(task.status).to.equal(expectedStatus); - await expect(iexecPocoAsAnyone.resultFor(taskId)).to.be.revertedWith('task-pending'); - }; }); }); @@ -309,3 +263,35 @@ async function createTask() { await iexecPocoAsAnyone.initialize(dealId, taskIndex).then((tx) => tx.wait()); return { dealId, taskId, taskIndex, startTime, timeRef }; } + +// Helper function to initialize and contribute to a task +const initializeTask = async (dealId: string, taskIndex: number) => { + await iexecPocoAsAnyone.initialize(dealId, taskIndex).then((tx) => tx.wait()); + return getTaskId(dealId, taskIndex); +}; + +const contributeTask = async (dealId: string, taskIndex: number, resultDigest: string) => { + const taskId = getTaskId(dealId, taskIndex); + const workerTaskStake = await iexecPocoAsAnyone + .viewDeal(dealId) + .then((deal) => deal.workerStake.toNumber()); + const { resultHash, resultSeal } = buildResultHashAndResultSeal(taskId, resultDigest, worker1); + const schedulerSignature = await buildAndSignContributionAuthorizationMessage( + worker1.address, + taskId, + AddressZero, + scheduler, + ); + await iexecWrapper.depositInIexecAccount(worker1, workerTaskStake); + await iexecPocoAsAnyone + .connect(worker1) + .contribute(taskId, resultHash, resultSeal, AddressZero, '0x', schedulerSignature) + .then((tx) => tx.wait()); + return taskId; +}; + +const verifyTaskStatusAndResult = async (taskId: string, expectedStatus: number) => { + const task = await iexecPocoAsAnyone.viewTask(taskId); + await expect(task.status).to.equal(expectedStatus); + await expect(iexecPocoAsAnyone.resultFor(taskId)).to.be.revertedWith('task-pending'); +}; From 67e5808c13d190c3b4791d250f6797d2a5e0aa13 Mon Sep 17 00:00:00 2001 From: Zied <26070035+zguesmi@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:34:30 +0200 Subject: [PATCH 9/9] Refactor tests and helper functions --- .../IexecAccessors/IexecAccessors.test.ts | 85 +++++++------------ 1 file changed, 33 insertions(+), 52 deletions(-) diff --git a/test/byContract/IexecAccessors/IexecAccessors.test.ts b/test/byContract/IexecAccessors/IexecAccessors.test.ts index 8c3ab5619..17385da7e 100644 --- a/test/byContract/IexecAccessors/IexecAccessors.test.ts +++ b/test/byContract/IexecAccessors/IexecAccessors.test.ts @@ -11,7 +11,7 @@ import { IexecInterfaceNative__factory, TestClient__factory, } from '../../../typechain'; -import { OrdersAssets, OrdersPrices, buildOrders } from '../../../utils/createOrders'; +import { OrdersAssets, buildOrders } from '../../../utils/createOrders'; import { TaskStatusEnum, buildAndSignContributionAuthorizationMessage, @@ -36,6 +36,7 @@ let iexecWrapper: IexecWrapper; let [appAddress, datasetAddress, workerpoolAddress]: string[] = []; let [requester, scheduler, worker1, anyone]: SignerWithAddress[] = []; let ordersAssets: OrdersAssets; +let callbackAddress: string; describe('IexecAccessors', async () => { beforeEach('Deploy', async () => { @@ -47,7 +48,7 @@ describe('IexecAccessors', async () => { async function initFixture() { const accounts = await getIexecAccounts(); - ({ requester, anyone } = accounts); + ({ requester, scheduler, worker1, anyone } = accounts); iexecWrapper = new IexecWrapper(proxyAddress, accounts); ({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets()); iexecPocoAsAnyone = IexecInterfaceNative__factory.connect(proxyAddress, anyone); @@ -56,6 +57,11 @@ describe('IexecAccessors', async () => { dataset: datasetAddress, workerpool: workerpoolAddress, }; + callbackAddress = await new TestClient__factory() + .connect(anyone) + .deploy() + .then((contract) => contract.deployed()) + .then((contract) => contract.address); } it('name', async function () { @@ -82,7 +88,9 @@ describe('IexecAccessors', async () => { }); it('viewTask', async function () { - const { dealId, taskId, taskIndex, startTime, timeRef } = await createTask(); + const { dealId, taskId, taskIndex, startTime, timeRef } = await createDeal(); + await initializeTask(dealId, taskIndex); + const contributionDeadlineRatio = ( await iexecPocoAsAnyone.contribution_deadline_ratio() ).toNumber(); @@ -178,21 +186,10 @@ describe('IexecAccessors', async () => { describe('resultFor', () => { it('Should get result of task', async function () { - const oracleConsumerInstance = await new TestClient__factory() - .connect(anyone) - .deploy() - .then((contract) => contract.deployed()); - const orders = buildOrders({ - assets: ordersAssets, - requester: requester.address, - callback: oracleConsumerInstance.address, - }); - const { dealId, taskId, taskIndex } = await iexecWrapper.signAndMatchOrders( - ...orders.toArray(), - ); + const { dealId, taskId, taskIndex } = await createDeal(); await initializeTask(dealId, taskIndex).then(() => - contributeTask(dealId, taskIndex, callbackResultDigest), + contributeToTask(dealId, taskIndex, callbackResultDigest), ); await iexecPocoAsAnyone .connect(worker1) @@ -206,20 +203,14 @@ describe('IexecAccessors', async () => { expect(task.status).to.equal(TaskStatusEnum.COMPLETED); expect(await iexecPocoAsAnyone.callStatic.resultFor(taskId)).to.equal(resultsCallback); }); - it('Should not get result when task is not completed', async function () { - const volume = 3; - const orders = buildOrders({ - assets: ordersAssets, - requester: requester.address, - volume, - }); - const { dealId } = await iexecWrapper.signAndMatchOrders(...orders.toArray()); + it('Should not get result when task is not completed', async function () { + const { dealId } = await createDeal(3); const unsetTaskId = getTaskId(dealId, 0); const activeTaskId = await initializeTask(dealId, 1); const revealingTaskId = await initializeTask(dealId, 2).then(() => - contributeTask(dealId, 2, resultDigest), + contributeToTask(dealId, 2, resultDigest), ); await verifyTaskStatusAndResult(unsetTaskId, TaskStatusEnum.UNSET); @@ -230,47 +221,37 @@ describe('IexecAccessors', async () => { }); /** - * Build orders, create a deal, and initialize task. + * Helper function to create a deal with a specific volume. */ -async function createTask() { - const appPrice = 1000; - const datasetPrice = 1_000_000; - const workerpoolPrice = 1_000_000_000; - const callback = ethers.Wallet.createRandom().address; - const ordersAssets: OrdersAssets = { - app: appAddress, - dataset: datasetAddress, - workerpool: workerpoolAddress, - }; - const ordersPrices: OrdersPrices = { - app: appPrice, - dataset: datasetPrice, - workerpool: workerpoolPrice, - }; +async function createDeal(volume: number = 1) { const orders = buildOrders({ assets: ordersAssets, - prices: ordersPrices, requester: requester.address, - callback: callback, + volume, + callback: callbackAddress, }); const { dealId, taskId, taskIndex, startTime } = await iexecWrapper.signAndMatchOrders( ...orders.toArray(), ); - const deal = await iexecPocoAsAnyone.viewDeal(dealId); + const dealCategory = (await iexecPocoAsAnyone.viewDeal(dealId)).category; const timeRef = ( - await iexecPocoAsAnyone.viewCategory(deal.category) + await iexecPocoAsAnyone.viewCategory(dealCategory) ).workClockTimeRef.toNumber(); - await iexecPocoAsAnyone.initialize(dealId, taskIndex).then((tx) => tx.wait()); return { dealId, taskId, taskIndex, startTime, timeRef }; } -// Helper function to initialize and contribute to a task -const initializeTask = async (dealId: string, taskIndex: number) => { +/** + * Helper function to initialize a task. + */ +async function initializeTask(dealId: string, taskIndex: number) { await iexecPocoAsAnyone.initialize(dealId, taskIndex).then((tx) => tx.wait()); return getTaskId(dealId, taskIndex); -}; +} -const contributeTask = async (dealId: string, taskIndex: number, resultDigest: string) => { +/** + * Helper function to contribute to a task. + */ +async function contributeToTask(dealId: string, taskIndex: number, resultDigest: string) { const taskId = getTaskId(dealId, taskIndex); const workerTaskStake = await iexecPocoAsAnyone .viewDeal(dealId) @@ -288,10 +269,10 @@ const contributeTask = async (dealId: string, taskIndex: number, resultDigest: s .contribute(taskId, resultHash, resultSeal, AddressZero, '0x', schedulerSignature) .then((tx) => tx.wait()); return taskId; -}; +} const verifyTaskStatusAndResult = async (taskId: string, expectedStatus: number) => { const task = await iexecPocoAsAnyone.viewTask(taskId); - await expect(task.status).to.equal(expectedStatus); + expect(task.status).to.equal(expectedStatus); await expect(iexecPocoAsAnyone.resultFor(taskId)).to.be.revertedWith('task-pending'); };