git.delta.rocks / unique-network / refs/commits / f4daa4228c60

difftreelog

fix collator-selection tests

Daniel Shiposha2023-09-08parent: #d892f52.patch.diff
in: master

8 files changed

modified.baedeker/collator-selection.jsonnetdiffbeforeafterboth
--- a/.baedeker/collator-selection.jsonnet
+++ b/.baedeker/collator-selection.jsonnet
@@ -31,7 +31,7 @@
 			bin: $.bin,
 			wantedKeys: 'para',
 		},
-		for name in ['alice', 'bob']
+		for name in ['alice', 'bob', 'charlie', 'dave']
 	},
 };
 
added.baedeker/node-stash-discover.jsonnetdiffbeforeafterboth
--- /dev/null
+++ b/.baedeker/node-stash-discover.jsonnet
@@ -0,0 +1,13 @@
+local {flattenNodes, flattenChains, ...} = import 'baedeker-library/util/mixin.libsonnet';
+
+function(prev, final)
+prev + {
+	_output+:: {
+		dockerComposeDiscover+: std.join('\n', [
+			'%s_STASH=%s' % [std.strReplace(std.asciiUpper(node.hostname), '-', '_'), node.wallets.stash]
+			for chain in flattenChains(prev)
+			if 'paraId' in chain
+			for node in flattenNodes(chain)
+		] + ['']),
+	},
+}
modified.github/workflows/collator-selection.ymldiffbeforeafterboth
--- a/.github/workflows/collator-selection.yml
+++ b/.github/workflows/collator-selection.yml
@@ -139,7 +139,7 @@
           context: .
           dockerfile: .docker/Dockerfile-unique
           args: |
-            --build-arg RUNTIME_FEATURES=${{ matrix.runtime_features }}
+            --build-arg RUNTIME_FEATURES=${{ matrix.runtime_features }},${{ matrix.wasm_name }}/session-test-timing
             --build-arg RUST_TOOLCHAIN=${{ env.RUST_TOOLCHAIN }}
           dockerhub_username: ${{ secrets.CORE_DOCKERHUB_USERNAME }}
           dockerhub_token: ${{ secrets.CORE_DOCKERHUB_TOKEN }}
@@ -164,6 +164,7 @@
             relay_spec=westend-local            
           inputs: |
             .baedeker/collator-selection.jsonnet
+            .baedeker/node-stash-discover.jsonnet
             snippet:(import 'baedeker-library/ops/rewrites.libsonnet').rewriteNodePaths({'bin/polkadot':{dockerImage:'${{ steps.polkadot.outputs.name }}'}})
             snippet:(import 'baedeker-library/ops/rewrites.libsonnet').rewriteNodePaths({'bin/unique':{dockerImage:'${{ steps.latest.outputs.name }}'}})
 
modifiedruntime/common/config/pallets/mod.rsdiffbeforeafterboth
--- a/runtime/common/config/pallets/mod.rs
+++ b/runtime/common/config/pallets/mod.rs
@@ -125,10 +125,20 @@
 	pub AppPromotionDailyRate: Perbill = Perbill::from_rational(5u32, 10_000);
 	pub const MaxCollators: u32 = MAX_COLLATORS;
 	pub const LicenseBond: Balance = GENESIS_LICENSE_BOND;
+
+	pub const DayRelayBlocks: BlockNumber = RELAY_DAYS;
+}
+
+#[cfg(not(feature = "session-test-timing"))]
+parameter_types! {
 	pub const SessionPeriod: BlockNumber = SESSION_LENGTH;
-	pub const DayRelayBlocks: BlockNumber = RELAY_DAYS;
 }
 
