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

Commit

Permalink
Revert "Revert "EnsureRootOrSigned""
Browse files Browse the repository at this point in the history
This reverts commit c699503.
  • Loading branch information
yrong committed Sep 3, 2024
1 parent 2bbee55 commit 90d60b4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
46 changes: 44 additions & 2 deletions bridges/snowbridge/pallets/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub mod weights;
pub use weights::*;

use frame_support::{
dispatch::RawOrigin,
pallet_prelude::*,
traits::{
fungible::{Inspect, Mutate},
Expand Down Expand Up @@ -134,6 +135,39 @@ 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>>);
fn try_origin(o: T::RuntimeOrigin) -> Result<Self::Success, T::RuntimeOrigin> {
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<T::RuntimeOrigin, ()> {
Ok(RawOrigin::Root.into())
}
}

#[frame_support::pallet]
pub mod pallet {
use snowbridge_core::StaticLookup;
Expand Down Expand Up @@ -175,6 +209,11 @@ pub mod pallet {

#[cfg(feature = "runtime-benchmarks")]
type Helper: BenchmarkHelper<Self::RuntimeOrigin>;

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

#[pallet::event]
Expand Down Expand Up @@ -617,12 +656,15 @@ pub mod pallet {
location: Box<VersionedLocation>,
metadata: AssetMetadata,
) -> DispatchResult {
ensure_root(origin)?;
let (is_sudo, who) = T::RegisterTokenOrigin::ensure_origin(origin)?;

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions bridges/snowbridge/pallets/system/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use xcm::prelude::*;

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

type Block = frame_system::mocking::MockBlock<Test>;
type Balance = u128;
Expand Down Expand Up @@ -210,6 +211,7 @@ impl crate::Config for Test {
type InboundDeliveryCost = InboundDeliveryCost;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type RegisterTokenOrigin = EnsureRootOrSigned<Test>;
}

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

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

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

0 comments on commit 90d60b4

Please sign in to comment.