Skip to content

Commit 8b1e146

Browse files
bkonturbkchr
authored andcommitted
Pokladot sdk backport to staging (#2746)
* Bump to `polkadot-sdk` master `ebe2aad6f0ae576a0e176f38a084fe7579f936dd` * align with others * Backport xcm version stuff from `polkadot-sdk` * Backport Slava's commit from `polkadot-sdk` * Workspace clippy as `polkadot-sdk` does * Make submodules use workspace lints * Add Bridges clippy addons to workspace lints * Looks like we dont need extra clippy addons anymore?
1 parent 23440ce commit 8b1e146

File tree

56 files changed

+288
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+288
-58
lines changed

bridges/bin/runtime-common/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ edition.workspace = true
77
repository.workspace = true
88
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
99

10+
[lints]
11+
workspace = true
12+
1013
[dependencies]
1114
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] }
1215
hash-db = { version = "0.16.0", default-features = false }
@@ -59,8 +62,8 @@ std = [
5962
"bp-polkadot-core/std",
6063
"bp-relayers/std",
6164
"bp-runtime/std",
62-
"bp-xcm-bridge-hub/std",
6365
"bp-xcm-bridge-hub-router/std",
66+
"bp-xcm-bridge-hub/std",
6467
"codec/std",
6568
"frame-support/std",
6669
"frame-system/std",

bridges/bin/runtime-common/src/messages_xcm_extension.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,28 @@ impl<H: XcmBlobHauler> LocalXcmQueueManager<H> {
309309
}
310310
}
311311

312+
/// Adapter for the implementation of `GetVersion`, which attempts to find the minimal
313+
/// configured XCM version between the destination `dest` and the bridge hub location provided as
314+
/// `Get<Location>`.
315+
pub struct XcmVersionOfDestAndRemoteBridge<Version, RemoteBridge>(
316+
sp_std::marker::PhantomData<(Version, RemoteBridge)>,
317+
);
318+
impl<Version: GetVersion, RemoteBridge: Get<MultiLocation>> GetVersion
319+
for XcmVersionOfDestAndRemoteBridge<Version, RemoteBridge>
320+
{
321+
fn get_version_for(dest: &MultiLocation) -> Option<XcmVersion> {
322+
let dest_version = Version::get_version_for(dest);
323+
let bridge_hub_version = Version::get_version_for(&RemoteBridge::get());
324+
325+
match (dest_version, bridge_hub_version) {
326+
(Some(dv), Some(bhv)) => Some(sp_std::cmp::min(dv, bhv)),
327+
(Some(dv), None) => Some(dv),
328+
(None, Some(bhv)) => Some(bhv),
329+
(None, None) => None,
330+
}
331+
}
332+
}
333+
312334
#[cfg(test)]
313335
mod tests {
314336
use super::*;

bridges/modules/beefy/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ authors = ["Parity Technologies <[email protected]>"]
66
edition = "2021"
77
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
88

9+
[lints]
10+
workspace = true
11+
912
[dependencies]
1013
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
1114
log = { version = "0.4.20", default-features = false }

bridges/modules/grandpa/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ authors.workspace = true
66
edition.workspace = true
77
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
88

9+
[lints]
10+
workspace = true
11+
912
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1013

1114
[dependencies]

bridges/modules/messages/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ authors.workspace = true
66
edition.workspace = true
77
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
88

9+
[lints]
10+
workspace = true
11+
912
[dependencies]
1013
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
1114
log = { version = "0.4.20", default-features = false }

bridges/modules/parachains/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ authors.workspace = true
66
edition.workspace = true
77
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
88

9+
[lints]
10+
workspace = true
11+
912
[dependencies]
1013
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
1114
log = { version = "0.4.20", default-features = false }

bridges/modules/relayers/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ authors.workspace = true
66
edition.workspace = true
77
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
88

9+
[lints]
10+
workspace = true
11+
912
[dependencies]
1013
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
1114
log = { version = "0.4.20", default-features = false }

bridges/modules/xcm-bridge-hub-router/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ authors.workspace = true
66
edition.workspace = true
77
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
88

9+
[lints]
10+
workspace = true
11+
912
[dependencies]
1013
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
1114
log = { version = "0.4.20", default-features = false }

bridges/modules/xcm-bridge-hub-router/src/benchmarking.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use crate::{Bridge, Call};
2222

2323
use bp_xcm_bridge_hub_router::{BridgeState, MINIMAL_DELIVERY_FEE_FACTOR};
24-
use frame_benchmarking::benchmarks_instance_pallet;
24+
use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError};
2525
use frame_support::traits::{EnsureOrigin, Get, Hooks, UnfilteredDispatchable};
2626
use sp_runtime::traits::Zero;
2727
use xcm::prelude::*;
@@ -37,11 +37,11 @@ pub trait Config<I: 'static>: crate::Config<I> {
3737
/// Returns destination which is valid for this router instance.
3838
/// (Needs to pass `T::Bridges`)
3939
/// Make sure that `SendXcm` will pass.
40-
fn ensure_bridged_target_destination() -> MultiLocation {
41-
MultiLocation::new(
40+
fn ensure_bridged_target_destination() -> Result<MultiLocation, BenchmarkError> {
41+
Ok(MultiLocation::new(
4242
Self::UniversalLocation::get().len() as u8,
4343
X1(GlobalConsensus(Self::BridgedNetworkId::get().unwrap())),
44-
)
44+
))
4545
}
4646
}
4747

