Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make it possible to calculate the storage root as often as you want #7714

Merged
merged 5 commits into from
Dec 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix bug with incorrectly calculating the extrinscs root
  • Loading branch information
bkchr committed Dec 11, 2020
commit bc5e925f7155439028dfc64cda2ab19c1a39629a
14 changes: 4 additions & 10 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,13 +1011,15 @@ impl<T: Config> Module<T> {
/// Remove temporary "environment" entries in storage.
pub fn finalize() -> T::Header {
ExecutionPhase::kill();
ExtrinsicCount::kill();
AllExtrinsicsLen::kill();

let number = <Number<T>>::take();
let parent_hash = <ParentHash<T>>::take();
let mut digest = <Digest<T>>::take();
let extrinsics_root = Self::derive_extrinsics_root();
let extrinsics = (0..ExtrinsicCount::take().unwrap_or_default())
.map(ExtrinsicData::take)
.collect();
let extrinsics_root = extrinsics_data_root::<T::Hashing>(extrinsics);

// move block hash pruning window by one block
let block_hash_count = T::BlockHashCount::get();
Expand Down Expand Up @@ -1168,14 +1170,6 @@ impl<T: Config> Module<T> {
ExecutionPhase::put(Phase::ApplyExtrinsic(0))
}

/// Remove all extrinsic data and calculate the extrinsics root.
fn derive_extrinsics_root() -> T::Hash {
let extrinsics = (0..ExtrinsicCount::get().unwrap_or_default())
.map(ExtrinsicData::take)
.collect();
extrinsics_data_root::<T::Hashing>(extrinsics)
}

/// An account is being created.
pub fn on_created_account(who: T::AccountId) {
T::OnNewAccount::on_new_account(&who);
Expand Down
27 changes: 26 additions & 1 deletion frame/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::*;
use mock::{*, Origin};
use sp_core::H256;
use sp_runtime::DispatchError;
use sp_runtime::{DispatchError, traits::{Header, BlakeTwo256}};
use frame_support::weights::WithPostDispatchInfo;

#[test]
Expand Down Expand Up @@ -417,3 +417,28 @@ fn ensure_one_of_works() {
assert_eq!(ensure_root_or_signed(RawOrigin::Signed(0)).unwrap(), Either::Right(0));
assert!(ensure_root_or_signed(RawOrigin::None).is_err())
}

#[test]
fn extrinsics_root_is_calculated_correctly() {
new_test_ext().execute_with(|| {
System::initialize(
&1,
&[0u8; 32].into(),
&Default::default(),
InitKind::Full,
);
System::note_finished_initialize();
System::note_extrinsic(vec![1]);
System::note_applied_extrinsic(&Ok(().into()), Default::default());
System::note_extrinsic(vec![2]);
System::note_applied_extrinsic(
&Err(DispatchError::BadOrigin.into()),
Default::default()
);
System::note_finished_extrinsics();
let header = System::finalize();

let ext_root = extrinsics_data_root::<BlakeTwo256>(vec![vec![1], vec![2]]);
assert_eq!(ext_root, *header.extrinsics_root());
});
}