Skip to content

Commit

Permalink
Tests for BridgeHub(s) <> remote GRANDPA chain (#2692)
Browse files Browse the repository at this point in the history
So far the `bridge-hub-test-utils` contained a tests set for testing
BridgeHub runtime that is bridging with the remote **parachain**. But we
have #2540 coming, which
would add Rococo <> Bulletin chain bridge (where Bulletin = standalone
chain that is using GRANDPA finality). Then it'll be expanded to
Polkadot BH as well.

So this PR adds the same set of tests to the `bridge-hub-test-utils`,
but for the case when remote chain is the chain with GRANDPA finality.
There's a lot of changes in this PR - I'll describe some:
- I've added `BasicParachainRuntime` trait to decrease number of lines
we need to add to `where` clause. Could revert, but imo it is useful;
- `cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_data` is
a submodule for generating test data for the test sets.
`from_parachain.rs` is used in tests for the case when remote chain is a
parachain, `from_grandpa_chain.rs` - for the bridges with remote GRANDPA
chains. `mod.rs` has some code, shared by both types of tests;
- `cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_data` is
a submodule with all test cases. The `mod.rs` has tests, suitable for
all cases. There's also `wth_parachain.rs` and `with_grandpa_chain.rs`
with the same meaning as above;
- I've merged the "core" code of two previous tests -
`relayed_incoming_message_works` and `complex_relay_extrinsic_works`
into one single `relayed_incoming_message_works` test. So now we are
always constructing extrinsics and are dispatching them using executive
module (meaning all signed extensions are also tested).

New test set is used here:
#2540. Once this PR is
merged, I'll merge that other PR with master to remove duplicate
changes.

I'm also planning to cleanup generic constraints + remove some
unnecessary assumptions about used chains in a follow-up PRs. But for
now I think this PR has enough changes, so don't want to complicate it
even more.

---

Breaking changes for the code that have used those tests before:
- the `construct_and_apply_extrinsic` callback now accepts the
`RuntimeCall` instead of the `pallet_utility::Call`;
- the `construct_and_apply_extrinsic` now may be called multiple times
for the single test, so make sure the `frame_system::CheckNonce` is
correctly constructed;
- all previous tests have been moved from
`bridge_hub_test_utils::test_cases` to
`bridge_hub_test_utils::test_cases::from_parachain` module;
- there are several changes in test arguments - please refer to
https://github.com/paritytech/polkadot-sdk/compare/sv-tests-for-bridge-with-remote-grandpa-chain?expand=1#diff-79a28d4d3e1749050341c2424f00c4c139825b1a20937767f83e58b95166735c
for details.
  • Loading branch information
svyatonik authored Dec 13, 2023
1 parent 3c5fcbe commit 9ecb2d3
Show file tree
Hide file tree
Showing 14 changed files with 2,588 additions and 1,651 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use bridge_hub_rococo_runtime::{
};
use codec::{Decode, Encode};
use frame_support::{dispatch::GetDispatchInfo, parameter_types, traits::ConstU8};
use frame_system::pallet_prelude::HeaderFor;
use parachains_common::{rococo::fee::WeightToFee, AccountId, AuraId, Balance};
use sp_keyring::AccountKeyring::Alice;
use sp_runtime::{
Expand All @@ -46,13 +45,16 @@ fn construct_extrinsic(
sender: sp_keyring::AccountKeyring,
call: RuntimeCall,
) -> UncheckedExtrinsic {
let account_id = AccountId32::from(sender.public());
let extra: SignedExtra = (
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckEra::<Runtime>::from(Era::immortal()),
frame_system::CheckNonce::<Runtime>::from(0),
frame_system::CheckNonce::<Runtime>::from(
frame_system::Pallet::<Runtime>::account(&account_id).nonce,
),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
BridgeRejectObsoleteHeadersAndMessages::default(),
Expand All @@ -62,18 +64,17 @@ fn construct_extrinsic(
let signature = payload.using_encoded(|e| sender.sign(e));
UncheckedExtrinsic::new_signed(
call,
AccountId32::from(sender.public()).into(),
account_id.into(),
Signature::Sr25519(signature.clone()),
extra,
)
}

fn construct_and_apply_extrinsic(
relayer_at_target: sp_keyring::AccountKeyring,
batch: pallet_utility::Call<Runtime>,
call: RuntimeCall,
) -> sp_runtime::DispatchOutcome {
let batch_call = RuntimeCall::Utility(batch);
let xt = construct_extrinsic(relayer_at_target, batch_call);
let xt = construct_extrinsic(relayer_at_target, call);
let r = Executive::apply_extrinsic(xt);
r.unwrap()
}
Expand All @@ -85,10 +86,6 @@ fn construct_and_estimate_extrinsic_fee(batch: pallet_utility::Call<Runtime>) ->
TransactionPayment::compute_fee(xt.encoded_size() as _, &batch_info, 0)
}

fn executive_init_block(header: &HeaderFor<Runtime>) {
Executive::initialize_block(header)
}

fn collator_session_keys() -> bridge_hub_test_utils::CollatorSessionKeys<Runtime> {
bridge_hub_test_utils::CollatorSessionKeys::new(
AccountId::from(Alice),
Expand Down Expand Up @@ -237,10 +234,9 @@ mod bridge_hub_rococo_tests {
#[test]
fn relayed_incoming_message_works() {
// from Westend
bridge_hub_test_utils::test_cases::relayed_incoming_message_works::<
bridge_hub_test_utils::test_cases::from_parachain::relayed_incoming_message_works::<
Runtime,
AllPalletsWithoutSystem,
XcmConfig,
ParachainSystem,
BridgeGrandpaWestendInstance,
BridgeParachainWestendInstance,
Expand All @@ -250,17 +246,19 @@ mod bridge_hub_rococo_tests {
collator_session_keys(),
bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID,
bp_bridge_hub_westend::BRIDGE_HUB_WESTEND_PARACHAIN_ID,
BridgeHubWestendChainId::get(),
SIBLING_PARACHAIN_ID,
Rococo,
XCM_LANE_FOR_ASSET_HUB_ROCOCO_TO_ASSET_HUB_WESTEND,
|| (),
construct_and_apply_extrinsic,
)
}

#[test]
pub fn complex_relay_extrinsic_works() {
// for Westend
bridge_hub_test_utils::test_cases::complex_relay_extrinsic_works::<
bridge_hub_test_utils::test_cases::from_parachain::complex_relay_extrinsic_works::<
Runtime,
AllPalletsWithoutSystem,
XcmConfig,
Expand All @@ -277,10 +275,8 @@ mod bridge_hub_rococo_tests {
BridgeHubWestendChainId::get(),
Rococo,
XCM_LANE_FOR_ASSET_HUB_ROCOCO_TO_ASSET_HUB_WESTEND,
ExistentialDeposit::get(),
executive_init_block,
construct_and_apply_extrinsic,
|| (),
construct_and_apply_extrinsic,
);
}

Expand All @@ -304,7 +300,7 @@ mod bridge_hub_rococo_tests {

#[test]
pub fn can_calculate_fee_for_complex_message_delivery_transaction() {
let estimated = bridge_hub_test_utils::test_cases::can_calculate_fee_for_complex_message_delivery_transaction::<
let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::<
Runtime,
BridgeGrandpaWestendInstance,
BridgeParachainWestendInstance,
Expand All @@ -327,7 +323,7 @@ mod bridge_hub_rococo_tests {

#[test]
pub fn can_calculate_fee_for_complex_message_confirmation_transaction() {
let estimated = bridge_hub_test_utils::test_cases::can_calculate_fee_for_complex_message_confirmation_transaction::<
let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::<
Runtime,
BridgeGrandpaWestendInstance,
BridgeParachainWestendInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use bridge_to_rococo_config::{
};
use codec::{Decode, Encode};
use frame_support::{dispatch::GetDispatchInfo, parameter_types, traits::ConstU8};
use frame_system::pallet_prelude::HeaderFor;
use parachains_common::{westend::fee::WeightToFee, AccountId, AuraId, Balance};
use sp_keyring::AccountKeyring::Alice;
use sp_runtime::{
Expand All @@ -52,13 +51,16 @@ fn construct_extrinsic(
sender: sp_keyring::AccountKeyring,
call: RuntimeCall,
) -> UncheckedExtrinsic {
let account_id = AccountId32::from(sender.public());
let extra: SignedExtra = (
frame_system::CheckNonZeroSender::<Runtime>::new(),
frame_system::CheckSpecVersion::<Runtime>::new(),
frame_system::CheckTxVersion::<Runtime>::new(),
frame_system::CheckGenesis::<Runtime>::new(),
frame_system::CheckEra::<Runtime>::from(Era::immortal()),
frame_system::CheckNonce::<Runtime>::from(0),
frame_system::CheckNonce::<Runtime>::from(
frame_system::Pallet::<Runtime>::account(&account_id).nonce,
),
frame_system::CheckWeight::<Runtime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<Runtime>::from(0),
BridgeRejectObsoleteHeadersAndMessages::default(),
Expand All @@ -68,18 +70,17 @@ fn construct_extrinsic(
let signature = payload.using_encoded(|e| sender.sign(e));
UncheckedExtrinsic::new_signed(
call,
AccountId32::from(sender.public()).into(),
account_id.into(),
Signature::Sr25519(signature.clone()),
extra,
)
}

fn construct_and_apply_extrinsic(
relayer_at_target: sp_keyring::AccountKeyring,
batch: pallet_utility::Call<Runtime>,
call: RuntimeCall,
) -> sp_runtime::DispatchOutcome {
let batch_call = RuntimeCall::Utility(batch);
let xt = construct_extrinsic(relayer_at_target, batch_call);
let xt = construct_extrinsic(relayer_at_target, call);
let r = Executive::apply_extrinsic(xt);
r.unwrap()
}
Expand All @@ -91,10 +92,6 @@ fn construct_and_estimate_extrinsic_fee(batch: pallet_utility::Call<Runtime>) ->
TransactionPayment::compute_fee(xt.encoded_size() as _, &batch_info, 0)
}

fn executive_init_block(header: &HeaderFor<Runtime>) {
Executive::initialize_block(header)
}

fn collator_session_keys() -> bridge_hub_test_utils::CollatorSessionKeys<Runtime> {
bridge_hub_test_utils::CollatorSessionKeys::new(
AccountId::from(Alice),
Expand Down Expand Up @@ -222,10 +219,9 @@ fn message_dispatch_routing_works() {

#[test]
fn relayed_incoming_message_works() {
bridge_hub_test_utils::test_cases::relayed_incoming_message_works::<
bridge_hub_test_utils::test_cases::from_parachain::relayed_incoming_message_works::<
Runtime,
AllPalletsWithoutSystem,
XcmConfig,
ParachainSystem,
BridgeGrandpaRococoInstance,
BridgeParachainRococoInstance,
Expand All @@ -235,16 +231,18 @@ fn relayed_incoming_message_works() {
collator_session_keys(),
bp_bridge_hub_westend::BRIDGE_HUB_WESTEND_PARACHAIN_ID,
bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID,
BridgeHubRococoChainId::get(),
SIBLING_PARACHAIN_ID,
Westend,
XCM_LANE_FOR_ASSET_HUB_WESTEND_TO_ASSET_HUB_ROCOCO,
|| (),
construct_and_apply_extrinsic,
)
}

#[test]
pub fn complex_relay_extrinsic_works() {
bridge_hub_test_utils::test_cases::complex_relay_extrinsic_works::<
bridge_hub_test_utils::test_cases::from_parachain::complex_relay_extrinsic_works::<
Runtime,
AllPalletsWithoutSystem,
XcmConfig,
Expand All @@ -261,10 +259,8 @@ pub fn complex_relay_extrinsic_works() {
BridgeHubRococoChainId::get(),
Westend,
XCM_LANE_FOR_ASSET_HUB_WESTEND_TO_ASSET_HUB_ROCOCO,
ExistentialDeposit::get(),
executive_init_block,
construct_and_apply_extrinsic,
|| (),
construct_and_apply_extrinsic,
);
}

Expand All @@ -288,7 +284,7 @@ pub fn can_calculate_weight_for_paid_export_message_with_reserve_transfer() {

#[test]
pub fn can_calculate_fee_for_complex_message_delivery_transaction() {
let estimated = bridge_hub_test_utils::test_cases::can_calculate_fee_for_complex_message_delivery_transaction::<
let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_delivery_transaction::<
Runtime,
BridgeGrandpaRococoInstance,
BridgeParachainRococoInstance,
Expand All @@ -311,7 +307,7 @@ pub fn can_calculate_fee_for_complex_message_delivery_transaction() {

#[test]
pub fn can_calculate_fee_for_complex_message_confirmation_transaction() {
let estimated = bridge_hub_test_utils::test_cases::can_calculate_fee_for_complex_message_confirmation_transaction::<
let estimated = bridge_hub_test_utils::test_cases::from_parachain::can_calculate_fee_for_complex_message_confirmation_transaction::<
Runtime,
BridgeGrandpaRococoInstance,
BridgeParachainRococoInstance,
Expand Down
3 changes: 3 additions & 0 deletions cumulus/parachains/runtimes/bridge-hubs/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "Apache-2.0"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "max-encoded-len"] }
impl-trait-for-tuples = "0.2"
log = { version = "0.4.20", default-features = false }

# Substrate
Expand All @@ -19,6 +20,7 @@ sp-core = { path = "../../../../../substrate/primitives/core", default-features
sp-io = { path = "../../../../../substrate/primitives/io", default-features = false }
sp-keyring = { path = "../../../../../substrate/primitives/keyring" }
sp-runtime = { path = "../../../../../substrate/primitives/runtime", default-features = false }
sp-std = { path = "../../../../../substrate/primitives/std", default-features = false }
sp-tracing = { path = "../../../../../substrate/primitives/tracing" }
pallet-balances = { path = "../../../../../substrate/frame/balances", default-features = false }
pallet-utility = { path = "../../../../../substrate/frame/utility", default-features = false }
Expand Down Expand Up @@ -90,6 +92,7 @@ std = [
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"xcm-builder/std",
"xcm-executor/std",
"xcm/std",
Expand Down
2 changes: 2 additions & 0 deletions cumulus/parachains/runtimes/bridge-hubs/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
//! Module contains predefined test-case scenarios for "BridgeHub" `Runtime`s.
pub mod test_cases;
pub mod test_data;

pub use bp_test_utils::test_header;
pub use parachains_runtimes_test_utils::*;
Loading

0 comments on commit 9ecb2d3

Please sign in to comment.