From 9fa684f2a436949ea78d5fb73246047e69f4f3a0 Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Fri, 9 Apr 2021 11:38:03 +0200 Subject: [PATCH] Backing/GetBacking: Abstraction over pluralistic origins for XCM. (#8579) * Backing/GetBacking: Abstraction over pluralistic origins for XCM. * Update frame/support/src/traits/misc.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/support/src/traits/misc.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> --- substrate/frame/collective/src/lib.rs | 11 ++++++++++- substrate/frame/support/src/traits.rs | 2 +- substrate/frame/support/src/traits/misc.rs | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/substrate/frame/collective/src/lib.rs b/substrate/frame/collective/src/lib.rs index d5768e4f5c..5c33bff300 100644 --- a/substrate/frame/collective/src/lib.rs +++ b/substrate/frame/collective/src/lib.rs @@ -55,7 +55,7 @@ use frame_support::{ PostDispatchInfo, }, ensure, - traits::{ChangeMembers, EnsureOrigin, Get, InitializeMembers}, + traits::{ChangeMembers, EnsureOrigin, Get, InitializeMembers, GetBacking, Backing}, weights::{DispatchClass, GetDispatchInfo, Weight, Pays}, }; use frame_system::{self as system, ensure_signed, ensure_root}; @@ -165,6 +165,15 @@ pub enum RawOrigin { _Phantom(sp_std::marker::PhantomData), } +impl GetBacking for RawOrigin { + fn get_backing(&self) -> Option { + match self { + RawOrigin::Members(n, d) => Some(Backing { approvals: *n, eligible: *d }), + _ => None, + } + } +} + /// Origin for the collective module. pub type Origin = RawOrigin<::AccountId, I>; diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index ba4869d4b8..6fd40aa9ba 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -46,7 +46,7 @@ pub use filter::{ mod misc; pub use misc::{ Len, Get, GetDefault, HandleLifetime, TryDrop, Time, UnixTime, IsType, IsSubType, ExecuteBlock, - SameOrOther, OnNewAccount, OnKilledAccount, OffchainWorker, + SameOrOther, OnNewAccount, OnKilledAccount, OffchainWorker, GetBacking, Backing, }; mod stored_map; diff --git a/substrate/frame/support/src/traits/misc.rs b/substrate/frame/support/src/traits/misc.rs index 2f21994290..d5cc68840d 100644 --- a/substrate/frame/support/src/traits/misc.rs +++ b/substrate/frame/support/src/traits/misc.rs @@ -269,3 +269,18 @@ pub trait OffchainWorker { fn offchain_worker(_n: BlockNumber) {} } +/// Some amount of backing from a group. The precise defintion of what it means to "back" something +/// is left flexible. +pub struct Backing { + /// The number of members of the group that back some motion. + pub approvals: u32, + /// The total count of group members. + pub eligible: u32, +} + +/// Retrieve the backing from an object's ref. +pub trait GetBacking { + /// Returns `Some` `Backing` if `self` represents a fractional/groupwise backing of some + /// implicit motion. `None` if it does not. + fn get_backing(&self) -> Option; +}