Xcm in Rialto<>Millau bridge (#1379)

* add XCM pallet to Millau runtime

* some progress

* messages are delivered (no fee)

* temp

* flush

* flush

* some progress

* progress

* Trap(42) is dispatched successfully

* fix spelling

* no more manual sending

* parametrize weight credit for tests

* actually charge fees

* enable send-using-messages-pallet to keep our test deployments alive + send valid XCM sometimes

* fix benchmarks build

* fix tests
This commit is contained in:
Svyatoslav Nikolsky
2022-05-16 11:46:55 +03:00
committed by Bastian Köcher
parent f54bd6c779
commit 748c265c47
19 changed files with 1059 additions and 431 deletions
@@ -217,6 +217,7 @@ fn testnet_genesis(
owner: Some(get_account_id_from_seed::<sr25519::Public>("RialtoMessagesOwner")),
..Default::default()
},
xcm_pallet: Default::default(),
}
}
+18
View File
@@ -11,6 +11,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
hex-literal = "0.3"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
libsecp256k1 = { version = "0.7", optional = true, default-features = false, features = ["hmac"] }
log = { version = "0.4.14", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
@@ -53,6 +54,7 @@ sp-consensus-aura = { git = "https://github.com/paritytech/substrate", branch =
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-finality-grandpa = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-inherents = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-mmr-primitives = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-offchain = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
@@ -62,8 +64,16 @@ sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
# Polkadot Dependencies
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "gav-xcm-v3", default-features = false }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "gav-xcm-v3", default-features = false }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "gav-xcm-v3", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "gav-xcm-v3", default-features = false }
[dev-dependencies]
bridge-runtime-common = { path = "../../runtime-common", features = ["integrity-test"] }
env_logger = "0.8"
static_assertions = "1.1"
[build-dependencies]
@@ -85,6 +95,7 @@ std = [
"frame-support/std",
"frame-system-rpc-runtime-api/std",
"frame-system/std",
"log/std",
"pallet-aura/std",
"pallet-balances/std",
"pallet-beefy/std",
@@ -100,6 +111,7 @@ std = [
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"pallet-xcm/std",
"scale-info/std",
"serde",
"sp-api/std",
@@ -108,6 +120,7 @@ std = [
"sp-core/std",
"sp-finality-grandpa/std",
"sp-inherents/std",
"sp-io/std",
"sp-offchain/std",
"sp-runtime/std",
"sp-session/std",
@@ -115,6 +128,9 @@ std = [
"sp-transaction-pool/std",
"sp-trie/std",
"sp-version/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
]
runtime-benchmarks = [
"bridge-runtime-common/runtime-benchmarks",
@@ -123,5 +139,7 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"libsecp256k1",
"pallet-bridge-messages/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
+6 -7
View File
@@ -29,6 +29,7 @@
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
pub mod rialto_messages;
pub mod xcm_config;
use crate::rialto_messages::{ToRialtoMessagePayload, WithRialtoMessageBridge};
@@ -451,13 +452,7 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
type TargetHeaderChain = crate::rialto_messages::Rialto;
type LaneMessageVerifier = crate::rialto_messages::ToRialtoMessageVerifier;
type MessageDeliveryAndDispatchPayment =
pallet_bridge_messages::instant_payments::InstantCurrencyPayments<
Runtime,
WithRialtoMessagesInstance,
pallet_balances::Pallet<Runtime>,
GetDeliveryConfirmationTransactionFee,
>;
type MessageDeliveryAndDispatchPayment = ();
type OnMessageAccepted = ();
type OnDeliveryConfirmed = ();
@@ -499,6 +494,9 @@ construct_runtime!(
// Westend bridge modules.
BridgeWestendGrandpa: pallet_bridge_grandpa::<Instance1>::{Pallet, Call, Config<T>, Storage},
// Pallet for sending XCM.
XcmPallet: pallet_xcm::{Pallet, Call, Storage, Event<T>, Origin, Config} = 99,
}
);
@@ -750,6 +748,7 @@ impl_runtime_apis! {
Runtime,
WithRialtoMessagesInstance,
WithRialtoMessageBridge,
xcm_config::OutboundXcmWeigher,
>(lane, begin, end)
}
}
@@ -16,7 +16,7 @@
//! Everything required to serve Millau <-> Rialto messages.
use crate::Runtime;
use crate::{Call, OriginCaller, Runtime};
use bp_messages::{
source_chain::{SenderOrigin, TargetHeaderChain},
@@ -40,6 +40,10 @@ pub const INITIAL_RIALTO_TO_MILLAU_CONVERSION_RATE: FixedU128 =
FixedU128::from_inner(FixedU128::DIV);
/// Initial value of `RialtoFeeMultiplier` parameter.
pub const INITIAL_RIALTO_FEE_MULTIPLIER: FixedU128 = FixedU128::from_inner(FixedU128::DIV);
/// Weight of 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction). It is used just for simplest manual
/// tests, confirming that we don't break encoding somewhere between.
pub const BASE_XCM_WEIGHT_TWICE: Weight = 2 * crate::xcm_config::BASE_XCM_WEIGHT;
parameter_types! {
/// Rialto to Millau conversion rate. Initially we treat both tokens as equal.
@@ -56,7 +60,7 @@ pub type ToRialtoMessageVerifier =
messages::source::FromThisChainMessageVerifier<WithRialtoMessageBridge>;
/// Message payload for Rialto -> Millau messages.
pub type FromRialtoMessagePayload = messages::target::FromBridgedChainMessagePayload;
pub type FromRialtoMessagePayload = messages::target::FromBridgedChainMessagePayload<Call>;
/// Messages proof for Rialto -> Millau messages.
pub type FromRialtoMessagesProof = messages::target::FromBridgedChainMessagesProof<bp_rialto::Hash>;
@@ -68,9 +72,11 @@ pub type ToRialtoMessagesDeliveryProof =
/// Call-dispatch based message dispatch for Rialto -> Millau messages.
pub type FromRialtoMessageDispatch = messages::target::FromBridgedChainMessageDispatch<
WithRialtoMessageBridge,
crate::Runtime,
pallet_balances::Pallet<Runtime>,
(),
xcm_executor::XcmExecutor<crate::xcm_config::XcmConfig>,
crate::xcm_config::XcmWeigher,
// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
// (it is prepended with `UniversalOrigin` instruction)
frame_support::traits::ConstU64<BASE_XCM_WEIGHT_TWICE>,
>;
/// Millau <-> Rialto message bridge.
@@ -115,7 +121,23 @@ impl messages::ThisChainWithMessages for Millau {
type Call = crate::Call;
fn is_message_accepted(send_origin: &Self::Origin, lane: &LaneId) -> bool {
(*lane == [0, 0, 0, 0] || *lane == [0, 0, 0, 1]) && send_origin.linked_account().is_some()
let here_location =
xcm::v3::MultiLocation::from(crate::xcm_config::UniversalLocation::get());
match send_origin.caller {
OriginCaller::XcmPallet(pallet_xcm::Origin::Xcm(ref location))
if *location == here_location =>
{
log::trace!(target: "runtime::bridge", "Verifying message sent using XCM pallet to Rialto");
},
_ => {
// keep in mind that in this case all messages are free (in term of fees)
// => it's just to keep testing bridge on our test deployments until we'll have a
// better option
log::trace!(target: "runtime::bridge", "Verifying message sent using messages pallet to Rialto");
},
}
*lane == [0, 0, 0, 0] || *lane == [0, 0, 0, 1]
}
fn maximal_pending_messages_at_outbound_lane() -> MessageNonce {
@@ -262,14 +284,8 @@ impl SourceHeaderChain<bp_rialto::Balance> for Rialto {
impl SenderOrigin<crate::AccountId> for crate::Origin {
fn linked_account(&self) -> Option<crate::AccountId> {
match self.caller {
crate::OriginCaller::system(frame_system::RawOrigin::Signed(ref submitter)) =>
Some(submitter.clone()),
crate::OriginCaller::system(frame_system::RawOrigin::Root) |
crate::OriginCaller::system(frame_system::RawOrigin::None) =>
crate::RootAccountForPayments::get(),
_ => None,
}
// XCM deals wit fees in our deployments
None
}
}
@@ -0,0 +1,317 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common 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.
// Parity Bridges Common 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 Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! XCM configurations for the Millau runtime.
use super::{
rialto_messages::WithRialtoMessageBridge, AccountId, AllPalletsWithSystem, Balances,
BridgeRialtoMessages, Call, Event, Origin, Runtime, XcmPallet,
};
use bp_messages::source_chain::MessagesBridge;
use bp_millau::{Balance, WeightToFee};
use bridge_runtime_common::messages::{
source::{estimate_message_dispatch_and_delivery_fee, FromThisChainMessagePayload},
MessageBridge,
};
use codec::Encode;
use frame_support::{
parameter_types,
traits::{Everything, Nothing},
weights::Weight,
};
use sp_std::marker::PhantomData;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowTopLevelPaidExecutionFrom,
CurrencyAdapter as XcmCurrencyAdapter, IsConcrete, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
};
parameter_types! {
/// The location of the `MLAU` token, from the context of this chain. Since this token is native to this
/// chain, we make it synonymous with it and thus it is the `Here` location, which means "equivalent to
/// the context".
pub const TokenLocation: MultiLocation = Here.into_location();
/// The Millau network ID, associated with Kusama.
pub const ThisNetwork: NetworkId = Kusama;
/// The Rialto network ID, associated with Polkadot.
pub const RialtoNetwork: NetworkId = Polkadot;
/// Our XCM location ancestry - i.e. our location within the Consensus Universe.
///
/// Since Kusama is a top-level relay-chain with its own consensus, it's just our network ID.
pub UniversalLocation: InteriorMultiLocation = ThisNetwork::get().into();
/// The check account, which holds any native assets that have been teleported out and not back in (yet).
pub CheckAccount: AccountId = XcmPallet::check_account();
}
/// The canonical means of converting a `MultiLocation` into an `AccountId`, used when we want to
/// determine the sovereign account controlled by a location.
pub type SovereignAccountOf = (
// We can directly alias an `AccountId32` into a local account.
AccountId32Aliases<ThisNetwork, AccountId>,
);
/// Our asset transactor. This is what allows us to interest with the runtime facilities from the
/// point of view of XCM-only concepts like `MultiLocation` and `MultiAsset`.
///
/// Ours is only aware of the Balances pallet, which is mapped to `TokenLocation`.
pub type LocalAssetTransactor = XcmCurrencyAdapter<
// Use this currency:
Balances,
// Use this currency when it is a fungible asset matching the given location or name:
IsConcrete<TokenLocation>,
// We can convert the MultiLocations with our converter above:
SovereignAccountOf,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
AccountId,
// We track our teleports in/out to keep total issuance correct.
CheckAccount,
>;
/// The means that we convert the XCM message origin location into a local dispatch origin.
type LocalOriginConverter = (
// A `Signed` origin of the sovereign account that the original location controls.
SovereignSignedViaLocation<SovereignAccountOf, Origin>,
// The AccountId32 location type can be expressed natively as a `Signed` origin.
SignedAccountId32AsNative<ThisNetwork, Origin>,
);
/// The amount of weight an XCM operation takes. This is a safe overestimate.
pub const BASE_XCM_WEIGHT: Weight = 1_000_000_000;
parameter_types! {
/// The amount of weight an XCM operation takes. This is a safe overestimate.
pub const BaseXcmWeight: Weight = BASE_XCM_WEIGHT;
/// Maximum number of instructions in a single XCM fragment. A sanity check against weight
/// calculations getting too crazy.
pub const MaxInstructions: u32 = 100;
}
/// The XCM router. When we want to send an XCM message, we use this type. It amalgamates all of our
/// individual routers.
pub type XcmRouter = (
// Router to send messages to Rialto.
ToRialtoBridge<BridgeRialtoMessages>,
);
parameter_types! {
pub const MaxAssetsIntoHolding: u32 = 64;
}
/// The barriers one of which must be passed for an XCM message to be executed.
pub type Barrier = (
// Weight that is paid for may be consumed.
TakeWeightCredit,
// If the message is one that immediately attemps to pay for execution, then allow it.
AllowTopLevelPaidExecutionFrom<Everything>,
// Expected responses are OK.
AllowKnownQueryResponses<XcmPallet>,
);
/// Outbound XCM weigher type.
pub type OutboundXcmWeigher = xcm_builder::FixedWeightBounds<BaseXcmWeight, (), MaxInstructions>;
/// XCM weigher type.
pub type XcmWeigher = xcm_builder::FixedWeightBounds<BaseXcmWeight, Call, MaxInstructions>;
pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type Call = Call;
type XcmSender = XcmRouter;
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = LocalOriginConverter;
type IsReserve = ();
type IsTeleporter = ();
type UniversalLocation = UniversalLocation;
type Barrier = Barrier;
type Weigher = XcmWeigher;
// The weight trader piggybacks on the existing transaction-fee conversion logic.
type Trader = UsingComponents<WeightToFee, TokenLocation, AccountId, Balances, ()>;
type ResponseHandler = XcmPallet;
type AssetTrap = XcmPallet;
type AssetLocker = ();
type AssetExchanger = ();
type AssetClaims = XcmPallet;
type SubscriptionService = XcmPallet;
type PalletInstancesInfo = AllPalletsWithSystem;
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
}
/// Type to convert an `Origin` type value into a `MultiLocation` value which represents an interior
/// location of this chain.
pub type LocalOriginToLocation = (
// Usual Signed origin to be used in XCM as a corresponding AccountId32
SignedToAccountId32<Origin, AccountId, ThisNetwork>,
);
impl pallet_xcm::Config for Runtime {
type Event = Event;
// We don't allow any messages to be sent via the transaction yet. This is basically safe to
// enable, (safe the possibility of someone spamming the parachain if they're willing to pay
// the DOT to send from the Relay-chain). But it's useless until we bring in XCM v3 which will
// make `DescendOrigin` a bit more useful.
type SendXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmRouter = XcmRouter;
// Anyone can execute XCM messages locally.
type ExecuteXcmOrigin = xcm_builder::EnsureXcmOrigin<Origin, LocalOriginToLocation>;
type XcmExecuteFilter = Everything;
type XcmExecutor = xcm_executor::XcmExecutor<XcmConfig>;
// Anyone is able to use teleportation regardless of who they are and what they want to
// teleport.
type XcmTeleportFilter = Everything;
// Anyone is able to use reserve transfers regardless of who they are and what they want to
// transfer.
type XcmReserveTransferFilter = Everything;
type Weigher = XcmWeigher;
type UniversalLocation = UniversalLocation;
type Origin = Origin;
type Call = Call;
const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100;
type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion;
type Currency = Balances;
type CurrencyMatcher = ();
type TrustedLockers = ();
type SovereignAccountOf = SovereignAccountOf;
type MaxLockers = frame_support::traits::ConstU32<8>;
}
/// With-rialto bridge.
pub struct ToRialtoBridge<MB>(PhantomData<MB>);
impl<MB: MessagesBridge<Origin, AccountId, Balance, FromThisChainMessagePayload>> SendXcm
for ToRialtoBridge<MB>
{
type Ticket = (Balance, FromThisChainMessagePayload);
fn validate(
dest: &mut Option<MultiLocation>,
msg: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let d = dest.take().ok_or(SendError::MissingArgument)?;
if !matches!(d, MultiLocation { parents: 1, interior: X1(GlobalConsensus(r)) } if r == RialtoNetwork::get())
{
*dest = Some(d);
return Err(SendError::NotApplicable)
};
let dest: InteriorMultiLocation = RialtoNetwork::get().into();
let here = UniversalLocation::get();
let route = dest.relative_to(&here);
let msg = (route, msg.take().unwrap()).encode();
let fee = estimate_message_dispatch_and_delivery_fee::<WithRialtoMessageBridge>(
&msg,
WithRialtoMessageBridge::RELAYER_FEE_PERCENT,
None,
)
.map_err(SendError::Transport)?;
let fee_assets = MultiAssets::from((Here, fee));
Ok(((fee, msg), fee_assets))
}
fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
let lane = [0, 0, 0, 0];
let (fee, msg) = ticket;
let result = MB::send_message(
pallet_xcm::Origin::from(MultiLocation::from(UniversalLocation::get())).into(),
lane,
msg,
fee,
);
result
.map(|artifacts| {
let hash = (lane, artifacts.nonce).using_encoded(sp_io::hashing::blake2_256);
log::debug!(target: "runtime::bridge", "Sent XCM message {:?}/{} to Millau: {:?}", lane, artifacts.nonce, hash);
hash
})
.map_err(|e| {
log::debug!(target: "runtime::bridge", "Failed to send XCM message over lane {:?} to Millau: {:?}", lane, e);
SendError::Transport("Bridge has rejected the message")
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use bp_messages::{
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
MessageKey,
};
use bp_runtime::messages::MessageDispatchResult;
use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch;
fn new_test_ext() -> sp_io::TestExternalities {
sp_io::TestExternalities::new(
frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap(),
)
}
#[test]
fn xcm_messages_to_rialto_are_sent() {
new_test_ext().execute_with(|| {
// the encoded message (origin ++ xcm) is 0x010109020419A8
let dest = (Parent, X1(GlobalConsensus(RialtoNetwork::get())));
let xcm: Xcm<()> = vec![Instruction::Trap(42)].into();
let send_result = send_xcm::<XcmRouter>(dest.into(), xcm);
let expected_fee = MultiAssets::from((Here, 4_345_002_552_u64));
let expected_hash =
([0u8, 0u8, 0u8, 0u8], 1u64).using_encoded(sp_io::hashing::blake2_256);
assert_eq!(send_result, Ok((expected_hash, expected_fee)),);
})
}
#[test]
fn xcm_messages_from_rialto_are_dispatched() {
type XcmExecutor = xcm_executor::XcmExecutor<XcmConfig>;
type MessageDispatcher = FromBridgedChainMessageDispatch<
WithRialtoMessageBridge,
XcmExecutor,
XcmWeigher,
frame_support::traits::ConstU64<BASE_XCM_WEIGHT>,
>;
new_test_ext().execute_with(|| {
let location: MultiLocation =
(Parent, X1(GlobalConsensus(RialtoNetwork::get()))).into();
let xcm: Xcm<Call> = vec![Instruction::Trap(42)].into();
let mut incoming_message = DispatchMessage {
key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 },
data: DispatchMessageData { payload: Ok((location, xcm).into()), fee: 0 },
};
let dispatch_weight = MessageDispatcher::dispatch_weight(&mut incoming_message);
assert_eq!(dispatch_weight, 1_000_000_000);
let dispatch_result =
MessageDispatcher::dispatch(&AccountId::from([0u8; 32]), incoming_message);
assert_eq!(
dispatch_result,
MessageDispatchResult {
dispatch_result: true,
unspent_weight: 0,
dispatch_fee_paid_during_dispatch: false,
}
);
})
}
}