Skip to content

Commit

Permalink
Moving messages code to pallet (#2180)
Browse files Browse the repository at this point in the history
* moved files around

* and fix compilation

Move Chain::ID from relay-level Chain to primitives-level Chain (#2181)

* move Chain::ID from relay-level Chain to primitives-level Chain

* removed chain IDs from bp-runtime

* add missing file header

Adjust weights (#2185)

fix clippy (#2186)

Remove source header chain (#2183)

* moved messages proof verification to messages pallet

* removed SourceHeaderChain

* cleanup

* benchmarks compilation

* fix benchmark tests compilation

* clippy

* fmt

Use compact proofs for messages delivery confirmation (#2187)

* Use compact proofs for messages delivery confirmation

* Fix benchmarks

Move messages delivery proof verification to pallet (#2189)

* rename messages_proof.rs to proofs.rs

* moved delivery proof verification to the messages pallet

* removed TargetHeaderChain

* cleaning up

* fixed benchmarks compilation

* Update modules/messages/README.md

Co-authored-by: Adrian Catangiu <adrian@parity.io>

* uncommented test and removed printlns

* vec![].into_iter().collect() -> vec![].into()

---------

Co-authored-by: Adrian Catangiu <adrian@parity.io>

removed MaxUnrewardedRelayerEntriesAtInboundLane and MaxUnconfirmedMessagesAtInboundLane from messages pallet config (#2190)

* removed MaxUnrewardedRelayerEntriesAtInboundLane and MaxUnconfirmedMessagesAtInboundLane from messages pallet config

* fixed doc

* fixed benchmarking code

* more fixes

remove MaximalOutboundPayloadSize and use BridgedChain::maximal_incomging_message_size instead (#2191)

remove MessageBridge trait (#2192)
  • Loading branch information
svyatonik authored and bkontur committed Jun 5, 2024
1 parent 161e5ca commit e049419
Show file tree
Hide file tree
Showing 34 changed files with 3,637 additions and 2,776 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bridges/bin/runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ xcm-builder = { package = "staging-xcm-builder", path = "../../../polkadot/xcm/x
[dev-dependencies]
bp-test-utils = { path = "../../primitives/test-utils" }
pallet-balances = { path = "../../../substrate/frame/balances" }
pallet-bridge-messages = { path = "../../modules/messages", features = ["std", "test-helpers"] }

[features]
default = ["std"]
Expand Down Expand Up @@ -93,6 +94,7 @@ runtime-benchmarks = [
"pallet-balances/runtime-benchmarks",
"pallet-bridge-grandpa/runtime-benchmarks",
"pallet-bridge-messages/runtime-benchmarks",
"pallet-bridge-messages/test-helpers",
"pallet-bridge-parachains/runtime-benchmarks",
"pallet-bridge-relayers/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ mod integrity_tests {
pub mod per_message {
use super::*;

use bp_messages::ChainWithMessages;
use pallet_bridge_messages::WeightInfoExt;

/// Ensures that the value of `PriorityBoostPerMessage` matches the value of
Expand All @@ -339,7 +340,7 @@ mod integrity_tests {
BalanceOf<Runtime>: Send + Sync + FixedPointOperand,
{
let maximal_messages_in_delivery_transaction =
Runtime::MaxUnconfirmedMessagesAtInboundLane::get();
Runtime::BridgedChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX;
super::ensure_priority_boost_is_sane::<PriorityBoostPerMessage, BalanceOf<Runtime>>(
"PriorityBoostPerMessage",
maximal_messages_in_delivery_transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
use crate::messages_call_ext::{
CallHelper as MessagesCallHelper, CallInfo as MessagesCallInfo, MessagesCallSubType,
};
use bp_messages::{LaneId, MessageNonce};
use bp_messages::{ChainWithMessages, LaneId, MessageNonce};
use bp_relayers::{ExplicitOrAccountParams, RewardsAccountOwner, RewardsAccountParams};
use bp_runtime::{Parachain, RangeInclusiveExt, StaticStrProvider};
use bp_runtime::{Chain, Parachain, RangeInclusiveExt, StaticStrProvider};
use codec::{Codec, Decode, Encode};
use frame_support::{
dispatch::{CallableCallFor, DispatchInfo, PostDispatchInfo},
Expand Down Expand Up @@ -293,7 +293,7 @@ pub trait RefundSignedExtension:
<Self::Msgs as RefundableMessagesLaneId>::Id::get(),
<Self::Runtime as MessagesConfig<
<Self::Msgs as RefundableMessagesLaneId>::Instance,
>>::BridgedChainId::get(),
>>::BridgedChain::ID,
if call_info.is_receive_messages_proof_call() {
RewardsAccountOwner::ThisChain
} else {
Expand Down Expand Up @@ -406,8 +406,7 @@ pub trait RefundSignedExtension:
// a quick check to avoid invalid high-priority transactions
let max_unconfirmed_messages_in_confirmation_tx = <Self::Runtime as MessagesConfig<
<Self::Msgs as RefundableMessagesLaneId>::Instance,
>>::MaxUnconfirmedMessagesAtInboundLane::get(
);
>>::BridgedChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX;
if bundled_messages > max_unconfirmed_messages_in_confirmation_tx {
return None
}
Expand Down Expand Up @@ -1163,7 +1162,7 @@ pub(crate) mod tests {
RuntimeCall::BridgeMessages(MessagesCall::receive_messages_delivery_proof {
proof: FromBridgedChainMessagesDeliveryProof {
bridged_header_hash: Default::default(),
storage_proof: vec![],
storage_proof: Default::default(),
lane: TestLaneId::get(),
},
relayers_state: UnrewardedRelayersState {
Expand Down Expand Up @@ -1326,8 +1325,10 @@ pub(crate) mod tests {
best_stored_nonce: 100,
},
unrewarded_relayers: UnrewardedRelayerOccupation {
free_relayer_slots: MaxUnrewardedRelayerEntriesAtInboundLane::get(),
free_message_slots: MaxUnconfirmedMessagesAtInboundLane::get(),
free_relayer_slots:
BridgedUnderlyingChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
free_message_slots:
BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
},
}),
),
Expand Down Expand Up @@ -1396,8 +1397,10 @@ pub(crate) mod tests {
best_stored_nonce: 100,
},
unrewarded_relayers: UnrewardedRelayerOccupation {
free_relayer_slots: MaxUnrewardedRelayerEntriesAtInboundLane::get(),
free_message_slots: MaxUnconfirmedMessagesAtInboundLane::get(),
free_relayer_slots:
BridgedUnderlyingChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
free_message_slots:
BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
},
}),
),
Expand Down Expand Up @@ -1458,8 +1461,10 @@ pub(crate) mod tests {
best_stored_nonce: 100,
},
unrewarded_relayers: UnrewardedRelayerOccupation {
free_relayer_slots: MaxUnrewardedRelayerEntriesAtInboundLane::get(),
free_message_slots: MaxUnconfirmedMessagesAtInboundLane::get(),
free_relayer_slots:
BridgedUnderlyingChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
free_message_slots:
BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
},
}),
),
Expand Down Expand Up @@ -1498,8 +1503,10 @@ pub(crate) mod tests {
best_stored_nonce: 100,
},
unrewarded_relayers: UnrewardedRelayerOccupation {
free_relayer_slots: MaxUnrewardedRelayerEntriesAtInboundLane::get(),
free_message_slots: MaxUnconfirmedMessagesAtInboundLane::get(),
free_relayer_slots:
BridgedUnderlyingChain::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
free_message_slots:
BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
},
},
)),
Expand Down Expand Up @@ -1734,14 +1741,16 @@ pub(crate) mod tests {

let fns = [run_validate, run_grandpa_validate, run_messages_validate];
for f in fns {
let priority_of_max_messages_delivery =
f(message_delivery_call(100 + MaxUnconfirmedMessagesAtInboundLane::get()))
.unwrap()
.priority;
let priority_of_more_than_max_messages_delivery =
f(message_delivery_call(100 + MaxUnconfirmedMessagesAtInboundLane::get() + 1))
.unwrap()
.priority;
let priority_of_max_messages_delivery = f(message_delivery_call(
100 + BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
))
.unwrap()
.priority;
let priority_of_more_than_max_messages_delivery = f(message_delivery_call(
100 + BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX + 1,
))
.unwrap()
.priority;

assert!(
priority_of_max_messages_delivery > priority_of_more_than_max_messages_delivery,
Expand Down Expand Up @@ -2864,7 +2873,8 @@ pub(crate) mod tests {
#[test]
fn does_not_panic_on_boosting_priority_of_empty_message_delivery_transaction() {
run_test(|| {
let best_delivered_message = MaxUnconfirmedMessagesAtInboundLane::get();
let best_delivered_message =
BridgedUnderlyingChain::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX;
initialize_environment(100, 100, best_delivered_message);

// register relayer so it gets priority boost
Expand Down
94 changes: 15 additions & 79 deletions bridges/bin/runtime-common/src/integrity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
//! Most of the tests in this module assume that the bridge is using standard (see `crate::messages`
//! module for details) configuration.

use crate::{messages, messages::MessageBridge};

use bp_messages::{InboundLaneData, MessageNonce};
use bp_runtime::{Chain, ChainId};
use bp_messages::{ChainWithMessages, InboundLaneData, MessageNonce};
use codec::Encode;
use frame_support::{storage::generator::StorageValue, traits::Get, weights::Weight};
use frame_system::limits;
Expand Down Expand Up @@ -50,56 +47,32 @@ macro_rules! assert_chain_types(
}
);

/// Macro that ensures that the bridge GRANDPA pallet is configured properly to bridge with given
/// chain.
#[macro_export]
macro_rules! assert_bridge_grandpa_pallet_types(
( runtime: $r:path, with_bridged_chain_grandpa_instance: $i:path, bridged_chain: $bridged:path ) => {
{
// if one of asserts fail, then either bridge isn't configured properly (or alternatively - non-standard
// configuration is used), or something has broke existing configuration (meaning that all bridged chains
// and relays will stop functioning)
use pallet_bridge_grandpa::Config as GrandpaConfig;
use static_assertions::assert_type_eq_all;

assert_type_eq_all!(<$r as GrandpaConfig<$i>>::BridgedChain, $bridged);
}
}
);

/// Macro that ensures that the bridge messages pallet is configured properly to bridge using given
/// configuration.
#[macro_export]
macro_rules! assert_bridge_messages_pallet_types(
(
runtime: $r:path,
with_bridged_chain_messages_instance: $i:path,
bridge: $bridge:path
) => {
{
// if one of asserts fail, then either bridge isn't configured properly (or alternatively - non-standard
// configuration is used), or something has broke existing configuration (meaning that all bridged chains
// and relays will stop functioning)
use $crate::messages::{
source::{FromThisChainMessagePayload, TargetHeaderChainAdapter},
target::{FromBridgedChainMessagePayload, SourceHeaderChainAdapter},
AccountIdOf, BalanceOf, BridgedChain, ThisChain,
};
use $crate::messages_xcm_extension::XcmAsPlainPayload;
use pallet_bridge_messages::Config as MessagesConfig;
use static_assertions::assert_type_eq_all;

assert_type_eq_all!(<$r as MessagesConfig<$i>>::OutboundPayload, FromThisChainMessagePayload);
assert_type_eq_all!(<$r as MessagesConfig<$i>>::OutboundPayload, XcmAsPlainPayload);

assert_type_eq_all!(<$r as MessagesConfig<$i>>::InboundRelayer, AccountIdOf<BridgedChain<$bridge>>);

assert_type_eq_all!(<$r as MessagesConfig<$i>>::TargetHeaderChain, TargetHeaderChainAdapter<$bridge>);
assert_type_eq_all!(<$r as MessagesConfig<$i>>::SourceHeaderChain, SourceHeaderChainAdapter<$bridge>);
// TODO: https://github.com/paritytech/parity-bridges-common/issues/1666: check ThisChain, BridgedChain
// and BridgedHeaderChain types
}
}
);

/// Macro that combines four other macro calls - `assert_chain_types`, `assert_bridge_types`,
/// `assert_bridge_grandpa_pallet_types` and `assert_bridge_messages_pallet_types`. It may be used
/// and `assert_bridge_messages_pallet_types`. It may be used
/// at the chain that is implementing complete standard messages bridge (i.e. with bridge GRANDPA
/// and messages pallets deployed).
#[macro_export]
Expand All @@ -108,20 +81,13 @@ macro_rules! assert_complete_bridge_types(
runtime: $r:path,
with_bridged_chain_grandpa_instance: $gi:path,
with_bridged_chain_messages_instance: $mi:path,
bridge: $bridge:path,
this_chain: $this:path,
bridged_chain: $bridged:path,
) => {
$crate::assert_chain_types!(runtime: $r, this_chain: $this);
$crate::assert_bridge_grandpa_pallet_types!(
runtime: $r,
with_bridged_chain_grandpa_instance: $gi,
bridged_chain: $bridged
);
$crate::assert_bridge_messages_pallet_types!(
runtime: $r,
with_bridged_chain_messages_instance: $mi,
bridge: $bridge
);
}
);
Expand Down Expand Up @@ -184,20 +150,8 @@ where
);
}

/// Parameters for asserting messages pallet constants.
#[derive(Debug)]
pub struct AssertBridgeMessagesPalletConstants {
/// Maximal number of unrewarded relayer entries in a confirmation transaction at the bridged
/// chain.
pub max_unrewarded_relayers_in_bridged_confirmation_tx: MessageNonce,
/// Maximal number of unconfirmed messages in a confirmation transaction at the bridged chain.
pub max_unconfirmed_messages_in_bridged_confirmation_tx: MessageNonce,
/// Identifier of the bridged chain.
pub bridged_chain_id: ChainId,
}

/// Test that the constants, used in messages pallet configuration are valid.
pub fn assert_bridge_messages_pallet_constants<R, MI>(params: AssertBridgeMessagesPalletConstants)
pub fn assert_bridge_messages_pallet_constants<R, MI>()
where
R: pallet_bridge_messages::Config<MI>,
MI: 'static,
Expand All @@ -207,19 +161,6 @@ where
"ActiveOutboundLanes ({:?}) must not be empty",
R::ActiveOutboundLanes::get(),
);
assert!(
R::MaxUnrewardedRelayerEntriesAtInboundLane::get() <= params.max_unrewarded_relayers_in_bridged_confirmation_tx,
"MaxUnrewardedRelayerEntriesAtInboundLane ({}) must be <= than the hardcoded value for bridged chain: {}",
R::MaxUnrewardedRelayerEntriesAtInboundLane::get(),
params.max_unrewarded_relayers_in_bridged_confirmation_tx,
);
assert!(
R::MaxUnconfirmedMessagesAtInboundLane::get() <= params.max_unconfirmed_messages_in_bridged_confirmation_tx,
"MaxUnrewardedRelayerEntriesAtInboundLane ({}) must be <= than the hardcoded value for bridged chain: {}",
R::MaxUnconfirmedMessagesAtInboundLane::get(),
params.max_unconfirmed_messages_in_bridged_confirmation_tx,
);
assert_eq!(R::BridgedChainId::get(), params.bridged_chain_id);
}

/// Parameters for asserting bridge pallet names.
Expand All @@ -238,14 +179,12 @@ pub struct AssertBridgePalletNames<'a> {

/// Tests that bridge pallet names used in `construct_runtime!()` macro call are matching constants
/// from chain primitives crates.
pub fn assert_bridge_pallet_names<B, R, GI, MI>(params: AssertBridgePalletNames)
pub fn assert_bridge_pallet_names<R, GI, MI>(params: AssertBridgePalletNames)
where
B: MessageBridge,
R: pallet_bridge_grandpa::Config<GI> + pallet_bridge_messages::Config<MI>,
GI: 'static,
MI: 'static,
{
assert_eq!(B::BRIDGED_MESSAGES_PALLET_NAME, params.with_this_chain_messages_pallet_name);
assert_eq!(
pallet_bridge_grandpa::PalletOwner::<R, GI>::storage_value_final_key().to_vec(),
bp_runtime::storage_value_key(params.with_bridged_chain_grandpa_pallet_name, "PalletOwner",).0,
Expand All @@ -265,32 +204,29 @@ where
pub struct AssertCompleteBridgeConstants<'a> {
/// Parameters to assert this chain constants.
pub this_chain_constants: AssertChainConstants,
/// Parameters to assert messages pallet constants.
pub messages_pallet_constants: AssertBridgeMessagesPalletConstants,
/// Parameters to assert pallet names constants.
pub pallet_names: AssertBridgePalletNames<'a>,
}

/// All bridge-related constants tests for the complete standard messages bridge (i.e. with bridge
/// GRANDPA and messages pallets deployed).
pub fn assert_complete_bridge_constants<R, GI, MI, B>(params: AssertCompleteBridgeConstants)
pub fn assert_complete_bridge_constants<R, GI, MI>(params: AssertCompleteBridgeConstants)
where
R: frame_system::Config
+ pallet_bridge_grandpa::Config<GI>
+ pallet_bridge_messages::Config<MI>,
GI: 'static,
MI: 'static,
B: MessageBridge,
{
assert_chain_constants::<R>(params.this_chain_constants);
assert_bridge_grandpa_pallet_constants::<R, GI>();
assert_bridge_messages_pallet_constants::<R, MI>(params.messages_pallet_constants);
assert_bridge_pallet_names::<B, R, GI, MI>(params.pallet_names);
assert_bridge_messages_pallet_constants::<R, MI>();
assert_bridge_pallet_names::<R, GI, MI>(params.pallet_names);
}

/// Check that the message lane weights are correct.
pub fn check_message_lane_weights<
C: Chain,
C: ChainWithMessages,
T: frame_system::Config + pallet_bridge_messages::Config<MessagesPalletInstance>,
MessagesPalletInstance: 'static,
>(
Expand All @@ -309,13 +245,13 @@ pub fn check_message_lane_weights<
pallet_bridge_messages::ensure_weights_are_correct::<Weights<T, MessagesPalletInstance>>();

// check that weights allow us to receive messages
let max_incoming_message_proof_size = bridged_chain_extra_storage_proof_size
.saturating_add(messages::target::maximal_incoming_message_size(C::max_extrinsic_size()));
let max_incoming_message_proof_size =
bridged_chain_extra_storage_proof_size.saturating_add(C::maximal_incoming_message_size());
pallet_bridge_messages::ensure_able_to_receive_message::<Weights<T, MessagesPalletInstance>>(
C::max_extrinsic_size(),
C::max_extrinsic_weight(),
max_incoming_message_proof_size,
messages::target::maximal_incoming_message_dispatch_weight(C::max_extrinsic_weight()),
C::maximal_incoming_message_dispatch_weight(),
);

// check that weights allow us to receive delivery confirmations
Expand Down
Loading

0 comments on commit e049419

Please sign in to comment.