mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 13:31:10 +00:00
Events to track extrinsic success (#640)
* Initial draft * Fixes * Extrinsic success events
This commit is contained in:
@@ -260,7 +260,7 @@ mod tests {
|
||||
// Blake
|
||||
// hex!("3437bf4b182ab17bb322af5c67e55f6be487a77084ad2b4e27ddac7242e4ad21").into(),
|
||||
// Keccak
|
||||
hex!("a3f5ce86e303f4001d14124ab690428d10cd9e60d21699b42096358c2422445f").into(),
|
||||
hex!("508a68a0918f614b86b2ccfd0975754f6d2abe1026a34e42d6d8d5abdf4db010").into(),
|
||||
vec![BareExtrinsic {
|
||||
signed: alice(),
|
||||
index: 0,
|
||||
@@ -276,7 +276,7 @@ mod tests {
|
||||
// Blake
|
||||
// hex!("741fcb660e6fa9f625fbcd993b49f6c1cc4040f5e0cc8727afdedf11fd3c464b").into(),
|
||||
// Keccak
|
||||
hex!("72dc147d2619a978adc38a38abc85bb77e25b0095ad38b15f97d56ccb66f36e8").into(),
|
||||
hex!("171f1b2c01c9c616e40ee2d842a699286b50a5a74874b56d826094dadedffb27").into(),
|
||||
vec![
|
||||
BareExtrinsic {
|
||||
signed: bob(),
|
||||
@@ -299,7 +299,7 @@ mod tests {
|
||||
// Blake
|
||||
// hex!("2c7231a9c210a7aa4bea169d944bc4aaacd517862b244b8021236ffa7f697991").into(),
|
||||
// Keccak
|
||||
hex!("7aa14ff631321ca5aa22e6fa53e3569faa732758993fa82e2dbde31a1b720391").into(),
|
||||
hex!("e45221804da3a3609454d4e09debe6364cc6af63c2ff067d802d1af62fea32ae").into(),
|
||||
vec![BareExtrinsic {
|
||||
signed: alice(),
|
||||
index: 0,
|
||||
@@ -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)
|
||||
}
|
||||
]);
|
||||
});
|
||||
@@ -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))
|
||||
|
||||
Generated
-4
@@ -652,15 +652,11 @@ dependencies = [
|
||||
"substrate-codec-derive 0.1.0",
|
||||
"substrate-keyring 0.1.0",
|
||||
"substrate-primitives 0.1.0",
|
||||
"substrate-runtime-consensus 0.1.0",
|
||||
"substrate-runtime-io 0.1.0",
|
||||
"substrate-runtime-primitives 0.1.0",
|
||||
"substrate-runtime-sandbox 0.1.0",
|
||||
"substrate-runtime-session 0.1.0",
|
||||
"substrate-runtime-std 0.1.0",
|
||||
"substrate-runtime-support 0.1.0",
|
||||
"substrate-runtime-system 0.1.0",
|
||||
"substrate-runtime-timestamp 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
BIN
Binary file not shown.
Binary file not shown.
@@ -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);
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -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 {
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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! {
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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> {
|
||||
@@ -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 }
|
||||
]);
|
||||
});
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user