git.delta.rocks / unique-network / refs/commits / 9667dc5f64e7

difftreelog

feat(CollatorSelection) inclusion + migration + genesis

Fahrrader2022-03-30parent: #997ec5a.patch.diff
in: master

10 files changed

modifiedCargo.lockdiffbeforeafterboth
before · Cargo.lock
971 packageslockfile v3
after · Cargo.lock
972 packageslockfile v3
modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
--- a/node/cli/src/chain_spec.rs
+++ b/node/cli/src/chain_spec.rs
@@ -24,6 +24,7 @@
 use serde_json::map::Map;
 
 use up_common::types::opaque::*;
+use up_common::constants::EXISTENTIAL_DEPOSIT;
 
 #[cfg(feature = "unique-runtime")]
 pub use unique_runtime as default_runtime;
@@ -146,7 +147,7 @@
 	(
 		$runtime:path,
 		$root_key:expr,
-		$initial_authorities:expr,
+		$initial_invulnerables:expr,
 		$endowed_accounts:expr,
 		$id:expr
 	) => {{
@@ -175,9 +176,31 @@
 				parachain_id: $id.into(),
 			},
 			parachain_system: Default::default(),
-			aura: AuraConfig {
-				authorities: $initial_authorities,
+			collator_selection: CollatorSelectionConfig {
+				invulnerables: $initial_invulnerables
+					.iter()
+					.cloned()
+					.map(|(acc, _)| acc)
+					.collect(),
+				candidacy_bond: EXISTENTIAL_DEPOSIT * 16,
+				..Default::default()
+			},
+			session: SessionConfig {
+				keys: $initial_invulnerables
+					.into_iter()
+					.map(|(acc, aura)| {
+						(
+							acc.clone(),          // account id
+							acc,                  // validator id
+							SessionKeys { aura }, // session keys
+						)
+					})
+					.collect(),
 			},
+			aura: Default::default(),
+			/*aura: AuraConfig {
+				authorities: $initial_authorities,
+			},*/
 			aura_ext: Default::default(),
 			evm: EVMConfig {
 				accounts: BTreeMap::new(),
@@ -217,8 +240,14 @@
 				// Sudo account
 				get_account_id_from_seed::<sr25519::Public>("Alice"),
 				vec![
-					get_from_seed::<AuraId>("Alice"),
-					get_from_seed::<AuraId>("Bob"),
+					(
+						get_account_id_from_seed::<sr25519::Public>("Alice"),
+						get_from_seed::<AuraId>("Alice"),
+					),
+					(
+						get_account_id_from_seed::<sr25519::Public>("Bob"),
+						get_from_seed::<AuraId>("Bob"),
+					),
 				],
 				// Pre-funded accounts
 				vec![
@@ -285,8 +314,14 @@
 				// Sudo account
 				get_account_id_from_seed::<sr25519::Public>("Alice"),
 				vec![
-					get_from_seed::<AuraId>("Alice"),
-					get_from_seed::<AuraId>("Bob"),
+					(
+						get_account_id_from_seed::<sr25519::Public>("Alice"),
+						get_from_seed::<AuraId>("Alice"),
+					),
+					(
+						get_account_id_from_seed::<sr25519::Public>("Bob"),
+						get_from_seed::<AuraId>("Bob"),
+					),
 				],
 				// Pre-funded accounts
 				vec![
modifiedprimitives/common/src/constants.rsdiffbeforeafterboth
--- a/primitives/common/src/constants.rs
+++ b/primitives/common/src/constants.rs
@@ -35,6 +35,8 @@
 pub const CENTIUNIQUE: Balance = 10 * MILLIUNIQUE;
 pub const UNIQUE: Balance = 100 * CENTIUNIQUE;
 
+pub const EXISTENTIAL_DEPOSIT: u128 = 0;
+
 // Targeting 0.1 UNQ per transfer
 pub const WEIGHT_TO_FEE_COEFF: u32 = /*<weight2fee>*/207_163_598/*</weight2fee>*/;
 
modifiedruntime/common/config/substrate.rsdiffbeforeafterboth
--- a/runtime/common/config/substrate.rs
+++ b/runtime/common/config/substrate.rs
@@ -33,8 +33,9 @@
 };
 use crate::{
 	runtime_common::DealWithFees, Runtime, Event, Call, Origin, PalletInfo, System, Balances,
-	Treasury, SS58Prefix, Version,
+	Treasury, SS58Prefix, Aura, Session, SessionKeys, CollatorSelection, Version,
 };
+use xcm::v1::BodyId;
 use up_common::{types::*, constants::*};
 
 parameter_types! {
@@ -128,7 +129,7 @@
 
 parameter_types! {
 	// pub const ExistentialDeposit: u128 = 500;
-	pub const ExistentialDeposit: u128 = 0;
+	pub const ExistentialDeposit: u128 = EXISTENTIAL_DEPOSIT;
 	pub const MaxLocks: u32 = 50;
 	pub const MaxReserves: u32 = 50;
 }
@@ -215,3 +216,62 @@
 	type DisabledValidators = ();
 	type MaxAuthorities = MaxAuthorities;
 }
+
+parameter_types! {
+	pub const Period: u32 = 6 * HOURS;
+	pub const Offset: u32 = 0;
+	//pub const MaxAuthorities: u32 = 100_000;
+}
+
+impl pallet_session::Config for Runtime {
+	type Event = Event;
+	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 SessionManager = CollatorSelection;
+	// Essentially just Aura, but lets be pedantic.
+	type SessionHandler = <SessionKeys as sp_runtime::traits::OpaqueKeys>::KeyTypeIdProviders;
+	type Keys = SessionKeys;
+	type WeightInfo = ();
+}
+
+parameter_types! {
+	pub const UncleGenerations: u32 = 0;
+}
+
+impl pallet_authorship::Config for Runtime {
+	type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
+	type UncleGenerations = UncleGenerations;
+	type FilterUncle = ();
+	type EventHandler = (CollatorSelection,);
+}
+
+parameter_types! {
+	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;
+	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;
+	type ValidatorId = <Self as frame_system::Config>::AccountId;
+	type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
+	type ValidatorRegistration = Session;
+	type WeightInfo = ();
+}
modifiedruntime/common/construct_runtime/mod.rsdiffbeforeafterboth
--- a/runtime/common/construct_runtime/mod.rs
+++ b/runtime/common/construct_runtime/mod.rs
@@ -32,8 +32,11 @@
                 ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Config, Storage, Inherent, Event<T>, ValidateUnsigned} = 20,
                 ParachainInfo: parachain_info::{Pallet, Storage, Config} = 21,
 
-                Aura: pallet_aura::{Pallet, Config<T>} = 22,
-                AuraExt: cumulus_pallet_aura_ext::{Pallet, Config} = 23,
+                Authorship: pallet_authorship::{Pallet, Call, Storage} = 22,
+                CollatorSelection: pallet_collator_selection::{Pallet, Call, Storage, Event<T>, Config<T>} = 23,
+                Session: pallet_session::{Pallet, Call, Storage, Event, Config<T>} = 24,
+                Aura: pallet_aura::{Pallet, Config<T>} = 25,
+                AuraExt: cumulus_pallet_aura_ext::{Pallet, Config} = 26,
 
                 Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>} = 30,
                 RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Pallet, Storage} = 31,
modifiedruntime/common/mod.rsdiffbeforeafterboth
--- a/runtime/common/mod.rs
+++ b/runtime/common/mod.rs
@@ -25,7 +25,10 @@
 pub mod weights;
 
 use sp_core::H160;
-use frame_support::traits::{Currency, OnUnbalanced, Imbalance};
+use frame_support::{
+	traits::{Currency, OnUnbalanced, Imbalance},
+	weights::Weight,
+};
 use sp_runtime::{
 	generic,
 	traits::{BlakeTwo256, BlockNumberProvider},
@@ -103,6 +106,7 @@
 	frame_system::ChainContext<Runtime>,
 	Runtime,
 	AllPalletsReversedWithSystemFirst,
+	AuraToCollatorSelection,
 >;
 
 type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;
@@ -165,3 +169,158 @@
 	/// Transfer tokens to the given account from the Parachain account.
 	TransferToken(XAccountId, XBalance),
 }
+
+pub struct AuraToCollatorSelection;
+impl frame_support::traits::OnRuntimeUpgrade for AuraToCollatorSelection {
+	fn on_runtime_upgrade() -> Weight {
+		use frame_support::{BoundedVec, storage::migration};
+		use sp_runtime::{
+			traits::{OpaqueKeys, Saturating},
+			RuntimeAppPublic,
+		};
+		use pallet_session::SessionManager;
+		use up_common::constants::EXISTENTIAL_DEPOSIT;
+		use crate::config::substrate::MaxInvulnerables;
+
+		let mut weight = <Runtime as frame_system::Config>::DbWeight::get().reads(1);
+
+		let version =
+			migration::get_storage_value::<()>(b"AuraToCollatorSelection", b"StorageVersion", &[]);
+
+		let should_upgrade = match version {
+			None => true,
+			Some(_) => false,
+		};
+
+		if should_upgrade {
+			log::info!(
+				target: "runtime::aura_to_collator_selection",
+				"Running migration of Aura authorities to Collator Selection invulnerables"
+			);
+
+			let invulnerables = pallet_aura::Pallet::<Runtime>::authorities()
+				.iter()
+				.cloned()
+				.filter_map(|authority_id| {
+					weight.saturating_accrue(<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1));
+					let vec = authority_id.clone().to_raw_vec();
+					let slice = vec.as_slice();
+					let array: Option<[u8; 32]> = match slice.try_into() {
+						Ok(a) => Some(a),
+						Err(_) => {
+							log::error!("Failed to convert an Aura authority to a Collator Selection invulnerable: {:?}", authority_id);
+							None
+						},
+					};
+					array.map(|a| (AccountId::from(a), authority_id))
+				})
+				.collect::<Vec<_>>();
+
+			let bounded_invulnerables = BoundedVec::<_, MaxInvulnerables>::try_from(
+				invulnerables
+					.iter()
+					.cloned()
+					.map(|(acc, _)| acc)
+					.collect::<Vec<_>>(),
+			)
+			.expect("Existing collators/invulnerables are more than MaxInvulnerables");
+			
+			<pallet_collator_selection::Invulnerables<Runtime>>::put(bounded_invulnerables);
+			<pallet_collator_selection::DesiredCandidates<Runtime>>::put(0);
+			<pallet_collator_selection::CandidacyBond<Runtime>>::put(EXISTENTIAL_DEPOSIT * 16);
+
+			let keys = invulnerables
+				.into_iter()
+				.map(|(acc, aura)| {
+					(
+						acc.clone(),                        // account id
+						acc,                                // validator id
+						SessionKeys { aura: aura.clone() }, // session keys
+					)
+				})
+				.collect::<Vec<_>>();
+
+			for (account, val, keys) in keys.iter().cloned() {
+				for id in <Runtime as pallet_session::Config>::Keys::key_ids() {
+					<pallet_session::KeyOwner<Runtime>>::insert((*id, keys.get_raw(*id)), &val)
+				}
+				<pallet_session::NextKeys<Runtime>>::insert(&val, &keys);
+				// todo exercise caution, the following is taken from genesis
+				if frame_system::Pallet::<Runtime>::inc_consumers_without_limit(&account).is_err() {
+					log::warn!(
+						"We have entered an error with incrementing consumers without limit"
+					);
+					// This will leak a provider reference, however it only happens once (at
+					// genesis) so it's really not a big deal and we assume that the user wants to
+					// do this since it's the only way a non-endowed account can contain a session
+					// key.
+					frame_system::Pallet::<Runtime>::inc_providers(&account);
+				}
+			}
+
+			let initial_validators_0 =
+				<Runtime as pallet_session::Config>::SessionManager::new_session(0).unwrap_or_else(
+					|| {
+						frame_support::print(
+							"No initial validator provided by `SessionManager`, use \
+						session config keys to generate initial validator set.",
+						);
+						keys.iter().map(|x| x.1.clone()).collect()
+					},
+				);
+			/*assert!(
+				!initial_validators_0.is_empty(),
+				"Empty validator set for session 0 in (pseudo) genesis block!"
+			);*/
+
+			let initial_validators_1 =
+				<Runtime as pallet_session::Config>::SessionManager::new_session(1)
+					.unwrap_or_else(|| initial_validators_0.clone());
+			/*assert!(
+				!initial_validators_1.is_empty(),
+				"Empty validator set for session 1 in (pseudo) genesis block!"
+			);*/
+
+			let queued_keys: Vec<_> = initial_validators_1
+				.iter()
+				.cloned()
+				.map(|v| {
+					(
+						v.clone(),
+						<pallet_session::NextKeys<Runtime>>::get(&v)
+							.expect("Validator in session 1 missing keys!"),
+					)
+				})
+				.collect();
+
+			// Tell everyone about the genesis session keys -- Aura must've already initialized it
+			//<Runtime as pallet_session::Config>::SessionHandler::on_genesis_session::<<Runtime as pallet_session::Config>::Keys>(&queued_keys);
+
+			<pallet_session::Validators<Runtime>>::put(initial_validators_0);
+			<pallet_session::QueuedKeys<Runtime>>::put(queued_keys);
+
+			<Runtime as pallet_session::Config>::SessionManager::start_session(0);
+
+			log::info!(
+				target: "runtime::aura_to_collator_selection",
+				"Migration of Aura authorities to Collator Selection invulnerables is complete."
+			);
+
+			migration::put_storage_value::<()>(
+				b"AuraToCollatorSelection",
+				b"StorageVersion",
+				&[],
+				(),
+			);
+
+			weight += <Runtime as frame_system::Config>::DbWeight::get().writes(1)
+		} else {
+			log::info!(
+				target: "runtime::aura_to_collator_selection",
+				"The storage migration has already been flagged as complete. No migration needs to be done.",
+			);
+		}
+
+		weight
+	}
+}
modifiedruntime/opal/Cargo.tomldiffbeforeafterboth
--- a/runtime/opal/Cargo.toml
+++ b/runtime/opal/Cargo.toml
@@ -27,6 +27,7 @@
     'pallet-evm-migration/runtime-benchmarks',
     'pallet-evm-coder-substrate/runtime-benchmarks',
     'pallet-balances/runtime-benchmarks',
