From f3cac5e112df41acc85e1a21544897d2c13c645e Mon Sep 17 00:00:00 2001 From: Clara van Staden Date: Fri, 26 Jan 2024 14:14:46 +0200 Subject: [PATCH] Adds Runtime tests (#98) * adds runtime tests * commit changes * revert unnessary changes and create new beacon fixtures crate * tests * Std Feature gating * update cargo lock * more tests * update cargo.lock * simply test code --------- Co-authored-by: claravanstaden Co-authored-by: ron --- Cargo.lock | 17 +++++ .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 4 +- .../bridge-hub-rococo/tests/snowbridge.rs | 72 ++++++++++++++++++- .../parachains/runtimes/test-utils/src/lib.rs | 6 +- 4 files changed, 94 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c967093238f1..6bfe27bb17a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17720,6 +17720,7 @@ dependencies = [ "snowbridge-beacon-primitives", "snowbridge-core", "snowbridge-ethereum", + "snowbridge-pallet-ethereum-client-fixtures", "sp-core", "sp-io", "sp-runtime", @@ -17729,6 +17730,20 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "snowbridge-pallet-ethereum-client-fixtures" +version = "0.9.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "hex-literal", + "snowbridge-beacon-primitives", + "snowbridge-core", + "sp-core", + "sp-std 8.0.0", +] + [[package]] name = "snowbridge-pallet-inbound-queue" version = "0.9.0" @@ -17904,6 +17919,7 @@ dependencies = [ "snowbridge-core", "snowbridge-outbound-queue-runtime-api", "snowbridge-pallet-ethereum-client", + "snowbridge-pallet-ethereum-client-fixtures", "snowbridge-pallet-inbound-queue", "snowbridge-pallet-outbound-queue", "snowbridge-pallet-system", @@ -17916,6 +17932,7 @@ dependencies = [ "sp-genesis-builder", "sp-inherents", "sp-io", + "sp-keyring", "sp-offchain", "sp-runtime", "sp-session", diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 287436f05bc0..cfaa846e4a4d 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -585,7 +585,7 @@ impl snowbridge_pallet_outbound_queue::Config for Runtime { type Channels = EthereumSystem; } -#[cfg(any(feature = "fast-runtime", feature = "runtime-benchmarks"))] +#[cfg(any(feature = "std", feature = "fast-runtime", feature = "runtime-benchmarks", test))] parameter_types! { pub const ChainForkVersions: ForkVersions = ForkVersions { genesis: Fork { @@ -611,7 +611,7 @@ parameter_types! { }; } -#[cfg(all(not(feature = "fast-runtime"), not(feature = "runtime-benchmarks")))] +#[cfg(not(any(feature = "std", feature = "fast-runtime", feature = "runtime-benchmarks", test)))] parameter_types! { pub const ChainForkVersions: ForkVersions = ForkVersions { genesis: Fork { diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/snowbridge.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/snowbridge.rs index f32c6cae69c6..e14755c89d4d 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/snowbridge.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/snowbridge.rs @@ -16,16 +16,25 @@ #![cfg(test)] +use bp_polkadot_core::Signature; use bridge_hub_rococo_runtime::{ - xcm_config::XcmConfig, MessageQueueServiceWeight, Runtime, RuntimeEvent, SessionKeys, + bridge_to_bulletin_config::OnBridgeHubRococoRefundRococoBulletinMessages, + bridge_to_westend_config::OnBridgeHubRococoRefundBridgeHubWestendMessages, + xcm_config::XcmConfig, BridgeRejectObsoleteHeadersAndMessages, Executive, + MessageQueueServiceWeight, Runtime, RuntimeCall, RuntimeEvent, SessionKeys, SignedExtra, + UncheckedExtrinsic, }; -use codec::Decode; +use codec::{Decode, Encode}; use cumulus_primitives_core::XcmError::{FailedToTransactAsset, NotHoldingFees}; use frame_support::parameter_types; use parachains_common::{AccountId, AuraId, Balance}; use snowbridge_pallet_ethereum_client::WeightInfo; use sp_core::H160; use sp_keyring::AccountKeyring::Alice; +use sp_runtime::{ + generic::{Era, SignedPayload}, + AccountId32, +}; parameter_types! { pub const DefaultBridgeHubEthereumBaseFee: Balance = 2_750_872_500_000; @@ -107,3 +116,62 @@ fn max_message_queue_service_weight_is_more_than_beacon_extrinsic_weights() { max_message_queue_weight.all_gt(force_checkpoint); max_message_queue_weight.all_gt(submit_checkpoint); } + +#[test] +fn ethereum_client_consensus_extrinsics_work() { + snowbridge_runtime_test_common::ethereum_extrinsic( + collator_session_keys(), + 1013, + construct_and_apply_extrinsic, + ); +} + +#[test] +fn ethereum_to_polkadot_message_extrinsics_work() { + snowbridge_runtime_test_common::ethereum_to_polkadot_message_extrinsics_work( + collator_session_keys(), + 1013, + construct_and_apply_extrinsic, + ); +} + +fn construct_extrinsic( + sender: sp_keyring::AccountKeyring, + call: RuntimeCall, +) -> UncheckedExtrinsic { + let account_id = AccountId32::from(sender.public()); + let extra: SignedExtra = ( + frame_system::CheckNonZeroSender::::new(), + frame_system::CheckSpecVersion::::new(), + frame_system::CheckTxVersion::::new(), + frame_system::CheckGenesis::::new(), + frame_system::CheckEra::::from(Era::immortal()), + frame_system::CheckNonce::::from( + frame_system::Pallet::::account(&account_id).nonce, + ), + frame_system::CheckWeight::::new(), + pallet_transaction_payment::ChargeTransactionPayment::::from(0), + BridgeRejectObsoleteHeadersAndMessages::default(), + ( + OnBridgeHubRococoRefundBridgeHubWestendMessages::default(), + OnBridgeHubRococoRefundRococoBulletinMessages::default(), + ), + ); + let payload = SignedPayload::new(call.clone(), extra.clone()).unwrap(); + let signature = payload.using_encoded(|e| sender.sign(e)); + UncheckedExtrinsic::new_signed( + call, + account_id.into(), + Signature::Sr25519(signature.clone()), + extra, + ) +} + +fn construct_and_apply_extrinsic( + origin: sp_keyring::AccountKeyring, + call: RuntimeCall, +) -> sp_runtime::DispatchOutcome { + let xt = construct_extrinsic(origin, call); + let r = Executive::apply_extrinsic(xt); + r.unwrap() +} diff --git a/cumulus/parachains/runtimes/test-utils/src/lib.rs b/cumulus/parachains/runtimes/test-utils/src/lib.rs index eb75c2f7ee0a..b4eb57fcb66f 100644 --- a/cumulus/parachains/runtimes/test-utils/src/lib.rs +++ b/cumulus/parachains/runtimes/test-utils/src/lib.rs @@ -33,7 +33,7 @@ use polkadot_parachain_primitives::primitives::{ HeadData, HrmpChannelId, RelayChainBlockNumber, XcmpMessageFormat, }; use sp_consensus_aura::{SlotDuration, AURA_ENGINE_ID}; -use sp_core::Encode; +use sp_core::{Encode, U256}; use sp_runtime::{traits::Header, BuildStorage, Digest, DigestItem}; use xcm::{ latest::{Asset, Location, XcmContext, XcmHash}, @@ -300,6 +300,10 @@ where ::RuntimeOrigin::root() } + pub fn block_number() -> U256 { + frame_system::Pallet::::block_number().into() + } + pub fn origin_of( account_id: AccountIdOf, ) -> ::RuntimeOrigin {