Skip to content

Commit

Permalink
Use FromNetwork (#80)
Browse files Browse the repository at this point in the history
* wip

* use from network

* add tests
  • Loading branch information
alistair-singh authored Dec 20, 2023
1 parent 6a3b064 commit 67e8718
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 9 deletions.
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]),
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() {
// 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));
}
}

0 comments on commit 67e8718

Please sign in to comment.