git.delta.rocks / unique-network / refs/commits / ca41422299a2

difftreelog

source

tests/src/shuffleCollators.test.ts4.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {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                // todo implement a timeout in case new blocks are not being produced78            }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            // todo add delay to check that new blocks are authored by these85        });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});