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

difftreelog

refactor(collator-selection) session config

Fahrrader2022-08-26parent: #06a95fc.patch.diff
in: master

4 files changed

modifiedruntime/common/config/substrate.rsdiffbeforeafterboth
--- a/runtime/common/config/substrate.rs
+++ b/runtime/common/config/substrate.rs
@@ -35,7 +35,6 @@
 	runtime_common::DealWithFees, Runtime, Event, Call, Origin, PalletInfo, System, Balances,
 	Treasury, SS58Prefix, Aura, Session, SessionKeys, CollatorSelection, Version,
 };
-use xcm::v1::BodyId;
 use up_common::{types::*, constants::*};
 
 parameter_types! {
@@ -218,9 +217,8 @@
 }
 
 parameter_types! {
-	pub const Period: u32 = 6 * HOURS;
-	pub const Offset: u32 = 0;
-	//pub const MaxAuthorities: u32 = 100_000;
+	pub const SessionPeriod: BlockNumber = 6 * HOURS;
+	pub const SessionOffset: BlockNumber = 0;
 }
 
 impl pallet_session::Config for Runtime {
@@ -228,8 +226,8 @@
 	type ValidatorId = <Self as frame_system::Config>::AccountId;
 	// we don't have stash and controller, thus we don't need the convert as well.
 	type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
-	type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;
-	type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;
+	type ShouldEndSession = pallet_session::PeriodicSessions<SessionPeriod, SessionOffset>;
+	type NextSessionRotation = pallet_session::PeriodicSessions<SessionPeriod, SessionOffset>;
 	type SessionManager = CollatorSelection;
 	// Essentially just Aura, but lets be pedantic.
 	type SessionHandler = <SessionKeys as sp_runtime::traits::OpaqueKeys>::KeyTypeIdProviders;
@@ -252,24 +250,20 @@
 	pub const PotId: PalletId = PalletId(*b"PotStake");
 	pub const MaxCandidates: u32 = 1000;
 	pub const MinCandidates: u32 = 5;
-	pub const SessionLength: BlockNumber = 6 * HOURS;
 	pub const MaxInvulnerables: u32 = 100;
-	pub const ExecutiveBody: BodyId = BodyId::Executive;
 }
