Skip to content

Commit

Permalink
gov. refactoring (#50)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ETeissonniere authored May 20, 2021
1 parent 7247057 commit 630f421
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
8 changes: 5 additions & 3 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Signature as Verify>::Signer;
Expand Down Expand Up @@ -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::<sr25519::Public>("Alice")),
pallet_collective_Instance0: Some(Default::default()),
pallet_membership_Instance0: Some(TechnicalMembershipConfig {
members: vec![root.unwrap_or(get_account_id_from_seed::<sr25519::Public>("Alice"))],
phantom: Default::default(),
}),

// Ternoa
Expand Down
9 changes: 7 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>, ValidateUnsigned, Config<T>},
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<T>},
Session: pallet_session::{Module, Call, Storage, Event, Config<T>},
Staking: pallet_curveless_staking::{Module, Call, Config<T>, Storage, Event<T>, ValidateUnsigned},
Sudo: pallet_sudo::{Module, Call, Config<T>, Storage, Event<T>},
System: frame_system::{Module, Call, Config, Storage, Event<T>},
TechnicalCommittee: pallet_collective::<Instance0>::{Module, Call, Storage, Origin<T>, Event<T>, Config<T>},
TechnicalMembership: pallet_membership::<Instance0>::{Module, Call, Storage, Event<T>, Config<T>},
Treasury: pallet_treasury::{Module, Call, Storage, Config, Event<T>},
TransactionPayment: pallet_transaction_payment::{Module, Storage},
Utility: pallet_utility::{Module, Call, Storage, Event},
Expand Down Expand Up @@ -134,6 +137,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllModules,
migrations::SudoToTechComm,
>;

impl_runtime_apis! {
Expand Down Expand Up @@ -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::<Runtime>);
add_benchmark!(params, batches, pallet_scheduler, Scheduler);
//add_benchmark!(params, batches, pallet_session, SessionBench::<Runtime>);
Expand Down
46 changes: 46 additions & 0 deletions runtime/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -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::<AccountId>(&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
<<Runtime as pallet_membership::Config<TechnicalCollectiveMembers>>::MembershipInitialized as InitializeMembers<AccountId>>::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
}
}
43 changes: 41 additions & 2 deletions runtime/src/pallets_governance.rs
Original file line number Diff line number Diff line change
@@ -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<TechnicalCollective> 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<TechnicalCollectiveMembers> 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;
}
2 changes: 1 addition & 1 deletion runtime/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 630f421

Please sign in to comment.