1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import privateKey from './substrate/privateKey';19import usingApi, {submitTransactionAsync} from './substrate/substrate-api';20import waitNewBlocks from './substrate/wait-new-blocks';21import {expect} from 'chai';22import {getBlockNumber, getGenericResult} from './util/helpers';2324let alice: IKeyringPair;25let bob: IKeyringPair;26let charlie: IKeyringPair;27let dave: IKeyringPair;28293031describe('Integration Test: Dynamic shuffling of collators', () => {32 before(async () => { 33 await usingApi(async api => {34 alice = privateKey('//Alice');35 bob = privateKey('//Bob');36 charlie = privateKey('//Charlie');37 dave = privateKey('//Dave');38 3940 const txC = api.tx.session.setKeys(41 '0x' + Buffer.from(charlie.addressRaw).toString('hex'),42 '0x0',43 );44 const eventsC = await submitTransactionAsync(charlie, txC);45 const resultC = getGenericResult(eventsC);46 expect(resultC.success).to.be.true;4748 const txD = api.tx.session.setKeys(49 '0x' + Buffer.from(dave.addressRaw).toString('hex'),50 '0x0',51 );52 const eventsD = await submitTransactionAsync(dave, txD);53 const resultD = getGenericResult(eventsD);54 expect(resultD.success).to.be.true;5556 const validators = (await api.query.session.validators()).toJSON();57 expect(validators).to.contain(alice.address).and.contain(bob.address).and.be.length(2);58 });59 });6061 it('Change invulnerables and make sure they start producing blocks', async () => {62 await usingApi(async (api) => {63 const tx = api.tx.collatorSelection.setInvulnerables([64 charlie.address,65 dave.address,66 ]);67 const sudoTx = api.tx.sudo.sudo(tx as any);68 const events = await submitTransactionAsync(alice, sudoTx);69 const result = getGenericResult(events);70 expect(result.success).to.be.true;7172 const newInvulnerables = (await api.query.collatorSelection.invulnerables()).toJSON();73 expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);7475 const expectedSessionIndex = (await api.query.session.currentIndex() as any).toNumber() + 2;76 let currentSessionIndex = -1;77 console.log('Waiting for the session after the next.' 78 + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');79 while (currentSessionIndex < expectedSessionIndex) {80 await waitNewBlocks(api, 1);81 currentSessionIndex = (await api.query.session.currentIndex() as any).toNumber();82 83 }8485 const newValidators = (await api.query.session.validators()).toJSON();86 expect(newValidators).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);8788 const lastBlockNumber = await getBlockNumber(api);89 await waitNewBlocks(api, 1);90 const lastCharlieBlock = (await api.query.collatorSelection.lastAuthoredBlock(charlie.address) as any).toNumber();91 const lastDaveBlock = (await api.query.collatorSelection.lastAuthoredBlock(dave.address) as any).toNumber();92 expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;93 });94 });9596 after(async () => {97 await usingApi(async (api) => {98 const tx = api.tx.collatorSelection.setInvulnerables([99 alice.address,100 bob.address,101 ]);102 const sudoTx = api.tx.sudo.sudo(tx as any);103 const events = await submitTransactionAsync(alice, sudoTx);104 const result = getGenericResult(events);105 expect(result.success).to.be.true;106 });107 });108});