-
-// We allow root only to execute privileged collator selection operations.
-pub type CollatorSelectionUpdateOrigin = EnsureRoot<AccountId>;
 
 impl pallet_collator_selection::Config for Runtime {
 	type Event = Event;
 	type Currency = Balances;
-	type UpdateOrigin = CollatorSelectionUpdateOrigin;
+	// We allow root only to execute privileged collator selection operations.
+	type UpdateOrigin = EnsureRoot<AccountId>;
 	type PotId = PotId;
 	type MaxCandidates = MaxCandidates;
 	type MinCandidates = MinCandidates;
 	type MaxInvulnerables = MaxInvulnerables;
-	// should be a multiple of session or things will get inconsistent
-	type KickThreshold = Period;
+	// Should be a multiple of session or things will get inconsistent.
+	type KickThreshold = SessionPeriod;
 	type ValidatorId = <Self as frame_system::Config>::AccountId;
 	type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
 	type ValidatorRegistration = Session;
modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -75,7 +75,7 @@
     "testScheduler": "mocha --timeout 9999999 -r ts-node/register ./**/scheduler.test.ts",
     "testSchedulingEVM": "mocha --timeout 9999999 -r ts-node/register ./**/eth/scheduling.test.ts",
     "testXcmTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/xcmTransfer.test.ts",
-    "testShuffleCollators": "mocha --timeout 9999999 -r ts-node/register ./**/shuffleCollators.test.ts",
+    "testCollatorSelection": "mocha --timeout 9999999 -r ts-node/register ./**/collatorSelection.test.ts",
     "testPalletPresence": "mocha --timeout 9999999 -r ts-node/register ./**/pallet-presence.test.ts",
     "testBlockProduction": "mocha --timeout 9999999 -r ts-node/register ./**/block-production.test.ts",
     "testEnableDisableTransfers": "mocha --timeout 9999999 -r ts-node/register ./**/enableDisableTransfer.test.ts",
addedtests/src/collatorSelection.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/collatorSelection.test.ts
@@ -0,0 +1,108 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import privateKey from './substrate/privateKey';
+import usingApi, {submitTransactionAsync} from './substrate/substrate-api';
+import waitNewBlocks from './substrate/wait-new-blocks';
+import {expect} from 'chai';
+import {getBlockNumber, getGenericResult} from './util/helpers';
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+let charlie: IKeyringPair;
+let dave: IKeyringPair;
+//let eve: IKeyringPair;
+
+// todo Most preferable to launch this test in parallel somehow -- or change the session period (6 hrs) for Opal specifically.
+describe('Integration Test: Dynamic shuffling of collators', () => {
+  before(async () => {    
+    await usingApi(async api => {
+      alice = privateKey('//Alice');
+      bob = privateKey('//Bob');
+      charlie = privateKey('//Charlie');
+      dave = privateKey('//Dave');
+      //eve = privateKey('//Eve');
+
+      const txC = api.tx.session.setKeys(
+        '0x' + Buffer.from(charlie.addressRaw).toString('hex'),
+        '0x0',
+      );
+      const eventsC = await submitTransactionAsync(charlie, txC);
+      const resultC = getGenericResult(eventsC);
+      expect(resultC.success).to.be.true;
+
+      const txD = api.tx.session.setKeys(
+        '0x' + Buffer.from(dave.addressRaw).toString('hex'),
+        '0x0',
+      );
+      const eventsD = await submitTransactionAsync(dave, txD);
+      const resultD = getGenericResult(eventsD);
+      expect(resultD.success).to.be.true;
+
+      const validators = (await api.query.session.validators()).toJSON();
+      expect(validators).to.contain(alice.address).and.contain(bob.address).and.be.length(2);
+    });
+  });
+
+  it('Change invulnerables and make sure they start producing blocks', async () => {
+    await usingApi(async (api) => {
+      const tx = api.tx.collatorSelection.setInvulnerables([
+        charlie.address,
+        dave.address,
+      ]);
+      const sudoTx = api.tx.sudo.sudo(tx as any);
+      const events = await submitTransactionAsync(alice, sudoTx);
+      const result = getGenericResult(events);
+      expect(result.success).to.be.true;
+
+      const newInvulnerables = (await api.query.collatorSelection.invulnerables()).toJSON();
+      expect(newInvulnerables).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);
+
+      const expectedSessionIndex = (await api.query.session.currentIndex() as any).toNumber() + 2;
+      let currentSessionIndex = -1;
+      console.log('Waiting for the session after the next.' 
+        + ' This might take a while -- check SessionPeriod in pallet_session::Config for session time.');
+      while (currentSessionIndex < expectedSessionIndex) {
+        await waitNewBlocks(api, 1);
+        currentSessionIndex = (await api.query.session.currentIndex() as any).toNumber();
+        // todo implement a timeout in case new blocks are not being produced? session length needed
+      }
+
+      const newValidators = (await api.query.session.validators()).toJSON();
+      expect(newValidators).to.contain(charlie.address).and.contain(dave.address).and.be.length(2);
+
+      const lastBlockNumber = await getBlockNumber(api);
+      await waitNewBlocks(api, 1);
+      const lastCharlieBlock = (await api.query.collatorSelection.lastAuthoredBlock(charlie.address) as any).toNumber();
+      const lastDaveBlock = (await api.query.collatorSelection.lastAuthoredBlock(dave.address) as any).toNumber();
+      expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;
+    });
+  });
+
+  after(async () => {
+    await usingApi(async (api) => {
+      const tx = api.tx.collatorSelection.setInvulnerables([
+        alice.address,
+        bob.address,
+      ]);
+      const sudoTx = api.tx.sudo.sudo(tx as any);
+      const events = await submitTransactionAsync(alice, sudoTx);
+      const result = getGenericResult(events);
+      expect(result.success).to.be.true;
+    });
+  });
+});
deletedtests/src/shuffleCollators.test.tsdiffbeforeafterboth
before · tests/src/shuffleCollators.test.ts
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      console.log('Waiting for the session after the next. This might take a while...');75      while (currentSessionIndex < expectedSessionIndex) {76        await waitNewBlocks(api, 1);77        currentSessionIndex = (await api.query.session.currentIndex() as any).toNumber();78        // todo implement a timeout in case new blocks are not being produced? session length needed79      }8081      const newValidators = (await api.query.session.validators()).toJSON();82      expect(newValidators).to.contain(charlie.address).and.contain(dave.address);83      expect(newValidators).to.be.length(2);8485      const lastBlockNumber = await getBlockNumber(api);86      await waitNewBlocks(api, 1);87      const lastCharlieBlock = (await api.query.collatorSelection.lastAuthoredBlock(charlie.address) as any).toNumber();88      const lastDaveBlock = (await api.query.collatorSelection.lastAuthoredBlock(dave.address) as any).toNumber();89      expect(lastCharlieBlock >= lastBlockNumber || lastDaveBlock >= lastBlockNumber).to.be.true;90    });91  });9293  after(async () => {94    await usingApi(async (api) => {95      const tx = api.tx.collatorSelection.setInvulnerables([96        alice.address,97        bob.address,98      ]);99      const sudoTx = api.tx.sudo.sudo(tx as any);100      const events = await submitTransactionAsync(alice, sudoTx);101      const result = getGenericResult(events);102      expect(result.success).to.be.true;103    });104  });105});