Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions runtimes/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ 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 }
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 }
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions runtimes/common/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down
1 change: 1 addition & 0 deletions runtimes/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
81 changes: 81 additions & 0 deletions runtimes/common/src/session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// KILT Blockchain – <https://kilt.io>
// 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 <https://www.gnu.org/licenses/>.

// If you feel like getting in touch with us, you can do so at <hello@kilt.io>

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_runtime::SaturatedConversion;
use sp_staking::SessionIndex;
use sp_std::{marker::PhantomData, vec::Vec};

use crate::constants::staking::DefaultBlocksPerRound;

type AccountIdOf<Runtime> = <Runtime as frame_system::Config>::AccountId;

/// The session manager for the collator set.
pub struct SessionManager<Runtime>(PhantomData<Runtime>);

impl<Runtime> SessionManagerTrait<AccountIdOf<Runtime>> for SessionManager<Runtime>
where
Runtime: MembershipConfig<Instance3> + pallet_session::Config,
<Runtime as pallet_session::Config>::ValidatorId: From<AccountIdOf<Runtime>>,
{
fn new_session(new_index: SessionIndex) -> Option<Vec<AccountIdOf<Runtime>>> {
let collators = MembershipPallet::<Runtime, Instance3>::members().to_vec();

log::debug!(
"assembling new collators for new session {} at #{:?} with {:?}",
new_index,
SystemPallet::<Runtime>::block_number(),
collators
);

let has_collator_keys = collators.iter().any(|collator| {
pallet_session::NextKeys::<Runtime>::contains_key(<Runtime as pallet_session::Config>::ValidatorId::from(
collator.clone(),
))
});

SystemPallet::<Runtime>::register_extra_weight_unchecked(
<Runtime as frame_system::Config>::DbWeight::get()
.reads(2u64.saturating_add(collators.len().saturated_into::<u64>())),
frame_support::pallet_prelude::DispatchClass::Mandatory,
);

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!");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a different error message depending on which condition is false would massively reduce headaches in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. I created a new PR: #887

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<DefaultBlocksPerRound, DefaultBlocksPerRound>;
1 change: 1 addition & 0 deletions runtimes/peregrine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
13 changes: 6 additions & 7 deletions runtimes/peregrine/src/benchmarks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ 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,
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,
};

pub(crate) mod asset_switch;
Expand All @@ -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::<Runtime>]
Expand Down Expand Up @@ -100,6 +98,7 @@ define_benchmarks!(
// pallet_membership instances
[pallet_membership, TipsMembership]
[pallet_technical_membership, TechnicalMembership]
[pallet_collators, Collators]
// pallet_did_lookup instances
[pallet_did_lookup, DidLookup]
// pallet_web3_names instances
Expand Down
2 changes: 1 addition & 1 deletion runtimes/peregrine/src/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl pallet_collective::Config<TechnicalCollective> for Runtime {
type SetMembersOrigin = EnsureRoot<AccountId>;
}

type RootOrMoreThanHalfCouncil = RootOrCollectiveProportion<CouncilCollective, 1, 2>;
pub(crate) type RootOrMoreThanHalfCouncil = RootOrCollectiveProportion<CouncilCollective, 1, 2>;

type TechnicalMembershipProvider = pallet_membership::Instance1;
impl pallet_membership::Config<TechnicalMembershipProvider> for Runtime {
Expand Down
1 change: 1 addition & 0 deletions runtimes/peregrine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ construct_runtime! {
ParachainStaking: parachain_staking = 21,
Authorship: pallet_authorship = 20,
AuraExt: cumulus_pallet_aura_ext = 24,
Collators: pallet_membership::<Instance3> = 25,

Democracy: pallet_democracy = 30,
Council: pallet_collective::<Instance1> = 31,
Expand Down
35 changes: 27 additions & 8 deletions runtimes/peregrine/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@

use frame_support::{
parameter_types,
traits::{AsEnsureOriginWithArg, Everything, PrivilegeCmp},
traits::{AsEnsureOriginWithArg, Everything, NeverEnsureOrigin, PrivilegeCmp},
weights::Weight,
};
use frame_system::EnsureRoot;
use runtime_common::{
asset_switch::EnsureRootAsTreasury,
constants,
fees::{ToAuthorCredit, WeightToFee},
session::{FixedLengthSession, SessionManager},
AccountId, AuthorityId, Balance, BlockHashCount, BlockLength, BlockWeights, FeeSplit, Hash, Nonce,
SendDustAndFeesToTreasury, SlowAdjustingFeeUpdate,
};
Expand All @@ -41,9 +42,9 @@ 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, Preimage, Runtime, RuntimeCall, RuntimeEvent,
RuntimeFreezeReason, RuntimeHoldReason, RuntimeOrigin, RuntimeTask, System, VERSION,
};

pub(crate) mod proxy;
Expand Down Expand Up @@ -145,9 +146,9 @@ 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 ShouldEndSession = FixedLengthSession;
type NextSessionRotation = FixedLengthSession;
type SessionManager = SessionManager<Runtime>;
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
type Keys = SessionKeys;
type WeightInfo = weights::pallet_session::WeightInfo<Runtime>;
Expand All @@ -163,7 +164,7 @@ impl pallet_aura::Config for Runtime {

impl pallet_authorship::Config for Runtime {
type FindAuthor = pallet_session::FindAccountFromAuthorIndex<Self, Aura>;
type EventHandler = ParachainStaking;
type EventHandler = ();
}

impl pallet_utility::Config for Runtime {
Expand Down Expand Up @@ -280,3 +281,21 @@ impl pallet_scheduler::Config for Runtime {
type OriginPrivilegeCmp = OriginPrivilegeCmp;
type Preimages = Preimage;
}

type CollatorMembershipProvider = pallet_membership::Instance3;
impl pallet_membership::Config<CollatorMembershipProvider> for Runtime {
type RuntimeEvent = RuntimeEvent;
type AddOrigin = RootOrMoreThanHalfCouncil;
type RemoveOrigin = RootOrMoreThanHalfCouncil;
type SwapOrigin = RootOrMoreThanHalfCouncil;
type ResetOrigin = RootOrMoreThanHalfCouncil;
type PrimeOrigin = NeverEnsureOrigin<AccountId>;
type MembershipInitialized = ();

#[cfg(feature = "runtime-benchmarks")]
type MembershipChanged = crate::benchmarks::governance::MockMembershipChangedForBenchmarks;
#[cfg(not(feature = "runtime-benchmarks"))]
type MembershipChanged = ();
type MaxMembers = constants::staking::MaxCollators;
type WeightInfo = weights::pallet_collators::WeightInfo<Runtime>;
}
1 change: 1 addition & 0 deletions runtimes/peregrine/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading
Loading