Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Integration-style tests for events.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork committed Aug 27, 2018
1 parent 90b003a commit 2154395
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/executor/Cargo.toml
Expand Up @@ -20,6 +20,7 @@ demo-runtime = { path = "../runtime" }
[dev-dependencies]
substrate-keyring = { path = "../../substrate/keyring" }
substrate-runtime-primitives = { path = "../../substrate/runtime/primitives" }
substrate-runtime-session = { path = "../../substrate/runtime/session" }
substrate-runtime-staking = { path = "../../substrate/runtime/staking" }
substrate-runtime-system = { path = "../../substrate/runtime/system" }
substrate-runtime-consensus = { path = "../../substrate/runtime/consensus" }
26 changes: 22 additions & 4 deletions demo/executor/src/lib.rs
Expand Up @@ -30,6 +30,7 @@ extern crate triehash;
#[cfg(test)] extern crate substrate_keyring as keyring;
#[cfg(test)] extern crate substrate_runtime_primitives as runtime_primitives;
#[cfg(test)] extern crate substrate_runtime_support as runtime_support;
#[cfg(test)] extern crate substrate_runtime_session as session;
#[cfg(test)] extern crate substrate_runtime_staking as staking;
#[cfg(test)] extern crate substrate_runtime_system as system;
#[cfg(test)] extern crate substrate_runtime_consensus as consensus;
Expand All @@ -51,9 +52,10 @@ mod tests {
use demo_primitives::{Hash, BlockNumber, AccountId};
use runtime_primitives::traits::Header as HeaderT;
use runtime_primitives::{ApplyOutcome, ApplyError, ApplyResult, MaybeUnsigned};
use {staking, system, consensus};
use {staking, session, system, consensus};
use system::{EventRecord, Phase};
use demo_runtime::{Header, Block, UncheckedExtrinsic, Extrinsic, Call, Concrete, Staking,
BuildStorage, GenesisConfig, SessionConfig, StakingConfig, BareExtrinsic};
BuildStorage, GenesisConfig, SessionConfig, StakingConfig, BareExtrinsic, System, Event};
use ed25519::{Public, Pair};

const BLOATY_CODE: &[u8] = include_bytes!("../../runtime/wasm/target/wasm32-unknown-unknown/release/demo_runtime.wasm");
Expand Down Expand Up @@ -255,7 +257,7 @@ mod tests {
// Blake
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
// Keccak
hex!("4956f00670e45ecb3c29be839fe6f981cb908ff0786536062096c628d577a142").into(),
hex!("56c9a542e48ccf4e0821de301ea4384e87425604278b12a9db31c6d4e89ca51e").into(),
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand All @@ -271,7 +273,7 @@ mod tests {
// Blake
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
// Keccak
hex!("61a6c7096a51b8ac79d6a6949c786cec7364be43dc4ec40cd9d4f6d631363a54").into(),
hex!("166a2593d35f2d1bc87eca8cf2e320ed06759000a02aa88e51fa85b12c6f1267").into(),
vec![
BareExtrinsic {
signed: bob(),
Expand Down Expand Up @@ -312,13 +314,29 @@ mod tests {
runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::voting_balance(&alice()), 41);
assert_eq!(Staking::voting_balance(&bob()), 69);
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::staking(staking::RawEvent::NewAccount(bob(), 1, staking::NewAccountOutcome::NoHint))
}
]);
});

executor().call(&mut t, COMPACT_CODE, "execute_block", &block2().0, true).0.unwrap();

runtime_io::with_externalities(&mut t, || {
assert_eq!(Staking::voting_balance(&alice()), 30);
assert_eq!(Staking::voting_balance(&bob()), 78);
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
event: Event::session(session::RawEvent::NewSession(1))
},
EventRecord {
phase: Phase::Finalization,
event: Event::staking(staking::RawEvent::Reward(0))
}
]);
});
}

Expand Down
Binary file not shown.
Binary file not shown.
7 changes: 4 additions & 3 deletions substrate/runtime/system/src/lib.rs
Expand Up @@ -86,7 +86,7 @@ decl_module! {

/// A phase of a block's execution.
#[derive(Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", derive(Serialize, PartialEq, Eq, Clone, Debug))]
pub enum Phase {
/// Applying an extrinsic.
ApplyExtrinsic(u32),
Expand All @@ -96,11 +96,12 @@ pub enum Phase {

/// Record of an event happening.
#[derive(Encode, Decode)]
#[cfg_attr(feature = "std", derive(Serialize, PartialEq, Eq, Clone, Debug))]
pub struct EventRecord<E: Parameter + Member> {
/// The phase of the block it happened in.
phase: Phase,
pub phase: Phase,
/// The event itself.
event: E,
pub event: E,
}

decl_storage! {
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 2154395

Please sign in to comment.