From 6cd2b3a42c1192c52fdfa0c55e8bac79338ba5a7 Mon Sep 17 00:00:00 2001 From: ron Date: Wed, 4 Sep 2024 00:19:43 +0800 Subject: [PATCH] EnsureOriginWithControlFlag --- bridges/snowbridge/pallets/system/src/lib.rs | 45 ++++++------------- bridges/snowbridge/pallets/system/src/mock.rs | 7 +-- .../src/bridge_to_ethereum_config.rs | 6 ++- .../src/bridge_to_ethereum_config.rs | 6 ++- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/bridges/snowbridge/pallets/system/src/lib.rs b/bridges/snowbridge/pallets/system/src/lib.rs index 4b550891c9e3e..e8efdf04175d8 100644 --- a/bridges/snowbridge/pallets/system/src/lib.rs +++ b/bridges/snowbridge/pallets/system/src/lib.rs @@ -135,36 +135,25 @@ where No, } -pub struct EnsureRootOrSigned(core::marker::PhantomData); -impl EnsureOrigin<::RuntimeOrigin> for EnsureRootOrSigned { - type Success = (bool, Option>); - fn try_origin(o: T::RuntimeOrigin) -> Result { - o.into().and_then(|o| match o { - RawOrigin::Root => Ok((true, None)), - RawOrigin::Signed(t) => Ok((false, Some(t))), - r => Err(T::RuntimeOrigin::from(r)), - }) - } - - #[cfg(feature = "runtime-benchmarks")] - fn try_successful_origin() -> Result { - Ok(RawOrigin::Root.into()) - } -} - -pub struct EnsureRootOnly(core::marker::PhantomData); -impl EnsureOrigin<::RuntimeOrigin> for EnsureRootOnly { - type Success = (bool, Option>); +pub struct EnsureOriginWithControlFlag(core::marker::PhantomData<(T, F, A)>); +impl, A: Get>> + EnsureOrigin<::RuntimeOrigin> for EnsureOriginWithControlFlag +{ + type Success = AccountIdOf; fn try_origin(o: T::RuntimeOrigin) -> Result { o.into().and_then(|o| match o { - RawOrigin::Root => Ok((true, None)), + RawOrigin::Root => Ok(A::get()), + RawOrigin::Signed(t) if F::get() => Ok(t), r => Err(T::RuntimeOrigin::from(r)), }) } #[cfg(feature = "runtime-benchmarks")] fn try_successful_origin() -> Result { - Ok(RawOrigin::Root.into()) + let zero_account_id = + T::AccountId::decode(&mut sp_runtime::traits::TrailingZeroInput::zeroes()) + .expect("infinite length input; no invalid inputs for type; qed"); + Ok(RawOrigin::Signed(zero_account_id).into()) } } @@ -210,10 +199,7 @@ pub mod pallet { #[cfg(feature = "runtime-benchmarks")] type Helper: BenchmarkHelper; - type RegisterTokenOrigin: EnsureOrigin< - Self::RuntimeOrigin, - Success = (bool, Option>), - >; + type RegisterTokenOrigin: EnsureOrigin>; } #[pallet::event] @@ -656,15 +642,12 @@ pub mod pallet { location: Box, metadata: AssetMetadata, ) -> DispatchResult { - let (is_sudo, who) = T::RegisterTokenOrigin::ensure_origin(origin)?; + let who = T::RegisterTokenOrigin::ensure_origin(origin)?; let location: Location = (*location).try_into().map_err(|_| Error::::UnsupportedLocationVersion)?; - let mut pays_fee = PaysFee::::No; - if !is_sudo && who.is_some() { - pays_fee = PaysFee::::Yes(who.unwrap()); - } + let pays_fee = PaysFee::::Yes(who); Self::do_register_token(&location, metadata, pays_fee)?; diff --git a/bridges/snowbridge/pallets/system/src/mock.rs b/bridges/snowbridge/pallets/system/src/mock.rs index a3946497b8cfb..696ccb017c644 100644 --- a/bridges/snowbridge/pallets/system/src/mock.rs +++ b/bridges/snowbridge/pallets/system/src/mock.rs @@ -22,7 +22,7 @@ use xcm::prelude::*; #[cfg(feature = "runtime-benchmarks")] use crate::BenchmarkHelper; -use crate::EnsureRootOrSigned; +use crate::EnsureOriginWithControlFlag; type Block = frame_system::mocking::MockBlock; type Balance = u128; @@ -189,7 +189,7 @@ parameter_types! { multiplier: FixedU128::from_rational(4, 3) }; pub const InboundDeliveryCost: u128 = 1_000_000_000; - + pub const EnableRegisterToken: bool = false; } #[cfg(feature = "runtime-benchmarks")] @@ -211,7 +211,8 @@ impl crate::Config for Test { type InboundDeliveryCost = InboundDeliveryCost; #[cfg(feature = "runtime-benchmarks")] type Helper = (); - type RegisterTokenOrigin = EnsureRootOrSigned; + type RegisterTokenOrigin = + EnsureOriginWithControlFlag; } // Build genesis storage according to the mock runtime. diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs index 49d436df07eee..0258a0afcb155 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_to_ethereum_config.rs @@ -37,7 +37,7 @@ use crate::xcm_config::RelayNetwork; use benchmark_helpers::DoNothingRouter; use frame_support::{parameter_types, weights::ConstantMultiplier}; use pallet_xcm::EnsureXcm; -use snowbridge_pallet_system::EnsureRootOnly; +use snowbridge_pallet_system::EnsureOriginWithControlFlag; use sp_runtime::{ traits::{ConstU32, ConstU8, Keccak256}, FixedU128, @@ -56,6 +56,7 @@ pub type SnowbridgeExporter = EthereumBlobExporter< // Ethereum Bridge parameter_types! { pub storage EthereumGatewayAddress: H160 = H160(hex_literal::hex!("EDa338E4dC46038493b885327842fD3E301CaB39")); + pub storage EnableRegisterToken: bool = false; } parameter_types! { @@ -189,7 +190,8 @@ impl snowbridge_pallet_system::Config for Runtime { type Helper = (); type DefaultPricingParameters = Parameters; type InboundDeliveryCost = EthereumInboundQueue; - type RegisterTokenOrigin = EnsureRootOnly; + type RegisterTokenOrigin = + EnsureOriginWithControlFlag; } #[cfg(feature = "runtime-benchmarks")] diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs index e11b55da619f3..4b2d715f2b9a2 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs @@ -38,7 +38,7 @@ use crate::xcm_config::RelayNetwork; use benchmark_helpers::DoNothingRouter; use frame_support::{parameter_types, weights::ConstantMultiplier}; use pallet_xcm::EnsureXcm; -use snowbridge_pallet_system::EnsureRootOnly; +use snowbridge_pallet_system::EnsureOriginWithControlFlag; use sp_runtime::{ traits::{ConstU32, ConstU8, Keccak256}, FixedU128, @@ -59,6 +59,7 @@ pub type SnowbridgeExporter = EthereumBlobExporter< // Ethereum Bridge parameter_types! { pub storage EthereumGatewayAddress: H160 = H160(hex_literal::hex!("EDa338E4dC46038493b885327842fD3E301CaB39")); + pub storage EnableRegisterToken: bool = false; } parameter_types! { @@ -189,7 +190,8 @@ impl snowbridge_pallet_system::Config for Runtime { type Helper = (); type DefaultPricingParameters = Parameters; type InboundDeliveryCost = EthereumInboundQueue; - type RegisterTokenOrigin = EnsureRootOnly; + type RegisterTokenOrigin = + EnsureOriginWithControlFlag; } #[cfg(feature = "runtime-benchmarks")]