+#[cfg(feature = "session-test-timing")]
+parameter_types! {
+	pub const SessionPeriod: BlockNumber = 3 * MINUTES;
+}
+
 impl pallet_configuration::Config for Runtime {
 	type RuntimeEvent = RuntimeEvent;
 	type Balance = Balance;
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -229,6 +229,7 @@
 preimage = []
 refungible = []
 unique-scheduler = []
+session-test-timing = []
 
 ################################################################################
 # local dependencies
modifiedruntime/quartz/Cargo.tomldiffbeforeafterboth
--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -218,6 +218,7 @@
 refungible = []
 unique-scheduler = []
 gov-test-timings = []
+session-test-timing = []
 
 ################################################################################
 # local dependencies
modifiedruntime/unique/Cargo.tomldiffbeforeafterboth
--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -221,6 +221,7 @@
 refungible = []
 unique-scheduler = []
 gov-test-timings = []
+session-test-timing = []
 
 ################################################################################
 # local dependencies
modifiedtests/src/collator-selection/collatorSelection.seqtest.tsdiffbeforeafterboth
17import {IKeyringPair} from '@polkadot/types/types';17import {IKeyringPair} from '@polkadot/types/types';
18import {usingPlaygrounds, expect, itSub, Pallets, requirePalletsOrSkip} from '../util';18import {usingPlaygrounds, expect, itSub, Pallets, requirePalletsOrSkip} from '../util';
19
20async function nodeAddress(name: string) {
21 // eslint-disable-next-line require-await
22 return await usingPlaygrounds(async (helper, _) => {
23 const envNodeStash = `RELAY_UNIQUE_NODE_${name.toUpperCase()}_STASH`;
24
25 const nodeStash = process.env[envNodeStash];
26 if(nodeStash) {
27 return helper.address.normalizeSubstrateToChainFormat(nodeStash);
28 } else {
29 throw Error(`"${envNodeStash}" env var is not set`);
30 }
31 });
32}
33
34async function getInitialInvulnerables() {
35 return await Promise.all([
36 nodeAddress('Alice'),
37 nodeAddress('Bob'),
38 nodeAddress('Charlie'),
39 nodeAddress('Dave'),
40 ]);
41}
1942
20async function resetInvulnerables() {43async function resetInvulnerables() {
21 await usingPlaygrounds(async (helper, privateKey) => {44 await usingPlaygrounds(async (helper, privateKey) => {
22 const superuser = await privateKey('//Alice');45 const superuser = await privateKey('//Alice');
23 const alice = await privateKey('//Alice');46 const initialInvulnerables = await getInitialInvulnerables();
24 const bob = await privateKey('//Bob');47
25 const invulnerables = await helper.collatorSelection.getInvulnerables();48 const invulnerables = await helper.collatorSelection.getInvulnerables();
26 if(!invulnerables.includes(alice.address) || !invulnerables.includes(bob.address) || invulnerables.length != 2) {49
27 console.warn('Alice and Bob are not the invulnerables! Reinstating them back. '50 // Remove all invulnerables but the first one
28 + 'Current invulnerables\' size: ' + invulnerables.length);51 const firstInvulnerable = invulnerables[0];
2952
30 let nonce = await helper.chain.getNonce(alice.address);53 let nonce = await helper.chain.getNonce(superuser.address);
31 // In case there are too many invulnerables already, remove some of them, leaving space for Alice and Bob.
32 if(invulnerables.length + 2 >= helper.collatorSelection.maxCollators()) {
33 await Promise.all([54 await Promise.all(invulnerables.slice(1).map(invulnerable => helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [invulnerable], true, {nonce: nonce++})));
34 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [invulnerables.pop()!], true, {nonce: nonce++}),55
35 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [invulnerables.pop()!], true, {nonce: nonce++}),56 // Add the initial invulnerables
36 ]);
37 }
38
39 nonce = await helper.chain.getNonce(alice.address);
40 await Promise.all([57 await Promise.all(initialInvulnerables.map(invulnerable => helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [invulnerable], true, {nonce: nonce++})));
41 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [alice.address], true, {nonce: nonce++}),58
42 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [bob.address], true, {nonce: nonce++}),59 // Remove the first invulnerable if it's not an initial one
43 ]);
44
45 nonce = await helper.chain.getNonce(alice.address);
46 await Promise.all(invulnerables.map((invulnerable: any) => {
47 if(invulnerable == alice.address || invulnerable == bob.address) return new Promise<void>(res => res());60 if(!initialInvulnerables.includes(firstInvulnerable)) {
48 return helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [invulnerable], true, {nonce: nonce++});61 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [firstInvulnerable]);
49 }));62 }
50 }
51 });63 });
52}64}
5365
7284
73 describe('Dynamic shuffling of collators', () => {85 describe('Dynamic shuffling of collators', () => {
74 // These two are the default invulnerables, and should return to be invulnerables after this suite.86 // These two are the default invulnerables, and should return to be invulnerables after this suite.
75 let alice: IKeyringPair;87 let aliceNode: string;
76 let bob: IKeyringPair;88 let bobNode: string;
7789
78 let charlie: IKeyringPair;90 let charlieNode: string;
79 let dave: IKeyringPair;91 let daveNode: string;
8092
81 before(async function() {93 before(async function() {
82 await usingPlaygrounds(async (helper, privateKey) => {94 await usingPlaygrounds(async (helper, privateKey) => {
83 // todo:collator see again if blocks start to be finalized in dev mode95 // todo:collator see again if blocks start to be finalized in dev mode
84 // Skip the collator block production in dev mode, since the blocks are sealed automatically.96 // Skip the collator block production in dev mode, since the blocks are sealed automatically.
85 if(await helper.arrange.isDevNode()) this.skip();97 if(await helper.arrange.isDevNode()) this.skip();
8698
87 alice = await privateKey('//Alice');99 [aliceNode, bobNode, charlieNode, daveNode] = await getInitialInvulnerables();
88 bob = await privateKey('//Bob');
89 charlie = await privateKey('//Charlie');
90 dave = await privateKey('//Dave');
91100
92 expect((await helper.session.setOwnKeysFromAddress(charlie))101 const invulnerables = await helper.collatorSelection.getInvulnerables();
93 .status.toLowerCase()).to.be.equal('success');
94 expect((await helper.session.setOwnKeysFromAddress(dave))102 expect(invulnerables.length, 'Invalid initial invulnerables number').to.be.equal(4);
95 .status.toLowerCase()).to.be.equal('success');
96
97 const invulnerables = await helper.collatorSelection.getInvulnerables();
98 if(!invulnerables.includes(alice.address) || !invulnerables.includes(bob.address) || invulnerables.length != 2) {
99 console.warn('Alice and Bob are not the invulnerables! Reinstating them back. '103 expect(invulnerables, 'Invalid initial invulnerables').containSubset([aliceNode, bobNode, charlieNode, daveNode]);
100 + 'Current invulnerables\' size: ' + invulnerables.length);
101
102 let nonce = await helper.chain.getNonce(superuser.address);
103 await Promise.all([
104 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [alice.address], true, {nonce: nonce++}),
105 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [bob.address], true, {nonce: nonce++}),
106 ]);
107
108 nonce = await helper.chain.getNonce(superuser.address);
109 await Promise.all(invulnerables.map((invulnerable: any) => {
110 if(invulnerable == alice.address || invulnerable == bob.address) return new Promise((res) => res);
111 return helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [invulnerable], true, {nonce: nonce++});
112 }));
113 }
114 });104 });
115 });105 });
116106
117 itSub('Change invulnerables and make sure they start producing blocks', async ({helper}) => {107 itSub('Change invulnerables and make sure they start producing blocks', async ({helper}) => {
118 let nonce = await helper.chain.getNonce(superuser.address);108 let nonce = await helper.chain.getNonce(superuser.address);
119 await expect(Promise.all([
120 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [charlie.address], true, {nonce: nonce++}),
121 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [dave.address], true, {nonce: nonce++}),
122 ])).to.be.fulfilled;
123109
124 nonce = await helper.chain.getNonce(superuser.address);110 nonce = await helper.chain.getNonce(superuser.address);
125 await expect(Promise.all([111 await expect(Promise.all([
126 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [alice.address], true, {nonce: nonce++}),112 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [aliceNode], true, {nonce: nonce++}),
127 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [bob.address], true, {nonce: nonce++}),113 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [bobNode], true, {nonce: nonce++}),
128 ])).to.be.fulfilled;114 ])).to.be.fulfilled;
129115
130 const newInvulnerables = await helper.collatorSelection.getInvulnerables();116 const newInvulnerables = await helper.collatorSelection.getInvulnerables();
131 expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);117 expect(newInvulnerables).to.contain(charlieNode).and.contain(daveNode).and.be.length(2);
132118
133 await helper.wait.newSessions(2);119 await helper.wait.newSessions(2);
134120
135 const newValidators = await helper.callRpc('api.query.session.validators');121 const newValidators = await helper.callRpc('api.query.session.validators');
136 expect(newValidators).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);122 expect(newValidators).to.contain(charlieNode).and.contain(daveNode).and.be.length(2);
137123
138 const lastBlockNumber = await helper.chain.getLatestBlockNumber();124 const lastBlockNumber = await helper.chain.getLatestBlockNumber();
139 await helper.wait.newBlocks(1);125 await helper.wait.newBlocks(1);
140 const lastCharlieBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [charlie.address])).toNumber();126 const lastCharlieBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [charlieNode])).toNumber();
141 const lastDaveBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [dave.address])).toNumber();127 const lastDaveBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [daveNode])).toNumber();
142 expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;128 expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;
143 });129 });
144130
145 after(async () => {131 after(async () => {
146 await usingPlaygrounds(async (helper) => {132 await resetInvulnerables();
147 if(await helper.arrange.isDevNode()) return;
148
149 let nonce = await helper.chain.getNonce(superuser.address);
150 await Promise.all([
151 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [alice.address], true, {nonce: nonce++}),
152 helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.addInvulnerable', [bob.address], true, {nonce: nonce++}),
153 ]);
154
155 nonce = await helper.chain.getNonce(superuser.address);
156 await Promise.all([
157 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [charlie.address], true, {nonce: nonce++}),
158 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.collatorSelection.removeInvulnerable', [dave.address], true, {nonce: nonce++}),
159 ]);
160 });
161 });133 });
162 });134 });
163135
164 describe('Getting and releasing licenses to collate', () => {136 describe('Getting and releasing licenses to collate', () => {
165 let charlie: IKeyringPair;
166 let dave: IKeyringPair;
167 let crowd: IKeyringPair[];137 let crowd: IKeyringPair[];
168138
169 before(async function() {139 before(async function() {
170 await usingPlaygrounds(async (helper, privateKey) => {140 await usingPlaygrounds(async (helper, privateKey) => {
171 charlie = await privateKey('//Charlie');
172 dave = await privateKey('//Dave');
173 crowd = await helper.arrange.createCrowd(20, 100n, superuser);141 crowd = await helper.arrange.createCrowd(20, 100n, superuser);
174142
175 // set session keys for everyone143 // set session keys for everyone
176 expect((await helper.session.setOwnKeysFromAddress(charlie))
177 .status.toLowerCase()).to.be.equal('success');
178 expect((await helper.session.setOwnKeysFromAddress(dave))
179 .status.toLowerCase()).to.be.equal('success');
180 await Promise.all(crowd.map(acc => helper.session.setOwnKeysFromAddress(acc)));144 await Promise.all(crowd.map(acc => helper.session.setOwnKeysFromAddress(acc)));
181 });145 });
182 });146 });
252 });216 });
253217
254 describe('Onboarding, collating, and offboarding as collator candidates', () => {218 describe('Onboarding, collating, and offboarding as collator candidates', () => {
255 // These two are the default invulnerables, and should return to be invulnerables after this suite.
256 let charlie: IKeyringPair;
257 let dave: IKeyringPair;
258 let crowd: IKeyringPair[];219 let crowd: IKeyringPair[];
259220
260 before(async function() {221 before(async function() {
261 await usingPlaygrounds(async (helper, privateKey) => {222 await usingPlaygrounds(async (helper, privateKey) => {
262 charlie = await privateKey('//Charlie');
263 dave = await privateKey('//Dave');
264 crowd = await helper.arrange.createCrowd(20, 100n, superuser);223 crowd = await helper.arrange.createCrowd(20, 100n, superuser);
265224
266 // set session keys for everyone225 // set session keys for everyone
267 expect((await helper.session.setOwnKeysFromAddress(charlie))
268 .status.toLowerCase()).to.be.equal('success');
269 expect((await helper.session.setOwnKeysFromAddress(dave))
270 .status.toLowerCase()).to.be.equal('success');
271 await Promise.all(crowd.map(acc => helper.session.setOwnKeysFromAddress(acc)));226 await Promise.all(crowd.map(acc => helper.session.setOwnKeysFromAddress(acc)));
272 });227 });
273 });228 });
341 });296 });
342297
343 describe('Addition and removal of invulnerables', () => {298 describe('Addition and removal of invulnerables', () => {
344 before(async function() {
345 await resetInvulnerables();
346 });
347
348 describe('Positive', () => {299 describe('Positive', () => {
349 itSub('Adds an invulnerable', async ({helper}) => {300 itSub('Adds an invulnerable', async ({helper}) => {
409 // 28 non-functioning collators, teehee.360 // 28 non-functioning collators, teehee.
410361
411 const invulnerablesLength = (await helper.collatorSelection.getInvulnerables()).length;362 const invulnerablesLength = (await helper.collatorSelection.getInvulnerables()).length;
412 const invulnerablesUntilLimit = helper.collatorSelection.maxCollators() - invulnerablesLength;363 const invulnerablesUntilLimit = (await helper.collatorSelection.getDesiredCollators()) - invulnerablesLength;
413 const newInvulnerables = await helper.arrange.createAccounts(Array(invulnerablesUntilLimit).fill(10n), superuser);364 const newInvulnerables = await helper.arrange.createAccounts(Array(invulnerablesUntilLimit).fill(10n), superuser);
414 const [lastInvulnerable] = await helper.arrange.createAccounts([10n], superuser);365 const [lastInvulnerable] = await helper.arrange.createAccounts([10n], superuser);
415366
449 expect(await helper.collatorSelection.getInvulnerables()).to.have.all.members(invulnerables);400 expect(await helper.collatorSelection.getInvulnerables()).to.have.all.members(invulnerables);
450 });401 });
402
403 after(async function() {
404 await resetInvulnerables();
405 });
451 });406 });
452 });407 });
453408