Skip to content

Commit

Permalink
Add some tests to check integrity of chain constants + bridge configu…
Browse files Browse the repository at this point in the history
…ration (paritytech#1316)

* add some tests to check integrity of chain constants + bridge configuration

* try to use named parameters where possible
  • Loading branch information
svyatonik authored and serban300 committed Apr 9, 2024
1 parent 229a736 commit c41569e
Show file tree
Hide file tree
Showing 10 changed files with 550 additions and 91 deletions.
4 changes: 4 additions & 0 deletions bridges/bin/millau/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-version = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }

[dev-dependencies]
bridge-runtime-common = { path = "../../runtime-common", features = ["integrity-test"] }
static_assertions = "1.1"

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }

Expand Down
43 changes: 0 additions & 43 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -951,49 +951,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use bp_runtime::Chain;
use bridge_runtime_common::messages;

#[test]
fn ensure_millau_message_lane_weights_are_correct() {
type Weights = pallet_bridge_messages::weights::MillauWeight<Runtime>;

pallet_bridge_messages::ensure_weights_are_correct::<Weights>(
bp_millau::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT,
bp_millau::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT,
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
bp_millau::PAY_INBOUND_DISPATCH_FEE_WEIGHT,
DbWeight::get(),
);

let max_incoming_message_proof_size = bp_rialto::EXTRA_STORAGE_PROOF_SIZE.saturating_add(
messages::target::maximal_incoming_message_size(bp_millau::Millau::max_extrinsic_size()),
);
pallet_bridge_messages::ensure_able_to_receive_message::<Weights>(
bp_millau::Millau::max_extrinsic_size(),
bp_millau::Millau::max_extrinsic_weight(),
max_incoming_message_proof_size,
messages::target::maximal_incoming_message_dispatch_weight(
bp_millau::Millau::max_extrinsic_weight(),
),
);

let max_incoming_inbound_lane_data_proof_size =
bp_messages::InboundLaneData::<()>::encoded_size_hint(
bp_millau::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX as _,
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX as _,
)
.unwrap_or(u32::MAX);
pallet_bridge_messages::ensure_able_to_receive_confirmation::<Weights>(
bp_millau::Millau::max_extrinsic_size(),
bp_millau::Millau::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
DbWeight::get(),
);
}

