-
Notifications
You must be signed in to change notification settings - Fork 0
[AHM Gov] AssetHub as Superuser on Polkadot relay - utils & config #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
a9492db
34963d7
87920f0
1557b77
a4c3168
d712ba3
e0f4476
bdbbd72
e3ef87c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| description = "Shared utilities" | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| name = "pallets-common" | ||
| repository.workspace = true | ||
| version.workspace = true | ||
|
|
||
| [dependencies] | ||
| codec = { features = ["derive", "max-encoded-len"], workspace = true } | ||
| scale-info = { features = ["derive"], workspace = true } | ||
| xcm = { workspace = true } | ||
| xcm-executor = { workspace = true } | ||
| frame-support = { workspace = true } | ||
| frame-system = { workspace = true } | ||
| log = { workspace = true } | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "codec/std", | ||
| "frame-support/std", | ||
| "frame-system/std", | ||
| "log/std", | ||
| "scale-info/std", | ||
| "xcm-executor/std", | ||
| "xcm/std", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Code shared between all runtimes | ||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use core::marker::PhantomData; | ||
| use frame_support::traits::{Contains, OriginTrait}; | ||
| use xcm::latest::{Location, OriginKind}; | ||
| use xcm_executor::traits::ConvertOrigin; | ||
|
|
||
| /// TODO: `LocationAsSuperuser` is temporary placed here, the final solution will be imported from | ||
| /// `xcm_builder` (depends on backports) instead. | ||
| /// | ||
| /// Implements `ConvertOrigin` to convert a `Location` into `RuntimeOrigin::root()`. | ||
| /// | ||
| /// This is typically used when configuring `pallet-xcm` to allow a remote `Location` | ||
| /// to act as the `Root` origin on the local chain. | ||
| pub struct LocationAsSuperuser<SuperuserLocation, RuntimeOrigin>( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add here some docs, because it is a
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in bdbbd72
bkontur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| PhantomData<(SuperuserLocation, RuntimeOrigin)>, | ||
| ); | ||
| impl<SuperuserLocation: Contains<Location>, RuntimeOrigin: OriginTrait> ConvertOrigin<RuntimeOrigin> | ||
| for LocationAsSuperuser<SuperuserLocation, RuntimeOrigin> | ||
| { | ||
| fn convert_origin( | ||
| origin: impl Into<Location>, | ||
| kind: OriginKind, | ||
| ) -> Result<RuntimeOrigin, Location> { | ||
| let origin = origin.into(); | ||
| log::trace!(target: "xcm::origin_conversion", "LocationAsSuperuser origin: {:?}, kind: {:?}", origin, kind); | ||
| match (kind, &origin) { | ||
| (OriginKind::Superuser, loc) if SuperuserLocation::contains(loc) => | ||
| Ok(RuntimeOrigin::root()), | ||
| _ => Err(origin), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use frame_support::{construct_runtime, derive_impl, parameter_types, traits::Equals}; | ||
| use xcm::latest::{Junction::*, Junctions::*, OriginKind}; | ||
|
|
||
| type Block = frame_system::mocking::MockBlock<Test>; | ||
|
|
||
| construct_runtime!( | ||
| pub enum Test | ||
| { | ||
| System: frame_system, | ||
| } | ||
| ); | ||
|
|
||
| #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] | ||
| impl frame_system::Config for Test { | ||
| type Block = Block; | ||
| } | ||
|
|
||
| parameter_types! { | ||
| pub SuperuserLocation: Location = Location::new(0, Parachain(1)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn superuser_location_works() { | ||
| let test_conversion = |loc, kind| { | ||
| LocationAsSuperuser::<Equals<SuperuserLocation>, RuntimeOrigin>::convert_origin( | ||
| loc, kind, | ||
| ) | ||
| }; | ||
|
|
||
| // Location that was set as SuperUserLocation should result in success conversion to Root | ||
| assert!(matches!(test_conversion(SuperuserLocation::get(), OriginKind::Superuser), Ok(..))); | ||
| // Same Location as SuperUserLocation::get() | ||
| assert!(matches!( | ||
| test_conversion(Location::new(0, Parachain(1)), OriginKind::Superuser), | ||
| Ok(..) | ||
| )); | ||
|
|
||
| // Same Location but different origin kind | ||
| assert!(matches!(test_conversion(SuperuserLocation::get(), OriginKind::Native), Err(..))); | ||
| assert!(matches!( | ||
| test_conversion(SuperuserLocation::get(), OriginKind::SovereignAccount), | ||
| Err(..) | ||
| )); | ||
| assert!(matches!(test_conversion(SuperuserLocation::get(), OriginKind::Xcm), Err(..))); | ||
|
|
||
| // No other location should result in successful conversion to Root | ||
| // thus expecting Err in all cases below | ||
| // | ||
| // Non-matching parachain number | ||
| assert!(matches!( | ||
| test_conversion(Location::new(0, Parachain(2)), OriginKind::Superuser), | ||
| Err(..) | ||
| )); | ||
| // Non-matching parents count | ||
| assert!(matches!( | ||
| test_conversion(Location::new(1, Parachain(1)), OriginKind::Superuser), | ||
| Err(..) | ||
| )); | ||
| // Child location of SuperUserLocation | ||
| assert!(matches!( | ||
| test_conversion( | ||
| Location::new(1, [Parachain(1), GeneralIndex(0)]), | ||
| OriginKind::Superuser | ||
| ), | ||
| Err(..) | ||
| )); | ||
| // Here | ||
| assert!(matches!(test_conversion(Location::new(0, Here), OriginKind::Superuser), Err(..))); | ||
| // Parent | ||
| assert!(matches!(test_conversion(Location::new(1, Here), OriginKind::Superuser), Err(..))); | ||
| // Some random account | ||
| assert!(matches!( | ||
| test_conversion( | ||
| Location::new(0, AccountId32 { network: None, id: [0u8; 32] }), | ||
| OriginKind::Superuser | ||
| ), | ||
| Err(..) | ||
| )); | ||
| // Child location of SuperUserLocation | ||
| assert!(matches!( | ||
| test_conversion( | ||
| Location::new(0, [Parachain(1), AccountId32 { network: None, id: [1u8; 32] }]), | ||
| OriginKind::Superuser | ||
| ), | ||
| Err(..) | ||
| )); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.