Skip to content

Commit

Permalink
Pokladot sdk backport from staging (#2746) (#2748)
Browse files Browse the repository at this point in the history
* Pokladot sdk backport from staging (#2746)

* fmt

---------

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Svyatoslav Nikolsky <[email protected]>
  • Loading branch information
3 people committed May 7, 2024
1 parent 24285a5 commit 5f631da
Show file tree
Hide file tree
Showing 57 changed files with 459 additions and 185 deletions.
321 changes: 160 additions & 161 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,34 @@ members = [
"relays/parachains",
"relays/utils",
]

# Setup clippy lints as `polkadot-sdk`,
# but let's better be strict here and disable what `polkadot-sdk` allows :)

[workspace.lints.rust]
# suspicious_double_ref_op = { level = "allow", priority = 2 }

[workspace.lints.clippy]
all = { level = "allow", priority = 0 }
correctness = { level = "deny", priority = 1 }
#if-same-then-else = { level = "allow", priority = 2 }
complexity = { level = "deny", priority = 1 }
#zero-prefixed-literal = { level = "allow", priority = 2 } # 00_1000_000
#type_complexity = { level = "allow", priority = 2 } # raison d'etre
#nonminimal-bool = { level = "allow", priority = 2 } # maybe
#borrowed-box = { level = "allow", priority = 2 } # Reasonable to fix this one
#too-many-arguments = { level = "allow", priority = 2 } # (Turning this on would lead to)
#unnecessary_cast = { level = "allow", priority = 2 } # Types may change
#identity-op = { level = "allow", priority = 2 } # One case where we do 0 +
#useless_conversion = { level = "allow", priority = 2 } # Types may change
#unit_arg = { level = "allow", priority = 2 } # styalistic.
#option-map-unit-fn = { level = "allow", priority = 2 } # styalistic
#bind_instead_of_map = { level = "allow", priority = 2 } # styalistic
#erasing_op = { level = "allow", priority = 2 } # E.g. 0 * DOLLARS
#eq_op = { level = "allow", priority = 2 } # In tests we test equality.
#while_immutable_condition = { level = "allow", priority = 2 } # false positives
#needless_option_as_deref = { level = "allow", priority = 2 } # false positives
#derivable_impls = { level = "allow", priority = 2 } # false positives
#stable_sort_primitive = { level = "allow", priority = 2 } # prefer stable sort
#extra-unused-type-parameters = { level = "allow", priority = 2 } # stylistic
#default_constructed_unit_structs = { level = "allow", priority = 2 } # stylistic
3 changes: 3 additions & 0 deletions bin/runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ edition.workspace = true
repository.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive"] }
hash-db = { version = "0.16.0", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions modules/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
log = { version = "0.4.20", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions modules/grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
3 changes: 3 additions & 0 deletions modules/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
log = { version = "0.4.19", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions modules/parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
log = { version = "0.4.19", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions modules/relayers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
log = { version = "0.4.19", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions modules/xcm-bridge-hub-router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
log = { version = "0.4.19", default-features = false }
Expand Down
38 changes: 33 additions & 5 deletions modules/xcm-bridge-hub-router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub mod pallet {
/// **possible fee**. Allows to externalize better control over allowed **bridged
/// networks/locations**.
type Bridges: ExporterFor;
/// Checks the XCM version for the destination.
type DestinationVersion: GetVersion;

/// Actual message sender (`HRMP` or `DMP`) to the sibling bridge hub location.
type ToBridgeHubSender: SendXcm;
Expand Down Expand Up @@ -312,19 +314,32 @@ impl<T: Config<I>, I: 'static> SendXcm for Pallet<T, I> {
dest: &mut Option<MultiLocation>,
xcm: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
// we won't have an access to `dest` and `xcm` in the `delvier` method, so precompute
// `dest` and `xcm` are required here
let dest_ref = dest.as_ref().ok_or(SendError::MissingArgument)?;
let xcm_ref = xcm.as_ref().ok_or(SendError::MissingArgument)?;

// we won't have an access to `dest` and `xcm` in the `deliver` method, so precompute
// everything required here
let message_size = xcm
.as_ref()
.map(|xcm| xcm.encoded_size() as _)
.ok_or(SendError::MissingArgument)?;
let message_size = xcm_ref.encoded_size() as _;

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

// We need to ensure that the known `dest`'s XCM version can comprehend the current `xcm`
// program. This may seem like an additional, unnecessary check, but it is not. A similar
// check is probably performed by the `ViaBridgeHubExporter`, which attempts to send a
// versioned message to the sibling bridge hub. However, the local bridge hub may have a
// higher XCM version than the remote `dest`. Once again, it is better to discard such
// messages here than at the bridge hub (e.g., to avoid losing funds).
let destination_version = T::DestinationVersion::get_version_for(dest_ref)
.ok_or(SendError::DestinationUnsupported)?;
let _ = VersionedXcm::from(xcm_ref.clone())
.into_version(destination_version)
.map_err(|()| SendError::DestinationUnsupported)?;

// just use exporter to validate destination and insert instructions to pay message fee
// at the sibling/child bridge hub
//
Expand Down Expand Up @@ -418,6 +433,19 @@ mod tests {
});
}

#[test]
fn destination_unsupported_if_wrap_version_fails() {
run_test(|| {
assert_eq!(
send_xcm::<XcmBridgeHubRouter>(
UnknownXcmVersionLocation::get(),
vec![ClearOrigin].into(),
),
Err(SendError::DestinationUnsupported),
);
});
}

#[test]
fn returns_proper_delivery_price() {
run_test(|| {
Expand Down
20 changes: 19 additions & 1 deletion modules/xcm-bridge-hub-router/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
use crate as pallet_xcm_bridge_hub_router;

use bp_xcm_bridge_hub::{BridgeId, LocalXcmChannelManager};
use frame_support::{construct_runtime, derive_impl, parameter_types};
use frame_support::{
construct_runtime, derive_impl, parameter_types,
traits::{Contains, Equals},
};
use sp_runtime::{traits::ConstU128, BuildStorage};
use xcm::prelude::*;
use xcm_builder::{NetworkExportTable, NetworkExportTableItem};
Expand Down Expand Up @@ -56,6 +59,7 @@ parameter_types! {
Some((BridgeFeeAsset::get(), BASE_FEE).into())
)
];
pub UnknownXcmVersionLocation: MultiLocation = MultiLocation::new(2, X2(GlobalConsensus(BridgedNetworkId::get()), Parachain(9999)));
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
Expand All @@ -70,6 +74,8 @@ impl pallet_xcm_bridge_hub_router::Config<()> for TestRuntime {
type SiblingBridgeHubLocation = SiblingBridgeHubLocation;
type BridgedNetworkId = BridgedNetworkId;
type Bridges = NetworkExportTable<BridgeTable>;
type DestinationVersion =
LatestOrNoneForLocationVersionChecker<Equals<UnknownXcmVersionLocation>>;

type ToBridgeHubSender = TestToBridgeHubSender;
type LocalXcmChannelManager = TestLocalXcmChannelManager;
Expand All @@ -78,6 +84,18 @@ impl pallet_xcm_bridge_hub_router::Config<()> for TestRuntime {
type FeeAsset = BridgeFeeAsset;
}

pub struct LatestOrNoneForLocationVersionChecker<Location>(sp_std::marker::PhantomData<Location>);
impl<Location: Contains<MultiLocation>> GetVersion
for LatestOrNoneForLocationVersionChecker<Location>
{
fn get_version_for(dest: &MultiLocation) -> Option<XcmVersion> {
if Location::contains(dest) {
return None
}
Some(XCM_VERSION)
}
}

pub struct TestToBridgeHubSender;

impl TestToBridgeHubSender {
Expand Down
3 changes: 3 additions & 0 deletions modules/xcm-bridge-hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ version = "0.1.0"
authors.workspace = true
edition.workspace = true

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
log = { version = "0.4.19", default-features = false }
Expand Down
3 changes: 2 additions & 1 deletion modules/xcm-bridge-hub/src/exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ const OUTBOUND_LANE_UNCONGESTED_THRESHOLD: MessageNonce = 1_024;
/// An easy way to access `HaulBlobExporter`.
pub type PalletAsHaulBlobExporter<T, I> = HaulBlobExporter<
DummyHaulBlob,
<T as Config<I>>::BridgedNetworkId,
<T as Config<I>>::BridgedNetwork,
<T as Config<I>>::DestinationVersion,
<T as Config<I>>::MessageExportPrice,
>;
/// An easy way to access associated messages pallet.
Expand Down
33 changes: 28 additions & 5 deletions modules/xcm-bridge-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub const LOG_TARGET: &str = "runtime::bridge-xcm";
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use frame_system::pallet_prelude::{BlockNumberFor, OriginFor};

#[pallet::config]
#[pallet::disable_frame_system_supertrait_check]
Expand All @@ -100,9 +100,9 @@ pub mod pallet {
// TODO: https://github.com/paritytech/parity-bridges-common/issues/1666 remove `ChainId` and
// replace it with the `NetworkId` - then we'll be able to use
// `T as pallet_bridge_messages::Config<T::BridgeMessagesPalletInstance>::BridgedChain::NetworkId`
/// Bridged network id.
/// Bridged network as relative location of bridged `GlobalConsensus`.
#[pallet::constant]
type BridgedNetworkId: Get<NetworkId>;
type BridgedNetwork: Get<MultiLocation>;
/// Associated messages pallet instance that bridges us with the
/// `BridgedNetworkId` consensus.
type BridgeMessagesPalletInstance: 'static;
Expand Down Expand Up @@ -132,6 +132,8 @@ pub mod pallet {
type BlobDispatcher: DispatchBlob;
/// Price of single message export to the bridged consensus (`Self::BridgedNetworkId`).
type MessageExportPrice: Get<MultiAssets>;
/// Checks the XCM version for the destination.
type DestinationVersion: GetVersion;
}

/// An alias for the bridge metadata.
Expand All @@ -158,7 +160,7 @@ pub mod pallet {
///
/// The caller must be within the `T::OpenBridgeOrigin` filter (presumably: a sibling
/// parachain or a parent relay chain). The `bridge_destination_universal_location` must be
/// a destination within the consensus of the `T::BridgedNetworkId` network.
/// a destination within the consensus of the `T::BridgedNetwork` network.
///
/// The `BridgeReserve` amount is reserved on the caller account. This reserve
/// is unreserved after bridge is closed.
Expand Down Expand Up @@ -354,7 +356,26 @@ pub mod pallet {
}
}

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn integrity_test() {
assert!(
Self::bridged_network_id().is_some(),
"Configured `T::BridgedNetwork`: {:?} does not contain `GlobalConsensus` junction with `NetworkId`",
T::BridgedNetwork::get()
)
}
}

impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Returns some `NetworkId` if contains `GlobalConsensus` junction.
fn bridged_network_id() -> Option<NetworkId> {
match T::BridgedNetwork::get().take_first_interior() {
Some(GlobalConsensus(network)) => Some(network),
_ => None,
}
}

/// Return bridge endpoint locations and dedicated lane identifier. This method converts
/// runtime `origin` argument to relative `MultiLocation` using the `T::OpenBridgeOrigin`
/// converter.
Expand All @@ -381,7 +402,7 @@ pub mod pallet {
.try_into()
.map_err(|_| Error::<T, I>::UnsupportedXcmVersion)?,
),
T::BridgedNetworkId::get(),
Self::bridged_network_id().ok_or_else(|| Error::<T, I>::InvalidBridgedNetwork)?,
)
.map_err(|e| Error::<T, I>::BridgeLocations(e).into())
}
Expand Down Expand Up @@ -491,6 +512,8 @@ pub mod pallet {
FailedToReserveBridgeReserve,
/// The version of XCM location argument is unsupported.
UnsupportedXcmVersion,
/// Invalid `T::BridgedNetwork` configuration - missing `GlobalConsensus` junction.
InvalidBridgedNetwork,
}
}

Expand Down
7 changes: 6 additions & 1 deletion modules/xcm-bridge-hub/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ impl pallet_bridge_messages::WeightInfoExt for TestMessagesWeights {
parameter_types! {
pub const RelayNetwork: NetworkId = NetworkId::Kusama;
pub const BridgedRelayNetwork: NetworkId = NetworkId::Polkadot;
pub const BridgedRelayNetworkLocation: MultiLocation = MultiLocation {
parents: 1,
interior: X1(GlobalConsensus(BridgedRelayNetwork::get()))
};
pub const NonBridgedRelayNetwork: NetworkId = NetworkId::Rococo;
pub const BridgeReserve: Balance = 100_000;
pub UniversalLocation: InteriorMultiLocation = X2(
Expand Down Expand Up @@ -230,7 +234,7 @@ impl pallet_xcm_bridge_hub::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;

type UniversalLocation = UniversalLocation;
type BridgedNetworkId = BridgedRelayNetwork;
type BridgedNetwork = BridgedRelayNetworkLocation;
type BridgeMessagesPalletInstance = ();

type OpenBridgeOrigin = OpenBridgeOrigin;
Expand All @@ -242,6 +246,7 @@ impl pallet_xcm_bridge_hub::Config for TestRuntime {
type LocalXcmChannelManager = TestLocalXcmChannelManager;
type BlobDispatcher = TestBlobDispatcher;
type MessageExportPrice = ();
type DestinationVersion = AlwaysLatest;
}

pub struct TestLocalXcmChannelManager;
Expand Down
3 changes: 3 additions & 0 deletions primitives/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "bit-vec"] }
scale-info = { version = "2.10.0", default-features = false, features = ["bit-vec", "derive", "serde"] }
Expand Down
3 changes: 3 additions & 0 deletions primitives/chain-asset-hub-rococo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
scale-info = { version = "2.9.0", default-features = false, features = ["derive"] }
Expand Down
3 changes: 3 additions & 0 deletions primitives/chain-asset-hub-westend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
scale-info = { version = "2.10.0", default-features = false, features = ["derive"] }
Expand Down
3 changes: 3 additions & 0 deletions primitives/chain-bridge-hub-cumulus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors.workspace = true
edition.workspace = true
license = "GPL-3.0-or-later WITH Classpath-exception-2.0"

[lints]
workspace = true

[dependencies]
# Bridge Dependencies

Expand Down
Loading

0 comments on commit 5f631da

Please sign in to comment.