git.delta.rocks / unique-network / refs/commits / 8f7419bc1620

difftreelog

source

tests/src/collatorSelection.test.ts4.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 {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, itSub, Pallets, requirePalletsOrSkip} from './util';1920// todo Most preferable to launch this test in parallel somehow -- or change the session period (1 hr).21describe('Integration Test: Dynamic shuffling of collators', () => {22  let superuser: IKeyringPair;2324  // These are the default invulnerables, and should return to be invulnerables after this suite.25  let aliceAddress: string;26  let bobAddress: string;2728  let charlie: IKeyringPair;29  let dave: IKeyringPair;30  //let eve: IKeyringPair;3132  before(async function() {33    await usingPlaygrounds(async (helper, privateKey) => {34      requirePalletsOrSkip(this, helper, [Pallets.CollatorSelection]);3536      //const donor = await privateKey({filename: __filename});37      //[charlie, dave] = await helper.arrange.createAccounts([100n, 100n], donor);38      charlie = await privateKey('//Charlie');39      dave = await privateKey('//Dave');4041      superuser = await privateKey('//Alice');42      aliceAddress = (await privateKey('//Alice')).address;43      bobAddress = (await privateKey('//Bob')).address;4445      expect((await helper.executeExtrinsic(charlie, 'api.tx.session.setKeys', [46        '0x' + Buffer.from(charlie.addressRaw).toString('hex'),47        '0x0',48      ])).status.toLowerCase()).to.be.equal('success');4950      expect((await helper.executeExtrinsic(dave, 'api.tx.session.setKeys', [51        '0x' + Buffer.from(dave.addressRaw).toString('hex'),52        '0x0',53      ])).status.toLowerCase()).to.be.equal('success');5455      const validators = await helper.callRpc('api.query.session.validators');56      expect(validators).to.not.contain(charlie.address).and.not.contain(dave.address);57    });58  });5960  itSub('Change invulnerables and make sure they start producing blocks', async ({helper}) => {6162    const tx = helper.constructApiCall('api.tx.collatorSelection.setInvulnerables', [[63      charlie.address,64      dave.address,65    ]]);66    await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [tx])).to.be.fulfilled;6768    const newInvulnerables = await helper.callRpc('api.query.collatorSelection.invulnerables');69    expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);7071    const expectedSessionIndex = (await helper.callRpc('api.query.session.currentIndex')).toNumber() + 2;72    let currentSessionIndex = -1;73    console.log('Waiting for the session after the next.' 74      + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');7576    while (currentSessionIndex < expectedSessionIndex) {77      // eslint-disable-next-line no-async-promise-executor78      currentSessionIndex = await expect(helper.wait.withTimeout(new Promise(async (resolve) => {79        await helper.wait.newBlocks(1);80        const res = (await helper.callRpc('api.query.session.currentIndex')).toNumber();81        resolve(res);82      }), 24000, 'The chain has stopped producing blocks!')).to.be.fulfilled;83    }8485    const newValidators = await helper.callRpc('api.query.session.validators');86    expect(newValidators).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);8788    const lastBlockNumber = await helper.chain.getLatestBlockNumber();89    await helper.wait.newBlocks(1);90    const lastCharlieBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [charlie.address])).toNumber();91    const lastDaveBlock = (await helper.callRpc('api.query.collatorSelection.lastAuthoredBlock', [dave.address])).toNumber();92    expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;93  });9495  after(async () => {96    await usingPlaygrounds(async (helper) => {97      if (helper.fetchMissingPalletNames([Pallets.AppPromotion]).length != 0) return;9899      const tx = helper.constructApiCall('api.tx.collatorSelection.setInvulnerables', [[100        aliceAddress,101        bobAddress,102      ]]);103      await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [tx])).to.be.fulfilled;104    });105  });106});