From 90d60b451382db582020cc7eaa95850bc55ca915 Mon Sep 17 00:00:00 2001 From: ron Date: Tue, 3 Sep 2024 23:20:04 +0800 Subject: [PATCH] Revert "Revert "EnsureRootOrSigned"" This reverts commit c699503046edbce76211c8e1ae93eb09456f4481. --- bridges/snowbridge/pallets/system/src/lib.rs | 46 ++++++++++++++++++- bridges/snowbridge/pallets/system/src/mock.rs | 2 + .../src/bridge_to_ethereum_config.rs | 2 + .../src/bridge_to_ethereum_config.rs | 2 + 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/bridges/snowbridge/pallets/system/src/lib.rs b/bridges/snowbridge/pallets/system/src/lib.rs index 051e15c387ad5..4b550891c9e3e 100644 --- a/bridges/snowbridge/pallets/system/src/lib.rs +++ b/bridges/snowbridge/pallets/system/src/lib.rs @@ -58,6 +58,7 @@ pub mod weights; pub use weights::*; use frame_support::{ + dispatch::RawOrigin, pallet_prelude::*, traits::{ fungible::{Inspect, Mutate}, @@ -134,6 +135,39 @@ 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>); + fn try_origin(o: T::RuntimeOrigin) -> Result { + o.into().and_then(|o| match o { + RawOrigin::Root => Ok((true, None)), + r => Err(T::RuntimeOrigin::from(r)), + }) + } + + #[cfg(feature = "runtime-benchmarks")] + fn try_successful_origin() -> Result { + Ok(RawOrigin::Root.into()) + } +} + #[frame_support::pallet] pub mod pallet { use snowbridge_core::StaticLookup; @@ -175,6 +209,11 @@ pub mod pallet { #[cfg(feature = "runtime-benchmarks")] type Helper: BenchmarkHelper; + + type RegisterTokenOrigin: EnsureOrigin< + Self::RuntimeOrigin, + Success = (bool, Option>), + >; } #[pallet::event] @@ -617,12 +656,15 @@ pub mod pallet { location: Box, metadata: AssetMetadata, ) -> DispatchResult { - ensure_root(origin)?; + let (is_sudo, who) = T::RegisterTokenOrigin::ensure_origin(origin)?; let location: Location = (*location).try_into().map_err(|_| Error::::UnsupportedLocationVersion)?; - let pays_fee = PaysFee::::No; + let mut pays_fee = PaysFee::::No; + if !is_sudo && who.is_some() { + pays_fee = PaysFee::::Yes(who.unwrap()); + } 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 98bd3da9ab27c..a3946497b8cfb 100644 --- a/bridges/snowbridge/pallets/system/src/mock.rs +++ b/bridges/snowbridge/pallets/system/src/mock.rs @@ -22,6 +22,7 @@ use xcm::prelude::*; #[cfg(feature = "runtime-benchmarks")] use crate::BenchmarkHelper; +use crate::EnsureRootOrSigned; type Block = frame_system::mocking::MockBlock; type Balance = u128; @@ -210,6 +211,7 @@ impl crate::Config for Test { type InboundDeliveryCost = InboundDeliveryCost; #[cfg(feature = "runtime-benchmarks")] type Helper = (); + type RegisterTokenOrigin = EnsureRootOrSigned; } // 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 26857d262c202..49d436df07eee 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,6 +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 sp_runtime::{ traits::{ConstU32, ConstU8, Keccak256}, FixedU128, @@ -188,6 +189,7 @@ impl snowbridge_pallet_system::Config for Runtime { type Helper = (); type DefaultPricingParameters = Parameters; type InboundDeliveryCost = EthereumInboundQueue; + type RegisterTokenOrigin = EnsureRootOnly; } #[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 621d39af5af01..e11b55da619f3 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,6 +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 sp_runtime::{ traits::{ConstU32, ConstU8, Keccak256}, FixedU128, @@ -188,6 +189,7 @@ impl snowbridge_pallet_system::Config for Runtime { type Helper = (); type DefaultPricingParameters = Parameters; type InboundDeliveryCost = EthereumInboundQueue; + type RegisterTokenOrigin = EnsureRootOnly; } #[cfg(feature = "runtime-benchmarks")]