From 8337f9b869e96b11cf247142423ddee673ecb2d7 Mon Sep 17 00:00:00 2001 From: ron Date: Fri, 6 Sep 2024 21:50:38 +0800 Subject: [PATCH] Use EnsureRoot directly --- bridges/snowbridge/pallets/system/src/lib.rs | 40 +++++-------------- bridges/snowbridge/pallets/system/src/mock.rs | 5 +-- .../src/bridge_to_ethereum_config.rs | 6 +-- .../src/bridge_to_ethereum_config.rs | 6 +-- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/bridges/snowbridge/pallets/system/src/lib.rs b/bridges/snowbridge/pallets/system/src/lib.rs index d25590fadf8f..3dda000e03f3 100644 --- a/bridges/snowbridge/pallets/system/src/lib.rs +++ b/bridges/snowbridge/pallets/system/src/lib.rs @@ -84,7 +84,6 @@ use sp_std::prelude::*; use xcm::prelude::*; use xcm_executor::traits::ConvertLocation; -#[cfg(feature = "runtime-benchmarks")] use frame_support::traits::OriginTrait; pub use pallet::*; @@ -135,28 +134,6 @@ where No, } -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(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 { - 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()) - } -} - #[frame_support::pallet] pub mod pallet { use frame_support::dispatch::PostDispatchInfo; @@ -206,7 +183,7 @@ pub mod pallet { #[cfg(feature = "runtime-benchmarks")] type Helper: BenchmarkHelper; - type RegisterTokenOrigin: EnsureOrigin>; + type RegisterTokenOrigin: EnsureOrigin; } #[pallet::event] @@ -648,18 +625,23 @@ pub mod pallet { location: Box, metadata: AssetMetadata, ) -> DispatchResultWithPostInfo { - let who = T::RegisterTokenOrigin::ensure_origin(origin)?; + T::RegisterTokenOrigin::ensure_origin_or_root(origin.clone())?; + + let pays_fee = match origin.as_system_ref() { + Some(&RawOrigin::Root) => Ok((PaysFee::::No, Pays::No)), + Some(&RawOrigin::Signed(ref who)) => + Ok((PaysFee::::Yes(who.clone()), Pays::Yes)), + _ => Err(BadOrigin), + }?; let location: Location = (*location).try_into().map_err(|_| Error::::UnsupportedLocationVersion)?; - let pays_fee = PaysFee::::Yes(who); - - Self::do_register_token(&location, metadata, pays_fee)?; + Self::do_register_token(&location, metadata, pays_fee.0)?; Ok(PostDispatchInfo { actual_weight: Some(T::WeightInfo::register_token()), - pays_fee: Pays::No, + pays_fee: pays_fee.1, }) } } diff --git a/bridges/snowbridge/pallets/system/src/mock.rs b/bridges/snowbridge/pallets/system/src/mock.rs index 8d57f3220c62..fba437882192 100644 --- a/bridges/snowbridge/pallets/system/src/mock.rs +++ b/bridges/snowbridge/pallets/system/src/mock.rs @@ -7,6 +7,7 @@ use frame_support::{ weights::IdentityFee, PalletId, }; +use frame_system::EnsureRoot; use sp_core::H256; use xcm_executor::traits::ConvertLocation; @@ -22,7 +23,6 @@ use xcm::prelude::*; #[cfg(feature = "runtime-benchmarks")] use crate::BenchmarkHelper; -use crate::EnsureOriginWithControlFlag; type Block = frame_system::mocking::MockBlock; type Balance = u128; @@ -214,8 +214,7 @@ impl crate::Config for Test { type EthereumNetwork = EthereumNetwork; #[cfg(feature = "runtime-benchmarks")] type Helper = (); - type RegisterTokenOrigin = - EnsureOriginWithControlFlag; + type RegisterTokenOrigin = EnsureRoot; } // 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 0d7ebaf4ab03..8422bd83d587 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 @@ -36,8 +36,8 @@ use crate::xcm_config::RelayNetwork; #[cfg(feature = "runtime-benchmarks")] use benchmark_helpers::DoNothingRouter; use frame_support::{parameter_types, weights::ConstantMultiplier}; +use frame_system::EnsureRoot; use pallet_xcm::EnsureXcm; -use snowbridge_pallet_system::EnsureOriginWithControlFlag; use sp_runtime::{ traits::{ConstU32, ConstU8, Keccak256}, FixedU128, @@ -56,7 +56,6 @@ pub type SnowbridgeExporter = EthereumBlobExporter< // Ethereum Bridge parameter_types! { pub storage EthereumGatewayAddress: H160 = H160(hex_literal::hex!("EDa338E4dC46038493b885327842fD3E301CaB39")); - pub storage EnableRegisterToken: bool = false; } parameter_types! { @@ -192,8 +191,7 @@ impl snowbridge_pallet_system::Config for Runtime { type InboundDeliveryCost = EthereumInboundQueue; type UniversalLocation = UniversalLocation; type EthereumNetwork = EthereumNetwork; - type RegisterTokenOrigin = - EnsureOriginWithControlFlag; + type RegisterTokenOrigin = EnsureRoot; } #[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 b311394aaef2..63b5ba8febf4 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 @@ -37,8 +37,8 @@ use crate::xcm_config::RelayNetwork; #[cfg(feature = "runtime-benchmarks")] use benchmark_helpers::DoNothingRouter; use frame_support::{parameter_types, weights::ConstantMultiplier}; +use frame_system::EnsureRoot; use pallet_xcm::EnsureXcm; -use snowbridge_pallet_system::EnsureOriginWithControlFlag; use sp_runtime::{ traits::{ConstU32, ConstU8, Keccak256}, FixedU128, @@ -59,7 +59,6 @@ pub type SnowbridgeExporter = EthereumBlobExporter< // Ethereum Bridge parameter_types! { pub storage EthereumGatewayAddress: H160 = H160(hex_literal::hex!("EDa338E4dC46038493b885327842fD3E301CaB39")); - pub storage EnableRegisterToken: bool = false; } parameter_types! { @@ -192,8 +191,7 @@ impl snowbridge_pallet_system::Config for Runtime { type InboundDeliveryCost = EthereumInboundQueue; type UniversalLocation = UniversalLocation; type EthereumNetwork = EthereumNetwork; - type RegisterTokenOrigin = - EnsureOriginWithControlFlag; + type RegisterTokenOrigin = EnsureRoot; } #[cfg(feature = "runtime-benchmarks")]