From 9b7f8f00b9e5606b3f31a829ce1355e2c441088d Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Thu, 17 Apr 2025 09:32:16 +0200 Subject: [PATCH 01/15] init commit for permissioned collators --- Cargo.lock | 22 ++ Cargo.toml | 36 +-- .../pallet-permissioned-collators/Cargo.toml | 39 +++ .../src/benchmarking.rs | 1 + .../pallet-permissioned-collators/src/lib.rs | 226 ++++++++++++++++++ .../src/migrations.rs | 63 +++++ .../pallet-permissioned-collators/src/mock.rs | 1 + .../src/tests.rs | 1 + .../src/types.rs | 51 ++++ 9 files changed, 422 insertions(+), 18 deletions(-) create mode 100644 pallets/pallet-permissioned-collators/Cargo.toml create mode 100644 pallets/pallet-permissioned-collators/src/benchmarking.rs create mode 100644 pallets/pallet-permissioned-collators/src/lib.rs create mode 100644 pallets/pallet-permissioned-collators/src/migrations.rs create mode 100644 pallets/pallet-permissioned-collators/src/mock.rs create mode 100644 pallets/pallet-permissioned-collators/src/tests.rs create mode 100644 pallets/pallet-permissioned-collators/src/types.rs diff --git a/Cargo.lock b/Cargo.lock index 1387cac858..80b4017652 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7896,6 +7896,27 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-permissioned-collators" +version = "0.0.1" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "log", + "pallet-balances", + "pallet-session", + "parachain-staking", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-io", + "sp-keystore", + "sp-runtime", + "sp-staking", + "sp-std", +] + [[package]] name = "pallet-postit" version = "1.16.0-dev" @@ -8772,6 +8793,7 @@ dependencies = [ "pallet-message-queue", "pallet-migration", "pallet-multisig", + "pallet-permissioned-collators", "pallet-preimage", "pallet-proxy", "pallet-scheduler", diff --git a/Cargo.toml b/Cargo.toml index 193926cb61..24301e52bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,24 +54,24 @@ substrate-fixed = { version = "0.5.9", default-features = false } xcm-integration-tests = { path = "integration-tests/emulated", default-features = false } # Internal pallets (with default disabled) -attestation = { path = "pallets/attestation", default-features = false } -ctype = { path = "pallets/ctype", default-features = false } -delegation = { path = "pallets/delegation", default-features = false } -did = { path = "pallets/did", default-features = false } -pallet-asset-switch = { path = "pallets/pallet-asset-switch", default-features = false } -pallet-bonded-coins = { path = "pallets/pallet-bonded-coins", default-features = false } -pallet-configuration = { path = "pallets/pallet-configuration", default-features = false } -pallet-deposit-storage = { path = "pallets/pallet-deposit-storage", default-features = false } -pallet-did-lookup = { path = "pallets/pallet-did-lookup", default-features = false } -pallet-dip-consumer = { path = "pallets/pallet-dip-consumer", default-features = false } -pallet-dip-provider = { path = "pallets/pallet-dip-provider", default-features = false } -pallet-inflation = { path = "pallets/pallet-inflation", default-features = false } -pallet-migration = { path = "pallets/pallet-migration", default-features = false } -pallet-relay-store = { path = "pallets/pallet-relay-store", default-features = false } -pallet-web3-names = { path = "pallets/pallet-web3-names", default-features = false } -parachain-staking = { path = "pallets/parachain-staking", default-features = false } -public-credentials = { path = "pallets/public-credentials", default-features = false } - +attestation = { path = "pallets/attestation", default-features = false } +ctype = { path = "pallets/ctype", default-features = false } +delegation = { path = "pallets/delegation", default-features = false } +did = { path = "pallets/did", default-features = false } +pallet-asset-switch = { path = "pallets/pallet-asset-switch", default-features = false } +pallet-bonded-coins = { path = "pallets/pallet-bonded-coins", default-features = false } +pallet-configuration = { path = "pallets/pallet-configuration", default-features = false } +pallet-deposit-storage = { path = "pallets/pallet-deposit-storage", default-features = false } +pallet-did-lookup = { path = "pallets/pallet-did-lookup", default-features = false } +pallet-dip-consumer = { path = "pallets/pallet-dip-consumer", default-features = false } +pallet-dip-provider = { path = "pallets/pallet-dip-provider", default-features = false } +pallet-inflation = { path = "pallets/pallet-inflation", default-features = false } +pallet-migration = { path = "pallets/pallet-migration", default-features = false } +pallet-permissioned-collators = { path = "pallets/pallet-permissioned-collators", default-features = false } +pallet-relay-store = { path = "pallets/pallet-relay-store", default-features = false } +pallet-web3-names = { path = "pallets/pallet-web3-names", default-features = false } +parachain-staking = { path = "pallets/parachain-staking", default-features = false } +public-credentials = { path = "pallets/public-credentials", default-features = false } # Internal support (with default disabled) kilt-asset-dids = { path = "crates/assets", default-features = false } kilt-dip-primitives = { path = "crates/kilt-dip-primitives", default-features = false } diff --git a/pallets/pallet-permissioned-collators/Cargo.toml b/pallets/pallet-permissioned-collators/Cargo.toml new file mode 100644 index 0000000000..c4bbde3d2e --- /dev/null +++ b/pallets/pallet-permissioned-collators/Cargo.toml @@ -0,0 +1,39 @@ +[package] +authors.workspace = true +description = "FRAME pallet template for permissioned collators" +edition.workspace = true +homepage.workspace = true +license = "Unlicense" +name = "pallet-permissioned-collators" +publish = false +repository.workspace = true +version = "0.0.1" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +# External dependencies +log = { workspace = true } +parity-scale-codec = { workspace = true, features = ["derive"] } +scale-info = { workspace = true, features = ["derive"] } + +# Internal dependencies +parachain-staking = { workspace = true } + +# Substrate dependencies +frame-benchmarking = { workspace = true, optional = true } +frame-support = { workspace = true } +frame-system = { workspace = true } +pallet-balances = { workspace = true, optional = true } +pallet-session = { workspace = true } +sp-core = { workspace = true, optional = true } +sp-io = { workspace = true, optional = true } +sp-keystore = { workspace = true, optional = true } +sp-runtime = { workspace = true } +sp-staking = { workspace = true } +sp-std = { workspace = true } + +[features] +default = ["std"] +std = [] diff --git a/pallets/pallet-permissioned-collators/src/benchmarking.rs b/pallets/pallet-permissioned-collators/src/benchmarking.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/benchmarking.rs @@ -0,0 +1 @@ + diff --git a/pallets/pallet-permissioned-collators/src/lib.rs b/pallets/pallet-permissioned-collators/src/lib.rs new file mode 100644 index 0000000000..dacd9335b7 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/lib.rs @@ -0,0 +1,226 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_staking::SessionIndex; + +pub use pallet::*; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + +mod types; +pub mod weights; + +mod migrations; +pub use migrations::MigrateV0ToV1; + +#[cfg(feature = "runtime-benchmarks")] +mod benchmarking; + +use frame_support::{pallet_prelude::*, traits::EstimateNextSessionRotation}; +use frame_system::pallet_prelude::*; +use pallet_session::{SessionManager, ShouldEndSession}; +use sp_runtime::{Permill, Saturating}; +use sp_std::vec::Vec; + +#[frame_support::pallet] +pub mod pallet { + use frame_support::{pallet_prelude::*, sp_runtime::traits::SaturatedConversion}; + use frame_system::pallet_prelude::*; + use sp_staking::SessionIndex; + + use crate::types::RoundInfo; + + #[pallet::config] + pub trait Config: frame_system::Config { + type RuntimeEvent: From> + IsType<::RuntimeEvent>; + + type MaxCollators: Get; + + type MinBlocksPerRound: Get>; + } + + #[pallet::pallet] + #[pallet::storage_version(STORAGE_VERSION)] + pub struct Pallet(_); + + #[pallet::storage] + pub type Collators = StorageValue<_, BoundedVec, ValueQuery>; + + #[pallet::storage] + #[pallet::getter(fn round)] + pub(crate) type Round = StorageValue<_, RoundInfo>, ValueQuery>; + + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + pub enum Event { + SomethingStored, + CollatorAdded(T::AccountId), + CollatorRemoved(T::AccountId), + BlocksPerRoundSet(RoundInfo>), + NewRound { + block_number: BlockNumberFor, + session_index: SessionIndex, + }, + } + + #[pallet::error] + pub enum Error { + MaxCollatorExceeded, + StorageOverflow, + IndexOutOfBounds, + CannotSetBelowMin, + } + + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_initialize(now: BlockNumberFor) -> frame_support::weights::Weight { + let post_weight = Weight::from_parts(10_000, 0) + T::DbWeight::get().reads(1); + + let mut round = Round::::get(); + + // check for round update + if round.should_update(now) { + // mutate round + round.update(now); + // start next round + Round::::put(round); + + Self::deposit_event(Event::NewRound { + block_number: round.first, + session_index: round.current, + }); + } + + post_weight + } + + #[cfg(feature = "try-runtime")] + fn try_state(_n: BlockNumberFor) -> Result<(), sp_runtime::TryRuntimeError> { + crate::try_state::do_try_state::() + } + } + + const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); + + #[pallet::call] + impl Pallet { + #[pallet::call_index(0)] + #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] + pub fn add_collator(origin: OriginFor, collator: T::AccountId) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + + let mut collators = Collators::::get(); + collators + .try_push(collator.clone()) + .map_err(|_| Error::::MaxCollatorExceeded)?; + + Collators::::put(collators); + + Self::deposit_event(Event::CollatorAdded(collator)); + + Ok(().into()) + } + + #[pallet::call_index(1)] + #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))] + pub fn remove_collator(origin: OriginFor, index: u32) -> DispatchResultWithPostInfo { + ensure_root(origin)?; + let mut collators = Collators::::get(); + + ensure!(index >= collators.len().saturated_into(), Error::::IndexOutOfBounds); + + let collator = collators.swap_remove(index.saturated_into()); + Collators::::put(collators); + + Self::deposit_event(Event::CollatorRemoved(collator)); + Ok(().into()) + } + + #[pallet::call_index(3)] + #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))] + pub fn set_blocks_per_round(origin: OriginFor, new: BlockNumberFor) -> DispatchResult { + ensure_root(origin)?; + ensure!(new >= T::MinBlocksPerRound::get(), Error::::CannotSetBelowMin); + + let old_round = Round::::get(); + + let new_round = RoundInfo { + length: new, + ..old_round + }; + + Round::::put(new_round.clone()); + + Self::deposit_event(Event::BlocksPerRoundSet(new_round)); + Ok(()) + } + } +} + +impl SessionManager for Pallet { + fn new_session(new_index: SessionIndex) -> Option> { + log::debug!( + "assembling new collators for new session {} at #{:?}", + new_index, + frame_system::Pallet::::block_number(), + ); + + let collators = Collators::::get().to_vec(); + if collators.is_empty() { + // we never want to pass an empty set of collators. This would brick the chain. + log::error!("💥 keeping old session because of empty collator set!"); + None + } else { + Some(collators) + } + } + + fn end_session(_end_index: SessionIndex) { + // we too are not caring. + } + + fn start_session(_start_index: SessionIndex) { + // we too are not caring. + } +} + +impl ShouldEndSession> for Pallet { + fn should_end_session(now: BlockNumberFor) -> bool { + frame_system::Pallet::::register_extra_weight_unchecked( + T::DbWeight::get().reads(1), + DispatchClass::Mandatory, + ); + let round = Round::::get(); + round.should_update(now) + } +} + +impl EstimateNextSessionRotation> for Pallet { + fn average_session_length() -> BlockNumberFor { + Round::::get().length + } + + fn estimate_current_session_progress(now: BlockNumberFor) -> (Option, Weight) { + let round = Round::::get(); + let passed_blocks = now.saturating_sub(round.first); + + ( + Some(Permill::from_rational(passed_blocks, round.length)), + // One read for the round info, blocknumber is read free + T::DbWeight::get().reads(1), + ) + } + + fn estimate_next_session_rotation(_now: BlockNumberFor) -> (Option>, Weight) { + let round = Round::::get(); + + ( + Some(round.first + round.length), + // One read for the round info, blocknumber is read free + T::DbWeight::get().reads(1), + ) + } +} diff --git a/pallets/pallet-permissioned-collators/src/migrations.rs b/pallets/pallet-permissioned-collators/src/migrations.rs new file mode 100644 index 0000000000..b4a5fb1e15 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/migrations.rs @@ -0,0 +1,63 @@ +use frame_support::{storage_alias, traits::OnRuntimeUpgrade, weights::Weight}; +use parachain_staking::{Config as StakingConfig, Round, TopCandidates}; +use sp_runtime::BoundedVec; +use sp_std::vec::Vec; + +#[cfg(feature = "try-runtime")] +use alloc::vec::Vec; + +use crate::{types::RoundInfo, Collators, Config, Round as PermissionedRound}; + +mod v0 { + use super::*; + + #[storage_alias] + pub type Value = StorageValue, u32>; +} + +pub struct InnerMigrateV0ToV1(core::marker::PhantomData); + +impl OnRuntimeUpgrade for InnerMigrateV0ToV1 { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { + Ok(vec![]) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + let top_candidates = TopCandidates::::get() + .into_bounded_vec() + .into_iter() + .map(|stake| stake.owner) + .collect::>(); + + let permissioned_collators = + BoundedVec::<::AccountId, T::MaxCollators>::truncate_from(top_candidates); + + Collators::::put(permissioned_collators); + + let round = Round::::get(); + + let new_round = RoundInfo { + current: round.current, + first: round.first, + length: round.length, + }; + + PermissionedRound::::put(new_round); + + Weight::from_parts(10_000, 0) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { + Ok(()) + } +} + +pub type MigrateV0ToV1 = frame_support::migrations::VersionedMigration< + 0, + 1, + InnerMigrateV0ToV1, + crate::pallet::Pallet, + ::DbWeight, +>; diff --git a/pallets/pallet-permissioned-collators/src/mock.rs b/pallets/pallet-permissioned-collators/src/mock.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/mock.rs @@ -0,0 +1 @@ + diff --git a/pallets/pallet-permissioned-collators/src/tests.rs b/pallets/pallet-permissioned-collators/src/tests.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/tests.rs @@ -0,0 +1 @@ + diff --git a/pallets/pallet-permissioned-collators/src/types.rs b/pallets/pallet-permissioned-collators/src/types.rs new file mode 100644 index 0000000000..2eee7085f8 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/types.rs @@ -0,0 +1,51 @@ +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::{ + prelude::ops::{Add, Sub}, + TypeInfo, +}; +use sp_runtime::{RuntimeDebug, Saturating}; +use sp_staking::SessionIndex; + +/// The current round index and transition information. +#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] +pub struct RoundInfo { + /// Current round index. + pub current: SessionIndex, + /// The first block of the current round. + pub first: BlockNumber, + /// The length of the current round in blocks. + pub length: BlockNumber, +} + +impl RoundInfo +where + B: Copy + Saturating + From + PartialOrd, +{ + pub const fn new(current: SessionIndex, first: B, length: B) -> RoundInfo { + RoundInfo { current, first, length } + } + + /// Checks if the round should be updated. + /// + /// The round should update if `self.length` or more blocks where produced + /// after `self.first`. + pub fn should_update(&self, now: B) -> bool { + let l = now.saturating_sub(self.first); + l >= self.length + } + + /// Starts a new round. + pub fn update(&mut self, now: B) { + self.current = self.current.saturating_add(1u32); + self.first = now; + } +} + +impl Default for RoundInfo +where + B: Copy + Saturating + Add + Sub + From + PartialOrd, +{ + fn default() -> RoundInfo { + RoundInfo::new(0u32, 0u32.into(), 20.into()) + } +} From 28d427b7ea25f54de5c17b1ad4493cb16e2bb1de Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Thu, 17 Apr 2025 09:32:29 +0200 Subject: [PATCH 02/15] weights --- pallets/pallet-permissioned-collators/src/weights.rs | 1 + 1 file changed, 1 insertion(+) create mode 100644 pallets/pallet-permissioned-collators/src/weights.rs diff --git a/pallets/pallet-permissioned-collators/src/weights.rs b/pallets/pallet-permissioned-collators/src/weights.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/pallets/pallet-permissioned-collators/src/weights.rs @@ -0,0 +1 @@ + From 514a30e6dc29f17011e80f32bfb457b3e6dd445b Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Tue, 22 Apr 2025 09:30:52 +0200 Subject: [PATCH 03/15] revert --- Cargo.lock | 24 +- Cargo.toml | 35 ++- .../pallet-permissioned-collators/Cargo.toml | 39 --- .../src/benchmarking.rs | 1 - .../pallet-permissioned-collators/src/lib.rs | 226 ------------------ .../src/migrations.rs | 63 ----- .../pallet-permissioned-collators/src/mock.rs | 1 - .../src/tests.rs | 1 - .../src/types.rs | 51 ---- .../src/weights.rs | 1 - pallets/parachain-staking/Cargo.toml | 1 + runtimes/peregrine/Cargo.toml | 1 + runtimes/peregrine/src/governance.rs | 2 +- runtimes/peregrine/src/migrations/mod.rs | 14 +- runtimes/peregrine/src/system/mod.rs | 18 +- 15 files changed, 39 insertions(+), 439 deletions(-) delete mode 100644 pallets/pallet-permissioned-collators/Cargo.toml delete mode 100644 pallets/pallet-permissioned-collators/src/benchmarking.rs delete mode 100644 pallets/pallet-permissioned-collators/src/lib.rs delete mode 100644 pallets/pallet-permissioned-collators/src/migrations.rs delete mode 100644 pallets/pallet-permissioned-collators/src/mock.rs delete mode 100644 pallets/pallet-permissioned-collators/src/tests.rs delete mode 100644 pallets/pallet-permissioned-collators/src/types.rs delete mode 100644 pallets/pallet-permissioned-collators/src/weights.rs diff --git a/Cargo.lock b/Cargo.lock index 80b4017652..1802afc664 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7896,27 +7896,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "pallet-permissioned-collators" -version = "0.0.1" -dependencies = [ - "frame-benchmarking", - "frame-support", - "frame-system", - "log", - "pallet-balances", - "pallet-session", - "parachain-staking", - "parity-scale-codec", - "scale-info", - "sp-core", - "sp-io", - "sp-keystore", - "sp-runtime", - "sp-staking", - "sp-std", -] - [[package]] name = "pallet-postit" version = "1.16.0-dev" @@ -8466,6 +8445,7 @@ dependencies = [ "pallet-aura", "pallet-authorship", "pallet-balances", + "pallet-membership", "pallet-session", "pallet-timestamp", "parity-scale-codec", @@ -8793,7 +8773,6 @@ dependencies = [ "pallet-message-queue", "pallet-migration", "pallet-multisig", - "pallet-permissioned-collators", "pallet-preimage", "pallet-proxy", "pallet-scheduler", @@ -8826,6 +8805,7 @@ dependencies = [ "sp-offchain", "sp-runtime", "sp-session", + "sp-staking", "sp-std", "sp-transaction-pool", "sp-version", diff --git a/Cargo.toml b/Cargo.toml index 24301e52bb..ad9e8430f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,24 +54,23 @@ substrate-fixed = { version = "0.5.9", default-features = false } xcm-integration-tests = { path = "integration-tests/emulated", default-features = false } # Internal pallets (with default disabled) -attestation = { path = "pallets/attestation", default-features = false } -ctype = { path = "pallets/ctype", default-features = false } -delegation = { path = "pallets/delegation", default-features = false } -did = { path = "pallets/did", default-features = false } -pallet-asset-switch = { path = "pallets/pallet-asset-switch", default-features = false } -pallet-bonded-coins = { path = "pallets/pallet-bonded-coins", default-features = false } -pallet-configuration = { path = "pallets/pallet-configuration", default-features = false } -pallet-deposit-storage = { path = "pallets/pallet-deposit-storage", default-features = false } -pallet-did-lookup = { path = "pallets/pallet-did-lookup", default-features = false } -pallet-dip-consumer = { path = "pallets/pallet-dip-consumer", default-features = false } -pallet-dip-provider = { path = "pallets/pallet-dip-provider", default-features = false } -pallet-inflation = { path = "pallets/pallet-inflation", default-features = false } -pallet-migration = { path = "pallets/pallet-migration", default-features = false } -pallet-permissioned-collators = { path = "pallets/pallet-permissioned-collators", default-features = false } -pallet-relay-store = { path = "pallets/pallet-relay-store", default-features = false } -pallet-web3-names = { path = "pallets/pallet-web3-names", default-features = false } -parachain-staking = { path = "pallets/parachain-staking", default-features = false } -public-credentials = { path = "pallets/public-credentials", default-features = false } +attestation = { path = "pallets/attestation", default-features = false } +ctype = { path = "pallets/ctype", default-features = false } +delegation = { path = "pallets/delegation", default-features = false } +did = { path = "pallets/did", default-features = false } +pallet-asset-switch = { path = "pallets/pallet-asset-switch", default-features = false } +pallet-bonded-coins = { path = "pallets/pallet-bonded-coins", default-features = false } +pallet-configuration = { path = "pallets/pallet-configuration", default-features = false } +pallet-deposit-storage = { path = "pallets/pallet-deposit-storage", default-features = false } +pallet-did-lookup = { path = "pallets/pallet-did-lookup", default-features = false } +pallet-dip-consumer = { path = "pallets/pallet-dip-consumer", default-features = false } +pallet-dip-provider = { path = "pallets/pallet-dip-provider", default-features = false } +pallet-inflation = { path = "pallets/pallet-inflation", default-features = false } +pallet-migration = { path = "pallets/pallet-migration", default-features = false } +pallet-relay-store = { path = "pallets/pallet-relay-store", default-features = false } +pallet-web3-names = { path = "pallets/pallet-web3-names", default-features = false } +parachain-staking = { path = "pallets/parachain-staking", default-features = false } +public-credentials = { path = "pallets/public-credentials", default-features = false } # Internal support (with default disabled) kilt-asset-dids = { path = "crates/assets", default-features = false } kilt-dip-primitives = { path = "crates/kilt-dip-primitives", default-features = false } diff --git a/pallets/pallet-permissioned-collators/Cargo.toml b/pallets/pallet-permissioned-collators/Cargo.toml deleted file mode 100644 index c4bbde3d2e..0000000000 --- a/pallets/pallet-permissioned-collators/Cargo.toml +++ /dev/null @@ -1,39 +0,0 @@ -[package] -authors.workspace = true -description = "FRAME pallet template for permissioned collators" -edition.workspace = true -homepage.workspace = true -license = "Unlicense" -name = "pallet-permissioned-collators" -publish = false -repository.workspace = true -version = "0.0.1" - -[package.metadata.docs.rs] -targets = ["x86_64-unknown-linux-gnu"] - -[dependencies] -# External dependencies -log = { workspace = true } -parity-scale-codec = { workspace = true, features = ["derive"] } -scale-info = { workspace = true, features = ["derive"] } - -# Internal dependencies -parachain-staking = { workspace = true } - -# Substrate dependencies -frame-benchmarking = { workspace = true, optional = true } -frame-support = { workspace = true } -frame-system = { workspace = true } -pallet-balances = { workspace = true, optional = true } -pallet-session = { workspace = true } -sp-core = { workspace = true, optional = true } -sp-io = { workspace = true, optional = true } -sp-keystore = { workspace = true, optional = true } -sp-runtime = { workspace = true } -sp-staking = { workspace = true } -sp-std = { workspace = true } - -[features] -default = ["std"] -std = [] diff --git a/pallets/pallet-permissioned-collators/src/benchmarking.rs b/pallets/pallet-permissioned-collators/src/benchmarking.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/pallets/pallet-permissioned-collators/src/benchmarking.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/pallets/pallet-permissioned-collators/src/lib.rs b/pallets/pallet-permissioned-collators/src/lib.rs deleted file mode 100644 index dacd9335b7..0000000000 --- a/pallets/pallet-permissioned-collators/src/lib.rs +++ /dev/null @@ -1,226 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -use sp_staking::SessionIndex; - -pub use pallet::*; - -#[cfg(test)] -mod mock; - -#[cfg(test)] -mod tests; - -mod types; -pub mod weights; - -mod migrations; -pub use migrations::MigrateV0ToV1; - -#[cfg(feature = "runtime-benchmarks")] -mod benchmarking; - -use frame_support::{pallet_prelude::*, traits::EstimateNextSessionRotation}; -use frame_system::pallet_prelude::*; -use pallet_session::{SessionManager, ShouldEndSession}; -use sp_runtime::{Permill, Saturating}; -use sp_std::vec::Vec; - -#[frame_support::pallet] -pub mod pallet { - use frame_support::{pallet_prelude::*, sp_runtime::traits::SaturatedConversion}; - use frame_system::pallet_prelude::*; - use sp_staking::SessionIndex; - - use crate::types::RoundInfo; - - #[pallet::config] - pub trait Config: frame_system::Config { - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - - type MaxCollators: Get; - - type MinBlocksPerRound: Get>; - } - - #[pallet::pallet] - #[pallet::storage_version(STORAGE_VERSION)] - pub struct Pallet(_); - - #[pallet::storage] - pub type Collators = StorageValue<_, BoundedVec, ValueQuery>; - - #[pallet::storage] - #[pallet::getter(fn round)] - pub(crate) type Round = StorageValue<_, RoundInfo>, ValueQuery>; - - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - SomethingStored, - CollatorAdded(T::AccountId), - CollatorRemoved(T::AccountId), - BlocksPerRoundSet(RoundInfo>), - NewRound { - block_number: BlockNumberFor, - session_index: SessionIndex, - }, - } - - #[pallet::error] - pub enum Error { - MaxCollatorExceeded, - StorageOverflow, - IndexOutOfBounds, - CannotSetBelowMin, - } - - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_initialize(now: BlockNumberFor) -> frame_support::weights::Weight { - let post_weight = Weight::from_parts(10_000, 0) + T::DbWeight::get().reads(1); - - let mut round = Round::::get(); - - // check for round update - if round.should_update(now) { - // mutate round - round.update(now); - // start next round - Round::::put(round); - - Self::deposit_event(Event::NewRound { - block_number: round.first, - session_index: round.current, - }); - } - - post_weight - } - - #[cfg(feature = "try-runtime")] - fn try_state(_n: BlockNumberFor) -> Result<(), sp_runtime::TryRuntimeError> { - crate::try_state::do_try_state::() - } - } - - const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); - - #[pallet::call] - impl Pallet { - #[pallet::call_index(0)] - #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] - pub fn add_collator(origin: OriginFor, collator: T::AccountId) -> DispatchResultWithPostInfo { - ensure_root(origin)?; - - let mut collators = Collators::::get(); - collators - .try_push(collator.clone()) - .map_err(|_| Error::::MaxCollatorExceeded)?; - - Collators::::put(collators); - - Self::deposit_event(Event::CollatorAdded(collator)); - - Ok(().into()) - } - - #[pallet::call_index(1)] - #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))] - pub fn remove_collator(origin: OriginFor, index: u32) -> DispatchResultWithPostInfo { - ensure_root(origin)?; - let mut collators = Collators::::get(); - - ensure!(index >= collators.len().saturated_into(), Error::::IndexOutOfBounds); - - let collator = collators.swap_remove(index.saturated_into()); - Collators::::put(collators); - - Self::deposit_event(Event::CollatorRemoved(collator)); - Ok(().into()) - } - - #[pallet::call_index(3)] - #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))] - pub fn set_blocks_per_round(origin: OriginFor, new: BlockNumberFor) -> DispatchResult { - ensure_root(origin)?; - ensure!(new >= T::MinBlocksPerRound::get(), Error::::CannotSetBelowMin); - - let old_round = Round::::get(); - - let new_round = RoundInfo { - length: new, - ..old_round - }; - - Round::::put(new_round.clone()); - - Self::deposit_event(Event::BlocksPerRoundSet(new_round)); - Ok(()) - } - } -} - -impl SessionManager for Pallet { - fn new_session(new_index: SessionIndex) -> Option> { - log::debug!( - "assembling new collators for new session {} at #{:?}", - new_index, - frame_system::Pallet::::block_number(), - ); - - let collators = Collators::::get().to_vec(); - if collators.is_empty() { - // we never want to pass an empty set of collators. This would brick the chain. - log::error!("💥 keeping old session because of empty collator set!"); - None - } else { - Some(collators) - } - } - - fn end_session(_end_index: SessionIndex) { - // we too are not caring. - } - - fn start_session(_start_index: SessionIndex) { - // we too are not caring. - } -} - -impl ShouldEndSession> for Pallet { - fn should_end_session(now: BlockNumberFor) -> bool { - frame_system::Pallet::::register_extra_weight_unchecked( - T::DbWeight::get().reads(1), - DispatchClass::Mandatory, - ); - let round = Round::::get(); - round.should_update(now) - } -} - -impl EstimateNextSessionRotation> for Pallet { - fn average_session_length() -> BlockNumberFor { - Round::::get().length - } - - fn estimate_current_session_progress(now: BlockNumberFor) -> (Option, Weight) { - let round = Round::::get(); - let passed_blocks = now.saturating_sub(round.first); - - ( - Some(Permill::from_rational(passed_blocks, round.length)), - // One read for the round info, blocknumber is read free - T::DbWeight::get().reads(1), - ) - } - - fn estimate_next_session_rotation(_now: BlockNumberFor) -> (Option>, Weight) { - let round = Round::::get(); - - ( - Some(round.first + round.length), - // One read for the round info, blocknumber is read free - T::DbWeight::get().reads(1), - ) - } -} diff --git a/pallets/pallet-permissioned-collators/src/migrations.rs b/pallets/pallet-permissioned-collators/src/migrations.rs deleted file mode 100644 index b4a5fb1e15..0000000000 --- a/pallets/pallet-permissioned-collators/src/migrations.rs +++ /dev/null @@ -1,63 +0,0 @@ -use frame_support::{storage_alias, traits::OnRuntimeUpgrade, weights::Weight}; -use parachain_staking::{Config as StakingConfig, Round, TopCandidates}; -use sp_runtime::BoundedVec; -use sp_std::vec::Vec; - -#[cfg(feature = "try-runtime")] -use alloc::vec::Vec; - -use crate::{types::RoundInfo, Collators, Config, Round as PermissionedRound}; - -mod v0 { - use super::*; - - #[storage_alias] - pub type Value = StorageValue, u32>; -} - -pub struct InnerMigrateV0ToV1(core::marker::PhantomData); - -impl OnRuntimeUpgrade for InnerMigrateV0ToV1 { - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::TryRuntimeError> { - Ok(vec![]) - } - - fn on_runtime_upgrade() -> frame_support::weights::Weight { - let top_candidates = TopCandidates::::get() - .into_bounded_vec() - .into_iter() - .map(|stake| stake.owner) - .collect::>(); - - let permissioned_collators = - BoundedVec::<::AccountId, T::MaxCollators>::truncate_from(top_candidates); - - Collators::::put(permissioned_collators); - - let round = Round::::get(); - - let new_round = RoundInfo { - current: round.current, - first: round.first, - length: round.length, - }; - - PermissionedRound::::put(new_round); - - Weight::from_parts(10_000, 0) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), sp_runtime::TryRuntimeError> { - Ok(()) - } -} - -pub type MigrateV0ToV1 = frame_support::migrations::VersionedMigration< - 0, - 1, - InnerMigrateV0ToV1, - crate::pallet::Pallet, - ::DbWeight, ->; diff --git a/pallets/pallet-permissioned-collators/src/mock.rs b/pallets/pallet-permissioned-collators/src/mock.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/pallets/pallet-permissioned-collators/src/mock.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/pallets/pallet-permissioned-collators/src/tests.rs b/pallets/pallet-permissioned-collators/src/tests.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/pallets/pallet-permissioned-collators/src/tests.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/pallets/pallet-permissioned-collators/src/types.rs b/pallets/pallet-permissioned-collators/src/types.rs deleted file mode 100644 index 2eee7085f8..0000000000 --- a/pallets/pallet-permissioned-collators/src/types.rs +++ /dev/null @@ -1,51 +0,0 @@ -use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; -use scale_info::{ - prelude::ops::{Add, Sub}, - TypeInfo, -}; -use sp_runtime::{RuntimeDebug, Saturating}; -use sp_staking::SessionIndex; - -/// The current round index and transition information. -#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)] -pub struct RoundInfo { - /// Current round index. - pub current: SessionIndex, - /// The first block of the current round. - pub first: BlockNumber, - /// The length of the current round in blocks. - pub length: BlockNumber, -} - -impl RoundInfo -where - B: Copy + Saturating + From + PartialOrd, -{ - pub const fn new(current: SessionIndex, first: B, length: B) -> RoundInfo { - RoundInfo { current, first, length } - } - - /// Checks if the round should be updated. - /// - /// The round should update if `self.length` or more blocks where produced - /// after `self.first`. - pub fn should_update(&self, now: B) -> bool { - let l = now.saturating_sub(self.first); - l >= self.length - } - - /// Starts a new round. - pub fn update(&mut self, now: B) { - self.current = self.current.saturating_add(1u32); - self.first = now; - } -} - -impl Default for RoundInfo -where - B: Copy + Saturating + Add + Sub + From + PartialOrd, -{ - fn default() -> RoundInfo { - RoundInfo::new(0u32, 0u32.into(), 20.into()) - } -} diff --git a/pallets/pallet-permissioned-collators/src/weights.rs b/pallets/pallet-permissioned-collators/src/weights.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/pallets/pallet-permissioned-collators/src/weights.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/pallets/parachain-staking/Cargo.toml b/pallets/parachain-staking/Cargo.toml index cdf30bccf4..3ec98539c9 100644 --- a/pallets/parachain-staking/Cargo.toml +++ b/pallets/parachain-staking/Cargo.toml @@ -34,6 +34,7 @@ frame-support = { workspace = true } frame-system = { workspace = true } pallet-authorship = { workspace = true } pallet-balances = { workspace = true } +pallet-membership = { workspace = true } pallet-session = { workspace = true } sp-runtime = { workspace = true } sp-staking = { workspace = true } diff --git a/runtimes/peregrine/Cargo.toml b/runtimes/peregrine/Cargo.toml index 63836a6bf1..e71fae39f9 100644 --- a/runtimes/peregrine/Cargo.toml +++ b/runtimes/peregrine/Cargo.toml @@ -64,6 +64,7 @@ sp-metadata-ir = { workspace = true } sp-offchain = { workspace = true } sp-runtime = { workspace = true } sp-session = { workspace = true } +sp-staking = { workspace = true } sp-std = { workspace = true } sp-transaction-pool = { workspace = true } sp-version = { workspace = true } diff --git a/runtimes/peregrine/src/governance.rs b/runtimes/peregrine/src/governance.rs index 0f1fc28116..5476e107de 100644 --- a/runtimes/peregrine/src/governance.rs +++ b/runtimes/peregrine/src/governance.rs @@ -175,7 +175,7 @@ impl pallet_collective::Config for Runtime { type SetMembersOrigin = EnsureRoot; } -type RootOrMoreThanHalfCouncil = RootOrCollectiveProportion; +pub(crate) type RootOrMoreThanHalfCouncil = RootOrCollectiveProportion; type TechnicalMembershipProvider = pallet_membership::Instance1; impl pallet_membership::Config for Runtime { diff --git a/runtimes/peregrine/src/migrations/mod.rs b/runtimes/peregrine/src/migrations/mod.rs index f6ca110fa1..90231d42ab 100644 --- a/runtimes/peregrine/src/migrations/mod.rs +++ b/runtimes/peregrine/src/migrations/mod.rs @@ -16,23 +16,11 @@ // If you feel like getting in touch with us, you can do so at -use frame_support::parameter_types; use runtime_common::constants; use crate::{weights, Balances, Runtime, RuntimeEvent}; -parameter_types! { - pub const DmpPalletName: &'static str = "DmpQueue"; - pub const DotNames: &'static str = "DotNames"; - pub const UniqueLinking: &'static str = "UniqueLinking"; -} - -pub type RuntimeMigrations = ( - pallet_xcm::migration::MigrateToLatestXcmVersion, - frame_support::migrations::RemovePallet::DbWeight>, - frame_support::migrations::RemovePallet::DbWeight>, - frame_support::migrations::RemovePallet::DbWeight>, -); +pub type RuntimeMigrations = (pallet_xcm::migration::MigrateToLatestXcmVersion,); impl pallet_migration::Config for Runtime { type RuntimeEvent = RuntimeEvent; diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index e8aab090b2..bc8e1f9d83 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -41,7 +41,7 @@ use sp_weights::ConstantMultiplier; use xcm::v4::Location; use crate::{ - governance::{CouncilCollective, RootOrCollectiveProportion}, + governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, }; @@ -163,7 +163,7 @@ impl pallet_aura::Config for Runtime { impl pallet_authorship::Config for Runtime { type FindAuthor = pallet_session::FindAccountFromAuthorIndex; - type EventHandler = ParachainStaking; + type EventHandler = (); } impl pallet_utility::Config for Runtime { @@ -280,3 +280,17 @@ impl pallet_scheduler::Config for Runtime { type OriginPrivilegeCmp = OriginPrivilegeCmp; type Preimages = Preimage; } + +type CollatorMembershipProvider = pallet_membership::Instance3; +impl pallet_membership::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AddOrigin = RootOrMoreThanHalfCouncil; + type RemoveOrigin = RootOrMoreThanHalfCouncil; + type SwapOrigin = EnsureRoot; + type ResetOrigin = EnsureRoot; + type PrimeOrigin = EnsureRoot; + type MembershipInitialized = (); + type MembershipChanged = (); + type MaxMembers = constants::governance::TechnicalMaxMembers; + type WeightInfo = weights::pallet_technical_membership::WeightInfo; +} From bb746e275c8b5ef34e1bf4363acd2057bdfcbaa7 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Tue, 22 Apr 2025 12:53:20 +0200 Subject: [PATCH 04/15] permissoned collators --- runtimes/peregrine/src/lib.rs | 1 + runtimes/peregrine/src/system/mod.rs | 43 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index 916eb8c878..d466ee073f 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -116,6 +116,7 @@ construct_runtime! { ParachainStaking: parachain_staking = 21, Authorship: pallet_authorship = 20, AuraExt: cumulus_pallet_aura_ext = 24, + PermissionedCollator: pallet_membership:: = 25, Democracy: pallet_democracy = 30, Council: pallet_collective:: = 31, diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index bc8e1f9d83..0f82b84344 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -42,8 +42,9 @@ use xcm::v4::Location; use crate::{ governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, - weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, - RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, + weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, PermissionedCollator, Preimage, + Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, + VERSION, }; pub(crate) mod proxy; @@ -141,13 +142,49 @@ impl_opaque_keys! { } } +pub struct SessionManager; + +impl pallet_session::SessionManager for SessionManager { + fn new_session(new_index: sp_staking::SessionIndex) -> Option> { + let collators = PermissionedCollator::members().to_vec(); + + log::debug!( + "assembling new collators for new session {} at #{:?} with {:?}", + new_index, + System::block_number(), + collators + ); + + System::register_extra_weight_unchecked( + ::DbWeight::get().reads(2), + frame_support::pallet_prelude::DispatchClass::Mandatory, + ); + + if collators.is_empty() { + // we never want to pass an empty set of collators. This would brick the chain. + log::error!("💥 keeping old session because of empty collator set!"); + return None; + } + + Some(collators) + } + + fn start_session(_start_index: sp_staking::SessionIndex) { + // We don't care + } + + fn end_session(_end_index: sp_staking::SessionIndex) { + // We don't care + } +} + impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = AccountId; type ValidatorIdOf = ConvertInto; type ShouldEndSession = ParachainStaking; type NextSessionRotation = ParachainStaking; - type SessionManager = ParachainStaking; + type SessionManager = SessionManager; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type WeightInfo = weights::pallet_session::WeightInfo; From 811d4954a95b06a9146a5d2cb5bfd0c4f740f30a Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Tue, 22 Apr 2025 14:19:53 +0200 Subject: [PATCH 05/15] compiling --- Cargo.lock | 2 +- Cargo.toml | 1 + pallets/parachain-staking/Cargo.toml | 1 - runtimes/peregrine/src/system/mod.rs | 1 + runtimes/spiritnet/Cargo.toml | 1 + runtimes/spiritnet/src/governance.rs | 2 +- runtimes/spiritnet/src/lib.rs | 1 + runtimes/spiritnet/src/system/mod.rs | 62 +++++++++++++++++++++++++--- 8 files changed, 63 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1802afc664..547acd9389 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8445,7 +8445,6 @@ dependencies = [ "pallet-aura", "pallet-authorship", "pallet-balances", - "pallet-membership", "pallet-session", "pallet-timestamp", "parity-scale-codec", @@ -13894,6 +13893,7 @@ dependencies = [ "sp-offchain", "sp-runtime", "sp-session", + "sp-staking", "sp-std", "sp-transaction-pool", "sp-version", diff --git a/Cargo.toml b/Cargo.toml index ad9e8430f8..193926cb61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ pallet-relay-store = { path = "pallets/pallet-relay-store", default-features pallet-web3-names = { path = "pallets/pallet-web3-names", default-features = false } parachain-staking = { path = "pallets/parachain-staking", default-features = false } public-credentials = { path = "pallets/public-credentials", default-features = false } + # Internal support (with default disabled) kilt-asset-dids = { path = "crates/assets", default-features = false } kilt-dip-primitives = { path = "crates/kilt-dip-primitives", default-features = false } diff --git a/pallets/parachain-staking/Cargo.toml b/pallets/parachain-staking/Cargo.toml index 3ec98539c9..cdf30bccf4 100644 --- a/pallets/parachain-staking/Cargo.toml +++ b/pallets/parachain-staking/Cargo.toml @@ -34,7 +34,6 @@ frame-support = { workspace = true } frame-system = { workspace = true } pallet-authorship = { workspace = true } pallet-balances = { workspace = true } -pallet-membership = { workspace = true } pallet-session = { workspace = true } sp-runtime = { workspace = true } sp-staking = { workspace = true } diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index 0f82b84344..327fc7a700 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -142,6 +142,7 @@ impl_opaque_keys! { } } +/// The session manager for the collator set. pub struct SessionManager; impl pallet_session::SessionManager for SessionManager { diff --git a/runtimes/spiritnet/Cargo.toml b/runtimes/spiritnet/Cargo.toml index c5558eec57..f05eaf145a 100644 --- a/runtimes/spiritnet/Cargo.toml +++ b/runtimes/spiritnet/Cargo.toml @@ -62,6 +62,7 @@ sp-metadata-ir = { workspace = true } sp-offchain = { workspace = true } sp-runtime = { workspace = true } sp-session = { workspace = true } +sp-staking = { workspace = true } sp-std = { workspace = true } sp-transaction-pool = { workspace = true } sp-version = { workspace = true } diff --git a/runtimes/spiritnet/src/governance.rs b/runtimes/spiritnet/src/governance.rs index 0f1fc28116..5476e107de 100644 --- a/runtimes/spiritnet/src/governance.rs +++ b/runtimes/spiritnet/src/governance.rs @@ -175,7 +175,7 @@ impl pallet_collective::Config for Runtime { type SetMembersOrigin = EnsureRoot; } -type RootOrMoreThanHalfCouncil = RootOrCollectiveProportion; +pub(crate) type RootOrMoreThanHalfCouncil = RootOrCollectiveProportion; type TechnicalMembershipProvider = pallet_membership::Instance1; impl pallet_membership::Config for Runtime { diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index 7822d21dc9..afb6025238 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -113,6 +113,7 @@ construct_runtime! { ParachainStaking: parachain_staking = 21, Authorship: pallet_authorship = 20, AuraExt: cumulus_pallet_aura_ext = 24, + PermissionedCollator: pallet_membership:: = 25, Democracy: pallet_democracy = 30, Council: pallet_collective:: = 31, diff --git a/runtimes/spiritnet/src/system/mod.rs b/runtimes/spiritnet/src/system/mod.rs index 33a9538903..2528eba75e 100644 --- a/runtimes/spiritnet/src/system/mod.rs +++ b/runtimes/spiritnet/src/system/mod.rs @@ -41,9 +41,10 @@ use sp_weights::ConstantMultiplier; use xcm::v4::Location; use crate::{ - governance::{CouncilCollective, RootOrCollectiveProportion}, - weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, - RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, + governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, + weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, PermissionedCollator, Preimage, + Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, + VERSION, }; pub(crate) mod proxy; @@ -141,13 +142,50 @@ impl_opaque_keys! { } } +/// The session manager for the collator set. +pub struct SessionManager; + +impl pallet_session::SessionManager for SessionManager { + fn new_session(new_index: sp_staking::SessionIndex) -> Option> { + let collators = PermissionedCollator::members().to_vec(); + + log::debug!( + "assembling new collators for new session {} at #{:?} with {:?}", + new_index, + System::block_number(), + collators + ); + + System::register_extra_weight_unchecked( + ::DbWeight::get().reads(2), + frame_support::pallet_prelude::DispatchClass::Mandatory, + ); + + if collators.is_empty() { + // we never want to pass an empty set of collators. This would brick the chain. + log::error!("💥 keeping old session because of empty collator set!"); + return None; + } + + Some(collators) + } + + fn start_session(_start_index: sp_staking::SessionIndex) { + // We don't care + } + + fn end_session(_end_index: sp_staking::SessionIndex) { + // We don't care + } +} + impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = AccountId; type ValidatorIdOf = ConvertInto; type ShouldEndSession = ParachainStaking; type NextSessionRotation = ParachainStaking; - type SessionManager = ParachainStaking; + type SessionManager = SessionManager; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type WeightInfo = weights::pallet_session::WeightInfo; @@ -163,7 +201,7 @@ impl pallet_aura::Config for Runtime { impl pallet_authorship::Config for Runtime { type FindAuthor = pallet_session::FindAccountFromAuthorIndex; - type EventHandler = ParachainStaking; + type EventHandler = (); } impl pallet_utility::Config for Runtime { @@ -274,3 +312,17 @@ impl pallet_scheduler::Config for Runtime { type OriginPrivilegeCmp = OriginPrivilegeCmp; type Preimages = Preimage; } + +type CollatorMembershipProvider = pallet_membership::Instance3; +impl pallet_membership::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type AddOrigin = RootOrMoreThanHalfCouncil; + type RemoveOrigin = RootOrMoreThanHalfCouncil; + type SwapOrigin = EnsureRoot; + type ResetOrigin = EnsureRoot; + type PrimeOrigin = EnsureRoot; + type MembershipInitialized = (); + type MembershipChanged = (); + type MaxMembers = constants::governance::TechnicalMaxMembers; + type WeightInfo = weights::pallet_technical_membership::WeightInfo; +} From cdcb73c4a7f57c6ed3673edda0616844a22a3567 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Tue, 22 Apr 2025 14:21:44 +0200 Subject: [PATCH 06/15] revert migrations --- runtimes/peregrine/src/migrations/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/runtimes/peregrine/src/migrations/mod.rs b/runtimes/peregrine/src/migrations/mod.rs index 90231d42ab..f6ca110fa1 100644 --- a/runtimes/peregrine/src/migrations/mod.rs +++ b/runtimes/peregrine/src/migrations/mod.rs @@ -16,11 +16,23 @@ // If you feel like getting in touch with us, you can do so at +use frame_support::parameter_types; use runtime_common::constants; use crate::{weights, Balances, Runtime, RuntimeEvent}; -pub type RuntimeMigrations = (pallet_xcm::migration::MigrateToLatestXcmVersion,); +parameter_types! { + pub const DmpPalletName: &'static str = "DmpQueue"; + pub const DotNames: &'static str = "DotNames"; + pub const UniqueLinking: &'static str = "UniqueLinking"; +} + +pub type RuntimeMigrations = ( + pallet_xcm::migration::MigrateToLatestXcmVersion, + frame_support::migrations::RemovePallet::DbWeight>, + frame_support::migrations::RemovePallet::DbWeight>, + frame_support::migrations::RemovePallet::DbWeight>, +); impl pallet_migration::Config for Runtime { type RuntimeEvent = RuntimeEvent; From 8aa19c604b359f6541091e87955e944087234b48 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 11:06:06 +0200 Subject: [PATCH 07/15] benchmarks --- runtimes/common/src/constants.rs | 4 + runtimes/peregrine/src/benchmarks/mod.rs | 11 +- runtimes/peregrine/src/system/mod.rs | 8 +- runtimes/peregrine/src/weights/mod.rs | 1 + .../peregrine/src/weights/pallet_collators.rs | 259 ++++++++++++++++++ runtimes/spiritnet/src/benchmarks/mod.rs | 10 +- runtimes/spiritnet/src/system/mod.rs | 7 +- runtimes/spiritnet/src/weights/mod.rs | 1 + .../spiritnet/src/weights/pallet_collators.rs | 259 ++++++++++++++++++ scripts/run_benches_for_runtime.sh | 1 + 10 files changed, 548 insertions(+), 13 deletions(-) create mode 100644 runtimes/peregrine/src/weights/pallet_collators.rs create mode 100644 runtimes/spiritnet/src/weights/pallet_collators.rs diff --git a/runtimes/common/src/constants.rs b/runtimes/common/src/constants.rs index 120f745763..41e53365e6 100644 --- a/runtimes/common/src/constants.rs +++ b/runtimes/common/src/constants.rs @@ -278,6 +278,8 @@ pub mod staking { pub const NETWORK_REWARD_RATE: Perquintill = Perquintill::from_percent(10); + pub const MAX_COLLATORS: u32 = 32; + parameter_types! { /// Minimum round length is 1 hour pub const MinBlocksPerRound: BlockNumber = MIN_BLOCKS_PER_ROUND; @@ -309,6 +311,8 @@ pub mod staking { pub const NetworkRewardStart: BlockNumber = super::treasury::INITIAL_PERIOD_LENGTH; /// The rate in percent for the network rewards pub const NetworkRewardRate: Perquintill = NETWORK_REWARD_RATE; + /// The maximum number of collators + pub const MaxCollators: u32 = MAX_COLLATORS; } } diff --git a/runtimes/peregrine/src/benchmarks/mod.rs b/runtimes/peregrine/src/benchmarks/mod.rs index 88fa2d3ee5..611eb6637d 100644 --- a/runtimes/peregrine/src/benchmarks/mod.rs +++ b/runtimes/peregrine/src/benchmarks/mod.rs @@ -39,9 +39,9 @@ use xcm::v4::{Asset, Assets, Fungibility, Location}; use crate::{ xcm::XcmConfig, AllPalletsWithSystem, AssetSwitchPool1, Attestation, Balances, BondedCurrencies, BondedFungibles, Council, Ctype, Delegation, Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, Inflation, - MessageQueue, Migration, Multisig, ParachainStaking, ParachainSystem, Preimage, Proxy, PublicCredentials, Runtime, - RuntimeEvent, Scheduler, Sudo, System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, - Treasury, Utility, Vesting, Web3Names, + MessageQueue, Migration, Multisig, ParachainStaking, ParachainSystem, PermissionedCollator, Preimage, Proxy, + PublicCredentials, Runtime, RuntimeEvent, Scheduler, Sudo, System, TechnicalCommittee, TechnicalMembership, + Timestamp, Tips, TipsMembership, Treasury, Utility, Vesting, Web3Names, }; pub(crate) mod asset_switch; @@ -56,11 +56,9 @@ use pallet_assets as pallet_bonded_assets; #[allow(unused_imports)] use pallet_collective as pallet_technical_committee_collective; #[allow(unused_imports)] -use pallet_did_lookup as pallet_unique_linking; -#[allow(unused_imports)] use pallet_membership as pallet_technical_membership; #[allow(unused_imports)] -use pallet_web3_names as pallet_dot_names; +use pallet_membership as pallet_collators; define_benchmarks!( [frame_system, frame_system_benchmarking::Pallet::] @@ -100,6 +98,7 @@ define_benchmarks!( // pallet_membership instances [pallet_membership, TipsMembership] [pallet_technical_membership, TechnicalMembership] + [pallet_collators, PermissionedCollator] // pallet_did_lookup instances [pallet_did_lookup, DidLookup] // pallet_web3_names instances diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index 327fc7a700..8059330621 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -328,7 +328,11 @@ impl pallet_membership::Config for Runtime { type ResetOrigin = EnsureRoot; type PrimeOrigin = EnsureRoot; type MembershipInitialized = (); + + #[cfg(feature = "runtime-benchmarks")] + type MembershipChanged = crate::benchmarks::governance::MockMembershipChangedForBenchmarks; + #[cfg(not(feature = "runtime-benchmarks"))] type MembershipChanged = (); - type MaxMembers = constants::governance::TechnicalMaxMembers; - type WeightInfo = weights::pallet_technical_membership::WeightInfo; + type MaxMembers = constants::staking::MaxCollators; + type WeightInfo = weights::pallet_collators::WeightInfo; } diff --git a/runtimes/peregrine/src/weights/mod.rs b/runtimes/peregrine/src/weights/mod.rs index ec7616fdc8..d9e0199501 100644 --- a/runtimes/peregrine/src/weights/mod.rs +++ b/runtimes/peregrine/src/weights/mod.rs @@ -28,6 +28,7 @@ pub mod pallet_assets; pub mod pallet_balances; pub mod pallet_bonded_assets; pub mod pallet_bonded_coins; +pub mod pallet_collators; pub mod pallet_collective; pub mod pallet_democracy; pub mod pallet_deposit_storage; diff --git a/runtimes/peregrine/src/weights/pallet_collators.rs b/runtimes/peregrine/src/weights/pallet_collators.rs new file mode 100644 index 0000000000..07dabe8963 --- /dev/null +++ b/runtimes/peregrine/src/weights/pallet_collators.rs @@ -0,0 +1,259 @@ +// KILT Blockchain – +// Copyright (C) 2025, KILT Foundation + +// The KILT Blockchain 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. + +// The KILT Blockchain 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 this program. If not, see . + +// If you feel like getting in touch with us, you can do so at + +//! Autogenerated weights for `pallet_collators` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 33.0.0 +//! DATE: 2025-04-23, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `rust-2`, CPU: `12th Gen Intel(R) Core(TM) i9-12900K` +//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/kilt-parachain +// benchmark +// pallet +// --heap-pages=4096 +// --chain=dev +// --pallet=pallet-collators +// --extrinsic=* +// --header=HEADER-GPL +// --template=.maintain/runtime-weight-template.hbs +// --output=./runtimes/peregrine/src/weights/ + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_imports)] +#![allow(clippy::as_conversions)] + +use frame_support::{traits::Get, weights::Weight}; +use sp_std::marker::PhantomData; + +// TODO: Remove once the benchmarking stuff fully supports pallet instances. +use pallet_membership as pallet_collators; + +/// Weight functions for `pallet_collators`. +pub struct WeightInfo(PhantomData); +impl pallet_collators::WeightInfo for WeightInfo { + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// The range of component `m` is `[1, 31]`. + fn add_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `170 + m * (32 ±0)` + // Estimated: `2510` + // Minimum execution time: 4_812_000 picoseconds. + Weight::from_parts(5_223_134, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 771 + .saturating_add(Weight::from_parts(16_478, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:0) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[2, 32]`. + fn remove_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_159_000 picoseconds. + Weight::from_parts(6_682_650, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 725 + .saturating_add(Weight::from_parts(15_110, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:0) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[2, 32]`. + fn swap_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_279_000 picoseconds. + Weight::from_parts(6_612_007, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 980 + .saturating_add(Weight::from_parts(26_782, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:0) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn reset_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_318_000 picoseconds. + Weight::from_parts(6_828_217, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 932 + .saturating_add(Weight::from_parts(72_412, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:1) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn change_key(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_538_000 picoseconds. + Weight::from_parts(7_074_832, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 891 + .saturating_add(Weight::from_parts(18_976, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(3)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:0) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:0 w:1) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn set_prime(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `170 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 3_643_000 picoseconds. + Weight::from_parts(4_129_209, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 497 + .saturating_add(Weight::from_parts(4_023, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Prime` (r:0 w:1) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn clear_prime(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_096_000 picoseconds. + Weight::from_parts(1_229_794, 0) + .saturating_add(Weight::from_parts(0, 0)) + // Standard Error: 94 + .saturating_add(Weight::from_parts(35, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().writes(2)) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_add_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_remove_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_swap_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_reset_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_change_key() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_set_prime() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } +} diff --git a/runtimes/spiritnet/src/benchmarks/mod.rs b/runtimes/spiritnet/src/benchmarks/mod.rs index fcb53b3132..34199432d8 100644 --- a/runtimes/spiritnet/src/benchmarks/mod.rs +++ b/runtimes/spiritnet/src/benchmarks/mod.rs @@ -39,9 +39,9 @@ use xcm::v4::{Asset, Assets, Fungibility, Location}; use crate::{ xcm::XcmConfig, AllPalletsWithSystem, AssetSwitchPool1, Attestation, Balances, Council, Ctype, Delegation, Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, Inflation, MessageQueue, Migration, - Multisig, ParachainStaking, ParachainSystem, Preimage, Proxy, PublicCredentials, Runtime, RuntimeEvent, Scheduler, - System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, Treasury, Utility, Vesting, - Web3Names, + Multisig, ParachainStaking, ParachainSystem, PermissionedCollator, Preimage, Proxy, PublicCredentials, Runtime, + RuntimeEvent, Scheduler, System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, + Treasury, Utility, Vesting, Web3Names, }; pub(crate) mod asset_switch; @@ -56,6 +56,9 @@ use pallet_collective as pallet_technical_committee_collective; #[allow(unused_imports)] use pallet_membership as pallet_technical_membership; +#[allow(unused_imports)] +use pallet_membership as pallet_collators; + define_benchmarks!( [frame_system, frame_system_benchmarking::Pallet::] [pallet_timestamp, Timestamp] @@ -93,6 +96,7 @@ define_benchmarks!( // pallet_membership instances [pallet_membership, TipsMembership] [pallet_technical_membership, TechnicalMembership] + [pallet_collators, PermissionedCollator] // pallet_did_lookup instances [pallet_did_lookup, DidLookup] // pallet_web3_names instances diff --git a/runtimes/spiritnet/src/system/mod.rs b/runtimes/spiritnet/src/system/mod.rs index 2528eba75e..9b6d526355 100644 --- a/runtimes/spiritnet/src/system/mod.rs +++ b/runtimes/spiritnet/src/system/mod.rs @@ -322,7 +322,10 @@ impl pallet_membership::Config for Runtime { type ResetOrigin = EnsureRoot; type PrimeOrigin = EnsureRoot; type MembershipInitialized = (); + #[cfg(feature = "runtime-benchmarks")] + type MembershipChanged = crate::benchmarks::governance::MockMembershipChangedForBenchmarks; + #[cfg(not(feature = "runtime-benchmarks"))] type MembershipChanged = (); - type MaxMembers = constants::governance::TechnicalMaxMembers; - type WeightInfo = weights::pallet_technical_membership::WeightInfo; + type MaxMembers = constants::staking::MaxCollators; + type WeightInfo = weights::pallet_collators::WeightInfo; } diff --git a/runtimes/spiritnet/src/weights/mod.rs b/runtimes/spiritnet/src/weights/mod.rs index 6e59fa1f9f..bbbd83504a 100644 --- a/runtimes/spiritnet/src/weights/mod.rs +++ b/runtimes/spiritnet/src/weights/mod.rs @@ -26,6 +26,7 @@ pub mod frame_system; pub mod pallet_asset_switch; pub mod pallet_assets; pub mod pallet_balances; +pub mod pallet_collators; pub mod pallet_collective; pub mod pallet_democracy; pub mod pallet_deposit_storage; diff --git a/runtimes/spiritnet/src/weights/pallet_collators.rs b/runtimes/spiritnet/src/weights/pallet_collators.rs new file mode 100644 index 0000000000..07dabe8963 --- /dev/null +++ b/runtimes/spiritnet/src/weights/pallet_collators.rs @@ -0,0 +1,259 @@ +// KILT Blockchain – +// Copyright (C) 2025, KILT Foundation + +// The KILT Blockchain 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. + +// The KILT Blockchain 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 this program. If not, see . + +// If you feel like getting in touch with us, you can do so at + +//! Autogenerated weights for `pallet_collators` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 33.0.0 +//! DATE: 2025-04-23, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! WORST CASE MAP SIZE: `1000000` +//! HOSTNAME: `rust-2`, CPU: `12th Gen Intel(R) Core(TM) i9-12900K` +//! EXECUTION: , WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// ./target/release/kilt-parachain +// benchmark +// pallet +// --heap-pages=4096 +// --chain=dev +// --pallet=pallet-collators +// --extrinsic=* +// --header=HEADER-GPL +// --template=.maintain/runtime-weight-template.hbs +// --output=./runtimes/peregrine/src/weights/ + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_imports)] +#![allow(clippy::as_conversions)] + +use frame_support::{traits::Get, weights::Weight}; +use sp_std::marker::PhantomData; + +// TODO: Remove once the benchmarking stuff fully supports pallet instances. +use pallet_membership as pallet_collators; + +/// Weight functions for `pallet_collators`. +pub struct WeightInfo(PhantomData); +impl pallet_collators::WeightInfo for WeightInfo { + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// The range of component `m` is `[1, 31]`. + fn add_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `170 + m * (32 ±0)` + // Estimated: `2510` + // Minimum execution time: 4_812_000 picoseconds. + Weight::from_parts(5_223_134, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 771 + .saturating_add(Weight::from_parts(16_478, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(1)) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:0) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[2, 32]`. + fn remove_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_159_000 picoseconds. + Weight::from_parts(6_682_650, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 725 + .saturating_add(Weight::from_parts(15_110, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:0) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[2, 32]`. + fn swap_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_279_000 picoseconds. + Weight::from_parts(6_612_007, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 980 + .saturating_add(Weight::from_parts(26_782, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:0) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn reset_member(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_318_000 picoseconds. + Weight::from_parts(6_828_217, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 932 + .saturating_add(Weight::from_parts(72_412, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:1) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:1 w:1) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn change_key(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `222 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 6_538_000 picoseconds. + Weight::from_parts(7_074_832, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 891 + .saturating_add(Weight::from_parts(18_976, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(2)) + .saturating_add(T::DbWeight::get().writes(3)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Members` (r:1 w:0) + /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `PermissionedCollator::Prime` (r:0 w:1) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn set_prime(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `170 + m * (32 ±0)` + // Estimated: `2510 + m * (32 ±0)` + // Minimum execution time: 3_643_000 picoseconds. + Weight::from_parts(4_129_209, 0) + .saturating_add(Weight::from_parts(0, 2510)) + // Standard Error: 497 + .saturating_add(Weight::from_parts(4_023, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().reads(1)) + .saturating_add(T::DbWeight::get().writes(2)) + .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) + } + /// Storage: `PermissionedCollator::Prime` (r:0 w:1) + /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) + /// The range of component `m` is `[1, 32]`. + fn clear_prime(m: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `0` + // Estimated: `0` + // Minimum execution time: 1_096_000 picoseconds. + Weight::from_parts(1_229_794, 0) + .saturating_add(Weight::from_parts(0, 0)) + // Standard Error: 94 + .saturating_add(Weight::from_parts(35, 0).saturating_mul(m.into())) + .saturating_add(T::DbWeight::get().writes(2)) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_add_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_remove_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_swap_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_reset_member() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_change_key() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } + #[test] + fn test_set_prime() { + assert!( + ::BlockWeights::get() + .per_class + .get(frame_support::dispatch::DispatchClass::Normal) + .max_extrinsic + .unwrap_or_else(::max_value) + .proof_size() + > 2510 + ); + } +} diff --git a/scripts/run_benches_for_runtime.sh b/scripts/run_benches_for_runtime.sh index 7720f99132..0e43280b42 100755 --- a/scripts/run_benches_for_runtime.sh +++ b/scripts/run_benches_for_runtime.sh @@ -45,6 +45,7 @@ pallets=( # `pallet-membership` instances pallet-membership pallet-technical-membership + pallet-collators # `pallet-collective` instances pallet-collective pallet-technical-committee-collective From 8a71d6ed28d6cf9516fdd1ce191f85811404e5c0 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 13:41:14 +0200 Subject: [PATCH 08/15] move session manager to runtime common --- runtimes/common/Cargo.toml | 4 +++ runtimes/common/src/lib.rs | 44 +++++++++++++++++++++++++- runtimes/peregrine/src/system/mod.rs | 46 +++------------------------- runtimes/spiritnet/src/system/mod.rs | 46 +++------------------------- 4 files changed, 55 insertions(+), 85 deletions(-) diff --git a/runtimes/common/Cargo.toml b/runtimes/common/Cargo.toml index 3e02afa07a..49437a8826 100644 --- a/runtimes/common/Cargo.toml +++ b/runtimes/common/Cargo.toml @@ -51,6 +51,7 @@ pallet-authorship = { workspace = true } pallet-balances = { workspace = true } pallet-membership = { workspace = true } pallet-multisig = { workspace = true } +pallet-session = { workspace = true } pallet-tips = { workspace = true } pallet-transaction-payment = { workspace = true } pallet-treasury = { workspace = true } @@ -58,6 +59,7 @@ sp-consensus-aura = { workspace = true } sp-core = { workspace = true } sp-io = { workspace = true } sp-runtime = { workspace = true } +sp-staking = { workspace = true } sp-std = { workspace = true } sp-trie = { workspace = true } sp-weights = { workspace = true } @@ -128,6 +130,7 @@ std = [ "pallet-membership/std", "pallet-multisig/std", "pallet-multisig/std", + "pallet-session/std", "pallet-tips/std", "pallet-transaction-payment/std", "pallet-treasury/std", @@ -164,6 +167,7 @@ try-runtime = [ "pallet-bonded-coins/try-runtime", "pallet-membership/try-runtime", "pallet-multisig/try-runtime", + "pallet-session/try-runtime", "pallet-transaction-payment/try-runtime", "pallet-treasury/try-runtime", "pallet-treasury/try-runtime", diff --git a/runtimes/common/src/lib.rs b/runtimes/common/src/lib.rs index 8e17e86e8a..b0deb750c0 100644 --- a/runtimes/common/src/lib.rs +++ b/runtimes/common/src/lib.rs @@ -40,7 +40,7 @@ use sp_runtime::{ traits::{BlakeTwo256, Bounded, IdentifyAccount, Verify}, FixedPointNumber, MultiAddress, MultiSignature, Perquintill, SaturatedConversion, }; -use sp_std::marker::PhantomData; +use sp_std::{marker::PhantomData, vec::Vec}; use sp_weights::Weight; pub mod asset_switch; @@ -239,3 +239,45 @@ where debug_assert!(result.is_ok(), "The whole credit cannot be countered"); } } + +type AccountIdOf = ::AccountId; + +/// The session manager for the collator set. +pub struct SessionManager(PhantomData); + +impl pallet_session::SessionManager> for SessionManager +where + Runtime: pallet_membership::Config, +{ + fn new_session(new_index: sp_staking::SessionIndex) -> Option>> { + let collators = pallet_membership::Pallet::::members().to_vec(); + + log::debug!( + "assembling new collators for new session {} at #{:?} with {:?}", + new_index, + frame_system::Pallet::::block_number(), + collators + ); + + frame_system::Pallet::::register_extra_weight_unchecked( + ::DbWeight::get().reads(2), + frame_support::pallet_prelude::DispatchClass::Mandatory, + ); + + if collators.is_empty() { + // we never want to pass an empty set of collators. This would brick the chain. + log::error!("💥 keeping old session because of empty collator set!"); + return None; + } + + Some(collators) + } + + fn start_session(_start_index: sp_staking::SessionIndex) { + // We don't care + } + + fn end_session(_end_index: sp_staking::SessionIndex) { + // We don't care + } +} diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index 8059330621..05caf34a0e 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -27,7 +27,7 @@ use runtime_common::{ constants, fees::{ToAuthorCredit, WeightToFee}, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockWeights, FeeSplit, Hash, Nonce, - SendDustAndFeesToTreasury, SlowAdjustingFeeUpdate, + SendDustAndFeesToTreasury, SessionManager, SlowAdjustingFeeUpdate, }; use sp_core::{ConstBool, ConstU128, ConstU16, ConstU32, ConstU64}; use sp_runtime::{ @@ -42,9 +42,8 @@ use xcm::v4::Location; use crate::{ governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, - weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, PermissionedCollator, Preimage, - Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, - VERSION, + weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, + RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, }; pub(crate) mod proxy; @@ -142,50 +141,13 @@ impl_opaque_keys! { } } -/// The session manager for the collator set. -pub struct SessionManager; - -impl pallet_session::SessionManager for SessionManager { - fn new_session(new_index: sp_staking::SessionIndex) -> Option> { - let collators = PermissionedCollator::members().to_vec(); - - log::debug!( - "assembling new collators for new session {} at #{:?} with {:?}", - new_index, - System::block_number(), - collators - ); - - System::register_extra_weight_unchecked( - ::DbWeight::get().reads(2), - frame_support::pallet_prelude::DispatchClass::Mandatory, - ); - - if collators.is_empty() { - // we never want to pass an empty set of collators. This would brick the chain. - log::error!("💥 keeping old session because of empty collator set!"); - return None; - } - - Some(collators) - } - - fn start_session(_start_index: sp_staking::SessionIndex) { - // We don't care - } - - fn end_session(_end_index: sp_staking::SessionIndex) { - // We don't care - } -} - impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = AccountId; type ValidatorIdOf = ConvertInto; type ShouldEndSession = ParachainStaking; type NextSessionRotation = ParachainStaking; - type SessionManager = SessionManager; + type SessionManager = SessionManager; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type WeightInfo = weights::pallet_session::WeightInfo; diff --git a/runtimes/spiritnet/src/system/mod.rs b/runtimes/spiritnet/src/system/mod.rs index 9b6d526355..bc543d1497 100644 --- a/runtimes/spiritnet/src/system/mod.rs +++ b/runtimes/spiritnet/src/system/mod.rs @@ -27,7 +27,7 @@ use runtime_common::{ constants, fees::{ToAuthorCredit, WeightToFee}, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockWeights, FeeSplit, Hash, Nonce, - SendDustAndFeesToTreasury, SlowAdjustingFeeUpdate, + SendDustAndFeesToTreasury, SessionManager, SlowAdjustingFeeUpdate, }; use sp_core::{ConstBool, ConstU128, ConstU16, ConstU32, ConstU64}; use sp_runtime::{ @@ -42,9 +42,8 @@ use xcm::v4::Location; use crate::{ governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, - weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, PermissionedCollator, Preimage, - Runtime, RuntimeCall, RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, - VERSION, + weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, + RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, }; pub(crate) mod proxy; @@ -142,50 +141,13 @@ impl_opaque_keys! { } } -/// The session manager for the collator set. -pub struct SessionManager; - -impl pallet_session::SessionManager for SessionManager { - fn new_session(new_index: sp_staking::SessionIndex) -> Option> { - let collators = PermissionedCollator::members().to_vec(); - - log::debug!( - "assembling new collators for new session {} at #{:?} with {:?}", - new_index, - System::block_number(), - collators - ); - - System::register_extra_weight_unchecked( - ::DbWeight::get().reads(2), - frame_support::pallet_prelude::DispatchClass::Mandatory, - ); - - if collators.is_empty() { - // we never want to pass an empty set of collators. This would brick the chain. - log::error!("💥 keeping old session because of empty collator set!"); - return None; - } - - Some(collators) - } - - fn start_session(_start_index: sp_staking::SessionIndex) { - // We don't care - } - - fn end_session(_end_index: sp_staking::SessionIndex) { - // We don't care - } -} - impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = AccountId; type ValidatorIdOf = ConvertInto; type ShouldEndSession = ParachainStaking; type NextSessionRotation = ParachainStaking; - type SessionManager = SessionManager; + type SessionManager = SessionManager; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type WeightInfo = weights::pallet_session::WeightInfo; From 6407d31edcc964c88b63f4fc71623f986f0fa19e Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 13:41:34 +0200 Subject: [PATCH 09/15] rename pallet --- Cargo.lock | 2 + runtimes/peregrine/src/benchmarks/mod.rs | 6 +-- runtimes/peregrine/src/lib.rs | 2 +- .../peregrine/src/weights/pallet_collators.rs | 48 +++++++++---------- runtimes/spiritnet/src/benchmarks/mod.rs | 4 +- runtimes/spiritnet/src/lib.rs | 2 +- .../spiritnet/src/weights/pallet_collators.rs | 48 +++++++++---------- 7 files changed, 57 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 793e365460..f9f96c2550 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10957,6 +10957,7 @@ dependencies = [ "pallet-dip-provider", "pallet-membership", "pallet-multisig", + "pallet-session", "pallet-tips", "pallet-transaction-payment", "pallet-treasury", @@ -10972,6 +10973,7 @@ dependencies = [ "sp-core", "sp-io", "sp-runtime", + "sp-staking", "sp-std", "sp-trie", "sp-weights", diff --git a/runtimes/peregrine/src/benchmarks/mod.rs b/runtimes/peregrine/src/benchmarks/mod.rs index 611eb6637d..7bc508d301 100644 --- a/runtimes/peregrine/src/benchmarks/mod.rs +++ b/runtimes/peregrine/src/benchmarks/mod.rs @@ -38,8 +38,8 @@ use xcm::v4::{Asset, Assets, Fungibility, Location}; use crate::{ xcm::XcmConfig, AllPalletsWithSystem, AssetSwitchPool1, Attestation, Balances, BondedCurrencies, BondedFungibles, - Council, Ctype, Delegation, Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, Inflation, - MessageQueue, Migration, Multisig, ParachainStaking, ParachainSystem, PermissionedCollator, Preimage, Proxy, + Collators, Council, Ctype, Delegation, Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, + Inflation, MessageQueue, Migration, Multisig, ParachainStaking, ParachainSystem, Preimage, Proxy, PublicCredentials, Runtime, RuntimeEvent, Scheduler, Sudo, System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, Treasury, Utility, Vesting, Web3Names, }; @@ -98,7 +98,7 @@ define_benchmarks!( // pallet_membership instances [pallet_membership, TipsMembership] [pallet_technical_membership, TechnicalMembership] - [pallet_collators, PermissionedCollator] + [pallet_collators, Collators] // pallet_did_lookup instances [pallet_did_lookup, DidLookup] // pallet_web3_names instances diff --git a/runtimes/peregrine/src/lib.rs b/runtimes/peregrine/src/lib.rs index d466ee073f..3bb07492ad 100644 --- a/runtimes/peregrine/src/lib.rs +++ b/runtimes/peregrine/src/lib.rs @@ -116,7 +116,7 @@ construct_runtime! { ParachainStaking: parachain_staking = 21, Authorship: pallet_authorship = 20, AuraExt: cumulus_pallet_aura_ext = 24, - PermissionedCollator: pallet_membership:: = 25, + Collators: pallet_membership:: = 25, Democracy: pallet_democracy = 30, Council: pallet_collective:: = 31, diff --git a/runtimes/peregrine/src/weights/pallet_collators.rs b/runtimes/peregrine/src/weights/pallet_collators.rs index 07dabe8963..fa6333702f 100644 --- a/runtimes/peregrine/src/weights/pallet_collators.rs +++ b/runtimes/peregrine/src/weights/pallet_collators.rs @@ -49,8 +49,8 @@ use pallet_membership as pallet_collators; /// Weight functions for `pallet_collators`. pub struct WeightInfo(PhantomData); impl pallet_collators::WeightInfo for WeightInfo { - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) /// The range of component `m` is `[1, 31]`. fn add_member(m: u32, ) -> Weight { // Proof Size summary in bytes: @@ -64,10 +64,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:0) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:0) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[2, 32]`. @@ -84,10 +84,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:0) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:0) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[2, 32]`. @@ -104,10 +104,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:0) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:0) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. @@ -124,10 +124,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:1) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:1) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. @@ -144,10 +144,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(3)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:0) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:0 w:1) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:0) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:0 w:1) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. @@ -164,8 +164,8 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Prime` (r:0 w:1) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:0 w:1) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. diff --git a/runtimes/spiritnet/src/benchmarks/mod.rs b/runtimes/spiritnet/src/benchmarks/mod.rs index 34199432d8..9c27b93148 100644 --- a/runtimes/spiritnet/src/benchmarks/mod.rs +++ b/runtimes/spiritnet/src/benchmarks/mod.rs @@ -39,7 +39,7 @@ use xcm::v4::{Asset, Assets, Fungibility, Location}; use crate::{ xcm::XcmConfig, AllPalletsWithSystem, AssetSwitchPool1, Attestation, Balances, Council, Ctype, Delegation, Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, Inflation, MessageQueue, Migration, - Multisig, ParachainStaking, ParachainSystem, PermissionedCollator, Preimage, Proxy, PublicCredentials, Runtime, + Multisig, ParachainStaking, ParachainSystem, Collators, Preimage, Proxy, PublicCredentials, Runtime, RuntimeEvent, Scheduler, System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, Treasury, Utility, Vesting, Web3Names, }; @@ -96,7 +96,7 @@ define_benchmarks!( // pallet_membership instances [pallet_membership, TipsMembership] [pallet_technical_membership, TechnicalMembership] - [pallet_collators, PermissionedCollator] + [pallet_collators, Collators] // pallet_did_lookup instances [pallet_did_lookup, DidLookup] // pallet_web3_names instances diff --git a/runtimes/spiritnet/src/lib.rs b/runtimes/spiritnet/src/lib.rs index afb6025238..fec68bca98 100644 --- a/runtimes/spiritnet/src/lib.rs +++ b/runtimes/spiritnet/src/lib.rs @@ -113,7 +113,7 @@ construct_runtime! { ParachainStaking: parachain_staking = 21, Authorship: pallet_authorship = 20, AuraExt: cumulus_pallet_aura_ext = 24, - PermissionedCollator: pallet_membership:: = 25, + Collators: pallet_membership:: = 25, Democracy: pallet_democracy = 30, Council: pallet_collective:: = 31, diff --git a/runtimes/spiritnet/src/weights/pallet_collators.rs b/runtimes/spiritnet/src/weights/pallet_collators.rs index 07dabe8963..fa6333702f 100644 --- a/runtimes/spiritnet/src/weights/pallet_collators.rs +++ b/runtimes/spiritnet/src/weights/pallet_collators.rs @@ -49,8 +49,8 @@ use pallet_membership as pallet_collators; /// Weight functions for `pallet_collators`. pub struct WeightInfo(PhantomData); impl pallet_collators::WeightInfo for WeightInfo { - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) /// The range of component `m` is `[1, 31]`. fn add_member(m: u32, ) -> Weight { // Proof Size summary in bytes: @@ -64,10 +64,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:0) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:0) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[2, 32]`. @@ -84,10 +84,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:0) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:0) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[2, 32]`. @@ -104,10 +104,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:0) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:0) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. @@ -124,10 +124,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:1) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:1 w:1) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:1) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:1 w:1) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. @@ -144,10 +144,10 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(3)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Members` (r:1 w:0) - /// Proof: `PermissionedCollator::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) - /// Storage: `PermissionedCollator::Prime` (r:0 w:1) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Members` (r:1 w:0) + /// Proof: `Collators::Members` (`max_values`: Some(1), `max_size`: Some(1025), added: 1520, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:0 w:1) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. @@ -164,8 +164,8 @@ impl pallet_collators::WeightInfo for WeightInfo { .saturating_add(T::DbWeight::get().writes(2)) .saturating_add(Weight::from_parts(0, 32).saturating_mul(m.into())) } - /// Storage: `PermissionedCollator::Prime` (r:0 w:1) - /// Proof: `PermissionedCollator::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) + /// Storage: `Collators::Prime` (r:0 w:1) + /// Proof: `Collators::Prime` (`max_values`: Some(1), `max_size`: Some(32), added: 527, mode: `MaxEncodedLen`) /// Storage: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// Proof: UNKNOWN KEY `0xdfe4dad7b89644d5962c7bb034ca6f7bb10896c85af113f86bae1ab193a37922` (r:0 w:1) /// The range of component `m` is `[1, 32]`. From 5e68c6aec3726646b8c35384f486daba7841fb5f Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 13:44:46 +0200 Subject: [PATCH 10/15] fmt --- runtimes/spiritnet/src/benchmarks/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/runtimes/spiritnet/src/benchmarks/mod.rs b/runtimes/spiritnet/src/benchmarks/mod.rs index 9c27b93148..ee4356ec5c 100644 --- a/runtimes/spiritnet/src/benchmarks/mod.rs +++ b/runtimes/spiritnet/src/benchmarks/mod.rs @@ -37,11 +37,11 @@ use sp_std::{boxed::Box, vec, vec::Vec}; use xcm::v4::{Asset, Assets, Fungibility, Location}; use crate::{ - xcm::XcmConfig, AllPalletsWithSystem, AssetSwitchPool1, Attestation, Balances, Council, Ctype, Delegation, - Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, Inflation, MessageQueue, Migration, - Multisig, ParachainStaking, ParachainSystem, Collators, Preimage, Proxy, PublicCredentials, Runtime, - RuntimeEvent, Scheduler, System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, - Treasury, Utility, Vesting, Web3Names, + xcm::XcmConfig, AllPalletsWithSystem, AssetSwitchPool1, Attestation, Balances, Collators, Council, Ctype, + Delegation, Democracy, DepositStorage, Did, DidLookup, DipProvider, Fungibles, Indices, Inflation, MessageQueue, + Migration, Multisig, ParachainStaking, ParachainSystem, Preimage, Proxy, PublicCredentials, Runtime, RuntimeEvent, + Scheduler, System, TechnicalCommittee, TechnicalMembership, Timestamp, Tips, TipsMembership, Treasury, Utility, + Vesting, Web3Names, }; pub(crate) mod asset_switch; From cc706fa9a0be759f9334eec88430dff89741a3d6 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 13:50:49 +0200 Subject: [PATCH 11/15] change origin checker --- runtimes/peregrine/src/system/mod.rs | 8 ++++---- runtimes/spiritnet/src/system/mod.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index 05caf34a0e..733cd80fe2 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -18,7 +18,7 @@ use frame_support::{ parameter_types, - traits::{AsEnsureOriginWithArg, Everything, PrivilegeCmp}, + traits::{AsEnsureOriginWithArg, Everything, NeverEnsureOrigin, PrivilegeCmp}, weights::Weight, }; use frame_system::EnsureRoot; @@ -286,9 +286,9 @@ impl pallet_membership::Config for Runtime { type RuntimeEvent = RuntimeEvent; type AddOrigin = RootOrMoreThanHalfCouncil; type RemoveOrigin = RootOrMoreThanHalfCouncil; - type SwapOrigin = EnsureRoot; - type ResetOrigin = EnsureRoot; - type PrimeOrigin = EnsureRoot; + type SwapOrigin = RootOrMoreThanHalfCouncil; + type ResetOrigin = RootOrMoreThanHalfCouncil; + type PrimeOrigin = NeverEnsureOrigin; type MembershipInitialized = (); #[cfg(feature = "runtime-benchmarks")] diff --git a/runtimes/spiritnet/src/system/mod.rs b/runtimes/spiritnet/src/system/mod.rs index bc543d1497..4196c1f60a 100644 --- a/runtimes/spiritnet/src/system/mod.rs +++ b/runtimes/spiritnet/src/system/mod.rs @@ -18,7 +18,7 @@ use frame_support::{ parameter_types, - traits::{AsEnsureOriginWithArg, Everything, PrivilegeCmp}, + traits::{AsEnsureOriginWithArg, Everything, NeverEnsureOrigin, PrivilegeCmp}, weights::Weight, }; use frame_system::EnsureRoot; @@ -280,9 +280,9 @@ impl pallet_membership::Config for Runtime { type RuntimeEvent = RuntimeEvent; type AddOrigin = RootOrMoreThanHalfCouncil; type RemoveOrigin = RootOrMoreThanHalfCouncil; - type SwapOrigin = EnsureRoot; - type ResetOrigin = EnsureRoot; - type PrimeOrigin = EnsureRoot; + type SwapOrigin = RootOrMoreThanHalfCouncil; + type ResetOrigin = RootOrMoreThanHalfCouncil; + type PrimeOrigin = NeverEnsureOrigin; type MembershipInitialized = (); #[cfg(feature = "runtime-benchmarks")] type MembershipChanged = crate::benchmarks::governance::MockMembershipChangedForBenchmarks; From 12a5b7f7a387e4a445abd8604a011f0541242850 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 14:11:24 +0200 Subject: [PATCH 12/15] other round handler --- runtimes/common/src/lib.rs | 45 +---------------- runtimes/common/src/session.rs | 72 ++++++++++++++++++++++++++++ runtimes/peregrine/src/system/mod.rs | 11 +++-- runtimes/spiritnet/src/system/mod.rs | 11 +++-- 4 files changed, 86 insertions(+), 53 deletions(-) create mode 100644 runtimes/common/src/session.rs diff --git a/runtimes/common/src/lib.rs b/runtimes/common/src/lib.rs index b0deb750c0..358d6e9e8a 100644 --- a/runtimes/common/src/lib.rs +++ b/runtimes/common/src/lib.rs @@ -40,7 +40,7 @@ use sp_runtime::{ traits::{BlakeTwo256, Bounded, IdentifyAccount, Verify}, FixedPointNumber, MultiAddress, MultiSignature, Perquintill, SaturatedConversion, }; -use sp_std::{marker::PhantomData, vec::Vec}; +use sp_std::marker::PhantomData; use sp_weights::Weight; pub mod asset_switch; @@ -55,6 +55,7 @@ pub mod errors; pub mod fees; pub mod migrations; pub mod pallet_id; +pub mod session; pub mod web3_names; pub mod xcm_config; pub use web3_names::Web3Name; @@ -239,45 +240,3 @@ where debug_assert!(result.is_ok(), "The whole credit cannot be countered"); } } - -type AccountIdOf = ::AccountId; - -/// The session manager for the collator set. -pub struct SessionManager(PhantomData); - -impl pallet_session::SessionManager> for SessionManager -where - Runtime: pallet_membership::Config, -{ - fn new_session(new_index: sp_staking::SessionIndex) -> Option>> { - let collators = pallet_membership::Pallet::::members().to_vec(); - - log::debug!( - "assembling new collators for new session {} at #{:?} with {:?}", - new_index, - frame_system::Pallet::::block_number(), - collators - ); - - frame_system::Pallet::::register_extra_weight_unchecked( - ::DbWeight::get().reads(2), - frame_support::pallet_prelude::DispatchClass::Mandatory, - ); - - if collators.is_empty() { - // we never want to pass an empty set of collators. This would brick the chain. - log::error!("💥 keeping old session because of empty collator set!"); - return None; - } - - Some(collators) - } - - fn start_session(_start_index: sp_staking::SessionIndex) { - // We don't care - } - - fn end_session(_end_index: sp_staking::SessionIndex) { - // We don't care - } -} diff --git a/runtimes/common/src/session.rs b/runtimes/common/src/session.rs new file mode 100644 index 0000000000..5c11641214 --- /dev/null +++ b/runtimes/common/src/session.rs @@ -0,0 +1,72 @@ +// KILT Blockchain – +// Copyright (C) 2025, KILT Foundation + +// The KILT Blockchain 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. + +// The KILT Blockchain 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 this program. If not, see . + +// If you feel like getting in touch with us, you can do so at + +pub use frame_support::weights::constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; +use frame_system::Pallet as SystemPallet; +use pallet_membership::{Config as MembershipConfig, Instance3, Pallet as MembershipPallet}; +use pallet_session::SessionManager as SessionManagerTrait; +pub use sp_consensus_aura::sr25519::AuthorityId; +use sp_core::Get; +use sp_staking::SessionIndex; +use sp_std::{marker::PhantomData, vec::Vec}; + +use crate::constants::staking::DefaultBlocksPerRound; + +type AccountIdOf = ::AccountId; + +/// The session manager for the collator set. +pub struct SessionManager(PhantomData); + +impl SessionManagerTrait> for SessionManager +where + Runtime: MembershipConfig, +{ + fn new_session(new_index: SessionIndex) -> Option>> { + let collators = MembershipPallet::::members().to_vec(); + + log::debug!( + "assembling new collators for new session {} at #{:?} with {:?}", + new_index, + SystemPallet::::block_number(), + collators + ); + + SystemPallet::::register_extra_weight_unchecked( + ::DbWeight::get().reads(2), + frame_support::pallet_prelude::DispatchClass::Mandatory, + ); + + if collators.is_empty() { + // we never want to pass an empty set of collators. This would brick the chain. + log::error!("💥 keeping old session because of empty collator set!"); + return None; + } + + Some(collators) + } + + fn start_session(_start_index: SessionIndex) { + // We don't care + } + + fn end_session(_end_index: SessionIndex) { + // We don't care + } +} + +pub type FixedLengthSession = pallet_session::PeriodicSessions; diff --git a/runtimes/peregrine/src/system/mod.rs b/runtimes/peregrine/src/system/mod.rs index 733cd80fe2..8645840ef8 100644 --- a/runtimes/peregrine/src/system/mod.rs +++ b/runtimes/peregrine/src/system/mod.rs @@ -26,8 +26,9 @@ use runtime_common::{ asset_switch::EnsureRootAsTreasury, constants, fees::{ToAuthorCredit, WeightToFee}, + session::{FixedLengthSession, SessionManager}, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockWeights, FeeSplit, Hash, Nonce, - SendDustAndFeesToTreasury, SessionManager, SlowAdjustingFeeUpdate, + SendDustAndFeesToTreasury, SlowAdjustingFeeUpdate, }; use sp_core::{ConstBool, ConstU128, ConstU16, ConstU32, ConstU64}; use sp_runtime::{ @@ -42,8 +43,8 @@ use xcm::v4::Location; use crate::{ governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, - weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, - RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, + weights, Aura, Balances, Block, OriginCaller, PalletInfo, Preimage, Runtime, RuntimeCall, RuntimeEvent, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, }; pub(crate) mod proxy; @@ -145,8 +146,8 @@ impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = AccountId; type ValidatorIdOf = ConvertInto; - type ShouldEndSession = ParachainStaking; - type NextSessionRotation = ParachainStaking; + type ShouldEndSession = FixedLengthSession; + type NextSessionRotation = FixedLengthSession; type SessionManager = SessionManager; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; diff --git a/runtimes/spiritnet/src/system/mod.rs b/runtimes/spiritnet/src/system/mod.rs index 4196c1f60a..3196034dfe 100644 --- a/runtimes/spiritnet/src/system/mod.rs +++ b/runtimes/spiritnet/src/system/mod.rs @@ -26,8 +26,9 @@ use runtime_common::{ asset_switch::EnsureRootAsTreasury, constants, fees::{ToAuthorCredit, WeightToFee}, + session::{FixedLengthSession, SessionManager}, AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockWeights, FeeSplit, Hash, Nonce, - SendDustAndFeesToTreasury, SessionManager, SlowAdjustingFeeUpdate, + SendDustAndFeesToTreasury, SlowAdjustingFeeUpdate, }; use sp_core::{ConstBool, ConstU128, ConstU16, ConstU32, ConstU64}; use sp_runtime::{ @@ -42,8 +43,8 @@ use xcm::v4::Location; use crate::{ governance::{CouncilCollective, RootOrCollectiveProportion, RootOrMoreThanHalfCouncil}, - weights, Aura, Balances, Block, OriginCaller, PalletInfo, ParachainStaking, Preimage, Runtime, RuntimeCall, - RuntimeEvent, RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, + weights, Aura, Balances, Block, OriginCaller, PalletInfo, Preimage, Runtime, RuntimeCall, RuntimeEvent, + RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION, }; pub(crate) mod proxy; @@ -145,8 +146,8 @@ impl pallet_session::Config for Runtime { type RuntimeEvent = RuntimeEvent; type ValidatorId = AccountId; type ValidatorIdOf = ConvertInto; - type ShouldEndSession = ParachainStaking; - type NextSessionRotation = ParachainStaking; + type ShouldEndSession = FixedLengthSession; + type NextSessionRotation = FixedLengthSession; type SessionManager = SessionManager; type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; From 2dcd0210157c976cbe8c9542eb04266297f37188 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Wed, 23 Apr 2025 14:59:29 +0200 Subject: [PATCH 13/15] additional check for collators --- runtimes/common/src/session.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/runtimes/common/src/session.rs b/runtimes/common/src/session.rs index 5c11641214..b833717fa3 100644 --- a/runtimes/common/src/session.rs +++ b/runtimes/common/src/session.rs @@ -22,6 +22,7 @@ use pallet_membership::{Config as MembershipConfig, Instance3, Pallet as Members use pallet_session::SessionManager as SessionManagerTrait; pub use sp_consensus_aura::sr25519::AuthorityId; use sp_core::Get; +use sp_runtime::SaturatedConversion; use sp_staking::SessionIndex; use sp_std::{marker::PhantomData, vec::Vec}; @@ -34,7 +35,8 @@ pub struct SessionManager(PhantomData); impl SessionManagerTrait> for SessionManager where - Runtime: MembershipConfig, + Runtime: MembershipConfig + pallet_session::Config, + ::ValidatorId: From>, { fn new_session(new_index: SessionIndex) -> Option>> { let collators = MembershipPallet::::members().to_vec(); @@ -46,12 +48,18 @@ where collators ); + let has_collator_keys = collators.iter().any(|collator| { + pallet_session::NextKeys::::contains_key(::ValidatorId::from( + collator.clone(), + )) + }); + SystemPallet::::register_extra_weight_unchecked( - ::DbWeight::get().reads(2), + ::DbWeight::get().reads(2 + collators.len().saturated_into::()), frame_support::pallet_prelude::DispatchClass::Mandatory, ); - if collators.is_empty() { + if collators.is_empty() || !has_collator_keys { // we never want to pass an empty set of collators. This would brick the chain. log::error!("💥 keeping old session because of empty collator set!"); return None; From 7239539edfe058af24c2f2f085a0aaaaf07fa287 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Thu, 24 Apr 2025 13:41:05 +0200 Subject: [PATCH 14/15] make test pass --- runtimes/common/src/session.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtimes/common/src/session.rs b/runtimes/common/src/session.rs index b833717fa3..b09254502c 100644 --- a/runtimes/common/src/session.rs +++ b/runtimes/common/src/session.rs @@ -55,7 +55,8 @@ where }); SystemPallet::::register_extra_weight_unchecked( - ::DbWeight::get().reads(2 + collators.len().saturated_into::()), + ::DbWeight::get() + .reads(2.saturating_add(collators.len().saturated_into::())), frame_support::pallet_prelude::DispatchClass::Mandatory, ); From f4baa2ce77349af6a2c200d279f6b53fe736baa7 Mon Sep 17 00:00:00 2001 From: Adel Golghalyani Date: Thu, 24 Apr 2025 13:43:27 +0200 Subject: [PATCH 15/15] make test pass --- runtimes/common/src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtimes/common/src/session.rs b/runtimes/common/src/session.rs index b09254502c..13d8dde99b 100644 --- a/runtimes/common/src/session.rs +++ b/runtimes/common/src/session.rs @@ -56,7 +56,7 @@ where SystemPallet::::register_extra_weight_unchecked( ::DbWeight::get() - .reads(2.saturating_add(collators.len().saturated_into::())), + .reads(2u64.saturating_add(collators.len().saturated_into::())), frame_support::pallet_prelude::DispatchClass::Mandatory, );