git.delta.rocks / unique-network / refs/commits / 5489e8c40ab3

difftreelog

source

tests/src/shuffleCollators.test.ts3.7 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 {ApiPromise} from '@polkadot/api';18import {IKeyringPair} from '@polkadot/types/types';19import privateKey from './substrate/privateKey';20import usingApi, { submitTransactionAsync } from './substrate/substrate-api';21import waitNewBlocks from './substrate/wait-new-blocks';22import {expect} from 'chai';23import { getGenericResult } from './util/helpers';24//import find from 'find-process';2526let alice: IKeyringPair;27let bob: IKeyringPair;28let charlie: IKeyringPair;29let dave: IKeyringPair;30let eve: IKeyringPair;3132const alice_port = 31200;33const bob_port = 31201;3435describe('Integration Test: Dynamic shuffling of collators', () => {36    before(async () => {    37        await usingApi(async () => {38            alice = privateKey('//Alice');39            bob = privateKey('//Bob');40            charlie = privateKey('//Charlie');41            dave = privateKey('//Dave');42            eve = privateKey('//Eve');43        });44    });4546    it('Change invulnerables and make sure they start producing blocks', async () => {47        await usingApi(async (api) => {48            const txC = api.tx.session.setKeys(49                '0x' + Buffer.from(charlie.addressRaw).toString('hex'),50                '0x0'51            );52            const eventsC = await submitTransactionAsync(charlie, txC);53            const resultC = getGenericResult(eventsC);54            expect(resultC.success).to.be.true;5556            const txD = api.tx.session.setKeys(57                '0x' + Buffer.from(dave.addressRaw).toString('hex'),58                '0x0'59            );60            const eventsD = await submitTransactionAsync(dave, txD);61            const resultD = getGenericResult(eventsD);62            expect(resultD.success).to.be.true;6364            const tx = api.tx.collatorSelection.setInvulnerables([65                charlie.address,66                dave.address67            ]);68            const sudoTx = api.tx.sudo.sudo(tx as any);69            const events = await submitTransactionAsync(alice, sudoTx);70            const result = getGenericResult(events);71            expect(result.success).to.be.true;7273            let newInvulnerables = (await api.query.collatorSelection.invulnerables()).toJSON();74            expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address);75            expect(newInvulnerables).to.be.length(2);7677            const expectedSessionIndex = (await api.query.session.currentIndex() as any).toNumber() + 2;78            let currentSessionIndex = -1;79            while (currentSessionIndex < expectedSessionIndex) {80                await waitNewBlocks(api, 1);81                currentSessionIndex = (await api.query.session.currentIndex() as any).toNumber();82                // todo implement a timeout in case new blocks are not being produced83            }8485            let newValidators = (await api.query.session.validators()).toJSON();86            expect(newValidators).to.contain(charlie.address).and.contain(dave.address);87            expect(newValidators).to.be.length(2);88        });89    });90});