@@ -61,7 +61,7 @@ benchmarks_instance_pallet! {
6161
delivery_fee_factor: MINIMAL_DELIVERY_FEE_FACTOR + MINIMAL_DELIVERY_FEE_FACTOR,
6262
});
6363

64-
let _ = T::ensure_bridged_target_destination();
64+
let _ = T::ensure_bridged_target_destination()?;
6565
T::make_congested();
6666
}: {
6767
crate::Pallet::<T, I>::on_initialize(Zero::zero())
@@ -81,7 +81,7 @@ benchmarks_instance_pallet! {
8181
}
8282

8383
send_message {
84-
let dest = T::ensure_bridged_target_destination();
84+
let dest = T::ensure_bridged_target_destination()?;
8585
let xcm = sp_std::vec![].into();
8686

8787
// make local queue congested, because it means additional db write

bridges/modules/xcm-bridge-hub-router/src/lib.rs

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ pub mod pallet {
8989
/// **possible fee**. Allows to externalize better control over allowed **bridged
9090
/// networks/locations**.
9191
type Bridges: ExporterFor;
92+
/// Checks the XCM version for the destination.
93+
type DestinationVersion: GetVersion;
9294

9395
/// Origin of the sibling bridge hub that is allowed to report bridge status.
9496
type BridgeHubOrigin: EnsureOrigin<Self::RuntimeOrigin>;
@@ -319,19 +321,32 @@ impl<T: Config<I>, I: 'static> SendXcm for Pallet<T, I> {
319321
dest: &mut Option<MultiLocation>,
320322
xcm: &mut Option<Xcm<()>>,
321323
) -> SendResult<Self::Ticket> {
322-
// we won't have an access to `dest` and `xcm` in the `delvier` method, so precompute
324+
// `dest` and `xcm` are required here
325+
let dest_ref = dest.as_ref().ok_or(SendError::MissingArgument)?;
326+
let xcm_ref = xcm.as_ref().ok_or(SendError::MissingArgument)?;
327+
328+
// we won't have an access to `dest` and `xcm` in the `deliver` method, so precompute
323329
// everything required here
324-
let message_size = xcm
325-
.as_ref()
326-
.map(|xcm| xcm.encoded_size() as _)
327-
.ok_or(SendError::MissingArgument)?;
330+
let message_size = xcm_ref.encoded_size() as _;
328331

329332
// bridge doesn't support oversized/overweight messages now. So it is better to drop such
330333
// messages here than at the bridge hub. Let's check the message size.
331334
if message_size > HARD_MESSAGE_SIZE_LIMIT {
332335
return Err(SendError::ExceedsMaxMessageSize)
333336
}
334337

338+
// We need to ensure that the known `dest`'s XCM version can comprehend the current `xcm`
339+
// program. This may seem like an additional, unnecessary check, but it is not. A similar
340+
// check is probably performed by the `ViaBridgeHubExporter`, which attempts to send a
341+
// versioned message to the sibling bridge hub. However, the local bridge hub may have a
342+
// higher XCM version than the remote `dest`. Once again, it is better to discard such
343+
// messages here than at the bridge hub (e.g., to avoid losing funds).
344+
let destination_version = T::DestinationVersion::get_version_for(dest_ref)
345+
.ok_or(SendError::DestinationUnsupported)?;
346+
let _ = VersionedXcm::from(xcm_ref.clone())
347+
.into_version(destination_version)
348+
.map_err(|()| SendError::DestinationUnsupported)?;
349+
335350
// just use exporter to validate destination and insert instructions to pay message fee
336351
// at the sibling/child bridge hub
337352
//
@@ -358,6 +373,7 @@ impl<T: Config<I>, I: 'static> SendXcm for Pallet<T, I> {
358373
#[cfg(test)]
359374
mod tests {
360375
use super::*;
376+
use frame_support::assert_ok;
361377
use mock::*;
362378

363379
use frame_support::traits::Hooks;
@@ -451,6 +467,19 @@ mod tests {
451467
});
452468
}
453469

470+
#[test]
471+
fn destination_unsupported_if_wrap_version_fails() {
472+
run_test(|| {
473+
assert_eq!(
474+
send_xcm::<XcmBridgeHubRouter>(
475+
UnknownXcmVersionLocation::get(),
476+
vec![ClearOrigin].into(),
477+
),
478+
Err(SendError::DestinationUnsupported),
479+
);
480+
});
481+
}
482+
454483
#[test]
455484
fn returns_proper_delivery_price() {
456485
run_test(|| {
@@ -488,17 +517,14 @@ mod tests {
488517
fn sent_message_doesnt_increase_factor_if_xcm_channel_is_uncongested() {
489518
run_test(|| {
490519
let old_bridge = XcmBridgeHubRouter::bridge();
491-
assert_eq!(
492-
send_xcm::<XcmBridgeHubRouter>(
493-
MultiLocation::new(
494-
2,
495-
X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(1000))
496-
),
497-
vec![ClearOrigin].into(),
498-
)
499-
.map(drop),
500-
Ok(()),
501-
);
520+
assert_ok!(send_xcm::<XcmBridgeHubRouter>(
521+
MultiLocation::new(
522+
2,
523+
X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(1000))
524+
),
525+
vec![ClearOrigin].into(),
526+
)
527+
.map(drop));
502528

503529
assert!(TestToBridgeHubSender::is_message_sent());
504530
assert_eq!(old_bridge, XcmBridgeHubRouter::bridge());
@@ -511,17 +537,14 @@ mod tests {
511537
TestWithBridgeHubChannel::make_congested();
512538

513539
let old_bridge = XcmBridgeHubRouter::bridge();
514-
assert_eq!(
515-
send_xcm::<XcmBridgeHubRouter>(
516-
MultiLocation::new(
517-
2,
518-
X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(1000))
519-
),
520-
vec![ClearOrigin].into(),
521-
)
522-
.map(drop),
523-
Ok(()),
524-
);
540+
assert_ok!(send_xcm::<XcmBridgeHubRouter>(
541+
MultiLocation::new(
542+
2,
543+
X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(1000))
544+
),
545+
vec![ClearOrigin].into(),
546+
)
547+
.map(drop));
525548

526549
assert!(TestToBridgeHubSender::is_message_sent());
527550
assert!(
@@ -536,17 +559,14 @@ mod tests {
536559
Bridge::<TestRuntime, ()>::put(congested_bridge(MINIMAL_DELIVERY_FEE_FACTOR));
537560

538561
let old_bridge = XcmBridgeHubRouter::bridge();
539-
assert_eq!(
540-
send_xcm::<XcmBridgeHubRouter>(
541-
MultiLocation::new(
542-
2,
543-
X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(1000))
544-
),
545-
vec![ClearOrigin].into(),
546-
)
547-
.map(drop),
548-
Ok(()),
549-
);
562+
assert_ok!(send_xcm::<XcmBridgeHubRouter>(
563+
MultiLocation::new(
564+
2,
565+
X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(1000))
566+
),
567+
vec![ClearOrigin].into(),
568+
)
569+
.map(drop));
550570

551571
assert!(TestToBridgeHubSender::is_message_sent());
552572
assert!(

0 commit comments

Comments
 (0)