Remove deprecated code from bridge-runtime-common (#1983)

* removed FromBridgedChainMessageDispatch in favor of XcmBlobMessageDispatch

* use HaulBlobExporter/HaulBlobExporterAdapter instead of XcmBridge/XcmBridgeAdapter

* tests for sending/dispatching messages

* use new schema in testnet bridges + some cleanup

* clippy

* spelling + added TODO

* cleanup some checks

* benchmarks compilation

* all is XCM

* updated README.md

* ref issue from TODO
This commit is contained in:
Svyatoslav Nikolsky
2023-03-23 10:44:53 +03:00
committed by Bastian Köcher
parent 2407228972
commit a75c28d5b2
21 changed files with 604 additions and 889 deletions
@@ -111,6 +111,7 @@ std = [
"frame-support/std",
"frame-executive/std",
"frame-system/std",
"frame-system-rpc-runtime-api/std",
"pallet-balances/std",
"pallet-bridge-grandpa/std",
"pallet-bridge-messages/std",
@@ -118,6 +119,7 @@ std = [
"pallet-timestamp/std",
"pallet-sudo/std",
"pallet-transaction-payment/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-xcm/std",
"parachain-info/std",
"polkadot-parachain/std",
+83 -96
View File
@@ -26,12 +26,7 @@
#[cfg(feature = "std")]
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
use crate::millau_messages::{WithMillauMessageBridge, XCM_LANE};
use bridge_runtime_common::{
generate_bridge_reject_obsolete_headers_and_messages,
messages::source::{XcmBridge, XcmBridgeAdapter},
};
use bridge_runtime_common::generate_bridge_reject_obsolete_headers_and_messages;
use codec::{Decode, Encode};
use cumulus_pallet_parachain_system::AnyRelayNumber;
use scale_info::TypeInfo;
@@ -88,11 +83,10 @@ use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter,
EnsureXcmOrigin, FixedWeightBounds, IsConcrete, NativeAsset, ParentAsSuperuser, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
UsingComponents,
AccountId32Aliases, CurrencyAdapter, EnsureXcmOrigin, FixedWeightBounds, IsConcrete,
NativeAsset, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
};
use xcm_executor::{Config, XcmExecutor};
@@ -351,7 +345,7 @@ parameter_types! {
pub const RelayLocation: MultiLocation = MultiLocation::parent();
pub const RelayNetwork: NetworkId = CustomNetworkId::Rialto.as_network_id();
pub RelayOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation = X1(Parachain(ParachainInfo::parachain_id().into()));
pub UniversalLocation: InteriorMultiLocation = ThisNetwork::get().into();
/// The Millau network ID.
pub const MillauNetwork: NetworkId = CustomNetworkId::Millau.as_network_id();
/// The RialtoParachain network ID.
@@ -412,8 +406,8 @@ pub type XcmOriginToTransactDispatchOrigin = (
// the following constant must match the similar constant in the Millau runtime.
parameter_types! {
/// The amount of weight an XCM operation takes. This is a safe overestimate.
pub const UnitWeightCost: Weight = Weight::from_parts(1_000_000, 64 * 1024);
/// The amount of weight an XCM operation takes. We don't care much about those values as we're on testnet.
pub const UnitWeightCost: Weight = Weight::from_parts(1_000_000, 1024);
// One UNIT buys 1 second of weight.
pub const WeightPrice: (MultiLocation, u128) = (MultiLocation::parent(), UNIT);
pub const MaxInstructions: u32 = 100;
@@ -428,12 +422,11 @@ match_types! {
};
}
pub type Barrier = (
TakeWeightCredit,
AllowTopLevelPaidExecutionFrom<Everything>,
AllowUnpaidExecutionFrom<ParentOrParentsUnitPlurality>,
// ^^^ Parent & its unit plurality gets free execution
);
pub type Barrier = TakeWeightCredit;
/// Dispatches received XCM messages from other chain.
pub type OnRialtoParachainBlobDispatcher =
xcm_builder::BridgeBlobDispatcher<XcmRouter, UniversalLocation>;
/// XCM weigher type.
pub type XcmWeigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
@@ -441,7 +434,7 @@ pub type XcmWeigher = FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstruct
pub struct XcmConfig;
impl Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = XcmRouter;
type XcmSender = ();
type AssetTransactor = LocalAssetTransactor;
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type IsReserve = NativeAsset;
@@ -459,7 +452,7 @@ impl Config for XcmConfig {
type AssetLocker = ();
type AssetExchanger = ();
type FeeManager = ();
type MessageExporter = ();
type MessageExporter = millau_messages::ToMillauBlobExporter;
type UniversalAliases = Nothing;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
@@ -468,42 +461,12 @@ impl Config for XcmConfig {
/// No local origins on this chain are allowed to dispatch XCM sends/executions.
pub type LocalOriginToLocation = SignedToAccountId32<RuntimeOrigin, AccountId, RelayNetwork>;
/// The means for routing XCM messages which are not for local execution into the right message
/// queues.
pub type XcmRouter = (
// Bridge is used to communicate with other relay chain (Millau).
XcmBridgeAdapter<ToMillauBridge>,
);
/// With-Millau bridge.
pub struct ToMillauBridge;
impl XcmBridge for ToMillauBridge {
type MessageBridge = WithMillauMessageBridge;
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithMillauMessagesInstance>;
fn universal_location() -> InteriorMultiLocation {
UniversalLocation::get()
}
fn verify_destination(dest: &MultiLocation) -> bool {
matches!(*dest, MultiLocation { parents: 1, interior: X1(GlobalConsensus(r)) } if r == MillauNetwork::get())
}
fn build_destination() -> MultiLocation {
let dest: InteriorMultiLocation = MillauNetwork::get().into();
let here = UniversalLocation::get();
dest.relative_to(&here)
}
fn xcm_lane() -> bp_messages::LaneId {
XCM_LANE
}
}
/// The XCM router. We are not sending messages to sibling/parent/child chains here.
pub type XcmRouter = ();
#[cfg(feature = "runtime-benchmarks")]
parameter_types! {
pub ReachableDest: Option<MultiLocation> = todo!("We dont use benchmarks for pallet_xcm, so if you hit this message, you need to remove this and define value instead");
pub ReachableDest: Option<MultiLocation> = None;
}
impl pallet_xcm::Config for Runtime {
@@ -875,17 +838,20 @@ cumulus_pallet_parachain_system::register_validate_block!(
#[cfg(test)]
mod tests {
use super::*;
use crate::millau_messages::WeightCredit;
use crate::millau_messages::{FromMillauMessageDispatch, XCM_LANE};
use bp_messages::{
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
LaneId, MessageKey,
};
use bp_runtime::messages::MessageDispatchResult;
use bridge_runtime_common::{
integrity::check_additional_signed, messages::target::FromBridgedChainMessageDispatch,
integrity::check_additional_signed,
messages_xcm_extension::{XcmBlobMessageDispatchResult, XcmRouterWeigher},
};
use codec::Encode;
use pallet_bridge_messages::OutboundLanes;
use sp_core::Get;
use sp_runtime::generic::Era;
use xcm_executor::XcmExecutor;
fn new_test_ext() -> sp_io::TestExternalities {
sp_io::TestExternalities::new(
@@ -893,54 +859,75 @@ mod tests {
)
}
#[test]
fn xcm_messages_to_millau_are_sent() {
new_test_ext().execute_with(|| {
// the encoded message (origin ++ xcm) is 0x010109020419A8
let dest = (Parent, X1(GlobalConsensus(MillauNetwork::get())));
let xcm: Xcm<()> = vec![Instruction::Trap(42)].into();
fn prepare_outbound_xcm_message(destination: NetworkId) -> Xcm<RuntimeCall> {
vec![ExportMessage {
network: destination,
destination: destination.into(),
xcm: vec![Instruction::Trap(42)].into(),
}]
.into()
}
let send_result = send_xcm::<XcmRouter>(dest.into(), xcm);
let expected_fee = MultiAssets::from((Here, Fungibility::Fungible(1_000_000_u128)));
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_to_millau_are_sent_using_bridge_exporter() {
new_test_ext().execute_with(|| {
// ensure that the there are no messages queued
assert_eq!(
OutboundLanes::<Runtime, WithMillauMessagesInstance>::get(XCM_LANE)
.latest_generated_nonce,
0,
);
// export message instruction "sends" message to Rialto
XcmExecutor::<XcmConfig>::execute_xcm_in_credit(
Here,
prepare_outbound_xcm_message(MillauNetwork::get()),
Default::default(),
Weight::MAX,
Weight::MAX,
)
.ensure_complete()
.expect("runtime configuration must be correct");
// ensure that the message has been queued
assert_eq!(
OutboundLanes::<Runtime, WithMillauMessagesInstance>::get(XCM_LANE)
.latest_generated_nonce,
1,
);
})
}
fn prepare_inbound_bridge_message() -> DispatchMessage<Vec<u8>> {
let xcm = xcm::VersionedXcm::<RuntimeCall>::V3(vec![Instruction::Trap(42)].into());
let location =
xcm::VersionedInteriorMultiLocation::V3(X1(GlobalConsensus(ThisNetwork::get())));
// this is the `BridgeMessage` from polkadot xcm builder, but it has no constructor
// or public fields, so just tuple
let bridge_message = (location, xcm).encode();
DispatchMessage {
key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 },
data: DispatchMessageData { payload: Ok(bridge_message) },
}
}
#[test]
fn xcm_messages_from_millau_are_dispatched() {
type XcmExecutor = xcm_executor::XcmExecutor<XcmConfig>;
type MessageDispatcher = FromBridgedChainMessageDispatch<
WithMillauMessageBridge,
XcmExecutor,
XcmWeigher,
WeightCredit,
>;
new_test_ext().execute_with(|| {
let location: MultiLocation =
(Parent, X1(GlobalConsensus(MillauNetwork::get()))).into();
let xcm: Xcm<RuntimeCall> = vec![Instruction::Trap(42)].into();
let mut incoming_message = prepare_inbound_bridge_message();
let mut incoming_message = DispatchMessage {
key: MessageKey { lane_id: LaneId([0, 0, 0, 0]), nonce: 1 },
data: DispatchMessageData { payload: Ok((location, xcm).into()) },
};
let dispatch_weight = MessageDispatcher::dispatch_weight(&mut incoming_message);
assert_eq!(dispatch_weight, UnitWeightCost::get());
let dispatch_weight = FromMillauMessageDispatch::dispatch_weight(&mut incoming_message);
assert_eq!(dispatch_weight, XcmRouterWeigher::<()>::get());
// we care only about handing message to the XCM dispatcher, so we don't care about its
// actual dispatch
let dispatch_result =
MessageDispatcher::dispatch(&AccountId::from([0u8; 32]), incoming_message);
assert_eq!(
dispatch_result,
MessageDispatchResult {
unspent_weight: frame_support::weights::Weight::zero(),
dispatch_level_result: (),
}
);
})
FromMillauMessageDispatch::dispatch(&AccountId::from([0u8; 32]), incoming_message);
assert!(matches!(
dispatch_result.dispatch_level_result,
XcmBlobMessageDispatchResult::NotDispatched(_),
));
});
}
#[test]
@@ -19,14 +19,18 @@
// TODO: this is almost exact copy of `millau_messages.rs` from Rialto runtime.
// Should be extracted to a separate crate and reused here.
use crate::{MillauGrandpaInstance, Runtime, RuntimeCall, RuntimeOrigin};
use crate::{MillauGrandpaInstance, Runtime, RuntimeOrigin, WithMillauMessagesInstance};
use bp_messages::{LaneId, MessageNonce};
use bp_runtime::{ChainId, MILLAU_CHAIN_ID, RIALTO_PARACHAIN_CHAIN_ID};
use bridge_runtime_common::messages::{
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
use bp_messages::LaneId;
use bridge_runtime_common::{
messages::{
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
},
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
};
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
use xcm::latest::prelude::*;
use xcm_builder::HaulBlobExporter;
/// Default lane that is used to send messages to Millau.
pub const XCM_LANE: LaneId = LaneId([0, 0, 0, 0]);
@@ -51,15 +55,16 @@ pub type ToMillauMessageVerifier =
messages::source::FromThisChainMessageVerifier<WithMillauMessageBridge>;
/// Message payload for Millau -> RialtoParachain messages.
pub type FromMillauMessagePayload = messages::target::FromBridgedChainMessagePayload<RuntimeCall>;
pub type FromMillauMessagePayload = messages::target::FromBridgedChainMessagePayload;
/// Call-dispatch based message dispatch for Millau -> RialtoParachain messages.
pub type FromMillauMessageDispatch = messages::target::FromBridgedChainMessageDispatch<
WithMillauMessageBridge,
xcm_executor::XcmExecutor<crate::XcmConfig>,
crate::XcmWeigher,
WeightCredit,
>;
pub type FromMillauMessageDispatch =
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
bp_rialto_parachain::RialtoParachain,
bp_millau::Millau,
crate::OnRialtoParachainBlobDispatcher,
bridge_runtime_common::messages_xcm_extension::XcmRouterWeigher<()>,
>;
/// Messages proof for Millau -> RialtoParachain messages.
pub type FromMillauMessagesProof = messages::target::FromBridgedChainMessagesProof<bp_millau::Hash>;
@@ -77,8 +82,6 @@ pub type ToMillauMaximalOutboundPayloadSize =
pub struct WithMillauMessageBridge;
impl MessageBridge for WithMillauMessageBridge {
const THIS_CHAIN_ID: ChainId = RIALTO_PARACHAIN_CHAIN_ID;
const BRIDGED_CHAIN_ID: ChainId = MILLAU_CHAIN_ID;
const BRIDGED_MESSAGES_PALLET_NAME: &'static str =
bp_rialto_parachain::WITH_RIALTO_PARACHAIN_MESSAGES_PALLET_NAME;
@@ -97,16 +100,7 @@ impl messages::UnderlyingChainProvider for RialtoParachain {
}
impl messages::ThisChainWithMessages for RialtoParachain {
type RuntimeCall = RuntimeCall;
type RuntimeOrigin = RuntimeOrigin;
fn is_message_accepted(_send_origin: &Self::RuntimeOrigin, _lane: &LaneId) -> bool {
true
}
fn maximal_pending_messages_at_outbound_lane() -> MessageNonce {
MessageNonce::MAX
}
}
/// Millau chain from message lane point of view.
@@ -121,8 +115,24 @@ impl messages::UnderlyingChainProvider for Millau {
type Chain = bp_millau::Millau;
}
impl messages::BridgedChainWithMessages for Millau {
fn verify_dispatch_weight(_message_payload: &[u8]) -> bool {
true
impl messages::BridgedChainWithMessages for Millau {}
/// Export XCM messages to be relayed to Millau.
pub type ToMillauBlobExporter =
HaulBlobExporter<XcmBlobHaulerAdapter<ToMillauXcmBlobHauler>, crate::MillauNetwork, ()>;
/// To-Millau XCM hauler.
pub struct ToMillauXcmBlobHauler;
impl XcmBlobHauler for ToMillauXcmBlobHauler {
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithMillauMessagesInstance>;
type MessageSenderOrigin = RuntimeOrigin;
fn message_sender_origin() -> RuntimeOrigin {
pallet_xcm::Origin::from(MultiLocation::new(1, crate::UniversalLocation::get())).into()
}
fn xcm_lane() -> LaneId {
XCM_LANE
}
}