Skip to content

Commit

Permalink
Support Client APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Oct 10, 2023
1 parent c344049 commit a15c45d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
27 changes: 25 additions & 2 deletions runtime/integration-tests/src/generic/cases/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn check_fee<T: Runtime>() {
});
}

fn using_fudge<T: Runtime + FudgeSupport>() {
fn fudge_example<T: Runtime + FudgeSupport>() {
let _env = FudgeEnv::<T>::from_storage(
Genesis::default()
.add(pallet_aura::GenesisConfig::<T> {
Expand All @@ -126,11 +126,34 @@ fn using_fudge<T: Runtime + FudgeSupport>() {
//TODO
}

fn fudge_call_api<T: Runtime + FudgeSupport>() {
let env = FudgeEnv::<T>::from_storage(
Genesis::default()
.add(pallet_aura::GenesisConfig::<T> {
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(),
<T as frame_system::Config>::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
Expand Down
32 changes: 30 additions & 2 deletions runtime/integration-tests/src/generic/envs/fudge_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -47,3 +49,29 @@ impl<T: Runtime + FudgeSupport> Env<T> for FudgeEnv<T> {
self.handle.evolve();
}
}

type ApiRefOf<'a, T> =
ApiRef<
'a,
<ParachainClient<
<T as FudgeHandle>::ParachainBlock,
<T as FudgeHandle>::ParachainConstructApi,
> as sp_api::ProvideRuntimeApi<<T as FudgeHandle>::ParachainBlock>>::Api,
>;

impl<T: Runtime + FudgeSupport> FudgeEnv<T> {
pub fn with_api<F>(&self, exec: F)
where
F: FnOnce(
ApiRefOf<T::FudgeHandle>,
BlockId<<T::FudgeHandle as FudgeHandle>::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);
}
}

0 comments on commit a15c45d

Please sign in to comment.