Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fetching and resetting events #41

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 69 additions & 13 deletions drink/src/chain_api.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Basic chain API.

use frame_support::dispatch::Dispatchable;
use frame_support::sp_runtime::DispatchResultWithInfo;
use frame_support::{sp_runtime::AccountId32, traits::tokens::currency::Currency};
use frame_support::{
dispatch::Dispatchable,
sp_runtime::{AccountId32, DispatchResultWithInfo},
traits::tokens::currency::Currency,
};

use crate::{DrinkResult, Error, Runtime, Sandbox};
use crate::{DrinkResult, Error, EventRecordOf, Runtime, Sandbox};

/// The runtime call type for a particular runtime.
pub type RuntimeCall<R> = <R as frame_system::Config>::RuntimeCall;
Expand Down Expand Up @@ -63,6 +65,12 @@ pub trait ChainApi<R: Runtime> {
call: RuntimeCall<R>,
origin: <RuntimeCall<R> as Dispatchable>::RuntimeOrigin,
) -> DispatchResultWithInfo<<RuntimeCall<R> as Dispatchable>::PostInfo>;

/// Return the events of the current block so far.
fn get_current_block_events(&mut self) -> Vec<EventRecordOf<R>>;

/// Reset the events of the current block.
fn reset_current_block_events(&mut self);
}

impl<R: Runtime> ChainApi<R> for Sandbox<R> {
Expand Down Expand Up @@ -115,12 +123,38 @@ impl<R: Runtime> ChainApi<R> for Sandbox<R> {
) -> DispatchResultWithInfo<<RuntimeCall<R> as Dispatchable>::PostInfo> {
self.externalities.execute_with(|| call.dispatch(origin))
}

fn get_current_block_events(&mut self) -> Vec<EventRecordOf<R>> {
self.externalities
.execute_with(|| frame_system::Pallet::<R>::events())
}

fn reset_current_block_events(&mut self) {
self.externalities
.execute_with(|| frame_system::Pallet::<R>::reset_events())
}
}

#[cfg(test)]
mod tests {
use crate::chain_api::{ChainApi, RuntimeCall};
use crate::{runtime::MinimalRuntime, AccountId32, Sandbox, DEFAULT_ACTOR};
use frame_support::dispatch::Dispatchable;

use crate::{
chain_api::{ChainApi, DispatchResultWithInfo, RuntimeCall},
runtime::{minimal::RuntimeEvent, MinimalRuntime},
AccountId32, Sandbox, DEFAULT_ACTOR,
};

fn make_transfer(
sandbox: &mut Sandbox<MinimalRuntime>,
dest: AccountId32,
value: u128,
) -> DispatchResultWithInfo<<RuntimeCall<MinimalRuntime> as Dispatchable>::PostInfo> {
let call = RuntimeCall::<MinimalRuntime>::Balances(
pallet_balances::Call::<MinimalRuntime>::transfer { dest, value },
);
sandbox.runtime_call(call, Some(DEFAULT_ACTOR).into())
}

#[test]
fn dry_run_works() {
Expand All @@ -143,16 +177,38 @@ mod tests {
assert_ne!(DEFAULT_ACTOR, RECIPIENT);
let initial_balance = sandbox.balance(&RECIPIENT);

let call = RuntimeCall::<MinimalRuntime>::Balances(
pallet_balances::Call::<MinimalRuntime>::transfer {
dest: RECIPIENT,
value: 100,
},
);
let result = sandbox.runtime_call(call, Some(DEFAULT_ACTOR).into());
let result = make_transfer(&mut sandbox, RECIPIENT, 100);
assert!(result.is_ok());

let expected_balance = initial_balance + 100;
assert_eq!(sandbox.balance(&RECIPIENT), expected_balance);
}

#[test]
fn current_events() {
let mut sandbox = Sandbox::<MinimalRuntime>::new().expect("Failed to create sandbox");

let events_before = sandbox.get_current_block_events();
assert!(events_before.is_empty());

make_transfer(&mut sandbox, DEFAULT_ACTOR, 1).expect("Failed to make transfer");

let events_after = sandbox.get_current_block_events();
assert_eq!(events_after.len(), 1);
assert!(matches!(events_after[0].event, RuntimeEvent::Balances(_)));
}

#[test]
fn resetting_events() {
let mut sandbox = Sandbox::<MinimalRuntime>::new().expect("Failed to create sandbox");

make_transfer(&mut sandbox, DEFAULT_ACTOR, 1).expect("Failed to make transfer");

assert!(!sandbox.get_current_block_events().is_empty());
sandbox.reset_current_block_events();
assert!(sandbox.get_current_block_events().is_empty());

make_transfer(&mut sandbox, DEFAULT_ACTOR, 1).expect("Failed to make transfer");
assert!(!sandbox.get_current_block_events().is_empty());
}
}
4 changes: 3 additions & 1 deletion drink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ impl<R: Runtime> Sandbox<R> {

sandbox
.externalities
.execute_with(|| R::initialize_block(0, Default::default()))
// We start the chain from the 1st block, so that events are collected (they are not
// recorded for the genesis block...).
.execute_with(|| R::initialize_block(1, Default::default()))
.map_err(Error::BlockInitialize)?;

Ok(sandbox)
Expand Down
7 changes: 3 additions & 4 deletions drink/src/runtime/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ impl Runtime for MinimalRuntime {

fn initialize_block(height: u64, parent_hash: H256) -> Result<(), String> {
System::reset_events();

if height > 0 {
System::initialize(&height, &parent_hash, &Default::default());
}
System::initialize(&height, &parent_hash, &Default::default());

Balances::on_initialize(height);
Timestamp::set_timestamp(
Expand All @@ -151,6 +148,8 @@ impl Runtime for MinimalRuntime {
Timestamp::on_initialize(height);
Contracts::on_initialize(height);

System::note_finished_initialize();

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion drink/src/runtime/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module containing the [`Runtime`](Runtime) trait and its example implementations. You can use
//! `drink` with any runtime that implements the `Runtime` trait.

mod minimal;
pub mod minimal;

use frame_support::sp_runtime::{AccountId32, Storage};
pub use minimal::MinimalRuntime;
Expand Down