Adds new event phase Initialization (#5302)

* Adds new event phase `Initialization`

Every event that was deposited inside of `on_initialize` was assigned to
the `ApplyExtrinsic(0)` phase which wasn't correct. This pr introduces a
new phase `Initialization`. This is the new phase while initializing
a block. After initialization we switch to `ApplyExtrinsic(N)` and at
the end to `Finalization` as before.

* Set `ExecutionPhase` in `initialize`

* Increment `spec_version`
This commit is contained in:
Bastian Köcher
2020-03-20 19:34:51 +01:00
committed by GitHub
parent 459557aadc
commit 7693bd5ee5
5 changed files with 96 additions and 71 deletions
+13 -13
View File
@@ -608,7 +608,7 @@ mod tests {
System::set_block_number(4);
assert_ok!(Collective::close(Origin::signed(4), hash.clone(), 0));
let record = |event| EventRecord { phase: Phase::Finalization, event, topics: vec![] };
let record = |event| EventRecord { phase: Phase::Initialization, event, topics: vec![] };
assert_eq!(System::events(), vec![
record(Event::collective_Instance1(RawEvent::Proposed(1, 0, hash.clone(), 3))),
record(Event::collective_Instance1(RawEvent::Voted(2, hash.clone(), true, 2, 0))),
@@ -632,7 +632,7 @@ mod tests {
System::set_block_number(4);
assert_ok!(Collective::close(Origin::signed(4), hash.clone(), 0));
let record = |event| EventRecord { phase: Phase::Finalization, event, topics: vec![] };
let record = |event| EventRecord { phase: Phase::Initialization, event, topics: vec![] };
assert_eq!(System::events(), vec![
record(Event::collective_Instance1(RawEvent::Proposed(1, 0, hash.clone(), 3))),
record(Event::collective_Instance1(RawEvent::Voted(2, hash.clone(), true, 2, 0))),
@@ -656,7 +656,7 @@ mod tests {
System::set_block_number(4);
assert_ok!(Collective::close(Origin::signed(4), hash.clone(), 0));
let record = |event| EventRecord { phase: Phase::Finalization, event, topics: vec![] };
let record = |event| EventRecord { phase: Phase::Initialization, event, topics: vec![] };
assert_eq!(System::events(), vec![
record(Event::collective_Instance1(RawEvent::Proposed(1, 0, hash.clone(), 3))),
record(Event::collective_Instance1(RawEvent::Voted(2, hash.clone(), true, 2, 0))),
@@ -754,7 +754,7 @@ mod tests {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Proposed(
1,
0,
@@ -835,7 +835,7 @@ mod tests {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Proposed(
1,
0,
@@ -845,7 +845,7 @@ mod tests {
topics: vec![],
},
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Voted(
1,
hex!["68eea8f20b542ec656c6ac2d10435ae3bd1729efc34d1354ab85af840aad2d35"].into(),
@@ -884,7 +884,7 @@ mod tests {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(
RawEvent::Proposed(
1,
@@ -895,7 +895,7 @@ mod tests {
topics: vec![],
},
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Voted(
2,
hex!["68eea8f20b542ec656c6ac2d10435ae3bd1729efc34d1354ab85af840aad2d35"].into(),
@@ -906,7 +906,7 @@ mod tests {
topics: vec![],
},
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Disapproved(
hex!["68eea8f20b542ec656c6ac2d10435ae3bd1729efc34d1354ab85af840aad2d35"].into(),
)),
@@ -927,7 +927,7 @@ mod tests {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Proposed(
1,
0,
@@ -937,7 +937,7 @@ mod tests {
topics: vec![],
},
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Voted(
2,
hex!["68eea8f20b542ec656c6ac2d10435ae3bd1729efc34d1354ab85af840aad2d35"].into(),
@@ -948,14 +948,14 @@ mod tests {
topics: vec![],
},
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Approved(
hex!["68eea8f20b542ec656c6ac2d10435ae3bd1729efc34d1354ab85af840aad2d35"].into(),
)),
topics: vec![],
},
EventRecord {
phase: Phase::Finalization,
phase: Phase::Initialization,
event: Event::collective_Instance1(RawEvent::Executed(
hex!["68eea8f20b542ec656c6ac2d10435ae3bd1729efc34d1354ab85af840aad2d35"].into(),
false,
+52 -52
View File
@@ -422,44 +422,44 @@ fn instantiate_and_call_and_deposit_event() {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(BOB)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(
pallet_balances::RawEvent::Endowed(BOB, 100)
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Transfer(ALICE, BOB, 100)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::ContractExecution(BOB, vec![1, 2, 3, 4])),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, BOB)),
topics: vec![],
}
@@ -505,17 +505,17 @@ fn dispatch_call() {
// wasm source this test will fail and will show you the actual hash.
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
@@ -539,58 +539,58 @@ fn dispatch_call() {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(BOB)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(
pallet_balances::RawEvent::Endowed(BOB, 100)
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Transfer(ALICE, BOB, 100)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, BOB)),
topics: vec![],
},
// Dispatching the call.
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(CHARLIE)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(
pallet_balances::RawEvent::Endowed(CHARLIE, 50)
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(
pallet_balances::RawEvent::Transfer(BOB, CHARLIE, 50)
),
@@ -599,7 +599,7 @@ fn dispatch_call() {
// Event emitted as a result of dispatch.
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Dispatched(BOB, true)),
topics: vec![],
}
@@ -643,17 +643,17 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() {
// wasm source this test will fail and will show you the actual hash.
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
@@ -681,39 +681,39 @@ fn dispatch_call_not_dispatched_after_top_level_transaction_failure() {
);
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(BOB)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(
pallet_balances::RawEvent::Endowed(BOB, 100)
),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Transfer(ALICE, BOB, 100)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(ALICE, BOB)),
topics: vec![],
},
@@ -898,17 +898,17 @@ fn test_set_rent_code_and_hash() {
// and will show you the actual hash.
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(code_hash.into())),
topics: vec![],
},
@@ -1225,7 +1225,7 @@ fn call_removed_contract() {
// Calling a contract that is about to evict shall emit an event.
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Evicted(BOB, true)),
topics: vec![],
},
@@ -1414,22 +1414,22 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
// and will show you the actual hash.
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(1)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(1, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(restoration_code_hash.into())),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::CodeStored(set_rent_code_hash.into())),
topics: vec![],
},
@@ -1473,7 +1473,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
assert!(ContractInfoOf::<Test>::get(BOB).unwrap().get_tombstone().is_some());
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(
RawEvent::Evicted(BOB.clone(), true)
),
@@ -1526,7 +1526,7 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
(true, false) => {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(
RawEvent::Restored(DJANGO, BOB, bob_code_hash, 50, false)
),
@@ -1537,42 +1537,42 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
(_, true) => {
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Evicted(BOB, true)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(CHARLIE)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(CHARLIE, 1_000_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(frame_system::RawEvent::NewAccount(DJANGO)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::balances(pallet_balances::RawEvent::Endowed(DJANGO, 30_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Transfer(CHARLIE, DJANGO, 30_000)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Instantiated(CHARLIE, DJANGO)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(RawEvent::Restored(
DJANGO,
BOB,
@@ -1599,12 +1599,12 @@ fn restoration(test_different_storage: bool, test_restore_to_with_dirty_storage:
assert!(ContractInfoOf::<Test>::get(DJANGO).is_none());
assert_eq!(System::events(), vec![
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::system(system::RawEvent::KilledAccount(DJANGO)),
topics: vec![],
},
EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: MetaEvent::contracts(
RawEvent::Restored(DJANGO, BOB, bob_contract.code_hash, 50, true)
),
+2
View File
@@ -199,6 +199,8 @@ where
<frame_system::Module<System>>::register_extra_weight_unchecked(
<AllModules as WeighBlock<System::BlockNumber>>::on_finalize(*block_number)
);
frame_system::Module::<System>::note_finished_initialize();
}
/// Returns if the runtime was upgraded since the last time this function was called.
+2 -2
View File
@@ -129,7 +129,7 @@ fn should_deposit_event() {
assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: TestEvent::offences(crate::Event::Offence(KIND, time_slot.encode())),
topics: vec![],
}]
@@ -164,7 +164,7 @@ fn doesnt_deposit_event_for_dups() {
assert_eq!(
System::events(),
vec![EventRecord {
phase: Phase::ApplyExtrinsic(0),
phase: Phase::Initialization,
event: TestEvent::offences(crate::Event::Offence(KIND, time_slot.encode())),
topics: vec![],
}]
+27 -4
View File
@@ -235,8 +235,16 @@ pub type KeyValue = (Vec<u8>, Vec<u8>);
pub enum Phase {
/// Applying an extrinsic.
ApplyExtrinsic(u32),
/// The end.
/// Finalizing the block.
Finalization,
/// Initializing the block.
Initialization,
}
impl Default for Phase {
fn default() -> Self {
Self::Initialization
}
}
/// Record of an event happening.
@@ -393,6 +401,9 @@ decl_storage! {
/// Stores the `spec_version` and `spec_name` of when the last runtime upgrade happened.
pub LastRuntimeUpgrade build(|_| Some(LastRuntimeUpgradeInfo::from(T::Version::get()))): Option<LastRuntimeUpgradeInfo>;
/// The execution phase of the block.
ExecutionPhase: Option<Phase>;
}
add_extra_genesis {
config(changes_trie_config): Option<ChangesTrieConfiguration>;
@@ -710,8 +721,7 @@ impl<T: Trait> Module<T> {
/// This will update storage entries that correspond to the specified topics.
/// It is expected that light-clients could subscribe to this topics.
pub fn deposit_event_indexed(topics: &[T::Hash], event: T::Event) {
let extrinsic_index = Self::extrinsic_index();
let phase = extrinsic_index.map_or(Phase::Finalization, |c| Phase::ApplyExtrinsic(c));
let phase = ExecutionPhase::get().unwrap_or_default();
let event = EventRecord {
phase,
event,
@@ -803,6 +813,7 @@ impl<T: Trait> Module<T> {
kind: InitKind,
) {
// populate environment
ExecutionPhase::put(Phase::Initialization);
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
<Number<T>>::put(number);
<Digest<T>>::put(digest);
@@ -819,6 +830,7 @@ impl<T: Trait> Module<T> {
/// Remove temporary "environment" entries in storage.
pub fn finalize() -> T::Header {
ExecutionPhase::kill();
ExtrinsicCount::kill();
AllExtrinsicsWeight::kill();
AllExtrinsicsLen::kill();
@@ -949,6 +961,7 @@ impl<T: Trait> Module<T> {
let next_extrinsic_index = Self::extrinsic_index().unwrap_or_default() + 1u32;
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &next_extrinsic_index);
ExecutionPhase::put(Phase::ApplyExtrinsic(next_extrinsic_index));
}
/// To be called immediately after `note_applied_extrinsic` of the last extrinsic of the block
@@ -957,6 +970,13 @@ impl<T: Trait> Module<T> {
let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX)
.unwrap_or_default();
ExtrinsicCount::put(extrinsic_index);
ExecutionPhase::put(Phase::Finalization);
}
/// To be called immediately after finishing the initialization of the block
/// (e.g., called `on_initialize` for all modules).
pub fn note_finished_initialize() {
ExecutionPhase::put(Phase::ApplyExtrinsic(0))
}
/// Remove all extrinsic data and save the extrinsics trie root.
@@ -1645,6 +1665,8 @@ mod tests {
&Default::default(),
InitKind::Full,
);
System::deposit_event(32u16);
System::note_finished_initialize();
System::deposit_event(42u16);
System::note_applied_extrinsic(&Ok(()), 0, Default::default());
System::note_applied_extrinsic(&Err(DispatchError::BadOrigin), 0, Default::default());
@@ -1654,6 +1676,7 @@ mod tests {
assert_eq!(
System::events(),
vec![
EventRecord { phase: Phase::Initialization, event: 32u16, topics: vec![] },
EventRecord { phase: Phase::ApplyExtrinsic(0), event: 42u16, topics: vec![] },
EventRecord { phase: Phase::ApplyExtrinsic(0), event: 100u16, topics: vec![] },
EventRecord { phase: Phase::ApplyExtrinsic(1), event: 101u16, topics: vec![] },
@@ -1997,7 +2020,7 @@ mod tests {
assert_eq!(
System::events(),
vec![EventRecord { phase: Phase::ApplyExtrinsic(0), event: 102u16, topics: vec![] }],
vec![EventRecord { phase: Phase::Initialization, event: 102u16, topics: vec![] }],
);
});
}