+	"pallet-collator-selection/runtime-benchmarks",
     'pallet-timestamp/runtime-benchmarks',
     'pallet-common/runtime-benchmarks',
     'pallet-structure/runtime-benchmarks',
@@ -66,6 +67,9 @@
     # 'pallet-contracts-primitives/std',
     # 'pallet-contracts-rpc-runtime-api/std',
     # 'pallet-contract-helpers/std',
+	"pallet-authorship/std",
+	'pallet-collator-selection/std',
+	"pallet-session/std",
     'pallet-randomness-collective-flip/std',
     'pallet-sudo/std',
     'pallet-timestamp/std',
@@ -190,6 +194,21 @@
 git = "https://github.com/paritytech/substrate"
 branch = "polkadot-v0.9.27"
 
+[dependencies.pallet-collator-selection]
+default-features = false
+git = 'https://github.com/paritytech/cumulus'
+branch = 'polkadot-v0.9.27'
+
+[dependencies.pallet-authorship]
+default-features = false
+git = 'https://github.com/paritytech/substrate'
+branch = 'polkadot-v0.9.27'
+
+[dependencies.pallet-session]
+default-features = false
+git = 'https://github.com/paritytech/substrate'
+branch = 'polkadot-v0.9.27'
+
 [dependencies.pallet-balances]
 default-features = false
 git = "https://github.com/paritytech/substrate"
