From 868cc81413a527f8a73b5d860a27477760d49b52 Mon Sep 17 00:00:00 2001 From: Aton Date: Mon, 8 Apr 2019 17:29:46 +0800 Subject: [PATCH 1/2] use a Event Cache to replace vec `Events` in `deposit_event` issue 2223 https://github.com/paritytech/substrate/issues/2223 suggestion 1 use a map `EventsCache` to replace vec `Events` in `deposit_event`, and flush cache into `Events` on `finalize()` --- srml/system/src/lib.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/srml/system/src/lib.rs b/srml/system/src/lib.rs index f226fcb7d1855..cf0a1a891ad24 100644 --- a/srml/system/src/lib.rs +++ b/srml/system/src/lib.rs @@ -180,9 +180,11 @@ decl_module! { pub fn deposit_event(event: T::Event) { let extrinsic_index = Self::extrinsic_index(); let phase = extrinsic_index.map_or(Phase::Finalization, |c| Phase::ApplyExtrinsic(c)); - let mut events = Self::events(); - events.push(EventRecord { phase, event }); - >::put(events); + + EventsCacheCount::::mutate(|index| { + EventsCache::::insert(*index, EventRecord { phase, event }); + *index += 1; + }); } } } @@ -306,6 +308,10 @@ decl_storage! { Digest get(digest): T::Digest; /// Events deposited for the current block. Events get(events): Vec>; + /// Events map cache for the current block. related to issue 2223 https://github.com/paritytech/substrate/issues/2223 + EventsCache: map u32 => Option>; + /// Events map cache index + EventsCacheCount: u32 = 0; } add_extra_genesis { config(changes_trie_config): Option; @@ -389,6 +395,8 @@ impl Module { >::put(txs_root); >::put(Self::calculate_random()); >::kill(); + + >::kill(); } /// Remove temporary "environment" entries in storage. @@ -413,10 +421,24 @@ impl Module { } // > stays to be inspected by the client. + // flush all event cache into `Events`, related to issue 2223 https://github.com/paritytech/substrate/issues/2223 + Self::flush_events(); ::new(number, extrinsics_root, storage_root, parent_hash, digest) } + pub fn flush_events() { + let mut events = Self::events(); + for i in 0..EventsCacheCount::::get() { + if let Some(e) = EventsCache::::get(i) { + events.push(e); + } + EventsCache::::remove(i); + } + EventsCacheCount::::kill(); + >::put(events); + } + /// Deposits a log and ensures it matches the blocks log data. pub fn deposit_log(item: ::Item) { let mut l = >::get(); From 9d7ff939368abb41c097bbc16e93970da203cd1b Mon Sep 17 00:00:00 2001 From: Aton Date: Mon, 8 Apr 2019 18:05:52 +0800 Subject: [PATCH 2/2] fix test --- srml/contract/src/tests.rs | 3 +++ srml/council/src/motions.rs | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/srml/contract/src/tests.rs b/srml/contract/src/tests.rs index 2f80023322d14..a34f5f369b9fb 100644 --- a/srml/contract/src/tests.rs +++ b/srml/contract/src/tests.rs @@ -341,6 +341,7 @@ fn instantiate_and_call_and_deposit_event() { vec![], ); + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), @@ -416,6 +417,7 @@ fn dispatch_call() { // Let's keep this assert even though it's redundant. If you ever need to update the // wasm source this test will fail and will show you the actual hash. + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), @@ -443,6 +445,7 @@ fn dispatch_call() { vec![], )); + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), diff --git a/srml/council/src/motions.rs b/srml/council/src/motions.rs index 3bbe463780c46..f704c1bed45f4 100644 --- a/srml/council/src/motions.rs +++ b/srml/council/src/motions.rs @@ -231,6 +231,7 @@ mod tests { assert_eq!(CouncilMotions::proposal_of(&hash), Some(proposal)); assert_eq!(CouncilMotions::voting(&hash), Some((0, 3, vec![1], Vec::::new()))); + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), @@ -284,6 +285,7 @@ mod tests { assert_eq!(CouncilMotions::voting(&hash), Some((0, 2, Vec::::new(), vec![1]))); assert_noop!(CouncilMotions::vote(Origin::signed(1), hash.clone(), 0, false), "duplicate vote ignored"); + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), @@ -306,6 +308,7 @@ mod tests { assert_ok!(CouncilMotions::propose(Origin::signed(1), 3, Box::new(proposal.clone()))); assert_ok!(CouncilMotions::vote(Origin::signed(2), hash.clone(), 0, false)); + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0), @@ -332,6 +335,7 @@ mod tests { assert_ok!(CouncilMotions::propose(Origin::signed(1), 2, Box::new(proposal.clone()))); assert_ok!(CouncilMotions::vote(Origin::signed(2), hash.clone(), 0, true)); + System::flush_events(); assert_eq!(System::events(), vec![ EventRecord { phase: Phase::ApplyExtrinsic(0),