diff --git a/runtime/integration-tests/src/generic/cases/example.rs b/runtime/integration-tests/src/generic/cases/example.rs index 484c15422d..a419b31f6a 100644 --- a/runtime/integration-tests/src/generic/cases/example.rs +++ b/runtime/integration-tests/src/generic/cases/example.rs @@ -111,7 +111,7 @@ fn check_fee() { }); } -fn using_fudge() { +fn fudge_example() { let _env = FudgeEnv::::from_storage( Genesis::default() .add(pallet_aura::GenesisConfig:: { @@ -126,11 +126,34 @@ fn using_fudge() { //TODO } +fn fudge_call_api() { + let env = FudgeEnv::::from_storage( + Genesis::default() + .add(pallet_aura::GenesisConfig:: { + authorities: vec![AuraId::from(Keyring::Charlie.public())], + }) + .storage(), + ); + + // Exclusive from fudge environment. + // It uses a client to access the runtime api. + env.with_api(|api, latest| { + // We include the API we want to use + use sp_api::Core; + + assert_eq!( + api.version(&latest).unwrap(), + ::Version::get() + ); + }) +} + // Generate tests for all runtimes 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], using_fudge); +crate::test_for_runtimes!([development], fudge_example); +crate::test_for_runtimes!([development], fudge_call_api); // Output: for `cargo test -p runtime-integration-tests transfer_balance` // running 6 tests diff --git a/runtime/integration-tests/src/generic/envs/fudge_env.rs b/runtime/integration-tests/src/generic/envs/fudge_env.rs index 95de2912a1..508274ebce 100644 --- a/runtime/integration-tests/src/generic/envs/fudge_env.rs +++ b/runtime/integration-tests/src/generic/envs/fudge_env.rs @@ -2,8 +2,10 @@ pub mod handle; use cfg_primitives::BlockNumber; use fudge::primitives::Chain; -use handle::FudgeHandle; -use sp_runtime::{DispatchResult, Storage}; +use handle::{FudgeHandle, ParachainClient}; +use sc_client_api::HeaderBackend; +use sp_api::{ApiRef, ProvideRuntimeApi}; +use sp_runtime::{generic::BlockId, DispatchResult, Storage}; use crate::{ generic::{environment::Env, runtime::Runtime}, @@ -47,3 +49,29 @@ impl Env for FudgeEnv { self.handle.evolve(); } } + +type ApiRefOf<'a, T> = + ApiRef< + 'a, + ::ParachainBlock, + ::ParachainConstructApi, + > as sp_api::ProvideRuntimeApi<::ParachainBlock>>::Api, + >; + +impl FudgeEnv { + pub fn with_api(&self, exec: F) + where + F: FnOnce( + ApiRefOf, + BlockId<::ParachainBlock>, + ), + { + let client = self.handle.parachain().client(); + let best_hash = client.info().best_hash; + let api = client.runtime_api(); + let best_hash = BlockId::hash(best_hash); + + exec(api, best_hash); + } +}