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;282930describe('Integration Test: Dynamic shuffling of collators', () => {31 before(async () => { 32 await usingApi(async () => {33 alice = privateKey('//Alice');34 bob = privateKey('//Bob');35 charlie = privateKey('//Charlie');36 dave = privateKey('//Dave');37 38 });39 });4041 it('Change invulnerables and make sure they start producing blocks', async () => {42 await usingApi(async (api) => {43 const txC = api.tx.session.setKeys(44 '0x' + Buffer.from(charlie.addressRaw).toString('hex'),45 '0x0',46 );47 const eventsC = await submitTransactionAsync(charlie, txC);48 const resultC = getGenericResult(eventsC);49 expect(resultC.success).to.be.true;5051 const txD = api.tx.session.setKeys(52 '0x' + Buffer.from(dave.addressRaw).toString('hex'),53 '0x0',54 );55 const eventsD = await submitTransactionAsync(dave, txD);56 const resultD = getGenericResult(eventsD);57 expect(resultD.success).to.be.true;5859 const tx = api.tx.collatorSelection.setInvulnerables([60 charlie.address,61 dave.address,62 ]);63 const sudoTx = api.tx.sudo.sudo(tx as any);64 const events = await submitTransactionAsync(alice, sudoTx);65 const result = getGenericResult(events);66 expect(result.success).to.be.true;6768 const newInvulnerables = (await api.query.collatorSelection.invulnerables()).toJSON();69 expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address);70 expect(newInvulnerables).to.be.length(2);7172 const expectedSessionIndex = (await api.query.session.currentIndex() as any).toNumber() + 2;73 let currentSessionIndex = -1;74 console.log('Waiting for the session after the next. This might take a while...');75 while (currentSessionIndex < expectedSessionIndex) {76 await waitNewBlocks(api, 1);77 currentSessionIndex = (await api.query.session.currentIndex() as any).toNumber();78 79 }8081 const newValidators = (await api.query.session.validators()).toJSON();82 expect(newValidators).to.contain(charlie.address).and.contain(dave.address);83 expect(newValidators).to.be.length(2);8485 const lastBlockNumber = await getBlockNumber(api);86 await waitNewBlocks(api, 1);87 const lastCharlieBlock = (await api.query.collatorSelection.lastAuthoredBlock(charlie.address) as any).toNumber();88 const lastDaveBlock = (await api.query.collatorSelection.lastAuthoredBlock(dave.address) as any).toNumber();89 expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;90 });91 });9293 after(async () => {94 await usingApi(async (api) => {95 const tx = api.tx.collatorSelection.setInvulnerables([96 alice.address,97 bob.address,98 ]);99 const sudoTx = api.tx.sudo.sudo(tx as any);100 const events = await submitTransactionAsync(alice, sudoTx);101 const result = getGenericResult(events);102 expect(result.success).to.be.true;103 });104 });105});