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
+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![] }],
);
});
}