Skip to content
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

Use FromNetwork #80

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ pub mod xcm_config;
use assets_common::{
foreign_creators::ForeignCreators,
local_and_foreign_assets::{LocalAndForeignAssets, MultiLocationConverter},
matching::FromSiblingParachain,
matching::{FromNetwork, FromSiblingParachain},
AssetIdForTrustBackedAssetsConvert, MultiLocationForAssetId,
};
use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
use cumulus_primitives_core::AggregateMessageOrigin;
use snowbridge_rococo_common::EthereumNetwork;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
Expand Down Expand Up @@ -95,8 +96,8 @@ use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use xcm::latest::prelude::*;

use crate::xcm_config::{
bridging::to_ethereum::EthereumLocation, ForeignCreatorsSovereignAccountOf,
LocalAndForeignAssetsMultiLocationMatcher, TrustBackedAssetsPalletLocation,
ForeignCreatorsSovereignAccountOf, LocalAndForeignAssetsMultiLocationMatcher,
TrustBackedAssetsPalletLocation,
};
use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

Expand Down Expand Up @@ -371,7 +372,7 @@ impl pallet_assets::Config<ForeignAssetsInstance> for Runtime {
type CreateOrigin = ForeignCreators<
(
FromSiblingParachain<parachain_info::Pallet<Runtime>>,
snowbridge_router_primitives::inbound::FromEthereumGlobalConsensus<EthereumLocation>,
FromNetwork<xcm_config::UniversalLocation, EthereumNetwork>,
),
ForeignCreatorsSovereignAccountOf,
AccountId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,8 +842,6 @@ pub mod bridging {
use super::*;

parameter_types! {
pub EthereumLocation: MultiLocation = MultiLocation::new(2, X1(GlobalConsensus(EthereumNetwork::get())));

/// User fee for ERC20 token transfer back to Ethereum.
/// (initially was calculated by test `OutboundQueue::calculate_fees` - ETH/ROC 1/400 and fee_per_gas 20 GWEI = 2200698000000 + *25%)
/// Needs to be more than fee calculated from DefaultFeeConfig FeeConfigRecord in snowbridge:parachain/pallets/outbound-queue/src/lib.rs
Expand All @@ -863,9 +861,7 @@ pub mod bridging {
pub BridgeTable: sp_std::vec::Vec<NetworkExportTableItem> = sp_std::vec![
NetworkExportTableItem::new(
EthereumNetwork::get(),
Some(sp_std::vec![
EthereumLocation::get().interior.split_global().expect("invalid configuration for Ethereum").1,
]),
Some(sp_std::vec![Junctions::Here]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, its basically the equivalent of

EthereumLocation::get().interior.split_global().expect("invalid configuration for Ethereum").1

Because EthereumLocation.get().interior is GlobalConsensus(Ethereum { chain_id: .. }), then we split_global() which strips the first global consensus part, leaving ( GlobalConsensus(Ethereum { chain_id: .. }) , Junctions::Here ). Then we take the second member of the tuple with .1.

SiblingBridgeHub::get(),
Some((
XcmBridgeHubRouterFeeAssetId::get(),
Expand Down
93 changes: 93 additions & 0 deletions cumulus/parachains/runtimes/assets/common/src/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl<SelfParaId: Get<ParaId>> ContainsPair<MultiLocation, MultiLocation>
}
}

/// Checks if `a` is from the expected global consensus network. Checks that `MultiLocation-a`
/// starts with `MultiLocation-b`, and that network is a foreign consensus system.
pub struct FromNetwork<UniversalLocation, ExpectedNetworkId>(
sp_std::marker::PhantomData<(UniversalLocation, ExpectedNetworkId)>,
);
Expand All @@ -72,6 +74,7 @@ impl<UniversalLocation: Get<InteriorMultiLocation>, ExpectedNetworkId: Get<Netwo

let universal_source = UniversalLocation::get();

// ensure that `a`` is remote and from the expected network
match ensure_is_remote(universal_source, a) {
Ok((network_id, _)) => network_id == ExpectedNetworkId::get(),
Err(e) => {
Expand All @@ -85,6 +88,7 @@ impl<UniversalLocation: Get<InteriorMultiLocation>, ExpectedNetworkId: Get<Netwo
}
}
}

/// Adapter verifies if it is allowed to receive `MultiAsset` from `MultiLocation`.
///
/// Note: `MultiLocation` has to be from a different global consensus.
Expand Down Expand Up @@ -122,3 +126,92 @@ impl<
Reserves::contains(asset, origin)
}
}

#[cfg(test)]
mod tests {
use super::*;
use frame_support::parameter_types;

parameter_types! {
pub UniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(Rococo), Parachain(1000));
pub ExpectedNetworkId: NetworkId = Wococo;
}

#[test]
fn from_network_contains_works() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

// asset and origin from foreign consensus works
let asset: MultiLocation = (
Parent,
Parent,
GlobalConsensus(Wococo),
Parachain(1000),
PalletInstance(1),
GeneralIndex(1),
)
.into();
let origin: MultiLocation =
(Parent, Parent, GlobalConsensus(Wococo), Parachain(1000)).into();
assert!(FromNetwork::<UniversalLocation, ExpectedNetworkId>::contains(&asset, &origin));

// asset and origin from local consensus fails
let asset: MultiLocation = (
Parent,
Parent,
GlobalConsensus(Rococo),
Parachain(1000),
PalletInstance(1),
GeneralIndex(1),
)
.into();
let origin: MultiLocation =
(Parent, Parent, GlobalConsensus(Rococo), Parachain(1000)).into();
assert!(!FromNetwork::<UniversalLocation, ExpectedNetworkId>::contains(&asset, &origin));

// asset and origin from here fails
let asset: MultiLocation = (PalletInstance(1), GeneralIndex(1)).into();
let origin: MultiLocation = Here.into();
assert!(!FromNetwork::<UniversalLocation, ExpectedNetworkId>::contains(&asset, &origin));

// asset from different consensus fails
let asset: MultiLocation = (
Parent,
Parent,
GlobalConsensus(Polkadot),
Parachain(1000),
PalletInstance(1),
GeneralIndex(1),
)
.into();
let origin: MultiLocation =
(Parent, Parent, GlobalConsensus(Wococo), Parachain(1000)).into();
assert!(!FromNetwork::<UniversalLocation, ExpectedNetworkId>::contains(&asset, &origin));

// origin from different consensus fails
let asset: MultiLocation = (
Parent,
Parent,
GlobalConsensus(Wococo),
Parachain(1000),
PalletInstance(1),
GeneralIndex(1),
)
.into();
let origin: MultiLocation =
(Parent, Parent, GlobalConsensus(Polkadot), Parachain(1000)).into();
assert!(!FromNetwork::<UniversalLocation, ExpectedNetworkId>::contains(&asset, &origin));

// asset and origin from unexpected consensus fails
let asset: MultiLocation = (
Parent,
Parent,
GlobalConsensus(Polkadot),
Parachain(1000),
PalletInstance(1),
GeneralIndex(1),
)
.into();
let origin: MultiLocation =
(Parent, Parent, GlobalConsensus(Polkadot), Parachain(1000)).into();
assert!(!FromNetwork::<UniversalLocation, ExpectedNetworkId>::contains(&asset, &origin));
}
}
Loading