-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store collator fullness in collator_assignment pallet (#672)
- Loading branch information
1 parent
e189687
commit 944c771
Showing
8 changed files
with
230 additions
and
62 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
79 changes: 79 additions & 0 deletions
79
test/suites/dev-tanssi/collator-assignment/test-collator-assignment.ts
This file contains 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,79 @@ | ||
import "@tanssi/api-augment"; | ||
import { describeSuite, expect, beforeAll } from "@moonwall/cli"; | ||
import { ApiPromise } from "@polkadot/api"; | ||
import { jumpBlocks, jumpSessions, jumpToSession } from "util/block"; | ||
import { filterAndApply } from "@moonwall/util"; | ||
import { EventRecord } from "@polkadot/types/interfaces"; | ||
import { bool, u32, u8, Vec } from "@polkadot/types-codec"; | ||
|
||
describeSuite({ | ||
id: "DT0801", | ||
title: "Collator assignment tests", | ||
foundationMethods: "dev", | ||
|
||
testCases: ({ it, context }) => { | ||
let polkadotJs: ApiPromise; | ||
|
||
beforeAll(async () => { | ||
polkadotJs = context.polkadotJs(); | ||
}); | ||
|
||
it({ | ||
id: "E01", | ||
title: "Collator should rotate", | ||
test: async function () { | ||
const fullRotationPeriod = (await context.polkadotJs().query.configuration.activeConfig())[ | ||
"fullRotationPeriod" | ||
].toString(); | ||
const sessionIndex = (await polkadotJs.query.session.currentIndex()).toNumber(); | ||
// Calculate the remaining sessions for next full rotation | ||
// This is a workaround for running moonwall in run mode | ||
// as it runs all tests in the same chain instance | ||
const remainingSessionsForRotation = | ||
sessionIndex > fullRotationPeriod ? sessionIndex % fullRotationPeriod : fullRotationPeriod; | ||
|
||
await jumpToSession(context, remainingSessionsForRotation - 2); | ||
|
||
const initialAssignment = (await polkadotJs.query.collatorAssignment.collatorContainerChain()).toJSON(); | ||
|
||
expect(initialAssignment.containerChains[2000].length).to.eq(2); | ||
expect((await polkadotJs.query.collatorAssignment.pendingCollatorContainerChain()).isNone); | ||
|
||
// remainingSessionsForRotation - 1 | ||
await jumpSessions(context, 1); | ||
const rotationEndAssignment = ( | ||
await polkadotJs.query.collatorAssignment.collatorContainerChain() | ||
).toJSON(); | ||
|
||
expect((await polkadotJs.query.collatorAssignment.pendingCollatorContainerChain()).isSome); | ||
// Assignment shouldn't have changed yet | ||
expect(initialAssignment.containerChains[2000].toSorted()).to.deep.eq( | ||
rotationEndAssignment.containerChains[2000].toSorted() | ||
); | ||
|
||
// As randomness isn't deterministic in starlight we can't be | ||
// 100% certain that the assignation will indeed change. So the | ||
// best we can do is verify that the pending rotation event for | ||
// next session is emitted and is a full rotation as expected | ||
const events = await polkadotJs.query.system.events(); | ||
const filteredEvents = filterAndApply( | ||
events, | ||
"collatorAssignment", | ||
["NewPendingAssignment"], | ||
({ event }: EventRecord) => | ||
event.data as unknown as { randomSeed: Vec<u8>; fullRotation: bool; targetSession: u32 } | ||
); | ||
expect(filteredEvents[0].fullRotation.toJSON()).toBe(true); | ||
|
||
// Check that the randomness is set in CollatorAssignment the | ||
// block previous to the full rotation | ||
const sessionDuration = 10; | ||
await jumpBlocks(context, sessionDuration - 1); | ||
const assignmentRandomness = await polkadotJs.query.collatorAssignment.randomness(); | ||
// TODO: in starlight isEmpty == false because we have randomness there | ||
// In dancebox dev tests there is no rotation because there is no randomness | ||
expect(assignmentRandomness.isEmpty).toBe(true); | ||
}, | ||
}); | ||
}, | ||
}); |
Oops, something went wrong.