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

Commit

Permalink
EnsureOriginWithControlFlag
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Sep 3, 2024
1 parent 90d60b4 commit 6cd2b3a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 38 deletions.
45 changes: 14 additions & 31 deletions bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,25 @@ where
No,
}

pub struct EnsureRootOrSigned<T>(core::marker::PhantomData<T>);
impl<T: Config> EnsureOrigin<<T as frame_system::Config>::RuntimeOrigin> for EnsureRootOrSigned<T> {
type Success = (bool, Option<AccountIdOf<T>>);
fn try_origin(o: T::RuntimeOrigin) -> Result<Self::Success, T::RuntimeOrigin> {
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<T::RuntimeOrigin, ()> {
Ok(RawOrigin::Root.into())
}
}

pub struct EnsureRootOnly<T>(core::marker::PhantomData<T>);
impl<T: Config> EnsureOrigin<<T as frame_system::Config>::RuntimeOrigin> for EnsureRootOnly<T> {
type Success = (bool, Option<AccountIdOf<T>>);
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((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<T::RuntimeOrigin, ()> {
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())
}
}

Expand Down Expand Up @@ -210,10 +199,7 @@ pub mod pallet {
#[cfg(feature = "runtime-benchmarks")]
type Helper: BenchmarkHelper<Self::RuntimeOrigin>;

type RegisterTokenOrigin: EnsureOrigin<
Self::RuntimeOrigin,
Success = (bool, Option<AccountIdOf<Self>>),
>;
type RegisterTokenOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = AccountIdOf<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -656,15 +642,12 @@ pub mod pallet {
location: Box<VersionedLocation>,
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::<T>::UnsupportedLocationVersion)?;

let mut pays_fee = PaysFee::<T>::No;
if !is_sudo && who.is_some() {
pays_fee = PaysFee::<T>::Yes(who.unwrap());
}
let pays_fee = PaysFee::<T>::Yes(who);

Self::do_register_token(&location, metadata, pays_fee)?;

Expand Down
7 changes: 4 additions & 3 deletions bridges/snowbridge/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>;
type Balance = u128;
Expand Down Expand Up @@ -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")]
Expand All @@ -211,7 +211,8 @@ impl crate::Config for Test {
type InboundDeliveryCost = InboundDeliveryCost;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type RegisterTokenOrigin = EnsureRootOrSigned<Test>;
type RegisterTokenOrigin =
EnsureOriginWithControlFlag<Test, EnableRegisterToken, TreasuryAccount>;
}

// Build genesis storage according to the mock runtime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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! {
Expand Down Expand Up @@ -189,7 +190,8 @@ impl snowbridge_pallet_system::Config for Runtime {
type Helper = ();
type DefaultPricingParameters = Parameters;
type InboundDeliveryCost = EthereumInboundQueue;
type RegisterTokenOrigin = EnsureRootOnly<Runtime>;
type RegisterTokenOrigin =
EnsureOriginWithControlFlag<Runtime, EnableRegisterToken, TreasuryAccount>;
}

#[cfg(feature = "runtime-benchmarks")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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! {
Expand Down Expand Up @@ -189,7 +190,8 @@ impl snowbridge_pallet_system::Config for Runtime {
type Helper = ();
type DefaultPricingParameters = Parameters;
type InboundDeliveryCost = EthereumInboundQueue;
type RegisterTokenOrigin = EnsureRootOnly<Runtime>;
type RegisterTokenOrigin =
EnsureOriginWithControlFlag<Runtime, EnableRegisterToken, TreasuryAccount>;
}

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

0 comments on commit 6cd2b3a

Please sign in to comment.