Skip to content

Commit

Permalink
Events to track extrinsic success (paritytech#640)
Browse files Browse the repository at this point in the history
* Initial draft

* Fixes

* Extrinsic success events
  • Loading branch information
gavofyork authored and gguoss committed Sep 3, 2018
1 parent 8b3e93b commit 3da7ce4
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 15 deletions.
18 changes: 15 additions & 3 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ mod tests {
// Blake
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
// Keccak
hex!("a3f5ce86e303f4001d14124ab690428d10cd9e60d21699b42096358c2422445f").into(),
hex!("508a68a0918f614b86b2ccfd0975754f6d2abe1026a34e42d6d8d5abdf4db010").into(),
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand All @@ -276,7 +276,7 @@ mod tests {
// Blake
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
// Keccak
hex!("72dc147d2619a978adc38a38abc85bb77e25b0095ad38b15f97d56ccb66f36e8").into(),
hex!("171f1b2c01c9c616e40ee2d842a699286b50a5a74874b56d826094dadedffb27").into(),
vec![
BareExtrinsic {
signed: bob(),
Expand All @@ -299,7 +299,7 @@ mod tests {
// Blake
// hex!("2c7231a9c210a7aa4bea169d944bc4aaacd517862b244b8021236ffa7f697991").into(),
// Keccak
hex!("7aa14ff631321ca5aa22e6fa53e3569faa732758993fa82e2dbde31a1b720391").into(),
hex!("e45221804da3a3609454d4e09debe6364cc6af63c2ff067d802d1af62fea32ae").into(),
vec![BareExtrinsic {
signed: alice(),
index: 0,
Expand All @@ -321,6 +321,10 @@ mod tests {
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::balances(balances::RawEvent::NewAccount(bob(), 1, balances::NewAccountOutcome::NoHint))
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::system(system::Event::ExtrinsicSuccess)
}
]);
});
Expand All @@ -331,6 +335,14 @@ mod tests {
assert_eq!(Balances::total_balance(&alice()), 30);
assert_eq!(Balances::total_balance(&bob()), 78);
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
event: Event::system(system::Event::ExtrinsicSuccess)
},
EventRecord {
phase: Phase::ApplyExtrinsic(1),
event: Event::system(system::Event::ExtrinsicSuccess)
},
EventRecord {
phase: Phase::Finalization,
event: Event::session(session::RawEvent::NewSession(1))
Expand Down
4 changes: 0 additions & 4 deletions demo/runtime/wasm/Cargo.lock

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

Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion substrate/bft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ mod tests {

let mut second = from_block_number(3);
second.parent_hash = first_hash;
let second_hash = second.hash();
let _second_hash = second.hash();

let mut first_bft = service.build_upon(&first).unwrap().unwrap();
assert!(service.live_agreement.lock().as_ref().unwrap().0 == first);
Expand Down
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions substrate/runtime-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,16 @@ macro_rules! impl_outer_event {
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $name {
system(system::Event),
$(
$module($module::Event<$trait>),
)*
}
impl From<system::Event> for $name {
fn from(x: system::Event) -> Self {
$name::system(x)
}
}
$(
impl From<$module::Event<$trait>> for $name {
fn from(x: $module::Event<$trait>) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl<
// decode parameters and dispatch
let r = xt.apply();

<system::Module<System>>::note_applied_extrinsic();
<system::Module<System>>::note_applied_extrinsic(&r);

r.map(|_| internal::ApplyOutcome::Success).or_else(|e| Ok(internal::ApplyOutcome::Fail(e)))
}
Expand Down Expand Up @@ -233,6 +233,7 @@ mod tests {
use primitives::BuildStorage;
use primitives::traits::{HasPublicAux, Identity, Header as HeaderT, BlakeTwo256, AuxLookup};
use primitives::testing::{Digest, Header, Block};
use system;

struct NullLookup;
impl AuxLookup for NullLookup {
Expand Down
39 changes: 33 additions & 6 deletions substrate/runtime/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub trait Trait: Eq + Clone {
Hash = Self::Hash,
Digest = Self::Digest
>;
type Event: Parameter + Member;
type Event: Parameter + Member + From<Event>;
}

decl_module! {
Expand All @@ -110,6 +110,20 @@ pub struct EventRecord<E: Parameter + Member> {
pub event: E,
}

/// Event for the system module.
#[derive(Encode, Decode, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub enum Event {
/// An extrinsic completed successfully.
ExtrinsicSuccess,
/// An extrinsic failed.
ExtrinsicFailed,
}

impl From<Event> for () {
fn from(_: Event) -> () { () }
}

decl_storage! {
trait Store for Module<T: Trait> as System {

Expand Down Expand Up @@ -230,7 +244,11 @@ impl<T: Trait> Module<T> {
}

/// To be called immediately after an extrinsic has been applied.
pub fn note_applied_extrinsic() {
pub fn note_applied_extrinsic(r: &Result<(), &'static str>) {
Self::deposit_event(match r {
Ok(_) => Event::ExtrinsicSuccess,
Err(_) => Event::ExtrinsicFailed,
}.into());
<ExtrinsicIndex<T>>::put(<ExtrinsicIndex<T>>::get().unwrap_or_default() + 1u32);
}

Expand Down Expand Up @@ -301,6 +319,15 @@ mod tests {
type Event = u16;
}

impl From<Event> for u16 {
fn from(e: Event) -> u16 {
match e {
Event::ExtrinsicSuccess => 100,
Event::ExtrinsicFailed => 101,
}
}
}

type System = Module<Test>;

fn new_test_ext() -> runtime_io::TestExternalities<KeccakHasher> {
Expand All @@ -318,15 +345,15 @@ mod tests {

System::initialise(&2, &[0u8; 32].into(), &[0u8; 32].into());
System::deposit_event(42u16);
System::note_applied_extrinsic();
System::deposit_event(69u16);
System::note_applied_extrinsic();
System::note_applied_extrinsic(&Ok(()));
System::note_applied_extrinsic(&Err(""));
System::note_finished_extrinsics();
System::deposit_event(3u16);
System::finalise();
assert_eq!(System::events(), vec![
EventRecord { phase: Phase::ApplyExtrinsic(0), event: 42u16 },
EventRecord { phase: Phase::ApplyExtrinsic(1), event: 69u16 },
EventRecord { phase: Phase::ApplyExtrinsic(0), event: 100u16 },
EventRecord { phase: Phase::ApplyExtrinsic(1), event: 101u16 },
EventRecord { phase: Phase::Finalization, event: 3u16 }
]);
});
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 3da7ce4

Please sign in to comment.