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

Commit

Permalink
Merge branch 'snowbridge' into ron/digest-item-index
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Dec 20, 2023
2 parents d8b89e9 + 89a79f5 commit 453d66c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 11 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,14 +842,12 @@ 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
/// Polkadot uses 10 decimals, Kusama and Rococo 12 decimals.
pub const DefaultBridgeHubEthereumBaseFee: u128 = 2_750_872_500_000;
pub storage BridgeHubEthereumBaseFee: u128 = DefaultBridgeHubEthereumBaseFee::get();
pub const DefaultBridgeHubEthereumBaseFee: Balance = 2_750_872_500_000;
pub storage BridgeHubEthereumBaseFee: Balance = DefaultBridgeHubEthereumBaseFee::get();
pub SiblingBridgeHubWithEthereumInboundQueueInstance: MultiLocation = MultiLocation::new(
1,
X2(
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
use snowbridge_rococo_common::EthereumNetwork;
use snowbridge_router_primitives::outbound::EthereumBlobExporter;

/// Exports message to the Ethereum Gateway contract.
pub type SnowbridgeExporter = EthereumBlobExporter<
UniversalLocation,
EthereumNetwork,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ impl cumulus_pallet_xcm::Config for Runtime {
type XcmExecutor = XcmExecutor<XcmConfig>;
}

/// Creates an AgentId from a MultiLocation. An AgentId is a unique mapping to a Agent contract on
/// Ethereum which acts as the sovereign account for the MultiLocation.
pub type AgentIdOf = HashedDescription<H256, (DescribeHere, DescribeFamily<DescribeAllTerminal>)>;

/// A `HandleFee` implementation that simply deposits the fees for `ExportMessage` XCM instructions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub enum AggregateMessageOrigin {
///
/// This is used by the HRMP queue.
Sibling(ParaId),
/// The message came from a snowbridge channel.
///
/// This is used by Snowbridge inbound queue.
Snowbridge(ChannelId),
}

Expand Down Expand Up @@ -109,6 +112,9 @@ where
}
}

/// Narrow the scope of the `Inner` query from `AggregateMessageOrigin` to `ParaId`.
///
/// All non-`Sibling` variants will be ignored.
pub struct NarrowOriginToSibling<Inner>(PhantomData<Inner>);
impl<Inner: QueuePausedQuery<ParaId>> QueuePausedQuery<AggregateMessageOrigin>
for NarrowOriginToSibling<Inner>
Expand Down

0 comments on commit 453d66c

Please sign in to comment.