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

Commit

Permalink
Message router for LCMP & XCMP (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiguantong authored Oct 28, 2022
1 parent 580f93d commit f628360
Show file tree
Hide file tree
Showing 23 changed files with 906 additions and 38 deletions.
59 changes: 40 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ frame-system = { default-features = false, git = "https://github.c
pallet-balances = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
pallet-transaction-payment = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
# polkadot
xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
xcm-executor = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
pallet-xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
polkadot-core-primitives = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
polkadot-parachain = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
polkadot-runtime-parachains = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
xcm-builder = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }
xcm-executor = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.27" }

[dev-dependencies]
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.27" }
Expand Down Expand Up @@ -67,7 +73,12 @@ std = [
"sp-runtime/std",
"sp-std/std",
# polkadot
"pallet-xcm/std",
"polkadot-core-primitives/std",
"polkadot-parachain/std",
"polkadot-runtime-parachains/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
]

Expand Down
1 change: 1 addition & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod helixbridge;
pub mod message_router;
pub mod remote_governance;
pub mod xcm_config;

Expand Down
78 changes: 78 additions & 0 deletions runtime/common/src/message_router/barriers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

//! Various implementations for `ShouldExecute`.

use frame_support::{ensure, log, traits::Contains, weights::Weight};
use sp_std::{marker::PhantomData, result::Result};
use xcm::latest::{Instruction::*, MultiLocation, WeightLimit::*, Xcm};
use xcm_executor::traits::ShouldExecute;

/// Allows execution from `origin` if it is contained in `T` (i.e. `T::Contains(origin)`) taking
/// payments into account.
///
/// Only allows for `TeleportAsset`, `WithdrawAsset`, `ClaimAsset` and `ReserveAssetDeposit` XCMs
/// because they are the only ones that place assets in the Holding Register to pay for execution.
/// Copy from https://github.com/paritytech/polkadot/blob/release-v0.9.26/xcm/xcm-builder/src/barriers.rs#L53
/// Allow `DescendOrigin` as the first instruction to specify the payment account
pub struct AllowDescendOriginPaidExecutionFrom<T>(PhantomData<T>);
impl<T: Contains<MultiLocation>> ShouldExecute for AllowDescendOriginPaidExecutionFrom<T> {
fn should_execute<Call>(
origin: &MultiLocation,
message: &mut Xcm<Call>,
max_weight: Weight,
_weight_credit: &mut Weight,
) -> Result<(), ()> {
log::trace!(
target: "xcm::barriers",
"AllowDescendOriginPaidExecutionFrom origin: {:?}, message: {:?}, max_weight: {:?}, weight_credit: {:?}",
origin, message, max_weight, _weight_credit,
);
ensure!(T::contains(origin), ());
let mut iter = message.0.iter_mut();
let i = iter.next().ok_or(())?;
// Modified: Only allows `DescendOrigin` as the first instruction
match i {
DescendOrigin(_) => (),
_ => return Err(()),
}
let i = iter.next().ok_or(())?;
match i {
ReceiveTeleportedAsset(..)
| WithdrawAsset(..)
| ReserveAssetDeposited(..)
| ClaimAsset { .. } => (),
_ => return Err(()),
}
let mut i = iter.next().ok_or(())?;
while let ClearOrigin = i {
i = iter.next().ok_or(())?;
}
match i {
BuyExecution { weight_limit: Limited(ref mut weight), .. } if *weight >= max_weight => {
*weight = max_weight;
Ok(())
},
BuyExecution { ref mut weight_limit, .. } if weight_limit == &Unlimited => {
*weight_limit = Limited(max_weight);
Ok(())
},
_ => Err(()),
}
}
}
51 changes: 51 additions & 0 deletions runtime/common/src/message_router/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of Darwinia.
//
// Copyright (C) 2018-2022 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia 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.
//
// Darwinia 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 Darwinia. If not, see <https://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

// --- paritytech ---
use frame_benchmarking::{account, benchmarks};
use frame_support::{assert_ok, traits::Get};
use frame_system::RawOrigin;
use sp_std::{boxed::Box, vec};
use xcm::{latest::prelude::*, VersionedXcm::V2};
// --- darwinia ---
use crate::message_router::{Call, Config, Pallet};
use dc_primitives::COIN;

benchmarks! {
set_target_xcm_exec_config {
let target_location = T::MoonbeamLocation::get();
let local_asset_units_per_second: u128 = 14719736222326895902025_u128;
}:_(RawOrigin::Root, target_location.clone(), local_asset_units_per_second)
verify {
assert_eq!(Pallet::<T>::target_xcm_exec_config(target_location), Some(local_asset_units_per_second));
}

forward_to_moonbeam {
let target_location = T::MoonbeamLocation::get();
let local_asset_units_per_second: u128 = 14719736222326895902025_u128;
assert_ok!(Pallet::<T>::set_target_xcm_exec_config(RawOrigin::Root.into(), target_location.clone(), local_asset_units_per_second));
let xcm = Xcm(vec![
DescendOrigin(X1(AccountKey20 {
network: Any,
key: [0u8;20].into()
})),
]);
}:_(RawOrigin::Root, Box::new(V2(xcm)))
}
Loading

0 comments on commit f628360

Please sign in to comment.