Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create custom origins and tracks for Kreivo Governance #401

Merged
merged 3 commits into from
May 11, 2024
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
6 changes: 3 additions & 3 deletions runtime/kreivo/src/collective/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ impl pallet_referenda::Config<KreivoReferendaInstance> for Runtime {
type Scheduler = Scheduler;
type Currency = Balances;
type SubmitOrigin = EnsureSigned<AccountId>;
type CancelOrigin = EnsureRoot<AccountId>;
type KillOrigin = EnsureRoot<AccountId>;
type Slash = ();
type CancelOrigin = ReferendumCanceller;
type KillOrigin = ReferendumKiller;
type Slash = Treasury;
type Votes = Votes;
type Tally = TallyOf<Runtime, KreivoCollectiveInstance>;
type SubmissionDeposit = ConstU128<{ UNITS }>;
Expand Down
1 change: 1 addition & 0 deletions runtime/kreivo/src/collective/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
114 changes: 94 additions & 20 deletions runtime/kreivo/src/collective/tracks.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,108 @@
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<Balance, BlockNumber> for TracksInfo {
type Id = TrackId;
type RuntimeOrigin = <RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin;
type TracksIter = pallet_referenda::StaticTracksIter<Self::Id, Balance, BlockNumber>;

fn tracks() -> Self::TracksIter {
const DATA: [pallet_referenda::Track<TrackId, Balance, BlockNumber>; 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<TrackId, Balance, BlockNumber>; 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)
}

Expand All @@ -44,6 +112,12 @@ impl pallet_referenda::TracksInfo<Balance, BlockNumber> 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(())
}
Expand Down
3 changes: 2 additions & 1 deletion runtime/kreivo/src/communities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use self::{
governance::{CommunityReferendaInstance, CommunityTracksInstance},
memberships::CommunityMembershipsInstance,
};
use pallet_custom_origins::CreateMemberships;

#[cfg(feature = "runtime-benchmarks")]
use {
Expand Down Expand Up @@ -83,7 +84,7 @@ impl pallet_communities_manager::Config for Runtime {
type RankedCollective = KreivoCollective;
type RegisterOrigin = EitherOf<RootCreatesCommunitiesForFree, AnyoneElsePays>;

type CreateMembershipsOrigin = EnsureRoot<AccountId>;
type CreateMembershipsOrigin = EitherOf<EnsureRoot<AccountId>, CreateMemberships>;
type MembershipId = MembershipId;
type MembershipsManagerOwner = TreasuryAccount;
type MembershipsManagerCollectionId = MembershipsCollectionId;
Expand Down
3 changes: 3 additions & 0 deletions runtime/kreivo/src/governance/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod origins;

pub use origins::*;
74 changes: 74 additions & 0 deletions runtime/kreivo/src/governance/origins.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

//! 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<T>(_);

#[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<O: Into<Result<Origin, O>> + From<Origin>>
EnsureOrigin<O> for $name
{
type Success = $success_type;
fn try_origin(o: O) -> Result<Self::Success, O> {
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<O, ()> {
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);
}
5 changes: 5 additions & 0 deletions runtime/kreivo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down