#[test]
fn call_size() {
Expand Down
104 changes: 104 additions & 0 deletions bridges/bin/millau/runtime/src/rialto_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,107 @@ impl MessagesParameter for MillauToRialtoMessagesParameter {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{DbWeight, RialtoGrandpaInstance, Runtime, WithRialtoMessagesInstance};

use bp_runtime::Chain;
use bridge_runtime_common::{
assert_complete_bridge_types,
integrity::{
assert_complete_bridge_constants, AssertBridgeMessagesPalletConstants,
AssertBridgePalletNames, AssertChainConstants, AssertCompleteBridgeConstants,
},
messages,
};

#[test]
fn ensure_millau_message_lane_weights_are_correct() {
type Weights = pallet_bridge_messages::weights::MillauWeight<Runtime>;

pallet_bridge_messages::ensure_weights_are_correct::<Weights>(
bp_millau::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT,
bp_millau::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT,
bp_millau::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
bp_millau::PAY_INBOUND_DISPATCH_FEE_WEIGHT,
DbWeight::get(),
);

let max_incoming_message_proof_size = bp_rialto::EXTRA_STORAGE_PROOF_SIZE.saturating_add(
messages::target::maximal_incoming_message_size(bp_millau::Millau::max_extrinsic_size()),
);
pallet_bridge_messages::ensure_able_to_receive_message::<Weights>(
bp_millau::Millau::max_extrinsic_size(),
bp_millau::Millau::max_extrinsic_weight(),
max_incoming_message_proof_size,
messages::target::maximal_incoming_message_dispatch_weight(
bp_millau::Millau::max_extrinsic_weight(),
),
);

let max_incoming_inbound_lane_data_proof_size =
bp_messages::InboundLaneData::<()>::encoded_size_hint(
bp_millau::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX as _,
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX as _,
)
.unwrap_or(u32::MAX);
pallet_bridge_messages::ensure_able_to_receive_confirmation::<Weights>(
bp_millau::Millau::max_extrinsic_size(),
bp_millau::Millau::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
DbWeight::get(),
);
}

#[test]
fn ensure_bridge_integrity() {
assert_complete_bridge_types!(
runtime: Runtime,
with_bridged_chain_grandpa_instance: RialtoGrandpaInstance,
with_bridged_chain_messages_instance: WithRialtoMessagesInstance,
bridge: WithRialtoMessageBridge,
this_chain: bp_millau::Millau,
bridged_chain: bp_rialto::Rialto,
this_chain_account_id_converter: bp_millau::AccountIdConverter
);

assert_complete_bridge_constants::<
Runtime,
RialtoGrandpaInstance,
WithRialtoMessagesInstance,
WithRialtoMessageBridge,
bp_millau::Millau,
>(AssertCompleteBridgeConstants {
this_chain_constants: AssertChainConstants {
block_length: bp_millau::BlockLength::get(),
block_weights: bp_millau::BlockWeights::get(),
},
messages_pallet_constants: AssertBridgeMessagesPalletConstants {
max_unrewarded_relayers_in_bridged_confirmation_tx:
bp_rialto::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
max_unconfirmed_messages_in_bridged_confirmation_tx:
bp_rialto::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
bridged_chain_id: bp_runtime::RIALTO_CHAIN_ID,
},
pallet_names: AssertBridgePalletNames {
with_this_chain_messages_pallet_name: bp_millau::WITH_MILLAU_MESSAGES_PALLET_NAME,
with_bridged_chain_grandpa_pallet_name: bp_rialto::WITH_RIALTO_GRANDPA_PALLET_NAME,
with_bridged_chain_messages_pallet_name:
bp_rialto::WITH_RIALTO_MESSAGES_PALLET_NAME,
},
});

assert_eq!(
RialtoToMillauConversionRate::key().to_vec(),
bp_runtime::storage_parameter_key(
bp_millau::RIALTO_TO_MILLAU_CONVERSION_RATE_PARAMETER_NAME
)
.0,
);
}
}
2 changes: 2 additions & 0 deletions bridges/bin/rialto/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ polkadot-runtime-common = { git = "https://github.com/paritytech/polkadot", bran
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "master", default-features = false }

[dev-dependencies]
bridge-runtime-common = { path = "../../runtime-common", features = ["integrity-test"] }
libsecp256k1 = { version = "0.7", features = ["hmac"] }
static_assertions = "1.1"

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
43 changes: 0 additions & 43 deletions bridges/bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,49 +936,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use bp_runtime::Chain;
use bridge_runtime_common::messages;

#[test]
fn ensure_rialto_message_lane_weights_are_correct() {
type Weights = pallet_bridge_messages::weights::MillauWeight<Runtime>;

pallet_bridge_messages::ensure_weights_are_correct::<Weights>(
bp_rialto::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT,
bp_rialto::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT,
bp_rialto::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
bp_rialto::PAY_INBOUND_DISPATCH_FEE_WEIGHT,
DbWeight::get(),
);

let max_incoming_message_proof_size = bp_millau::EXTRA_STORAGE_PROOF_SIZE.saturating_add(
messages::target::maximal_incoming_message_size(bp_rialto::Rialto::max_extrinsic_size()),
);
pallet_bridge_messages::ensure_able_to_receive_message::<Weights>(
bp_rialto::Rialto::max_extrinsic_size(),
bp_rialto::Rialto::max_extrinsic_weight(),
max_incoming_message_proof_size,
messages::target::maximal_incoming_message_dispatch_weight(
bp_rialto::Rialto::max_extrinsic_weight(),
),
);

let max_incoming_inbound_lane_data_proof_size =
bp_messages::InboundLaneData::<()>::encoded_size_hint(
bp_rialto::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
bp_rialto::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX as _,
bp_rialto::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX as _,
)
.unwrap_or(u32::MAX);
pallet_bridge_messages::ensure_able_to_receive_confirmation::<Weights>(
bp_rialto::Rialto::max_extrinsic_size(),
bp_rialto::Rialto::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
bp_rialto::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
bp_rialto::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
DbWeight::get(),
);
}

#[test]
fn call_size() {
Expand Down
104 changes: 100 additions & 4 deletions bridges/bin/rialto/runtime/src/millau_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,23 @@ impl MessagesParameter for RialtoToMillauMessagesParameter {
#[cfg(test)]
mod tests {
use super::*;
use crate::{AccountId, Call, ExistentialDeposit, Runtime, SystemCall, SystemConfig, VERSION};
use crate::{
AccountId, Call, DbWeight, ExistentialDeposit, MillauGrandpaInstance, Runtime, SystemCall,
SystemConfig, WithMillauMessagesInstance, VERSION,
};
use bp_message_dispatch::CallOrigin;
use bp_messages::{
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
MessageKey,
};
use bp_runtime::{derive_account_id, messages::DispatchFeePayment, SourceAccount};
use bridge_runtime_common::messages::target::{
FromBridgedChainEncodedMessageCall, FromBridgedChainMessagePayload,
use bp_runtime::{derive_account_id, messages::DispatchFeePayment, Chain, SourceAccount};
use bridge_runtime_common::{
assert_complete_bridge_types,
integrity::{
assert_complete_bridge_constants, AssertBridgeMessagesPalletConstants,
AssertBridgePalletNames, AssertChainConstants, AssertCompleteBridgeConstants,
},
messages::target::{FromBridgedChainEncodedMessageCall, FromBridgedChainMessagePayload},
};
use frame_support::{
traits::Currency,
Expand Down Expand Up @@ -377,4 +385,92 @@ mod tests {
);
});
}

#[test]
fn ensure_rialto_message_lane_weights_are_correct() {
type Weights = pallet_bridge_messages::weights::MillauWeight<Runtime>;

pallet_bridge_messages::ensure_weights_are_correct::<Weights>(
bp_rialto::DEFAULT_MESSAGE_DELIVERY_TX_WEIGHT,
bp_rialto::ADDITIONAL_MESSAGE_BYTE_DELIVERY_WEIGHT,
bp_rialto::MAX_SINGLE_MESSAGE_DELIVERY_CONFIRMATION_TX_WEIGHT,
bp_rialto::PAY_INBOUND_DISPATCH_FEE_WEIGHT,
DbWeight::get(),
);

let max_incoming_message_proof_size = bp_millau::EXTRA_STORAGE_PROOF_SIZE.saturating_add(
messages::target::maximal_incoming_message_size(bp_rialto::Rialto::max_extrinsic_size()),
);
pallet_bridge_messages::ensure_able_to_receive_message::<Weights>(
bp_rialto::Rialto::max_extrinsic_size(),
bp_rialto::Rialto::max_extrinsic_weight(),
max_incoming_message_proof_size,
messages::target::maximal_incoming_message_dispatch_weight(
bp_rialto::Rialto::max_extrinsic_weight(),
),
);

let max_incoming_inbound_lane_data_proof_size =
bp_messages::InboundLaneData::<()>::encoded_size_hint(
bp_rialto::MAXIMAL_ENCODED_ACCOUNT_ID_SIZE,
bp_rialto::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX as _,
bp_rialto::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX as _,
)
.unwrap_or(u32::MAX);
pallet_bridge_messages::ensure_able_to_receive_confirmation::<Weights>(
bp_rialto::Rialto::max_extrinsic_size(),
bp_rialto::Rialto::max_extrinsic_weight(),
max_incoming_inbound_lane_data_proof_size,
bp_rialto::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
bp_rialto::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
DbWeight::get(),
);
}

#[test]
fn ensure_bridge_integrity() {
assert_complete_bridge_types!(
runtime: Runtime,
with_bridged_chain_grandpa_instance: MillauGrandpaInstance,
with_bridged_chain_messages_instance: WithMillauMessagesInstance,
bridge: WithMillauMessageBridge,
this_chain: bp_rialto::Rialto,
bridged_chain: bp_millau::Millau,
this_chain_account_id_converter: bp_rialto::AccountIdConverter
);

assert_complete_bridge_constants::<
Runtime,
MillauGrandpaInstance,
WithMillauMessagesInstance,
WithMillauMessageBridge,
bp_rialto::Rialto,
>(AssertCompleteBridgeConstants {
this_chain_constants: AssertChainConstants {
block_length: bp_rialto::BlockLength::get(),
block_weights: bp_rialto::BlockWeights::get(),
},
messages_pallet_constants: AssertBridgeMessagesPalletConstants {
max_unrewarded_relayers_in_bridged_confirmation_tx:
bp_millau::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX,
max_unconfirmed_messages_in_bridged_confirmation_tx:
bp_millau::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX,
bridged_chain_id: bp_runtime::MILLAU_CHAIN_ID,
},
pallet_names: AssertBridgePalletNames {
with_this_chain_messages_pallet_name: bp_rialto::WITH_RIALTO_MESSAGES_PALLET_NAME,
with_bridged_chain_grandpa_pallet_name: bp_millau::WITH_MILLAU_GRANDPA_PALLET_NAME,
with_bridged_chain_messages_pallet_name:
bp_millau::WITH_MILLAU_MESSAGES_PALLET_NAME,
},
});

assert_eq!(
MillauToRialtoConversionRate::key().to_vec(),
bp_runtime::storage_parameter_key(
bp_rialto::MILLAU_TO_RIALTO_CONVERSION_RATE_PARAMETER_NAME
)
.0,
);
}
}
5 changes: 5 additions & 0 deletions bridges/bin/runtime-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ codec = { package = "parity-scale-codec", version = "2.2.0", default-features =
ed25519-dalek = { version = "1.0", default-features = false, optional = true }
hash-db = { version = "0.15.2", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
static_assertions = { version = "1.1", optional = true }

# Bridge dependencies

Expand Down Expand Up @@ -64,3 +65,7 @@ runtime-benchmarks = [
"sp-state-machine",
"sp-version",
]
integrity-test = [
"frame-system",
"static_assertions",
]
Loading

0 comments on commit c41569e

Please sign in to comment.