git.delta.rocks / unique-network / refs/commits / 0602b8017b0f

difftreelog

source

tests/src/collatorSelection.test.ts4.5 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;2930// todo Most preferable to launch this test in parallel somehow -- or change the session period (6 hrs) for Opal specifically.31describe('Integration Test: Dynamic shuffling of collators', () => {32  before(async () => {    33    await usingApi(async api => {34      alice = privateKey('//Alice');35      bob = privateKey('//Bob');36      charlie = privateKey('//Charlie');37      dave = privateKey('//Dave');38      //eve = privateKey('//Eve');3940      const txC = api.tx.session.setKeys(41        '0x' + Buffer.from(charlie.addressRaw).toString('hex'),42        '0x0',43      );44      const eventsC = await submitTransactionAsync(charlie, txC);45      const resultC = getGenericResult(eventsC);46      expect(resultC.success).to.be.true;4748      const txD = api.tx.session.setKeys(49        '0x' + Buffer.from(dave.addressRaw).toString('hex'),50        '0x0',51      );52      const eventsD = await submitTransactionAsync(dave, txD);53      const resultD = getGenericResult(eventsD);54      expect(resultD.success).to.be.true;5556      const validators = (await api.query.session.validators()).toJSON();57      expect(validators).to.contain(alice.address).and.contain(bob.address).and.be.length(2);58    });59  });6061  it('Change invulnerables and make sure they start producing blocks', async () => {62    await usingApi(async (api) => {63      const tx = api.tx.collatorSelection.setInvulnerables([64        charlie.address,65        dave.address,66      ]);67      const sudoTx = api.tx.sudo.sudo(tx as any);68      const events = await submitTransactionAsync(alice, sudoTx);69      const result = getGenericResult(events);70      expect(result.success).to.be.true;7172      const newInvulnerables = (await api.query.collatorSelection.invulnerables()).toJSON();73      expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);7475      const expectedSessionIndex = (await api.query.session.currentIndex() as any).toNumber() + 2;76      let currentSessionIndex = -1;77      console.log('Waiting for the session after the next.' 78        + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');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 produced? session length needed83      }8485      const newValidators = (await api.query.session.validators()).toJSON();86      expect(newValidators).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);8788      const lastBlockNumber = await getBlockNumber(api);89      await waitNewBlocks(api, 1);90      const lastCharlieBlock = (await api.query.collatorSelection.lastAuthoredBlock(charlie.address) as any).toNumber();91      const lastDaveBlock = (await api.query.collatorSelection.lastAuthoredBlock(dave.address) as any).toNumber();92      expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;93    });94  });9596  after(async () => {97    await usingApi(async (api) => {98      const tx = api.tx.collatorSelection.setInvulnerables([99        alice.address,100        bob.address,101      ]);102      const sudoTx = api.tx.sudo.sudo(tx as any);103      const events = await submitTransactionAsync(alice, sudoTx);104      const result = getGenericResult(events);105      expect(result.success).to.be.true;106    });107  });108});