modifiedruntime/quartz/Cargo.tomldiffbeforeafterboth
--- a/runtime/quartz/Cargo.toml
+++ b/runtime/quartz/Cargo.toml
@@ -27,6 +27,7 @@
     'pallet-evm-migration/runtime-benchmarks',
     'pallet-evm-coder-substrate/runtime-benchmarks',
     'pallet-balances/runtime-benchmarks',
+	"pallet-collator-selection/runtime-benchmarks",
     'pallet-timestamp/runtime-benchmarks',
     'pallet-common/runtime-benchmarks',
     'pallet-structure/runtime-benchmarks',
@@ -66,6 +67,9 @@
     # 'pallet-contracts-primitives/std',
     # 'pallet-contracts-rpc-runtime-api/std',
     # 'pallet-contract-helpers/std',
+	"pallet-authorship/std",
+	'pallet-collator-selection/std',
+	"pallet-session/std",
     'pallet-randomness-collective-flip/std',
     'pallet-sudo/std',
     'pallet-timestamp/std',
@@ -187,6 +191,21 @@
 git = "https://github.com/paritytech/substrate"
 branch = "polkadot-v0.9.27"
 
+[dependencies.pallet-collator-selection]
+default-features = false
+git = 'https://github.com/paritytech/cumulus'
+branch = 'polkadot-v0.9.27'
+
+[dependencies.pallet-authorship]
+default-features = false
+git = 'https://github.com/paritytech/substrate'
+branch = 'polkadot-v0.9.27'
+
+[dependencies.pallet-session]
+default-features = false
+git = 'https://github.com/paritytech/substrate'
+branch = 'polkadot-v0.9.27'
+
 [dependencies.pallet-balances]
 default-features = false
 git = "https://github.com/paritytech/substrate"
