From e62b62ddfb980f6445ceb13f96ef0d37073aa309 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 20 Sep 2025 01:36:11 -0400 Subject: [PATCH] Restore usage of `pallet-grandpa` to `serai-validator-sets-pallet` --- Cargo.lock | 3 ++ substrate/runtime/src/lib.rs | 27 ++------------- substrate/validator-sets/Cargo.toml | 4 +++ substrate/validator-sets/src/lib.rs | 52 ++++++++++++++++++----------- 4 files changed, 42 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ba2d01a..29ca6cfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9740,6 +9740,9 @@ dependencies = [ "borsh", "frame-support", "frame-system", + "pallet-babe", + "pallet-grandpa", + "pallet-session", "parity-scale-codec", "rand_core 0.6.4", "serai-abi", diff --git a/substrate/runtime/src/lib.rs b/substrate/runtime/src/lib.rs index 6d38d8ec..7e82f4ec 100644 --- a/substrate/runtime/src/lib.rs +++ b/substrate/runtime/src/lib.rs @@ -136,7 +136,9 @@ impl serai_coins_pallet::Config for Runtime { impl serai_coins_pallet::Config for Runtime { type AllowMint = serai_coins_pallet::AlwaysAllowMint; } -impl serai_validator_sets_pallet::Config for Runtime {} +impl serai_validator_sets_pallet::Config for Runtime { + type ShouldEndSession = Babe; +} impl serai_signals_pallet::Config for Runtime { type RetirementValidityDuration = sp_core::ConstU64<0>; // TODO type RetirementLockInDuration = sp_core::ConstU64<0>; // TODO @@ -424,28 +426,11 @@ impl timestamp::Config for Runtime { type WeightInfo = (); } -impl transaction_payment::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type OnChargeTransaction = Coins; - type OperationalFeeMultiplier = ConstU8<5>; - type WeightToFee = IdentityFee; - type LengthToFee = IdentityFee; - type FeeMultiplierUpdate = (); -} - impl coins::Config for Runtime { - type RuntimeEvent = RuntimeEvent; type AllowMint = ValidatorSets; } -impl coins::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type AllowMint = (); -} - impl dex::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type LPFee = ConstU32<3>; // 0.3% type MintMinLiquidity = ConstU64<10000>; @@ -456,12 +441,6 @@ impl dex::Config for Runtime { type WeightInfo = dex::weights::SubstrateWeight; } -impl validator_sets::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - - type ShouldEndSession = Babe; -} - pub struct IdentityValidatorIdOf; impl Convert> for IdentityValidatorIdOf { fn convert(key: PublicKey) -> Option { diff --git a/substrate/validator-sets/Cargo.toml b/substrate/validator-sets/Cargo.toml index 9549246e..0c344597 100644 --- a/substrate/validator-sets/Cargo.toml +++ b/substrate/validator-sets/Cargo.toml @@ -29,6 +29,7 @@ frame-support = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev = pallet-session = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev = "16336c737dbe833e9d138a256af99698aba637c7", default-features = false } pallet-babe = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev = "16336c737dbe833e9d138a256af99698aba637c7", default-features = false } +pallet-grandpa = { git = "https://github.com/serai-dex/patch-polkadot-sdk", rev = "16336c737dbe833e9d138a256af99698aba637c7", default-features = false } serai-abi = { path = "../abi", default-features = false, features = ["substrate"] } @@ -55,6 +56,7 @@ std = [ "pallet-session/std", "pallet-babe/std", + "pallet-grandpa/std", "serai-abi/std", @@ -69,6 +71,7 @@ try-runtime = [ "pallet-session/try-runtime", "pallet-babe/try-runtime", + "pallet-grandpa/try-runtime", ] runtime-benchmarks = [ @@ -76,6 +79,7 @@ runtime-benchmarks = [ "frame-support/runtime-benchmarks", "pallet-babe/runtime-benchmarks", + "pallet-grandpa/runtime-benchmarks", ] default = ["std"] diff --git a/substrate/validator-sets/src/lib.rs b/substrate/validator-sets/src/lib.rs index 893d8cad..0ba6d84a 100644 --- a/substrate/validator-sets/src/lib.rs +++ b/substrate/validator-sets/src/lib.rs @@ -49,6 +49,7 @@ mod pallet { use pallet_session::ShouldEndSession; use pallet_babe::Pallet as Babe; + use pallet_grandpa::Pallet as Grandpa; use serai_abi::primitives::{ crypto::SignedEmbeddedEllipticCurveKeys, @@ -68,6 +69,7 @@ mod pallet { frame_system::Config + pallet_session::Config + pallet_babe::Config + + pallet_grandpa::Config + serai_coins_pallet::Config { type ShouldEndSession: ShouldEndSession>; @@ -245,11 +247,17 @@ mod pallet { "failed to attempt the next session for the Serai network on genesis" ); - // Spawn BABE's genesis session + // Spawn BABE's, GRANDPA's genesis session + let genesis_serai_validators = Abstractions::::serai_validators(Session(0)); Babe::::on_genesis_session( - Abstractions::::serai_validators(Session(0)) + genesis_serai_validators .iter() - .map(|(validator, key)| (validator, pallet_babe::AuthorityId::from(*key))), + .map(|(validator, key)| (validator, (*key).into())), + ); + Grandpa::::on_genesis_session( + genesis_serai_validators + .iter() + .map(|(validator, key)| (validator, (*key).into())), ); } } @@ -304,10 +312,7 @@ mod pallet { Abstractions::::attempt_new_session(network.into(), include_genesis_validators); } - /* TODO - Dex::::on_new_session(network); - Grandpa::new_session - */ + // TODO Dex::::on_new_session(network); } /* @@ -421,15 +426,6 @@ mod pallet { pub(crate) fn rotate_session() { Self::retire_set(ValidatorSet { network: NetworkId::Serai, session: prior_serai_session }); - // make a new session and get the next validator set. - Self::new_session(); - - Grandpa::::new_session( - true, - session, - now_validators.into_iter().map(|(id, w)| (GrandpaAuthorityId::from(id), w)).collect(), - ); - // Clear SeraiDisabledIndices, only preserving keys still present in the new session // First drain so we don't mutate as we iterate let mut disabled = vec![]; @@ -566,6 +562,7 @@ mod pallet { fn on_initialize(n: BlockNumberFor) -> Weight { if ::ShouldEndSession::should_end_session(n) { Babe::::on_before_session_ending(); + Grandpa::::on_before_session_ending(); { // Accept the hand-over to the next session for the Serai network @@ -598,17 +595,32 @@ mod pallet { !prior_serai_validators.is_empty(), "prior Serai validators weren't able to be fetched from storage", ); + let serai_validators = Abstractions::::serai_validators(current_serai_session); + + let validators_changed = prior_serai_validators != serai_validators; + let queued_serai_validators = Abstractions::::serai_validators(latest_decided_serai_session); - fn map((validator, key): &(Public, Public)) -> (&Public, pallet_babe::AuthorityId) { + fn map_babe((validator, key): &(Public, Public)) -> (&Public, pallet_babe::AuthorityId) { (validator, (*key).into()) } Babe::::on_new_session( - prior_serai_validators != serai_validators, - serai_validators.iter().map(map), - queued_serai_validators.iter().map(map), + validators_changed, + serai_validators.iter().map(map_babe), + queued_serai_validators.iter().map(map_babe), + ); + + fn map_grandpa( + (validator, key): &(Public, Public), + ) -> (&Public, pallet_grandpa::AuthorityId) { + (validator, (*key).into()) + } + Grandpa::::on_new_session( + validators_changed, + serai_validators.iter().map(map_grandpa), + queued_serai_validators.iter().map(map_grandpa), ); }