1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, itSub, Pallets, requirePalletsOrSkip} from './util';192021describe('Integration Test: Dynamic shuffling of collators', () => {22 let superuser: IKeyringPair;2324 25 let aliceAddress: string;26 let bobAddress: string;2728 let charlie: IKeyringPair;29 let dave: IKeyringPair;30 3132 before(async function() {33 await usingPlaygrounds(async (helper, privateKey) => {34 requirePalletsOrSkip(this, helper, [Pallets.CollatorSelection]);3536 37 38 charlie = await privateKey('//Charlie');39 dave = await privateKey('//Dave');4041 superuser = await privateKey('//Alice');42 aliceAddress = (await privateKey('//Alice')).address;43 bobAddress = (await privateKey('//Bob')).address;4445 expect((await helper.executeExtrinsic(charlie, 'api.tx.session.setKeys', [46 '0x' + Buffer.from(charlie.addressRaw).toString('hex'),47 '0x0',48 ])).status.toLowerCase()).to.be.equal('success');4950 expect((await helper.executeExtrinsic(dave, 'api.tx.session.setKeys', [51 '0x' + Buffer.from(dave.addressRaw).toString('hex'),52 '0x0',53 ])).status.toLowerCase()).to.be.equal('success');5455 const validators = await helper.callRpc('api.query.session.validators');56 expect(validators).to.not.contain(charlie.address).and.not.contain(dave.address);57 });58 });5960 itSub('Change invulnerables and make sure they start producing blocks', async ({helper}) => {6162 const tx = helper.constructApiCall('api.tx.collatorSelection.setInvulnerables', [[63 charlie.address,64 dave.address,65 ]]);66 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [tx])).to.be.fulfilled;6768 const newInvulnerables = await helper.callRpc('api.query.collatorSelection.invulnerables');69 expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);7071 const expectedSessionIndex = (await helper.callRpc('api.query.session.currentIndex')).toNumber() + 2;72 let currentSessionIndex = -1;73 console.log('Waiting for the session after the next.' 74 + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');7576 while (currentSessionIndex < expectedSessionIndex) {77 78 currentSessionIndex = await expect(helper.wait.withTimeout(new Promise(async (resolve) => {79 await helper.wait.newBlocks(1);80 const res = (await helper.callRpc('api.query.session.currentIndex')).toNumber();81 resolve(res);82 }), 24000, 'The chain has stopped producing blocks!')).to.be.fulfilled;83 }8485 const newValidators = await helper.callRpc('api.query.session.validators');86 expect(newValidators).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);8788 const lastBlockNumber = await helper.chain.getLatestBlockNumber();89 await helper.wait.newBlocks(1);90 const lastCharlieBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [charlie.address])).toNumber();91 const lastDaveBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [dave.address])).toNumber();92 expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;93 });9495 after(async () => {96 await usingPlaygrounds(async (helper) => {97 if (helper.fetchMissingPalletNames([Pallets.AppPromotion]).length != 0) return;9899 const tx = helper.constructApiCall('api.tx.collatorSelection.setInvulnerables', [[100 aliceAddress,101 bobAddress,102 ]]);103 await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [tx])).to.be.fulfilled;104 });105 });106});