modifiedruntime/tests/src/lib.rsdiffbeforeafterboth
--- a/runtime/tests/src/lib.rs
+++ b/runtime/tests/src/lib.rs
@@ -189,6 +189,9 @@
 	fn conv_eq(&self, other: &Self) -> bool {
 		self.as_sub() == other.as_sub()
 	}
+	fn is_canonical_substrate(&self) -> bool {
+		todo!()
+	}
 }
 
 impl Default for TestCrossAccountId {
modifiedruntime/unique/Cargo.tomldiffbeforeafterboth
--- a/runtime/unique/Cargo.toml
+++ b/runtime/unique/Cargo.toml
@@ -27,6 +27,7 @@
     'pallet-evm-migration/runtime-benchmarks',
     'pallet-evm-coder-substrate/runtime-benchmarks',
     'pallet-balances/runtime-benchmarks',
+	"pallet-collator-selection/runtime-benchmarks",
     'pallet-timestamp/runtime-benchmarks',
     'pallet-common/runtime-benchmarks',
     'pallet-structure/runtime-benchmarks',
@@ -67,6 +68,9 @@
     # 'pallet-contracts-primitives/std',
     # 'pallet-contracts-rpc-runtime-api/std',
     # 'pallet-contract-helpers/std',
+	"pallet-authorship/std",
+	'pallet-collator-selection/std',
+	"pallet-session/std",
     'pallet-randomness-collective-flip/std',
     'pallet-sudo/std',
     'pallet-timestamp/std',
@@ -188,6 +192,21 @@
 git = "https://github.com/paritytech/substrate"
 branch = "polkadot-v0.9.27"
 
+[dependencies.pallet-collator-selection]
+default-features = false
+git = 'https://github.com/paritytech/cumulus'
+branch = 'polkadot-v0.9.27'
+
+[dependencies.pallet-authorship]
+default-features = false
+git = 'https://github.com/paritytech/substrate'
+branch = 'polkadot-v0.9.27'
+
+[dependencies.pallet-session]
+default-features = false
+git = 'https://github.com/paritytech/substrate'
+branch = 'polkadot-v0.9.27'
+
 [dependencies.pallet-balances]
 default-features = false
 git = "https://github.com/paritytech/substrate"