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
35 runtime_common::DealWithFees, Runtime, Event, Call, Origin, PalletInfo, System, Balances,35 runtime_common::DealWithFees, Runtime, Event, Call, Origin, PalletInfo, System, Balances,
36 Treasury, SS58Prefix, Aura, Session, SessionKeys, CollatorSelection, Version,36 Treasury, SS58Prefix, Aura, Session, SessionKeys, CollatorSelection, Version,
37};37};
38use xcm::v1::BodyId;
39use up_common::{types::*, constants::*};38use up_common::{types::*, constants::*};
4039
41parameter_types! {40parameter_types! {
218}217}
219218
220parameter_types! {219parameter_types! {
221 pub const Period: u32 = 6 * HOURS;220 pub const SessionPeriod: BlockNumber = 6 * HOURS;
222 pub const Offset: u32 = 0;221 pub const SessionOffset: BlockNumber = 0;
223 //pub const MaxAuthorities: u32 = 100_000;
224}222}
225223
226impl pallet_session::Config for Runtime {224impl pallet_session::Config for Runtime {
227 type Event = Event;225 type Event = Event;
228 type ValidatorId = <Self as frame_system::Config>::AccountId;226 type ValidatorId = <Self as frame_system::Config>::AccountId;
229 // we don't have stash and controller, thus we don't need the convert as well.227 // we don't have stash and controller, thus we don't need the convert as well.
230 type ValidatorIdOf = pallet_collator_selection::IdentityCollator;228 type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
231 type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;229 type ShouldEndSession = pallet_session::PeriodicSessions<SessionPeriod, SessionOffset>;
232 type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;230 type NextSessionRotation = pallet_session::PeriodicSessions<SessionPeriod, SessionOffset>;
233 type SessionManager = CollatorSelection;231 type SessionManager = CollatorSelection;
234 // Essentially just Aura, but lets be pedantic.232 // Essentially just Aura, but lets be pedantic.
235 type SessionHandler = <SessionKeys as sp_runtime::traits::OpaqueKeys>::KeyTypeIdProviders;233 type SessionHandler = <SessionKeys as sp_runtime::traits::OpaqueKeys>::KeyTypeIdProviders;
252 pub const PotId: PalletId = PalletId(*b"PotStake");250 pub const PotId: PalletId = PalletId(*b"PotStake");
253 pub const MaxCandidates: u32 = 1000;251 pub const MaxCandidates: u32 = 1000;
254 pub const MinCandidates: u32 = 5;252 pub const MinCandidates: u32 = 5;
255 pub const SessionLength: BlockNumber = 6 * HOURS;
256 pub const MaxInvulnerables: u32 = 100;253 pub const MaxInvulnerables: u32 = 100;
257 pub const ExecutiveBody: BodyId = BodyId::Executive;
258}254}
259
260// We allow root only to execute privileged collator selection operations.
261pub type CollatorSelectionUpdateOrigin = EnsureRoot<AccountId>;
262255
263impl pallet_collator_selection::Config for Runtime {256impl pallet_collator_selection::Config for Runtime {
264 type Event = Event;257 type Event = Event;
265 type Currency = Balances;258 type Currency = Balances;
259 // We allow root only to execute privileged collator selection operations.
266 type UpdateOrigin = CollatorSelectionUpdateOrigin;260 type UpdateOrigin = EnsureRoot<AccountId>;
267 type PotId = PotId;261 type PotId = PotId;
268 type MaxCandidates = MaxCandidates;262 type MaxCandidates = MaxCandidates;
269 type MinCandidates = MinCandidates;263 type MinCandidates = MinCandidates;
270 type MaxInvulnerables = MaxInvulnerables;264 type MaxInvulnerables = MaxInvulnerables;
271 // should be a multiple of session or things will get inconsistent265 // Should be a multiple of session or things will get inconsistent.
272 type KickThreshold = Period;266 type KickThreshold = SessionPeriod;
273 type ValidatorId = <Self as frame_system::Config>::AccountId;267 type ValidatorId = <Self as frame_system::Config>::AccountId;
274 type ValidatorIdOf = pallet_collator_selection::IdentityCollator;268 type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
275 type ValidatorRegistration = Session;269 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
--- a/tests/src/shuffleCollators.test.ts
+++ /dev/null
@@ -1,105 +0,0 @@
-// 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;
-
-describe('Integration Test: Dynamic shuffling of collators', () => {
-  before(async () => {    
-    await usingApi(async () => {
-      alice = privateKey('//Alice');
-      bob = privateKey('//Bob');
-      charlie = privateKey('//Charlie');
-      dave = privateKey('//Dave');
-      //eve = privateKey('//Eve');
-    });
-  });
-
-  it('Change invulnerables and make sure they start producing blocks', async () => {
-    await usingApi(async (api) => {
-      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 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);
-      expect(newInvulnerables).to.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...');
-      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);
-      expect(newValidators).to.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;
-    });
-  });
-});