git.delta.rocks / unique-network / refs/commits / 32beeafd531f

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 {getBlockNumber, getGenericResult} from './util/helpers';2324let alice: IKeyringPair;25let bob: IKeyringPair;26let charlie: IKeyringPair;27let dave: IKeyringPair;28//let 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.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        // todo implement a timeout in case new blocks are not being produced? session length needed78      }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});