forked from polkadot-fellows/runtimes
-
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
Merged
bkontur
merged 9 commits into
bkontur:bko-ahm-governance-root
from
karolk91:kk-ahm-governance-root-origin-conv
Apr 10, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a9492db
[AHM Gov] AssetHub as Superuser on Polkadot relay - utils & config
karolk91 34963d7
[Governance tests] LocationAsSuperuser - shared utils & chains config…
karolk91 87920f0
[Governance tests] Formatting - cargo fmt
karolk91 1557b77
[AHM Gov] Replace custom ContainsAssetHub with Equals
karolk91 a4c3168
Fix Cargo.toml(s) formatting with taplo
karolk91 d712ba3
Use Equals instead of custom struct for tests
karolk91 e0f4476
Update pallets/common/src/lib.rs
bkontur bdbbd72
Rustdocs for LocationAsSuperuser
karolk91 e3ef87c
Update pallets/common/src/lib.rs
bkontur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // 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. | ||
| /// | ||
| /// A converter that allows a specific `Location` to act as a superuser (`RuntimeOrigin::root()`) | ||
| /// if it matches the predefined `SuperuserLocation` filter and `OriginKind::Superuser`. | ||
| pub struct LocationAsSuperuser<SuperuserLocation, RuntimeOrigin>( | ||
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(..) | ||
| )); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add here some docs, because it is a
pub structand all pub stuff should have rustdocsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in bdbbd72