From fa5d30802d72d0ec88310598e35deb861560b772 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Wed, 11 Oct 2023 19:52:05 +0200 Subject: [PATCH] fudge support for all runtimes --- .../src/generic/cases/example.rs | 10 +- runtime/integration-tests/src/generic/mod.rs | 189 ++++++++++++++---- 2 files changed, 160 insertions(+), 39 deletions(-) diff --git a/runtime/integration-tests/src/generic/cases/example.rs b/runtime/integration-tests/src/generic/cases/example.rs index 88c597b896..dd21f58e2b 100644 --- a/runtime/integration-tests/src/generic/cases/example.rs +++ b/runtime/integration-tests/src/generic/cases/example.rs @@ -42,9 +42,6 @@ fn transfer_balance() { ) .unwrap(); - // Pass blocks to evolve the system - env.pass(Blocks::ByNumber(1)); - // Check for an even occurred in this block env.check_event(pallet_balances::Event::Transfer { from: Keyring::Alice.to_account_id(), @@ -53,6 +50,9 @@ fn transfer_balance() { }) .unwrap(); + // Pass blocks to evolve the system + env.pass(Blocks::ByNumber(1)); + // Check the state env.state(|| { assert_eq!( @@ -162,5 +162,5 @@ fn check_fee() { crate::test_for_runtimes!([development, altair, centrifuge], transfer_balance); crate::test_for_runtimes!(all, call_api); crate::test_for_runtimes!(all, check_fee); -crate::test_for_runtimes!([development], fudge_transfer_balance); -crate::test_for_runtimes!([development], fudge_call_api); +crate::test_for_runtimes!(all, fudge_transfer_balance); +crate::test_for_runtimes!(all, fudge_call_api); diff --git a/runtime/integration-tests/src/generic/mod.rs b/runtime/integration-tests/src/generic/mod.rs index f0195ee267..9646d6d070 100644 --- a/runtime/integration-tests/src/generic/mod.rs +++ b/runtime/integration-tests/src/generic/mod.rs @@ -20,37 +20,34 @@ mod cases { use runtime::{Runtime, RuntimeKind}; -macro_rules! impl_config { - ($runtime:ident, $kind:ident) => { - impl Runtime for $runtime::Runtime { - type Block = $runtime::Block; - type RuntimeCallExt = $runtime::RuntimeCall; - type RuntimeEventExt = $runtime::RuntimeEvent; - - const KIND: RuntimeKind = RuntimeKind::$kind; - } - }; -} - -impl_config!(development_runtime, Development); -impl_config!(altair_runtime, Altair); -impl_config!(centrifuge_runtime, Centrifuge); - -/// Generate tests for all runtimes -/// See `example.rs` file for the usage +/// Generate tests for the specified runtimes or all runtimes. +/// Usage +/// +/// ```rust +/// use crate::generic::runtime::Runtime; /// -/// ```sh -/// Output: for `cargo test -p runtime-integration-tests transfer_balance` -/// running 3 tests +/// fn foo { +/// /// Your test here... +/// } +/// +/// crate::test_for_runtimes!([development, altair, centrifuge], foo); +/// ``` +/// For the following command: `cargo test -p runtime-integration-tests foo`, +/// it will generate the following output: /// -/// test generic::cases::example::transfer_balance::altair ... ok -/// test generic::cases::example::transfer_balance::development ... ok -/// test generic::cases::example::transfer_balance::centrifuge ... ok +/// ```text +/// test generic::foo::altair ... ok +/// test generic::foo::development ... ok +/// test generic::foo::centrifuge ... ok /// ``` +/// +/// Available input for the first argument is: +/// - Any combination of `development`, `altair`, `centrifuge` inside `[]`. +/// - The world `all`. #[macro_export] macro_rules! test_for_runtimes { - ( [ $($runtime:ident),* ], $name:ident ) => { - mod $name { + ( [ $($runtime_name:ident),* ], $test_name:ident ) => { + mod $test_name { use super::*; #[allow(unused)] @@ -64,17 +61,142 @@ macro_rules! test_for_runtimes { $( #[tokio::test] - async fn $runtime() { - $name::<$runtime::Runtime>() + async fn $runtime_name() { + $test_name::<$runtime_name::Runtime>() } )* } }; - ( all , $name:ident ) => { - $crate::test_for_runtimes!([development, altair, centrifuge], $name); + ( all , $test_name:ident ) => { + $crate::test_for_runtimes!([development, altair, centrifuge], $test_name); }; } +/// Implements the `Runtime` trait for a runtime +macro_rules! impl_runtime { + ($runtime_path:ident, $kind:ident) => { + impl Runtime for $runtime_path::Runtime { + type Block = $runtime_path::Block; + type RuntimeCallExt = $runtime_path::RuntimeCall; + type RuntimeEventExt = $runtime_path::RuntimeEvent; + + const KIND: RuntimeKind = RuntimeKind::$kind; + } + }; +} + +impl_runtime!(development_runtime, Development); +impl_runtime!(altair_runtime, Altair); +impl_runtime!(centrifuge_runtime, Centrifuge); + +/// Implements fudge support for a runtime +macro_rules! impl_fudge_support { + ( + $fudge_companion_type:ident, + $relay_path:ident, + $parachain_path:ident, + $parachain_id:literal + ) => { + const _: () = { + use fudge::primitives::Chain; + use polkadot_core_primitives::Block as RelayBlock; + use sp_api::ConstructRuntimeApi; + use sp_runtime::Storage; + + use crate::generic::envs::fudge_env::{ + handle::{ + FudgeHandle, ParachainBuilder, ParachainClient, RelayClient, RelaychainBuilder, + }, + FudgeSupport, + }; + + #[fudge::companion] + pub struct $fudge_companion_type { + #[fudge::relaychain] + pub relay: RelaychainBuilder<$relay_path::RuntimeApi, $relay_path::Runtime>, + + #[fudge::parachain($parachain_id)] + pub parachain: + ParachainBuilder<$parachain_path::Block, $parachain_path::RuntimeApi>, + } + + // Implement for T only one time when fudge::companion + // supports generic in the struct signature. + impl FudgeHandle<$parachain_path::Runtime> for $fudge_companion_type { + type ParachainApi = <$parachain_path::RuntimeApi as ConstructRuntimeApi< + $parachain_path::Block, + ParachainClient<$parachain_path::Block, Self::ParachainConstructApi>, + >>::RuntimeApi; + type ParachainConstructApi = $parachain_path::RuntimeApi; + type RelayApi = <$relay_path::RuntimeApi as ConstructRuntimeApi< + RelayBlock, + RelayClient, + >>::RuntimeApi; + type RelayConstructApi = $relay_path::RuntimeApi; + type RelayRuntime = $relay_path::Runtime; + + const PARACHAIN_CODE: Option<&'static [u8]> = $parachain_path::WASM_BINARY; + const PARA_ID: u32 = $parachain_id; + const RELAY_CODE: Option<&'static [u8]> = $relay_path::WASM_BINARY; + + fn build(relay_storage: Storage, parachain_storage: Storage) -> Self { + let relay = Self::build_relay(relay_storage); + let parachain = Self::build_parachain(&relay, parachain_storage); + + Self::new(relay, parachain).unwrap() + } + + fn relay(&self) -> &RelaychainBuilder { + &self.relay + } + + fn relay_mut( + &mut self, + ) -> &mut RelaychainBuilder { + &mut self.relay + } + + fn parachain( + &self, + ) -> &ParachainBuilder<$parachain_path::Block, Self::ParachainConstructApi> { + &self.parachain + } + + fn parachain_mut( + &mut self, + ) -> &mut ParachainBuilder<$parachain_path::Block, Self::ParachainConstructApi> { + &mut self.parachain + } + + fn append_extrinsic(&mut self, chain: Chain, extrinsic: Vec) -> Result<(), ()> { + self.append_extrinsic(chain, extrinsic) + } + + fn with_state(&self, chain: Chain, f: impl FnOnce() -> R) -> R { + self.with_state(chain, f).unwrap() + } + + fn with_mut_state(&mut self, chain: Chain, f: impl FnOnce() -> R) -> R { + self.with_mut_state(chain, f).unwrap() + } + + fn evolve(&mut self) { + self.evolve().unwrap() + } + } + + impl FudgeSupport for $parachain_path::Runtime { + type FudgeHandle = $fudge_companion_type; + } + }; + }; +} + +impl_fudge_support!(FudgeDevelopment, rococo_runtime, development_runtime, 2000); +impl_fudge_support!(FudgeAltair, kusama_runtime, altair_runtime, 2088); +impl_fudge_support!(CentrifugeAltair, polkadot_runtime, centrifuge_runtime, 2031); + +/* /// TODO generate this for all runtimes with a macro mod fudge_handles { use fudge::primitives::Chain; @@ -87,14 +209,12 @@ mod fudge_handles { FudgeSupport, }; - const DEVELOPMENT_PARA_ID: u32 = 2000; - #[fudge::companion] pub struct DevelopmentFudge { #[fudge::relaychain] pub relay: RelaychainBuilder, - #[fudge::parachain(DEVELOPMENT_PARA_ID)] + #[fudge::parachain(2000)] pub parachain: ParachainBuilder, } @@ -116,7 +236,7 @@ mod fudge_handles { type RelayRuntime = rococo_runtime::Runtime; const PARACHAIN_CODE: Option<&'static [u8]> = development_runtime::WASM_BINARY; - const PARA_ID: u32 = DEVELOPMENT_PARA_ID; + const PARA_ID: u32 = 2000; const RELAY_CODE: Option<&'static [u8]> = rococo_runtime::WASM_BINARY; fn build(relay_storage: Storage, parachain_storage: Storage) -> Self { @@ -169,3 +289,4 @@ mod fudge_handles { type FudgeHandle = DevelopmentFudge; } } +*/