diff --git a/runtime/kreivo/src/collective/governance.rs b/runtime/kreivo/src/collective/governance.rs index 69cf8caa..944b04b3 100644 --- a/runtime/kreivo/src/collective/governance.rs +++ b/runtime/kreivo/src/collective/governance.rs @@ -14,9 +14,9 @@ impl pallet_referenda::Config for Runtime { type Scheduler = Scheduler; type Currency = Balances; type SubmitOrigin = EnsureSigned; - type CancelOrigin = EnsureRoot; - type KillOrigin = EnsureRoot; - type Slash = (); + type CancelOrigin = ReferendumCanceller; + type KillOrigin = ReferendumKiller; + type Slash = Treasury; type Votes = Votes; type Tally = TallyOf; type SubmissionDeposit = ConstU128<{ UNITS }>; diff --git a/runtime/kreivo/src/collective/mod.rs b/runtime/kreivo/src/collective/mod.rs index 0a3ca0bd..80a07ff5 100644 --- a/runtime/kreivo/src/collective/mod.rs +++ b/runtime/kreivo/src/collective/mod.rs @@ -5,6 +5,7 @@ use pallet_ranked_collective::Rank; use sp_core::ConstU16; use sp_runtime::traits::Convert; +pub use pallet_custom_origins::*; pub mod governance; pub mod tracks; diff --git a/runtime/kreivo/src/collective/tracks.rs b/runtime/kreivo/src/collective/tracks.rs index 2645aadc..512d4917 100644 --- a/runtime/kreivo/src/collective/tracks.rs +++ b/runtime/kreivo/src/collective/tracks.rs @@ -1,11 +1,15 @@ use super::*; use pallet_referenda::{impl_tracksinfo_get, Track}; -use sp_runtime::str_array as s; +use sp_runtime::{str_array as s, FixedI64}; use sp_std::borrow::Cow; pub type TrackId = u16; +const fn percent(x: i32) -> FixedI64 { + FixedI64::from_rational(x as u128, 100) +} + pub struct TracksInfo; impl pallet_referenda::TracksInfo for TracksInfo { type Id = TrackId; @@ -13,28 +17,92 @@ impl pallet_referenda::TracksInfo for TracksInfo { type TracksIter = pallet_referenda::StaticTracksIter; fn tracks() -> Self::TracksIter { - const DATA: [pallet_referenda::Track; 1] = [Track { - id: 0, - info: pallet_referenda::TrackInfo { - name: s("Root"), - max_deciding: 1, - decision_deposit: UNITS, - prepare_period: 15 * MINUTES, - decision_period: 4 * DAYS, - confirm_period: 15 * MINUTES, - min_enactment_period: 1, - min_approval: pallet_referenda::Curve::LinearDecreasing { - length: Perbill::from_percent(100), - floor: Perbill::from_percent(90), - ceil: Perbill::from_percent(100), + const DATA: [pallet_referenda::Track; 4] = [ + Track { + id: 0, + info: pallet_referenda::TrackInfo { + name: s("Root"), + max_deciding: 1, + decision_deposit: 10 * UNITS, + prepare_period: 15 * MINUTES, + decision_period: 4 * DAYS, + confirm_period: 15 * MINUTES, + min_enactment_period: 1, + min_approval: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(90), + ceil: Perbill::from_percent(100), + }, + min_support: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(0), + ceil: Perbill::from_percent(100), + }, + }, + }, + Track { + id: 1, + info: pallet_referenda::TrackInfo { + name: s("Referendum Canceller"), + max_deciding: 1, + decision_deposit: UNITS, + prepare_period: 15 * MINUTES, + decision_period: 4 * DAYS, + confirm_period: 15 * MINUTES, + min_enactment_period: 1, + min_approval: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(90), + ceil: Perbill::from_percent(100), + }, + min_support: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(0), + ceil: Perbill::from_percent(100), + }, }, - min_support: pallet_referenda::Curve::LinearDecreasing { - length: Perbill::from_percent(100), - floor: Perbill::from_percent(0), - ceil: Perbill::from_percent(100), + }, + Track { + id: 2, + info: pallet_referenda::TrackInfo { + name: s("Referendum Killer"), + max_deciding: 1, + decision_deposit: UNITS, + prepare_period: 15 * MINUTES, + decision_period: 4 * DAYS, + confirm_period: 15 * MINUTES, + min_enactment_period: 1, + min_approval: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(90), + ceil: Perbill::from_percent(100), + }, + min_support: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(0), + ceil: Perbill::from_percent(100), + }, + }, + }, + Track { + id: 3, + info: pallet_referenda::TrackInfo { + name: s("Create Memberships"), + max_deciding: 1, + decision_deposit: UNITS, + prepare_period: 15 * MINUTES, + decision_period: 4 * DAYS, + confirm_period: 15 * MINUTES, + min_enactment_period: 1, + min_approval: pallet_referenda::Curve::LinearDecreasing { + length: Perbill::from_percent(100), + floor: Perbill::from_percent(50), + ceil: Perbill::from_percent(100), + }, + min_support: pallet_referenda::Curve::make_linear(28, 28, percent(50), percent(100)), }, }, - }]; + ]; DATA.iter().map(Cow::Borrowed) } @@ -44,6 +112,12 @@ impl pallet_referenda::TracksInfo for TracksInfo { frame_system::RawOrigin::Root => Ok(0), _ => Err(()), } + } else if let Ok(custom_origin) = pallet_custom_origins::Origin::try_from(id.clone()) { + match custom_origin { + pallet_custom_origins::Origin::ReferendumCanceller => Ok(1), + pallet_custom_origins::Origin::ReferendumKiller => Ok(2), + pallet_custom_origins::Origin::CreateMemberships => Ok(3), + } } else { Err(()) } diff --git a/runtime/kreivo/src/communities/mod.rs b/runtime/kreivo/src/communities/mod.rs index 3b60cd21..5501229f 100644 --- a/runtime/kreivo/src/communities/mod.rs +++ b/runtime/kreivo/src/communities/mod.rs @@ -14,6 +14,7 @@ use self::{ governance::{CommunityReferendaInstance, CommunityTracksInstance}, memberships::CommunityMembershipsInstance, }; +use pallet_custom_origins::CreateMemberships; #[cfg(feature = "runtime-benchmarks")] use { @@ -83,7 +84,7 @@ impl pallet_communities_manager::Config for Runtime { type RankedCollective = KreivoCollective; type RegisterOrigin = EitherOf; - type CreateMembershipsOrigin = EnsureRoot; + type CreateMembershipsOrigin = EitherOf, CreateMemberships>; type MembershipId = MembershipId; type MembershipsManagerOwner = TreasuryAccount; type MembershipsManagerCollectionId = MembershipsCollectionId; diff --git a/runtime/kreivo/src/governance/mod.rs b/runtime/kreivo/src/governance/mod.rs new file mode 100644 index 00000000..e24f52cd --- /dev/null +++ b/runtime/kreivo/src/governance/mod.rs @@ -0,0 +1,3 @@ +pub mod origins; + +pub use origins::*; diff --git a/runtime/kreivo/src/governance/origins.rs b/runtime/kreivo/src/governance/origins.rs new file mode 100644 index 00000000..903068bd --- /dev/null +++ b/runtime/kreivo/src/governance/origins.rs @@ -0,0 +1,74 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot 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. + +// Polkadot 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 Polkadot. If not, see . + +//! Custom origins for governance interventions. + +pub use pallet_custom_origins::*; + +#[frame_support::pallet] +pub mod pallet_custom_origins { + use frame_support::pallet_prelude::*; + + #[pallet::config] + pub trait Config: frame_system::Config {} + + #[pallet::pallet] + pub struct Pallet(_); + + #[derive(PartialEq, Eq, Clone, MaxEncodedLen, Encode, Decode, TypeInfo, RuntimeDebug)] + #[pallet::origin] + pub enum Origin { + /// Origin for issuing new memberships. + CreateMemberships, + /// Origin able to cancel referenda. + ReferendumCanceller, + /// Origin able to kill referenda. + ReferendumKiller, + } + + macro_rules! decl_unit_ensures { + ( $name:ident: $success_type:ty = $success:expr ) => { + pub struct $name; + impl> + From> + EnsureOrigin for $name + { + type Success = $success_type; + fn try_origin(o: O) -> Result { + o.into().and_then(|o| match o { + Origin::$name => Ok($success), + r => Err(O::from(r)), + }) + } + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + Ok(O::from(Origin::$name)) + } + } + }; + ( $name:ident ) => { decl_unit_ensures! { $name : () = () } }; + ( $name:ident: $success_type:ty = $success:expr, $( $rest:tt )* ) => { + decl_unit_ensures! { $name: $success_type = $success } + decl_unit_ensures! { $( $rest )* } + }; + ( $name:ident, $( $rest:tt )* ) => { + decl_unit_ensures! { $name } + decl_unit_ensures! { $( $rest )* } + }; + () => {} + } + + decl_unit_ensures!(CreateMemberships, ReferendumCanceller, ReferendumKiller); +} diff --git a/runtime/kreivo/src/lib.rs b/runtime/kreivo/src/lib.rs index 8fd8c254..b6d219b7 100644 --- a/runtime/kreivo/src/lib.rs +++ b/runtime/kreivo/src/lib.rs @@ -11,12 +11,14 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); mod tests; pub mod constants; +pub mod governance; pub mod impls; mod weights; pub mod xcm_config; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use cumulus_primitives_core::{AggregateMessageOrigin, Concrete, ParaId}; +use governance::pallet_custom_origins; use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling}; use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery; use sp_api::impl_runtime_apis; @@ -169,6 +171,7 @@ construct_runtime!( ParachainSystem: cumulus_pallet_parachain_system = 1, Timestamp: pallet_timestamp = 2, ParachainInfo: parachain_info = 3, + Origins: pallet_custom_origins = 4, // Monetary stuff. Balances: pallet_balances = 10, @@ -244,6 +247,8 @@ parameter_types! { pub const SS58Prefix: u16 = 2; } +impl pallet_custom_origins::Config for Runtime {} + pub struct CommunityLookup; impl StaticLookup for CommunityLookup { type Source = Address;