From ae6fa34332b437c904d47a6461710d8b44cb756d Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Thu, 24 Nov 2022 18:05:07 +1300 Subject: [PATCH 1/3] add EnsureWithSuccess --- bin/node/runtime/src/lib.rs | 5 +++-- frame/system/src/lib.rs | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index cff33e0918981..c43ae63c14aa0 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -44,7 +44,7 @@ use frame_support::{ }; use frame_system::{ limits::{BlockLength, BlockWeights}, - EnsureRoot, EnsureRootWithSuccess, EnsureSigned, + EnsureRoot, EnsureRootWithSuccess, EnsureSigned, EnsureWithSuccess, }; pub use node_primitives::{AccountId, Signature}; use node_primitives::{AccountIndex, Balance, BlockNumber, Hash, Index, Moment}; @@ -1076,6 +1076,7 @@ parameter_types! { pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry"); pub const MaximumReasonLength: u32 = 300; pub const MaxApprovals: u32 = 100; + pub const MaxBalance: Balance = Balance::max_value(); } impl pallet_treasury::Config for Runtime { @@ -1100,7 +1101,7 @@ impl pallet_treasury::Config for Runtime { type SpendFunds = Bounties; type WeightInfo = pallet_treasury::weights::SubstrateWeight; type MaxApprovals = MaxApprovals; - type SpendOrigin = frame_support::traits::NeverEnsureOrigin; + type SpendOrigin = EnsureWithSuccess, AccountId, MaxBalance>; } parameter_types! { diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 477ebb97fbd95..4d9c37efe1f68 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -816,6 +816,27 @@ impl< } } +pub struct EnsureWithSuccess( + sp_std::marker::PhantomData<(Ensure, AccountId, Success)>, +); +impl< + O: Into, O>> + From>, + Ensure: EnsureOrigin, + AccountId, + Success: TypedGet, + > EnsureOrigin for EnsureWithSuccess +{ + type Success = Success::Type; + fn try_origin(o: O) -> Result { + Ensure::try_origin(o).map(|_| Success::get()) + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + Ensure::try_successful_origin() + } +} + pub struct EnsureSigned(sp_std::marker::PhantomData); impl, O>> + From>, AccountId: Decode> EnsureOrigin for EnsureSigned From 32b5128634d8b7031c8caee37609099d5b793ea6 Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Fri, 25 Nov 2022 00:10:32 +1300 Subject: [PATCH 2/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- frame/system/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 4d9c37efe1f68..5a0d9777f1ac3 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -819,6 +819,7 @@ impl< pub struct EnsureWithSuccess( sp_std::marker::PhantomData<(Ensure, AccountId, Success)>, ); + impl< O: Into, O>> + From>, Ensure: EnsureOrigin, @@ -827,6 +828,7 @@ impl< > EnsureOrigin for EnsureWithSuccess { type Success = Success::Type; + fn try_origin(o: O) -> Result { Ensure::try_origin(o).map(|_| Success::get()) } From 29c291374bc2d1b289e7ea5e211962d099bbc911 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Fri, 25 Nov 2022 00:15:36 +1300 Subject: [PATCH 3/3] add docs --- frame/system/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 5a0d9777f1ac3..7c4c4683958e3 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -775,6 +775,7 @@ impl From for LastRuntimeUpgradeInfo { } } +/// Ensure the origin is Root. pub struct EnsureRoot(sp_std::marker::PhantomData); impl, O>> + From>, AccountId> EnsureOrigin for EnsureRoot @@ -793,6 +794,7 @@ impl, O>> + From>, Acco } } +/// Ensure the origin is Root and return the provided `Success` value. pub struct EnsureRootWithSuccess( sp_std::marker::PhantomData<(AccountId, Success)>, ); @@ -816,6 +818,7 @@ impl< } } +/// Ensure the origin is provided `Ensure` origin and return the provided `Success` value. pub struct EnsureWithSuccess( sp_std::marker::PhantomData<(Ensure, AccountId, Success)>, ); @@ -839,6 +842,7 @@ impl< } } +/// Ensure the origin is any `Signed` origin. pub struct EnsureSigned(sp_std::marker::PhantomData); impl, O>> + From>, AccountId: Decode> EnsureOrigin for EnsureSigned @@ -859,6 +863,7 @@ impl, O>> + From>, Acco } } +/// Ensure the origin is `Signed` origin from the given `AccountId`. pub struct EnsureSignedBy(sp_std::marker::PhantomData<(Who, AccountId)>); impl< O: Into, O>> + From>, @@ -887,6 +892,7 @@ impl< } } +/// Ensure the origin is `None`. i.e. unsigned transaction. pub struct EnsureNone(sp_std::marker::PhantomData); impl, O>> + From>, AccountId> EnsureOrigin for EnsureNone @@ -905,6 +911,7 @@ impl, O>> + From>, Acco } } +/// Always fail. pub struct EnsureNever(sp_std::marker::PhantomData); impl EnsureOrigin for EnsureNever { type Success = T;