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 while (currentSessionIndex < expectedSessionIndex) {75 await waitNewBlocks(api, 1);76 currentSessionIndex = (await api.query.session.currentIndex() as any).toNumber();77 78 }7980 const newValidators = (await api.query.session.validators()).toJSON();81 expect(newValidators).to.contain(charlie.address).and.contain(dave.address);82 expect(newValidators).to.be.length(2);8384 const lastBlockNumber = await getBlockNumber(api);85 await waitNewBlocks(api, 1);86 const lastCharlieBlock = (await api.query.collatorSelection.lastAuthoredBlock(charlie.address) as any).toNumber();87 const lastDaveBlock = (await api.query.collatorSelection.lastAuthoredBlock(dave.address) as any).toNumber();88 expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;89 });90 });9192 after(async () => {93 await usingApi(async (api) => {94 const tx = api.tx.collatorSelection.setInvulnerables([95 alice.address,96 bob.address,97 ]);98 const sudoTx = api.tx.sudo.sudo(tx as any);99 const events = await submitTransactionAsync(alice, sudoTx);100 const result = getGenericResult(events);101 expect(result.success).to.be.true;102 });103 });104});