Skip to content

Commit

Permalink
Dynamic fees for bridges-v1 (paritytech#2294)
Browse files Browse the repository at this point in the history
* impl backpressure in the XcmBlobHaulerAdapter

* LocalXcmQueueManager + more adapters

* OnMessageDelviered callback

* forbid mesage delivery transactions when the channel between target bridge hub and target asset hub is suspended

* pallet-xcm-bridge-hub-router

* removed commented code

* improvements and tests for palle-xcm-bridge-router

* use LocalXcmChannel in XcmBlobMessageDispatch

* new tests for logic changes in messages pallet

* tests for LocalXcmQueueSuspender

* tests for LocalXcmQueueMessageProcessor

* tests for new logic in the XcmBlobHaulerAdapter

* fix other tests in the bridge-runtime-common

* extension_reject_call_when_dispatcher_is_inactive

* benchmarks for pallet-xcm-bridge-hub-router

* get rid of redundant storage value

* add new pallet to verify-pallets-build.sh

* fixing spellcheck, clippy and rustdoc

* trigger CI

* Revert "trigger CI"

This reverts commit 48f1ba032334e3c6d8470436483736988aa060ac.

* change log target for xcm bridge router pallet

* Update modules/xcm-bridge-hub-router/src/lib.rs

Co-authored-by: Branislav Kontur <bkontur@gmail.com>

* use saturated_len where possible

* fmt

* (Suggestion) Ability to externalize configuration for `ExporterFor` (paritytech#2313)

* Ability to externalize configuration for `ExporterFor`
(Replaced `BridgedNetworkId/SiblingBridgeHubLocation` with `Bridges: ExporterFor`)

* Fix millau

* Compile fix

* Return back `BridgedNetworkId` but as optional filter

* Replaced `BaseFee` with fees from inner `Bridges: ExporterFor`

* typo

* Clippy

* Rename LocalXcmChannel to XcmChannelStatusProvider (paritytech#2319)

* Rename LocalXcmChannel to XcmChannelStatusProvider

* fmt

* added/fixed some docs

* Dynamic fees v1: report congestion status to sending chain (paritytech#2318)

* report congestion status: changes at the sending chain

* OnMessagesDelivered is back

* report congestion status: changes at the bridge hub

* moer logging

* fix? benchmarks

* spelling

* tests for XcmBlobHaulerAdapter and LocalXcmQueueManager

* tests for messages pallet

* fix typo

* rustdoc

* Update modules/messages/src/lib.rs

* apply review suggestions

* ".git/.scripts/commands/fmt/fmt.sh"

* Added `XcmBridgeHubRouterCall::report_bridge_status` encodings for AHK/P (paritytech#2350)

* Added `XcmBridgeHubRouterCall::report_bridge_status` encodings for AHK/P

* Spellcheck

* Added const for `XcmBridgeHubRouterTransactCallMaxWeight`

* Cargo.lock

* Introduced base delivery fee constants

* Congestion messages as Optional to turn on/off `supports_congestion_detection`

* Spellcheck

* Ability to externalize dest for benchmarks

* Ability to externalize dest for benchmarks

---------

Co-authored-by: Branislav Kontur <bkontur@gmail.com>
Co-authored-by: command-bot <>
  • Loading branch information
2 people authored and serban300 committed Apr 10, 2024
1 parent 8f3a012 commit 08f5d3e
Show file tree
Hide file tree
Showing 30 changed files with 2,031 additions and 71 deletions.
5 changes: 5 additions & 0 deletions bridges/bin/millau/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ bp-rialto = { path = "../../../primitives/chain-rialto", default-features = fals
bp-rialto-parachain = { path = "../../../primitives/chain-rialto-parachain", default-features = false }
bp-runtime = { path = "../../../primitives/runtime", default-features = false }
bp-westend = { path = "../../../primitives/chain-westend", default-features = false }
bp-xcm-bridge-hub-router = { path = "../../../primitives/xcm-bridge-hub-router", default-features = false }
bridge-runtime-common = { path = "../../runtime-common", default-features = false }
pallet-bridge-grandpa = { path = "../../../modules/grandpa", default-features = false }
pallet-bridge-messages = { path = "../../../modules/messages", default-features = false }
pallet-bridge-parachains = { path = "../../../modules/parachains", default-features = false }
pallet-bridge-relayers = { path = "../../../modules/relayers", default-features = false }
pallet-shift-session-manager = { path = "../../../modules/shift-session-manager", default-features = false }
pallet-xcm-bridge-hub-router = { path = "../../../modules/xcm-bridge-hub-router", default-features = false }

# Substrate Dependencies

Expand Down Expand Up @@ -90,6 +92,7 @@ std = [
"bp-rialto-parachain/std",
"bp-runtime/std",
"bp-westend/std",
"bp-xcm-bridge-hub-router/std",
"bridge-runtime-common/std",
"codec/std",
"frame-executive/std",
Expand All @@ -114,6 +117,7 @@ std = [
"pallet-transaction-payment/std",
"pallet-utility/std",
"pallet-xcm/std",
"pallet-xcm-bridge-hub-router/std",
"scale-info/std",
"sp-api/std",
"sp-block-builder/std",
Expand All @@ -140,6 +144,7 @@ runtime-benchmarks = [
"pallet-bridge-parachains/runtime-benchmarks",
"pallet-bridge-relayers/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-xcm-bridge-hub-router/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
43 changes: 41 additions & 2 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
use xcm_builder::NetworkExportTable;

// to be able to use Millau runtime in `bridge-runtime-common` tests
pub use bridge_runtime_common;
Expand All @@ -65,8 +66,8 @@ pub use frame_support::{
dispatch::DispatchClass,
parameter_types,
traits::{
ConstBool, ConstU32, ConstU64, ConstU8, Currency, ExistenceRequirement, Imbalance,
KeyOwnerProofSystem,
ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, Currency, ExistenceRequirement,
Imbalance, KeyOwnerProofSystem,
},
weights::{
constants::WEIGHT_REF_TIME_PER_SECOND, ConstantMultiplier, IdentityFee, RuntimeDbWeight,
Expand Down Expand Up @@ -464,6 +465,7 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
WithRialtoMessagesInstance,
frame_support::traits::ConstU64<100_000>,
>;
type OnMessagesDelivered = ();

type SourceHeaderChain = crate::rialto_messages::RialtoAsSourceHeaderChain;
type MessageDispatch = crate::rialto_messages::FromRialtoMessageDispatch;
Expand Down Expand Up @@ -495,6 +497,7 @@ impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Run
WithRialtoParachainMessagesInstance,
frame_support::traits::ConstU64<100_000>,
>;
type OnMessagesDelivered = ();

type SourceHeaderChain = crate::rialto_parachain_messages::RialtoParachainAsSourceHeaderChain;
type MessageDispatch = crate::rialto_parachain_messages::FromRialtoParachainMessageDispatch;
Expand Down Expand Up @@ -544,6 +547,27 @@ impl pallet_utility::Config for Runtime {
type WeightInfo = ();
}

// this config is totally incorrect - the pallet is not actually used at this runtime. We need
// it only to be able to run benchmarks and make required traits (and default weights for tests).
parameter_types! {
pub BridgeTable: Vec<(xcm::prelude::NetworkId, xcm::prelude::MultiLocation, Option<xcm::prelude::MultiAsset>)>
= vec![(xcm_config::RialtoNetwork::get(), xcm_config::TokenLocation::get(), Some((xcm_config::TokenAssetId::get(), 1_000_000_000_u128).into()))];
}
impl pallet_xcm_bridge_hub_router::Config for Runtime {
type WeightInfo = ();

type UniversalLocation = xcm_config::UniversalLocation;
type BridgedNetworkId = xcm_config::RialtoNetwork;
type Bridges = NetworkExportTable<BridgeTable>;

type BridgeHubOrigin = frame_system::EnsureRoot<AccountId>;
type ToBridgeHubSender = xcm_config::XcmRouter;
type WithBridgeHubChannel = xcm_config::EmulatedSiblingXcmpChannel;

type ByteFee = ConstU128<1_000>;
type FeeAsset = xcm_config::TokenAssetId;
}

construct_runtime!(
pub enum Runtime {
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Expand Down Expand Up @@ -582,6 +606,9 @@ construct_runtime!(

// Pallet for sending XCM.
XcmPallet: pallet_xcm::{Pallet, Call, Storage, Event<T>, Origin, Config<T>} = 99,

// Pallets that are not actually used here (yet?), but we need to run benchmarks on it.
XcmBridgeHubRouter: pallet_xcm_bridge_hub_router::{Pallet, Storage} = 200,
}
);

Expand Down Expand Up @@ -654,6 +681,7 @@ mod benches {
[pallet_bridge_grandpa, BridgeRialtoGrandpa]
[pallet_bridge_parachains, ParachainsBench::<Runtime, WithRialtoParachainsInstance>]
[pallet_bridge_relayers, RelayersBench::<Runtime>]
[pallet_xcm_bridge_hub_router, XcmBridgeHubRouterBench::<Runtime>]
);
}

Expand Down Expand Up @@ -980,6 +1008,7 @@ impl_runtime_apis! {
use pallet_bridge_messages::benchmarking::Pallet as MessagesBench;
use pallet_bridge_parachains::benchmarking::Pallet as ParachainsBench;
use pallet_bridge_relayers::benchmarking::Pallet as RelayersBench;
use pallet_xcm_bridge_hub_router::benchmarking::Pallet as XcmBridgeHubRouterBench;

let mut list = Vec::<BenchmarkList>::new();
list_benchmarks!(list, extra);
Expand Down Expand Up @@ -1026,6 +1055,10 @@ impl_runtime_apis! {
Pallet as RelayersBench,
Config as RelayersConfig,
};
use pallet_xcm_bridge_hub_router::benchmarking::{
Pallet as XcmBridgeHubRouterBench,
Config as XcmBridgeHubRouterConfig,
};
use rialto_messages::WithRialtoMessageBridge;
use rialto_parachain_messages::WithRialtoParachainMessageBridge;

Expand Down Expand Up @@ -1136,6 +1169,12 @@ impl_runtime_apis! {
}
}

impl XcmBridgeHubRouterConfig<()> for Runtime {
fn make_congested() {
xcm_config::EmulatedSiblingXcmpChannel::make_congested()
}
}

let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);

Expand Down
19 changes: 14 additions & 5 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ use bridge_runtime_common::{
messages::{
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
},
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
messages_xcm_extension::{SenderAndLane, XcmBlobHauler, XcmBlobHaulerAdapter},
};
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
use pallet_bridge_relayers::WeightInfoExt as _;
use xcm::latest::prelude::*;
use xcm_builder::HaulBlobExporter;

/// Default lane that is used to send messages to Rialto.
Expand All @@ -42,6 +43,11 @@ parameter_types! {
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
/// Lane used by the with-Rialto bridge.
pub RialtoSenderAndLane: SenderAndLane = SenderAndLane::new(Here.into(), XCM_LANE);

/// Dummy message used in configuration.
pub DummyXcmMessage: Xcm<()> = Xcm::new();
}

/// Message payload for Millau -> Rialto messages.
Expand All @@ -66,6 +72,7 @@ pub type FromRialtoMessageDispatch =
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
crate::xcm_config::OnMillauBlobDispatcher,
(),
(),
>;

/// Maximal outbound payload size of Millau -> Rialto messages.
Expand Down Expand Up @@ -122,11 +129,13 @@ pub type ToRialtoBlobExporter = HaulBlobExporter<
pub struct ToRialtoXcmBlobHauler;

impl XcmBlobHauler for ToRialtoXcmBlobHauler {
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithRialtoMessagesInstance>;
type Runtime = Runtime;
type MessagesInstance = WithRialtoMessagesInstance;
type SenderAndLane = RialtoSenderAndLane;

fn xcm_lane() -> LaneId {
XCM_LANE
}
type ToSourceChainSender = crate::xcm_config::XcmRouter;
type CongestedMessage = DummyXcmMessage;
type UncongestedMessage = DummyXcmMessage;
}

impl pallet_bridge_messages::WeightInfoExt for crate::weights::RialtoMessagesWeightInfo<Runtime> {
Expand Down
20 changes: 14 additions & 6 deletions bridges/bin/millau/runtime/src/rialto_parachain_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ use bridge_runtime_common::{
messages::{
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
},
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
messages_xcm_extension::{SenderAndLane, XcmBlobHauler, XcmBlobHaulerAdapter},
};
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
use pallet_bridge_relayers::WeightInfoExt as _;
use xcm::latest::prelude::*;
use xcm_builder::HaulBlobExporter;

/// Default lane that is used to send messages to Rialto parachain.
Expand All @@ -44,6 +45,11 @@ parameter_types! {
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
/// Lane used by the with-RialtoParachain bridge.
pub RialtoParachainSenderAndLane: SenderAndLane = SenderAndLane::new(Here.into(), XCM_LANE);

/// Dummy message used in configuration.
pub DummyXcmMessage: Xcm<()> = Xcm::new();
}

/// Message payload for Millau -> RialtoParachain messages.
Expand All @@ -61,6 +67,7 @@ pub type FromRialtoParachainMessageDispatch =
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
crate::xcm_config::OnMillauBlobDispatcher,
(),
(),
>;

/// Maximal outbound payload size of Millau -> RialtoParachain messages.
Expand Down Expand Up @@ -122,12 +129,13 @@ pub type ToRialtoParachainBlobExporter = HaulBlobExporter<
pub struct ToRialtoParachainXcmBlobHauler;

impl XcmBlobHauler for ToRialtoParachainXcmBlobHauler {
type MessageSender =
pallet_bridge_messages::Pallet<Runtime, WithRialtoParachainMessagesInstance>;
type Runtime = Runtime;
type MessagesInstance = WithRialtoParachainMessagesInstance;
type SenderAndLane = RialtoParachainSenderAndLane;

fn xcm_lane() -> LaneId {
XCM_LANE
}
type ToSourceChainSender = crate::xcm_config::XcmRouter;
type CongestedMessage = DummyXcmMessage;
type UncongestedMessage = DummyXcmMessage;
}

impl pallet_bridge_messages::WeightInfoExt
Expand Down
40 changes: 37 additions & 3 deletions bridges/bin/millau/runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ parameter_types! {
/// 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();
/// Token asset identifier.
pub TokenAssetId: AssetId = TokenLocation::get().into();
/// The Millau network ID.
pub const ThisNetwork: NetworkId = CustomNetworkId::Millau.as_network_id();
/// The Rialto network ID.
Expand Down Expand Up @@ -98,7 +100,7 @@ parameter_types! {
}

/// The XCM router. We are not sending messages to sibling/parent/child chains here.
pub type XcmRouter = ();
pub type XcmRouter = EmulatedSiblingXcmpChannel;

/// The barriers one of which must be passed for an XCM message to be executed.
pub type Barrier = (
Expand Down Expand Up @@ -235,6 +237,38 @@ impl ExportXcm for ToRialtoOrRialtoParachainSwitchExporter {
}
}

/// Emulating XCMP channel with sibling chain. We don't have required infra here, at Millau,
/// so we have to provide at least something to be able to run benchmarks.
pub struct EmulatedSiblingXcmpChannel;

impl SendXcm for EmulatedSiblingXcmpChannel {
type Ticket = ();

fn validate(
_destination: &mut Option<MultiLocation>,
_message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
Ok(((), Default::default()))
}

fn deliver(_ticket: Self::Ticket) -> Result<XcmHash, SendError> {
Ok(XcmHash::default())
}
}

impl EmulatedSiblingXcmpChannel {
/// Start emulating congested channel.
pub fn make_congested() {
frame_support::storage::unhashed::put(b"EmulatedSiblingXcmpChannel.Congested", &true);
}
}

impl bp_xcm_bridge_hub_router::XcmChannelStatusProvider for EmulatedSiblingXcmpChannel {
fn is_congested() -> bool {
frame_support::storage::unhashed::get_or_default(b"EmulatedSiblingXcmpChannel.Congested")
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -357,7 +391,7 @@ mod tests {
let dispatch_result = FromRialtoMessageDispatch::dispatch(incoming_message);
assert!(matches!(
dispatch_result.dispatch_level_result,
XcmBlobMessageDispatchResult::NotDispatched(_),
XcmBlobMessageDispatchResult::Dispatched,
));
}

Expand All @@ -370,7 +404,7 @@ mod tests {
let dispatch_result = FromRialtoMessageDispatch::dispatch(incoming_message);
assert!(matches!(
dispatch_result.dispatch_level_result,
XcmBlobMessageDispatchResult::NotDispatched(_),
XcmBlobMessageDispatchResult::Dispatched,
));
}
}
1 change: 1 addition & 0 deletions bridges/bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {
WithMillauMessagesInstance,
frame_support::traits::ConstU128<100_000>,
>;
type OnMessagesDelivered = ();

type SourceHeaderChain = crate::millau_messages::MillauAsSourceHeaderChain;
type MessageDispatch = crate::millau_messages::FromMillauMessageDispatch;
Expand Down
19 changes: 14 additions & 5 deletions bridges/bin/rialto-parachain/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ use bridge_runtime_common::{
messages::{
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
},
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
messages_xcm_extension::{SenderAndLane, 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.
Expand All @@ -44,6 +45,11 @@ parameter_types! {
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
/// (it is prepended with `UniversalOrigin` instruction).
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
/// Lane used by the with-Millau bridge.
pub MullauSenderAndLane: SenderAndLane = SenderAndLane::new(Here.into(), XCM_LANE);

/// Dummy message used in configuration.
pub DummyXcmMessage: Xcm<()> = Xcm::new();
}

/// Message payload for RialtoParachain -> Millau messages.
Expand All @@ -61,6 +67,7 @@ pub type FromMillauMessageDispatch =
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
crate::OnRialtoParachainBlobDispatcher,
(),
(),
>;

/// Messages proof for Millau -> RialtoParachain messages.
Expand Down Expand Up @@ -122,11 +129,13 @@ pub type ToMillauBlobExporter =
pub struct ToMillauXcmBlobHauler;

impl XcmBlobHauler for ToMillauXcmBlobHauler {
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithMillauMessagesInstance>;
type Runtime = Runtime;
type MessagesInstance = WithMillauMessagesInstance;
type SenderAndLane = MullauSenderAndLane;

fn xcm_lane() -> LaneId {
XCM_LANE
}
type ToSourceChainSender = crate::XcmRouter;
type CongestedMessage = DummyXcmMessage;
type UncongestedMessage = DummyXcmMessage;
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {
WithMillauMessagesInstance,
frame_support::traits::ConstU128<100_000>,
>;
type OnMessagesDelivered = ();

type SourceHeaderChain = crate::millau_messages::MillauAsSourceHeaderChain;
type MessageDispatch = crate::millau_messages::FromMillauMessageDispatch;
Expand Down
Loading

0 comments on commit 08f5d3e

Please sign in to comment.