|
| 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