Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Use EnsureRoot directly
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Sep 6, 2024
1 parent dfa5f83 commit 8337f9b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
40 changes: 11 additions & 29 deletions bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -135,28 +134,6 @@ where
No,
}

pub struct EnsureOriginWithControlFlag<T, F, A>(core::marker::PhantomData<(T, F, A)>);
impl<T: Config, F: Get<bool>, A: Get<AccountIdOf<T>>>
EnsureOrigin<<T as frame_system::Config>::RuntimeOrigin> for EnsureOriginWithControlFlag<T, F, A>
{
type Success = AccountIdOf<T>;
fn try_origin(o: T::RuntimeOrigin) -> Result<Self::Success, T::RuntimeOrigin> {
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<T::RuntimeOrigin, ()> {
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;
Expand Down Expand Up @@ -206,7 +183,7 @@ pub mod pallet {
#[cfg(feature = "runtime-benchmarks")]
type Helper: BenchmarkHelper<Self::RuntimeOrigin>;

type RegisterTokenOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = AccountIdOf<Self>>;
type RegisterTokenOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}

#[pallet::event]
Expand Down Expand Up @@ -648,18 +625,23 @@ pub mod pallet {
location: Box<VersionedLocation>,
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::<T>::No, Pays::No)),
Some(&RawOrigin::Signed(ref who)) =>
Ok((PaysFee::<T>::Yes(who.clone()), Pays::Yes)),
_ => Err(BadOrigin),
}?;

let location: Location =
(*location).try_into().map_err(|_| Error::<T>::UnsupportedLocationVersion)?;

let pays_fee = PaysFee::<T>::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,
})
}
}
Expand Down
5 changes: 2 additions & 3 deletions bridges/snowbridge/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use frame_support::{
weights::IdentityFee,
PalletId,
};
use frame_system::EnsureRoot;
use sp_core::H256;
use xcm_executor::traits::ConvertLocation;

Expand All @@ -22,7 +23,6 @@ use xcm::prelude::*;

#[cfg(feature = "runtime-benchmarks")]
use crate::BenchmarkHelper;
use crate::EnsureOriginWithControlFlag;

type Block = frame_system::mocking::MockBlock<Test>;
type Balance = u128;
Expand Down Expand Up @@ -214,8 +214,7 @@ impl crate::Config for Test {
type EthereumNetwork = EthereumNetwork;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type RegisterTokenOrigin =
EnsureOriginWithControlFlag<Test, EnableRegisterToken, TreasuryAccount>;
type RegisterTokenOrigin = EnsureRoot<AccountId>;
}

// Build genesis storage according to the mock runtime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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! {
Expand Down Expand Up @@ -192,8 +191,7 @@ impl snowbridge_pallet_system::Config for Runtime {
type InboundDeliveryCost = EthereumInboundQueue;
type UniversalLocation = UniversalLocation;
type EthereumNetwork = EthereumNetwork;
type RegisterTokenOrigin =
EnsureOriginWithControlFlag<Runtime, EnableRegisterToken, TreasuryAccount>;
type RegisterTokenOrigin = EnsureRoot<Self::AccountId>;
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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! {
Expand Down Expand Up @@ -192,8 +191,7 @@ impl snowbridge_pallet_system::Config for Runtime {
type InboundDeliveryCost = EthereumInboundQueue;
type UniversalLocation = UniversalLocation;
type EthereumNetwork = EthereumNetwork;
type RegisterTokenOrigin =
EnsureOriginWithControlFlag<Runtime, EnableRegisterToken, TreasuryAccount>;
type RegisterTokenOrigin = EnsureRoot<Self::AccountId>;
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down

0 comments on commit 8337f9b

Please sign in to comment.