Skip to content

Commit

Permalink
evolving and state support for fudge env
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Oct 10, 2023
1 parent d0bb145 commit c344049
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 53 deletions.
41 changes: 40 additions & 1 deletion runtime/integration-tests/src/generic/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,44 @@ pub trait Env<T: Runtime> {
fn submit(&mut self, who: Keyring, call: impl Into<T::RuntimeCall>) -> DispatchResult;

/// Pass any number of blocks
fn pass(&mut self, blocks: Blocks<T>);
fn pass(&mut self, blocks: Blocks<T>) {
let (next, end_block) = self.state(|| {
let next = frame_system::Pallet::<T>::block_number() + 1;

let end_block = match blocks {
Blocks::ByNumber(n) => next + n,
Blocks::BySeconds(secs) => {
let blocks = secs / pallet_aura::Pallet::<T>::slot_duration();
if blocks % pallet_aura::Pallet::<T>::slot_duration() != 0 {
blocks as BlockNumber + 1
} else {
blocks as BlockNumber
}
}
Blocks::UntilEvent { limit, .. } => limit,
};

(next, end_block)
});

for i in next..end_block {
self.__priv_build_block(i);

if let Blocks::UntilEvent { event, .. } = blocks.clone() {
let found = self.state(|| {
let event: T::RuntimeEventExt = event.into();
frame_system::Pallet::<T>::events()
.into_iter()
.find(|record| record.event == event)
.is_some()
});

if found {
break;
}
}
}
}

/// Allows to mutate the storage state through the closure
fn state_mut<R>(&mut self, f: impl FnOnce() -> R) -> R;
Expand Down Expand Up @@ -82,4 +119,6 @@ pub trait Env<T: Runtime> {
})
.expect("Expected transaction")
}

fn __priv_build_block(&mut self, i: BlockNumber);
}
24 changes: 11 additions & 13 deletions runtime/integration-tests/src/generic/envs/fudge_env.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
pub mod handle;

use cfg_primitives::BlockNumber;
use fudge::primitives::Chain;
use handle::FudgeHandle;
use sp_runtime::{DispatchResult, Storage};

use crate::{
generic::{
environment::{Blocks, Env},
runtime::Runtime,
},
generic::{environment::Env, runtime::Runtime},
utils::accounts::Keyring,
};

Expand All @@ -34,18 +33,17 @@ impl<T: Runtime + FudgeSupport> Env<T> for FudgeEnv<T> {
todo!()
}

fn pass(&mut self, _blocks: Blocks<T>) {
// Access to the handle to do everything
todo!()
fn state_mut<R>(&mut self, f: impl FnOnce() -> R) -> R {
self.handle
.with_mut_state(Chain::Para(T::FudgeHandle::PARA_ID), f)
}

fn state_mut<R>(&mut self, _f: impl FnOnce() -> R) -> R {
// Access to the handle to do everything
todo!()
fn state<R>(&self, f: impl FnOnce() -> R) -> R {
self.handle
.with_state(Chain::Para(T::FudgeHandle::PARA_ID), f)
}

fn state<R>(&self, _f: impl FnOnce() -> R) -> R {
// Access to the handle to do everything
todo!()
fn __priv_build_block(&mut self, _i: BlockNumber) {
self.handle.evolve();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fudge::{
CreateInherentDataProviders, FudgeDummyInherentRelayParachain, FudgeInherentParaParachain,
FudgeInherentTimestamp,
},
primitives::Chain,
state::StateProvider,
TWasmExecutor,
};
Expand Down Expand Up @@ -123,6 +124,10 @@ pub trait FudgeHandle {
&mut self,
) -> &mut ParachainBuilder<Self::ParachainBlock, Self::ParachainConstructApi>;

fn with_state<R>(&self, chain: Chain, f: impl FnOnce() -> R) -> R;
fn with_mut_state<R>(&mut self, chain: Chain, f: impl FnOnce() -> R) -> R;
fn evolve(&mut self);

fn build(relay_storage: Storage, parachain_storage: Storage) -> Self;

fn build_relay(
Expand Down
47 changes: 8 additions & 39 deletions runtime/integration-tests/src/generic/envs/runtime_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use sp_runtime::{
use sp_timestamp::Timestamp;

use crate::{
generic::{
environment::{Blocks, Env},
runtime::Runtime,
},
generic::{environment::Env, runtime::Runtime},
utils::accounts::Keyring,
};

Expand Down Expand Up @@ -77,41 +74,6 @@ impl<T: Runtime> Env<T> for RuntimeEnv<T> {
})
}

fn pass(&mut self, blocks: Blocks<T>) {
self.ext.borrow_mut().execute_with(|| {
let next = frame_system::Pallet::<T>::block_number() + 1;

let end_block = match blocks {
Blocks::ByNumber(n) => next + n,
Blocks::BySeconds(secs) => {
let blocks = secs / pallet_aura::Pallet::<T>::slot_duration();
if blocks % pallet_aura::Pallet::<T>::slot_duration() != 0 {
blocks as BlockNumber + 1
} else {
blocks as BlockNumber
}
}
Blocks::UntilEvent { limit, .. } => limit,
};

for i in next..end_block {
T::finalize_block();
Self::prepare_block(i);

if let Blocks::UntilEvent { event, .. } = blocks.clone() {
let event: T::RuntimeEventExt = event.into();
if frame_system::Pallet::<T>::events()
.into_iter()
.find(|record| record.event == event)
.is_some()
{
break;
}
}
}
})
}

fn state_mut<R>(&mut self, f: impl FnOnce() -> R) -> R {
self.ext.borrow_mut().execute_with(f)
}
Expand All @@ -125,6 +87,13 @@ impl<T: Runtime> Env<T> for RuntimeEnv<T> {
.unwrap()
})
}

fn __priv_build_block(&mut self, i: BlockNumber) {
self.state_mut(|| {
T::finalize_block();
Self::prepare_block(i);
});
}
}

impl<T: Runtime> RuntimeEnv<T> {
Expand Down
11 changes: 11 additions & 0 deletions runtime/integration-tests/src/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ macro_rules! test_for_runtimes {

/// TODO generate this for all runtimes with a macro
mod fudge_handles {
use fudge::primitives::Chain;
use polkadot_core_primitives::Block as RelayBlock;
use sp_api::ConstructRuntimeApi;
use sp_runtime::Storage;
Expand Down Expand Up @@ -132,6 +133,16 @@ mod fudge_handles {
fn parachain_mut(&mut self) -> &mut Parachain {
&mut self.parachain
}

fn with_state<R>(&self, chain: Chain, f: impl FnOnce() -> R) -> R {
self.with_state(chain, f).unwrap()
}

fn with_mut_state<R>(&mut self, chain: Chain, f: impl FnOnce() -> R) -> R {
self.with_mut_state(chain, f).unwrap()
}

fn evolve(&mut self) {}
}

impl FudgeSupport for development_runtime::Runtime {
Expand Down

0 comments on commit c344049

Please sign in to comment.