-
Notifications
You must be signed in to change notification settings - Fork 14
Migrate 201_fullchain-bot-dualPool.js file to Hardhat/TS #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c24afaa
Clean
zguesmi 9d88679
Add note
zguesmi e469193
Clean utility files
zguesmi 05154f9
Migrate dual pool test file
zguesmi 1172c01
Fix volume calculation
zguesmi 51f648d
Fix test deal volume calculation in _signAndMatchOrders function
zguesmi 5106516
Debug dual pool IT
zguesmi 691567f
Fix workers reward calculation issue
zguesmi 7c2161a
Clean test file
zguesmi 414f109
Check remaining volumes
zguesmi 8c8dbc5
Rename file
zguesmi 0a5a62f
Merge branch 'develop' into feature/fullchain-dual-pool-migration
zguesmi 4ac18c5
Update changelog
zguesmi 09970c2
Clean
zguesmi 5b7c9c2
Use plain prices for workerpool orders and check request order consumed
zguesmi b2c9e6b
Create a new dummy commit to unblock CI
zguesmi b2cb3a3
Revert "Create a new dummy commit to unblock CI"
zguesmi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
// SPDX-FileCopyrightText: 2024 IEXEC BLOCKCHAIN TECH <contact@iex.ec> | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers'; | ||
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; | ||
import { ethers, expect } from 'hardhat'; | ||
import { loadHardhatFixtureDeployment } from '../scripts/hardhat-fixture-deployer'; | ||
import { | ||
IexecInterfaceNative, | ||
IexecInterfaceNative__factory, | ||
IexecPocoAccessors, | ||
IexecPocoAccessors__factory, | ||
} from '../typechain'; | ||
import { | ||
IexecOrders, | ||
OrdersActors, | ||
OrdersAssets, | ||
OrdersPrices, | ||
buildOrders, | ||
} from '../utils/createOrders'; | ||
import { | ||
PocoMode, | ||
TaskStatusEnum, | ||
buildUtf8ResultAndDigest, | ||
getIexecAccounts, | ||
} from '../utils/poco-tools'; | ||
import { IexecWrapper } from './utils/IexecWrapper'; | ||
|
||
const standardDealTag = '0x0000000000000000000000000000000000000000000000000000000000000000'; | ||
const appPrice = 1000; | ||
const datasetPrice = 1_000_000; | ||
const workerpoolPrice1 = 1_000_000_015; | ||
const workerpoolPrice2 = 1_000_000_025; | ||
const { results, resultDigest } = buildUtf8ResultAndDigest('result'); | ||
|
||
let proxyAddress: string; | ||
let iexecPoco: IexecInterfaceNative; | ||
let iexecPocoAccessors: IexecPocoAccessors; // To use `computeDealVolume()` | ||
let iexecWrapper: IexecWrapper; | ||
let [appAddress, workerpoolAddress, datasetAddress]: string[] = []; | ||
let [requester, appProvider, datasetProvider, scheduler, anyone, worker1]: SignerWithAddress[] = []; | ||
let ordersActors: OrdersActors; | ||
let ordersAssets: OrdersAssets; | ||
let ordersPrices: OrdersPrices; | ||
|
||
describe('Integration tests', function () { | ||
beforeEach('Deploy', async () => { | ||
// Deploy all contracts | ||
proxyAddress = await loadHardhatFixtureDeployment(); | ||
// Initialize test environment | ||
await loadFixture(initFixture); | ||
}); | ||
|
||
async function initFixture() { | ||
const accounts = await getIexecAccounts(); | ||
({ requester, appProvider, datasetProvider, scheduler, anyone, worker1 } = accounts); | ||
iexecWrapper = new IexecWrapper(proxyAddress, accounts); | ||
({ appAddress, datasetAddress, workerpoolAddress } = await iexecWrapper.createAssets()); | ||
iexecPoco = IexecInterfaceNative__factory.connect(proxyAddress, anyone); | ||
iexecPocoAccessors = IexecPocoAccessors__factory.connect(proxyAddress, ethers.provider); | ||
ordersActors = { | ||
appOwner: appProvider, | ||
datasetOwner: datasetProvider, | ||
workerpoolOwner: scheduler, | ||
requester: requester, | ||
}; | ||
ordersAssets = { | ||
app: appAddress, | ||
dataset: datasetAddress, | ||
workerpool: workerpoolAddress, | ||
}; | ||
ordersPrices = { | ||
app: appPrice, | ||
dataset: datasetPrice, | ||
workerpool: 0, // Overridden below. | ||
}; | ||
} | ||
|
||
/** | ||
* A test to run full workflow (matchOrders..finalize) with 2 orders having 2 different volumes | ||
* for the same workerpool and only 1 request order. | ||
*/ | ||
it('[1] No sponsorship, no beneficiary, no callback, BoT, no replication, 2 workerpool orders', async function () { | ||
const volume = 3; | ||
const workerpoolOrderVolume1 = 2; | ||
const workerpoolOrderVolume2 = 10; | ||
const dealVolume1 = Math.min(workerpoolOrderVolume1, volume); // min(2, 3); | ||
const dealVolume2 = Math.min(workerpoolOrderVolume2, volume - dealVolume1); // min(10, 1) | ||
const taskPrice1 = appPrice + datasetPrice + workerpoolPrice1; | ||
const taskPrice2 = appPrice + datasetPrice + workerpoolPrice2; | ||
// Create default orders. | ||
const { | ||
appOrder, | ||
datasetOrder, | ||
workerpoolOrder, | ||
requesterOrder: requestOrder, | ||
} = buildOrders({ | ||
assets: ordersAssets, | ||
prices: ordersPrices, | ||
requester: requester.address, | ||
tag: standardDealTag, | ||
volume, | ||
}).toObject(); | ||
// Create 2 different orders for the same workerpool. | ||
const workerpoolOrder1 = { ...workerpoolOrder }; // Shallow cloning is fine here. | ||
const workerpoolOrder2 = { ...workerpoolOrder }; | ||
workerpoolOrder1.volume = workerpoolOrderVolume1; | ||
workerpoolOrder1.workerpoolprice = workerpoolPrice1; | ||
workerpoolOrder2.volume = workerpoolOrderVolume2; | ||
workerpoolOrder2.workerpoolprice = workerpoolPrice2; | ||
requestOrder.workerpoolmaxprice = Math.max(workerpoolPrice1, workerpoolPrice2); | ||
// Match both workerpool orders with the same request order. | ||
const dealOrders1 = new IexecOrders( | ||
appOrder, | ||
datasetOrder, | ||
workerpoolOrder1, | ||
requestOrder, | ||
).toArray(); | ||
const dealOrders2 = new IexecOrders( | ||
appOrder, | ||
datasetOrder, | ||
workerpoolOrder2, | ||
requestOrder, | ||
).toArray(); | ||
expect(await iexecPocoAccessors.computeDealVolume(...dealOrders1)).to.equal(dealVolume1); | ||
const { | ||
dealId: dealId1, | ||
taskIndex: taskIndex1, | ||
schedulerStakePerDeal: schedulerStakeForDeal1, | ||
} = await iexecWrapper.signAndMatchOrders( | ||
appOrder, | ||
datasetOrder, | ||
workerpoolOrder1, | ||
requestOrder, | ||
); // First task index is 0. | ||
expect(await iexecPocoAccessors.computeDealVolume(...dealOrders2)).to.equal(dealVolume2); | ||
const { | ||
dealId: dealId2, | ||
taskIndex: taskIndex2, | ||
schedulerStakePerDeal: schedulerStakeForDeal2, | ||
} = await iexecWrapper.signAndMatchOrders( | ||
appOrder, | ||
datasetOrder, | ||
workerpoolOrder2, | ||
requestOrder, | ||
); // First task index is 2. | ||
const deal1 = await iexecPoco.viewDeal(dealId1); | ||
expect(deal1.botFirst).to.equal(0); | ||
expect(deal1.botSize).to.equal(dealVolume1); | ||
const deal2 = await iexecPoco.viewDeal(dealId2); | ||
expect(deal2.botFirst).to.equal(dealVolume1); | ||
expect(deal2.botSize).to.equal(dealVolume2); | ||
// Compute stakes and rewards for each deal. | ||
const schedulerStakePerTaskOfDeal1 = schedulerStakeForDeal1 / dealVolume1; | ||
const schedulerStakePerTaskOfDeal2 = schedulerStakeForDeal2 / dealVolume2; | ||
const workersRewardPerTaskOfDeal1 = await iexecWrapper.computeWorkersRewardPerTask( | ||
dealId1, | ||
PocoMode.CLASSIC, | ||
); | ||
const workersRewardPerTaskOfDeal2 = await iexecWrapper.computeWorkersRewardPerTask( | ||
dealId2, | ||
PocoMode.CLASSIC, | ||
); | ||
const schedulerRewardPerTaskOfDeal1 = workerpoolPrice1 - workersRewardPerTaskOfDeal1; | ||
const schedulerRewardPerTaskOfDeal2 = workerpoolPrice2 - workersRewardPerTaskOfDeal2; | ||
// Finalize each task and run checks. | ||
await runTaskThenCheckBalancesAndVolumes( | ||
dealId1, | ||
taskIndex1, | ||
taskPrice1, | ||
schedulerStakePerTaskOfDeal1, | ||
schedulerRewardPerTaskOfDeal1, | ||
workersRewardPerTaskOfDeal1, | ||
); | ||
await runTaskThenCheckBalancesAndVolumes( | ||
dealId1, | ||
taskIndex1 + 1, | ||
taskPrice1, | ||
schedulerStakePerTaskOfDeal1, | ||
schedulerRewardPerTaskOfDeal1, | ||
workersRewardPerTaskOfDeal1, | ||
); | ||
await runTaskThenCheckBalancesAndVolumes( | ||
dealId2, | ||
taskIndex2, | ||
taskPrice2, | ||
schedulerStakePerTaskOfDeal2, | ||
schedulerRewardPerTaskOfDeal2, | ||
workersRewardPerTaskOfDeal2, | ||
); | ||
// Check remaining volumes. | ||
expect(await iexecPoco.viewConsumed(iexecWrapper.hashOrder(requestOrder))).to.equal(volume); | ||
expect(await iexecPoco.viewConsumed(iexecWrapper.hashOrder(workerpoolOrder1))).to.equal( | ||
dealVolume1, | ||
); | ||
expect(await iexecPoco.viewConsumed(iexecWrapper.hashOrder(workerpoolOrder2))).to.equal( | ||
dealVolume2, | ||
); | ||
}); | ||
|
||
async function runTaskThenCheckBalancesAndVolumes( | ||
dealId: string, | ||
taskIndex: number, | ||
taskPrice: number, | ||
schedulerStake: number, | ||
schedulerReward: number, | ||
workerReward: number, | ||
) { | ||
// Save frozens before task execution. | ||
const accounts = [requester, scheduler, appProvider, datasetProvider, worker1]; | ||
const accountsInitialFrozens = await iexecWrapper.getInitialFrozens(accounts); | ||
// Run task. | ||
const taskId = await iexecWrapper.initializeTask(dealId, taskIndex); | ||
const { workerStakePerTask: workerStake } = await iexecWrapper.contributeToTask( | ||
dealId, | ||
taskIndex, | ||
resultDigest, | ||
worker1, | ||
); | ||
await iexecPoco | ||
.connect(worker1) | ||
.reveal(taskId, resultDigest) | ||
.then((tx) => tx.wait()); | ||
const finalizeTx = await iexecPoco.connect(scheduler).finalize(taskId, results, '0x'); | ||
await finalizeTx.wait(); | ||
// Check task. | ||
const task = await iexecPoco.viewTask(taskId); | ||
expect(task.status).to.equal(TaskStatusEnum.COMPLETED); | ||
expect(task.idx).to.equal(taskIndex); | ||
// Verify token balance changes. | ||
const expectedProxyBalanceChange = -(taskPrice + schedulerStake + workerStake); | ||
await expect(finalizeTx).to.changeTokenBalances( | ||
iexecPoco, | ||
[proxyAddress, requester, scheduler, appProvider, datasetProvider, worker1], | ||
[ | ||
expectedProxyBalanceChange, // Proxy | ||
0, // Requester | ||
schedulerStake + schedulerReward, // Scheduler | ||
appPrice, // AppProvider | ||
datasetPrice, // DatasetProvider | ||
workerStake + workerReward, // Worker | ||
], | ||
); | ||
// Calculate expected frozen changes | ||
const expectedFrozenChanges = [ | ||
0, // Proxy | ||
-taskPrice, // Requester | ||
-schedulerStake, // Scheduler | ||
0, // AppProvider | ||
0, // DatasetProvider | ||
0, // Worker | ||
]; | ||
await iexecWrapper.checkFrozenChanges(accountsInitialFrozens, expectedFrozenChanges); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.