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 { getGenericResult } from './util/helpers';2324let alice: IKeyringPair;25let bob: IKeyringPair;26let charlie: IKeyringPair;27let dave: IKeyringPair;28let eve: IKeyringPair;2930describe('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 eve = privateKey('//Eve');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.address62 ]);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 let 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 let 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 85 });86 });8788 after(async () => {89 await usingApi(async (api) => {90 const tx = api.tx.collatorSelection.setInvulnerables([91 alice.address,92 bob.address93 ]);94 const sudoTx = api.tx.sudo.sudo(tx as any);95 const events = await submitTransactionAsync(alice, sudoTx);96 const result = getGenericResult(events);97 expect(result.success).to.be.true;98 });99 });100});