Skip to content

Commit 31a6cbe

Browse files
svyatonikbkontur
authored andcommitted
Dynamic fees for bridges-v1 (#2294)
* 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 <[email protected]> * use saturated_len where possible * fmt * (Suggestion) Ability to externalize configuration for `ExporterFor` (#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 (#2319) * Rename LocalXcmChannel to XcmChannelStatusProvider * fmt * added/fixed some docs * Dynamic fees v1: report congestion status to sending chain (#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 (#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 <[email protected]> Co-authored-by: command-bot <>
1 parent dc8aa5d commit 31a6cbe

File tree

30 files changed

+2031
-71
lines changed

30 files changed

+2031
-71
lines changed

bridges/bin/millau/runtime/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ bp-rialto = { path = "../../../primitives/chain-rialto", default-features = fals
2323
bp-rialto-parachain = { path = "../../../primitives/chain-rialto-parachain", default-features = false }
2424
bp-runtime = { path = "../../../primitives/runtime", default-features = false }
2525
bp-westend = { path = "../../../primitives/chain-westend", default-features = false }
26+
bp-xcm-bridge-hub-router = { path = "../../../primitives/xcm-bridge-hub-router", default-features = false }
2627
bridge-runtime-common = { path = "../../runtime-common", default-features = false }
2728
pallet-bridge-grandpa = { path = "../../../modules/grandpa", default-features = false }
2829
pallet-bridge-messages = { path = "../../../modules/messages", default-features = false }
2930
pallet-bridge-parachains = { path = "../../../modules/parachains", default-features = false }
3031
pallet-bridge-relayers = { path = "../../../modules/relayers", default-features = false }
3132
pallet-shift-session-manager = { path = "../../../modules/shift-session-manager", default-features = false }
33+
pallet-xcm-bridge-hub-router = { path = "../../../modules/xcm-bridge-hub-router", default-features = false }
3234

3335
# Substrate Dependencies
3436

@@ -90,6 +92,7 @@ std = [
9092
"bp-rialto-parachain/std",
9193
"bp-runtime/std",
9294
"bp-westend/std",
95+
"bp-xcm-bridge-hub-router/std",
9396
"bridge-runtime-common/std",
9497
"codec/std",
9598
"frame-executive/std",
@@ -114,6 +117,7 @@ std = [
114117
"pallet-transaction-payment/std",
115118
"pallet-utility/std",
116119
"pallet-xcm/std",
120+
"pallet-xcm-bridge-hub-router/std",
117121
"scale-info/std",
118122
"sp-api/std",
119123
"sp-block-builder/std",
@@ -140,6 +144,7 @@ runtime-benchmarks = [
140144
"pallet-bridge-parachains/runtime-benchmarks",
141145
"pallet-bridge-relayers/runtime-benchmarks",
142146
"pallet-xcm/runtime-benchmarks",
147+
"pallet-xcm-bridge-hub-router/runtime-benchmarks",
143148
"sp-runtime/runtime-benchmarks",
144149
"xcm-builder/runtime-benchmarks",
145150
]

bridges/bin/millau/runtime/src/lib.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use sp_std::prelude::*;
5555
#[cfg(feature = "std")]
5656
use sp_version::NativeVersion;
5757
use sp_version::RuntimeVersion;
58+
use xcm_builder::NetworkExportTable;
5859

5960
// to be able to use Millau runtime in `bridge-runtime-common` tests
6061
pub use bridge_runtime_common;
@@ -65,8 +66,8 @@ pub use frame_support::{
6566
dispatch::DispatchClass,
6667
parameter_types,
6768
traits::{
68-
ConstBool, ConstU32, ConstU64, ConstU8, Currency, ExistenceRequirement, Imbalance,
69-
KeyOwnerProofSystem,
69+
ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, Currency, ExistenceRequirement,
70+
Imbalance, KeyOwnerProofSystem,
7071
},
7172
weights::{
7273
constants::WEIGHT_REF_TIME_PER_SECOND, ConstantMultiplier, IdentityFee, RuntimeDbWeight,
@@ -464,6 +465,7 @@ impl pallet_bridge_messages::Config<WithRialtoMessagesInstance> for Runtime {
464465
WithRialtoMessagesInstance,
465466
frame_support::traits::ConstU64<100_000>,
466467
>;
468+
type OnMessagesDelivered = ();
467469

468470
type SourceHeaderChain = crate::rialto_messages::RialtoAsSourceHeaderChain;
469471
type MessageDispatch = crate::rialto_messages::FromRialtoMessageDispatch;
@@ -495,6 +497,7 @@ impl pallet_bridge_messages::Config<WithRialtoParachainMessagesInstance> for Run
495497
WithRialtoParachainMessagesInstance,
496498
frame_support::traits::ConstU64<100_000>,
497499
>;
500+
type OnMessagesDelivered = ();
498501

499502
type SourceHeaderChain = crate::rialto_parachain_messages::RialtoParachainAsSourceHeaderChain;
500503
type MessageDispatch = crate::rialto_parachain_messages::FromRialtoParachainMessageDispatch;
@@ -544,6 +547,27 @@ impl pallet_utility::Config for Runtime {
544547
type WeightInfo = ();
545548
}
546549

550+
// this config is totally incorrect - the pallet is not actually used at this runtime. We need
551+
// it only to be able to run benchmarks and make required traits (and default weights for tests).
552+
parameter_types! {
553+
pub BridgeTable: Vec<(xcm::prelude::NetworkId, xcm::prelude::MultiLocation, Option<xcm::prelude::MultiAsset>)>
554+
= vec![(xcm_config::RialtoNetwork::get(), xcm_config::TokenLocation::get(), Some((xcm_config::TokenAssetId::get(), 1_000_000_000_u128).into()))];
555+
}
556+
impl pallet_xcm_bridge_hub_router::Config for Runtime {
557+
type WeightInfo = ();
558+
559+
type UniversalLocation = xcm_config::UniversalLocation;
560+
type BridgedNetworkId = xcm_config::RialtoNetwork;
561+
type Bridges = NetworkExportTable<BridgeTable>;
562+
563+
type BridgeHubOrigin = frame_system::EnsureRoot<AccountId>;
564+
type ToBridgeHubSender = xcm_config::XcmRouter;
565+
type WithBridgeHubChannel = xcm_config::EmulatedSiblingXcmpChannel;
566+
567+
type ByteFee = ConstU128<1_000>;
568+
type FeeAsset = xcm_config::TokenAssetId;
569+
}
570+
547571
construct_runtime!(
548572
pub enum Runtime {
549573
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
@@ -582,6 +606,9 @@ construct_runtime!(
582606

583607
// Pallet for sending XCM.
584608
XcmPallet: pallet_xcm::{Pallet, Call, Storage, Event<T>, Origin, Config<T>} = 99,
609+
610+
// Pallets that are not actually used here (yet?), but we need to run benchmarks on it.
611+
XcmBridgeHubRouter: pallet_xcm_bridge_hub_router::{Pallet, Storage} = 200,
585612
}
586613
);
587614

@@ -654,6 +681,7 @@ mod benches {
654681
[pallet_bridge_grandpa, BridgeRialtoGrandpa]
655682
[pallet_bridge_parachains, ParachainsBench::<Runtime, WithRialtoParachainsInstance>]
656683
[pallet_bridge_relayers, RelayersBench::<Runtime>]
684+
[pallet_xcm_bridge_hub_router, XcmBridgeHubRouterBench::<Runtime>]
657685
);
658686
}
659687

@@ -980,6 +1008,7 @@ impl_runtime_apis! {
9801008
use pallet_bridge_messages::benchmarking::Pallet as MessagesBench;
9811009
use pallet_bridge_parachains::benchmarking::Pallet as ParachainsBench;
9821010
use pallet_bridge_relayers::benchmarking::Pallet as RelayersBench;
1011+
use pallet_xcm_bridge_hub_router::benchmarking::Pallet as XcmBridgeHubRouterBench;
9831012

9841013
let mut list = Vec::<BenchmarkList>::new();
9851014
list_benchmarks!(list, extra);
@@ -1026,6 +1055,10 @@ impl_runtime_apis! {
10261055
Pallet as RelayersBench,
10271056
Config as RelayersConfig,
10281057
};
1058+
use pallet_xcm_bridge_hub_router::benchmarking::{
1059+
Pallet as XcmBridgeHubRouterBench,
1060+
Config as XcmBridgeHubRouterConfig,
1061+
};
10291062
use rialto_messages::WithRialtoMessageBridge;
10301063
use rialto_parachain_messages::WithRialtoParachainMessageBridge;
10311064

@@ -1136,6 +1169,12 @@ impl_runtime_apis! {
11361169
}
11371170
}
11381171

1172+
impl XcmBridgeHubRouterConfig<()> for Runtime {
1173+
fn make_congested() {
1174+
xcm_config::EmulatedSiblingXcmpChannel::make_congested()
1175+
}
1176+
}
1177+
11391178
let mut batches = Vec::<BenchmarkBatch>::new();
11401179
let params = (&config, &whitelist);
11411180

bridges/bin/millau/runtime/src/rialto_messages.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ use bridge_runtime_common::{
2323
messages::{
2424
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
2525
},
26-
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
26+
messages_xcm_extension::{SenderAndLane, XcmBlobHauler, XcmBlobHaulerAdapter},
2727
};
2828
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
2929
use pallet_bridge_relayers::WeightInfoExt as _;
30+
use xcm::latest::prelude::*;
3031
use xcm_builder::HaulBlobExporter;
3132

3233
/// Default lane that is used to send messages to Rialto.
@@ -42,6 +43,11 @@ parameter_types! {
4243
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
4344
/// (it is prepended with `UniversalOrigin` instruction).
4445
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
46+
/// Lane used by the with-Rialto bridge.
47+
pub RialtoSenderAndLane: SenderAndLane = SenderAndLane::new(Here.into(), XCM_LANE);
48+
49+
/// Dummy message used in configuration.
50+
pub DummyXcmMessage: Xcm<()> = Xcm::new();
4551
}
4652

4753
/// Message payload for Millau -> Rialto messages.
@@ -66,6 +72,7 @@ pub type FromRialtoMessageDispatch =
6672
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
6773
crate::xcm_config::OnMillauBlobDispatcher,
6874
(),
75+
(),
6976
>;
7077

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

124131
impl XcmBlobHauler for ToRialtoXcmBlobHauler {
125-
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithRialtoMessagesInstance>;
132+
type Runtime = Runtime;
133+
type MessagesInstance = WithRialtoMessagesInstance;
134+
type SenderAndLane = RialtoSenderAndLane;
126135

127-
fn xcm_lane() -> LaneId {
128-
XCM_LANE
129-
}
136+
type ToSourceChainSender = crate::xcm_config::XcmRouter;
137+
type CongestedMessage = DummyXcmMessage;
138+
type UncongestedMessage = DummyXcmMessage;
130139
}
131140

132141
impl pallet_bridge_messages::WeightInfoExt for crate::weights::RialtoMessagesWeightInfo<Runtime> {

bridges/bin/millau/runtime/src/rialto_parachain_messages.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ use bridge_runtime_common::{
2525
messages::{
2626
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
2727
},
28-
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
28+
messages_xcm_extension::{SenderAndLane, XcmBlobHauler, XcmBlobHaulerAdapter},
2929
};
3030
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
3131
use pallet_bridge_relayers::WeightInfoExt as _;
32+
use xcm::latest::prelude::*;
3233
use xcm_builder::HaulBlobExporter;
3334

3435
/// Default lane that is used to send messages to Rialto parachain.
@@ -44,6 +45,11 @@ parameter_types! {
4445
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
4546
/// (it is prepended with `UniversalOrigin` instruction).
4647
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
48+
/// Lane used by the with-RialtoParachain bridge.
49+
pub RialtoParachainSenderAndLane: SenderAndLane = SenderAndLane::new(Here.into(), XCM_LANE);
50+
51+
/// Dummy message used in configuration.
52+
pub DummyXcmMessage: Xcm<()> = Xcm::new();
4753
}
4854

4955
/// Message payload for Millau -> RialtoParachain messages.
@@ -61,6 +67,7 @@ pub type FromRialtoParachainMessageDispatch =
6167
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
6268
crate::xcm_config::OnMillauBlobDispatcher,
6369
(),
70+
(),
6471
>;
6572

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

124131
impl XcmBlobHauler for ToRialtoParachainXcmBlobHauler {
125-
type MessageSender =
126-
pallet_bridge_messages::Pallet<Runtime, WithRialtoParachainMessagesInstance>;
132+
type Runtime = Runtime;
133+
type MessagesInstance = WithRialtoParachainMessagesInstance;
134+
type SenderAndLane = RialtoParachainSenderAndLane;
127135

128-
fn xcm_lane() -> LaneId {
129-
XCM_LANE
130-
}
136+
type ToSourceChainSender = crate::xcm_config::XcmRouter;
137+
type CongestedMessage = DummyXcmMessage;
138+
type UncongestedMessage = DummyXcmMessage;
131139
}
132140

133141
impl pallet_bridge_messages::WeightInfoExt

bridges/bin/millau/runtime/src/xcm_config.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ parameter_types! {
4242
/// chain, we make it synonymous with it and thus it is the `Here` location, which means "equivalent to
4343
/// the context".
4444
pub const TokenLocation: MultiLocation = Here.into_location();
45+
/// Token asset identifier.
46+
pub TokenAssetId: AssetId = TokenLocation::get().into();
4547
/// The Millau network ID.
4648
pub const ThisNetwork: NetworkId = CustomNetworkId::Millau.as_network_id();
4749
/// The Rialto network ID.
@@ -98,7 +100,7 @@ parameter_types! {
98100
}
99101

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

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

240+
/// Emulating XCMP channel with sibling chain. We don't have required infra here, at Millau,
241+
/// so we have to provide at least something to be able to run benchmarks.
242+
pub struct EmulatedSiblingXcmpChannel;
243+
244+
impl SendXcm for EmulatedSiblingXcmpChannel {
245+
type Ticket = ();
246+
247+
fn validate(
248+
_destination: &mut Option<MultiLocation>,
249+
_message: &mut Option<Xcm<()>>,
250+
) -> SendResult<Self::Ticket> {
251+
Ok(((), Default::default()))
252+
}
253+
254+
fn deliver(_ticket: Self::Ticket) -> Result<XcmHash, SendError> {
255+
Ok(XcmHash::default())
256+
}
257+
}
258+
259+
impl EmulatedSiblingXcmpChannel {
260+
/// Start emulating congested channel.
261+
pub fn make_congested() {
262+
frame_support::storage::unhashed::put(b"EmulatedSiblingXcmpChannel.Congested", &true);
263+
}
264+
}
265+
266+
impl bp_xcm_bridge_hub_router::XcmChannelStatusProvider for EmulatedSiblingXcmpChannel {
267+
fn is_congested() -> bool {
268+
frame_support::storage::unhashed::get_or_default(b"EmulatedSiblingXcmpChannel.Congested")
269+
}
270+
}
271+
238272
#[cfg(test)]
239273
mod tests {
240274
use super::*;
@@ -357,7 +391,7 @@ mod tests {
357391
let dispatch_result = FromRialtoMessageDispatch::dispatch(incoming_message);
358392
assert!(matches!(
359393
dispatch_result.dispatch_level_result,
360-
XcmBlobMessageDispatchResult::NotDispatched(_),
394+
XcmBlobMessageDispatchResult::Dispatched,
361395
));
362396
}
363397

@@ -370,7 +404,7 @@ mod tests {
370404
let dispatch_result = FromRialtoMessageDispatch::dispatch(incoming_message);
371405
assert!(matches!(
372406
dispatch_result.dispatch_level_result,
373-
XcmBlobMessageDispatchResult::NotDispatched(_),
407+
XcmBlobMessageDispatchResult::Dispatched,
374408
));
375409
}
376410
}

bridges/bin/rialto-parachain/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {
584584
WithMillauMessagesInstance,
585585
frame_support::traits::ConstU128<100_000>,
586586
>;
587+
type OnMessagesDelivered = ();
587588

588589
type SourceHeaderChain = crate::millau_messages::MillauAsSourceHeaderChain;
589590
type MessageDispatch = crate::millau_messages::FromMillauMessageDispatch;

bridges/bin/rialto-parachain/runtime/src/millau_messages.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ use bridge_runtime_common::{
2626
messages::{
2727
self, source::TargetHeaderChainAdapter, target::SourceHeaderChainAdapter, MessageBridge,
2828
},
29-
messages_xcm_extension::{XcmBlobHauler, XcmBlobHaulerAdapter},
29+
messages_xcm_extension::{SenderAndLane, XcmBlobHauler, XcmBlobHaulerAdapter},
3030
};
3131
use frame_support::{parameter_types, weights::Weight, RuntimeDebug};
32+
use xcm::latest::prelude::*;
3233
use xcm_builder::HaulBlobExporter;
3334

3435
/// Default lane that is used to send messages to Millau.
@@ -44,6 +45,11 @@ parameter_types! {
4445
/// 2 XCM instructions is for simple `Trap(42)` program, coming through bridge
4546
/// (it is prepended with `UniversalOrigin` instruction).
4647
pub const WeightCredit: Weight = BASE_XCM_WEIGHT_TWICE;
48+
/// Lane used by the with-Millau bridge.
49+
pub MullauSenderAndLane: SenderAndLane = SenderAndLane::new(Here.into(), XCM_LANE);
50+
51+
/// Dummy message used in configuration.
52+
pub DummyXcmMessage: Xcm<()> = Xcm::new();
4753
}
4854

4955
/// Message payload for RialtoParachain -> Millau messages.
@@ -61,6 +67,7 @@ pub type FromMillauMessageDispatch =
6167
bridge_runtime_common::messages_xcm_extension::XcmBlobMessageDispatch<
6268
crate::OnRialtoParachainBlobDispatcher,
6369
(),
70+
(),
6471
>;
6572

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

124131
impl XcmBlobHauler for ToMillauXcmBlobHauler {
125-
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithMillauMessagesInstance>;
132+
type Runtime = Runtime;
133+
type MessagesInstance = WithMillauMessagesInstance;
134+
type SenderAndLane = MullauSenderAndLane;
126135

127-
fn xcm_lane() -> LaneId {
128-
XCM_LANE
129-
}
136+
type ToSourceChainSender = crate::XcmRouter;
137+
type CongestedMessage = DummyXcmMessage;
138+
type UncongestedMessage = DummyXcmMessage;
130139
}
131140

132141
#[cfg(test)]

bridges/bin/rialto/runtime/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ impl pallet_bridge_messages::Config<WithMillauMessagesInstance> for Runtime {
442442
WithMillauMessagesInstance,
443443
frame_support::traits::ConstU128<100_000>,
444444
>;
445+
type OnMessagesDelivered = ();
445446

446447
type SourceHeaderChain = crate::millau_messages::MillauAsSourceHeaderChain;
447448
type MessageDispatch = crate::millau_messages::FromMillauMessageDispatch;

0 commit comments

Comments
 (0)