From 630f4215b8f05c7841d948ba77535de536f8b217 Mon Sep 17 00:00:00 2001 From: Eliott Teissonniere <10683430+ETeissonniere@users.noreply.github.com> Date: Thu, 20 May 2021 13:32:57 +0200 Subject: [PATCH] gov. refactoring (#50) * add tech com * add mandate to tech comm * bump spec version * remove sudo pallet * sudo and tech members migration * remove prime member migration as it gets cleared if we add more members later * clean unused import --- node/src/chain_spec.rs | 8 ++++-- runtime/Cargo.toml | 9 ++++-- runtime/src/lib.rs | 7 ++++- runtime/src/migrations.rs | 46 +++++++++++++++++++++++++++++++ runtime/src/pallets_governance.rs | 43 +++++++++++++++++++++++++++-- runtime/src/version.rs | 2 +- 6 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 runtime/src/migrations.rs diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index ac1e0ba3..960c3339 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -14,7 +14,7 @@ use ternoa_primitives::{AccountId, Balance, Signature}; use ternoa_runtime::{ constants::currency::UNIT, wasm_binary_unwrap, AuthorityDiscoveryConfig, BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig, ImOnlineConfig, SessionConfig, SessionKeys, - StakerStatus, StakingConfig, SudoConfig, SystemConfig, + StakerStatus, StakingConfig, SystemConfig, TechnicalMembershipConfig, }; type AccountPublic = ::Signer; @@ -151,8 +151,10 @@ pub fn testnet_genesis( pallet_treasury: Default::default(), // Governance - pallet_sudo: Some(SudoConfig { - key: root.unwrap_or(get_account_id_from_seed::("Alice")), + pallet_collective_Instance0: Some(Default::default()), + pallet_membership_Instance0: Some(TechnicalMembershipConfig { + members: vec![root.unwrap_or(get_account_id_from_seed::("Alice"))], + phantom: Default::default(), }), // Ternoa diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index b86bce51..5f743f32 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -21,16 +21,18 @@ pallet-authorship = { version = "3.0.0", default-features = false } pallet-babe = { version = "3.0.0", default-features = false } pallet-balances = { version = "3.0.0", default-features = false } pallet-bounties = { version = "3.0.0", default-features = false } +pallet-collective = { version = "3.0.0", default-features = false } pallet-curveless-staking = { version = "0.1.0", default-features = false } pallet-grandpa = { version = "3.0.0", default-features = false } pallet-im-online = { version = "3.0.0", default-features = false } +pallet-mandate = { version = "2.0.8", default-features = false } +pallet-membership = { version = "3.0.0", default-features = false } pallet-offences = { version = "3.0.0", default-features = false } #pallet-offences-benchmarking = { version = "3.0.0", default-features = false, optional = true } pallet-randomness-collective-flip = { version = "3.0.0", default-features = false } pallet-scheduler = { version = "3.0.0", default-features = false } pallet-session = { version = "3.0.0", features = ["historical"], default-features = false } #pallet-session-benchmarking = { version = "3.0.0", default-features = false, optional = true } -pallet-sudo = { version = "3.0.0", default-features = false } pallet-timestamp = { version = "3.0.0", default-features = false } pallet-transaction-payment = { version = "3.0.0", default-features = false } pallet-transaction-payment-rpc-runtime-api = { version = "3.0.0", default-features = false } @@ -73,14 +75,16 @@ std = [ "pallet-babe/std", "pallet-balances/std", "pallet-bounties/std", + "pallet-collective/std", "pallet-curveless-staking/std", "pallet-grandpa/std", "pallet-im-online/std", + "pallet-mandate/std", + "pallet-membership/std", "pallet-offences/std", "pallet-randomness-collective-flip/std", "pallet-scheduler/std", "pallet-session/std", - "pallet-sudo/std", "pallet-timestamp/std", "pallet-transaction-payment/std", "pallet-transaction-payment-rpc-runtime-api/std", @@ -115,6 +119,7 @@ runtime-benchmarks = [ "pallet-babe/runtime-benchmarks", "pallet-balances/runtime-benchmarks", "pallet-bounties/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", "pallet-curveless-staking/runtime-benchmarks", "pallet-grandpa/runtime-benchmarks", "pallet-im-online/runtime-benchmarks", diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index af104b36..375a6fce 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -30,6 +30,7 @@ use sp_version::RuntimeVersion; use ternoa_primitives::{AccountId, Balance, BlockNumber, Index, Signature}; pub mod constants; +mod migrations; mod pallets_core; mod pallets_economy; mod pallets_governance; @@ -76,13 +77,15 @@ construct_runtime!( Grandpa: pallet_grandpa::{Module, Call, Storage, Config, Event, ValidateUnsigned}, Historical: pallet_session_historical::{Module}, ImOnline: pallet_im_online::{Module, Call, Storage, Event, ValidateUnsigned, Config}, + Mandate: pallet_mandate::{Module, Call, Event}, Offences: pallet_offences::{Module, Call, Storage, Event}, RandomnessCollectiveFlip: pallet_randomness_collective_flip::{Module, Call, Storage}, Scheduler: pallet_scheduler::{Module, Call, Storage, Event}, Session: pallet_session::{Module, Call, Storage, Event, Config}, Staking: pallet_curveless_staking::{Module, Call, Config, Storage, Event, ValidateUnsigned}, - Sudo: pallet_sudo::{Module, Call, Config, Storage, Event}, System: frame_system::{Module, Call, Config, Storage, Event}, + TechnicalCommittee: pallet_collective::::{Module, Call, Storage, Origin, Event, Config}, + TechnicalMembership: pallet_membership::::{Module, Call, Storage, Event, Config}, Treasury: pallet_treasury::{Module, Call, Storage, Config, Event}, TransactionPayment: pallet_transaction_payment::{Module, Storage}, Utility: pallet_utility::{Module, Call, Storage, Event}, @@ -134,6 +137,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllModules, + migrations::SudoToTechComm, >; impl_runtime_apis! { @@ -357,6 +361,7 @@ impl_runtime_apis! { add_benchmark!(params, batches, pallet_bounties, Bounties); add_benchmark!(params, batches, pallet_grandpa, Grandpa); add_benchmark!(params, batches, pallet_im_online, ImOnline); + add_benchmark!(params, batches, pallet_collective, TechnicalCommittee); //add_benchmark!(params, batches, pallet_offences, OffencesBench::); add_benchmark!(params, batches, pallet_scheduler, Scheduler); //add_benchmark!(params, batches, pallet_session, SessionBench::); diff --git a/runtime/src/migrations.rs b/runtime/src/migrations.rs new file mode 100644 index 00000000..46e59fae --- /dev/null +++ b/runtime/src/migrations.rs @@ -0,0 +1,46 @@ +use crate::{ + pallets_core::RuntimeBlockWeights, pallets_governance::TechnicalCollectiveMembers, Runtime, +}; +use frame_support::{ + debug, + storage::unhashed, + traits::{InitializeMembers, OnRuntimeUpgrade}, + weights::Weight, + StorageHasher, Twox128, +}; +use sp_std::vec::Vec; +use ternoa_primitives::AccountId; + +pub struct SudoToTechComm; +impl OnRuntimeUpgrade for SudoToTechComm { + fn on_runtime_upgrade() -> Weight { + debug::RuntimeLogger::init(); + debug::print!("🕊️ Starting sudo migration..."); + + let mut sudo_key_storage_key = Vec::new(); + sudo_key_storage_key.extend_from_slice(&Twox128::hash(b"Sudo")); + sudo_key_storage_key.extend_from_slice(&Twox128::hash(b"Key")); + + let mut members_storage_key = Vec::new(); + members_storage_key.extend_from_slice(&Twox128::hash(b"Instance0Membership")); + members_storage_key.extend_from_slice(&Twox128::hash(b"Members")); + + if let Some(sudo_key) = unhashed::get::(&sudo_key_storage_key) { + // Configure the tech membership with the old sudo key + let mut members = Vec::new(); + members.push(sudo_key.clone()); + unhashed::put(&members_storage_key, &members); + + // Let the tech committee pallet know about the membership changes + <>::MembershipInitialized as InitializeMembers>::initialize_members(&members); + } + + // Clean sudo storage + unhashed::kill(&sudo_key_storage_key); + + debug::print!("🕊️ Sudo migration done"); + + // Keep things simple, take a full block to execute when the migration is deployed + RuntimeBlockWeights::get().max_block + } +} diff --git a/runtime/src/pallets_governance.rs b/runtime/src/pallets_governance.rs index aa419710..316cde46 100644 --- a/runtime/src/pallets_governance.rs +++ b/runtime/src/pallets_governance.rs @@ -1,6 +1,45 @@ -use crate::{Call, Event, Runtime}; +use crate::{constants::time::DAYS, Call, Event, Origin, Runtime, TechnicalCommittee}; +use frame_support::parameter_types; +use sp_core::u32_trait::{_1, _2}; +use ternoa_primitives::{AccountId, BlockNumber}; -impl pallet_sudo::Config for Runtime { +// Shared parameters with all collectives / committees +parameter_types! { + pub const MotionDuration: BlockNumber = 2 * DAYS; + pub const MaxProposals: u32 = 100; + pub const MaxMembers: u32 = 50; +} + +// --- Technical committee +pub type TechnicalCollective = pallet_collective::Instance0; +pub type TechnicalCollectiveMembers = pallet_membership::Instance0; +pub type MoreThanHalfOfTheTechnicalCollective = + pallet_collective::EnsureProportionMoreThan<_1, _2, AccountId, TechnicalCollective>; + +impl pallet_collective::Config for Runtime { + type Origin = Origin; + type Proposal = Call; + type Event = Event; + type MotionDuration = MotionDuration; + type MaxProposals = MaxProposals; + type WeightInfo = (); + type MaxMembers = MaxMembers; + type DefaultVote = pallet_collective::PrimeDefaultVote; +} + +impl pallet_membership::Config for Runtime { + type Event = Event; + type AddOrigin = MoreThanHalfOfTheTechnicalCollective; + type RemoveOrigin = MoreThanHalfOfTheTechnicalCollective; + type SwapOrigin = MoreThanHalfOfTheTechnicalCollective; + type ResetOrigin = MoreThanHalfOfTheTechnicalCollective; + type PrimeOrigin = MoreThanHalfOfTheTechnicalCollective; + type MembershipInitialized = TechnicalCommittee; + type MembershipChanged = TechnicalCommittee; +} + +impl pallet_mandate::Config for Runtime { type Event = Event; type Call = Call; + type ExternalOrigin = MoreThanHalfOfTheTechnicalCollective; } diff --git a/runtime/src/version.rs b/runtime/src/version.rs index b73252a5..a96b7208 100644 --- a/runtime/src/version.rs +++ b/runtime/src/version.rs @@ -16,7 +16,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 12, + spec_version: 13, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,