Skip to content

Commit

Permalink
introduce config param to fetch state version instead using a default…
Browse files Browse the repository at this point in the history
… one
  • Loading branch information
vedhavyas committed Sep 23, 2023
1 parent 20be5f3 commit d9bdb2b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions polkadot/runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ impl Contains<RuntimeCall> for BaseFilter {
parameter_types! {
pub const Version: RuntimeVersion = VERSION;
pub const SS58Prefix: u8 = 2;
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;
}

impl frame_system::Config for Runtime {
Expand All @@ -194,6 +195,7 @@ impl frame_system::Config for Runtime {
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub fn native_version() -> NativeVersion {
parameter_types! {
pub const Version: RuntimeVersion = VERSION;
pub const SS58Prefix: u8 = 0;
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;
}

impl frame_system::Config for Runtime {
Expand All @@ -177,6 +178,7 @@ impl frame_system::Config for Runtime {
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl Contains<RuntimeCall> for BaseFilter {
parameter_types! {
pub const Version: RuntimeVersion = VERSION;
pub const SS58Prefix: u8 = 42;
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;
}

impl frame_system::Config for Runtime {
Expand All @@ -171,6 +172,7 @@ impl frame_system::Config for Runtime {
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub fn native_version() -> NativeVersion {
parameter_types! {
pub const Version: RuntimeVersion = VERSION;
pub const SS58Prefix: u8 = 42;
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;
}

impl frame_system::Config for Runtime {
Expand All @@ -170,6 +171,7 @@ impl frame_system::Config for Runtime {
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

parameter_types! {
Expand Down
28 changes: 23 additions & 5 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,22 @@ const LOG_TARGET: &str = "runtime::system";
///
/// The merkle proof is using the same trie as runtime state with
/// `state_version` 0.
pub fn extrinsics_root<H: Hash, E: codec::Encode>(extrinsics: &[E]) -> H::Output {
extrinsics_data_root::<H>(extrinsics.iter().map(codec::Encode::encode).collect())
pub fn extrinsics_root<H: Hash, E: codec::Encode>(
extrinsics: &[E],
state_version: sp_core::storage::StateVersion,
) -> H::Output {
extrinsics_data_root::<H>(extrinsics.iter().map(codec::Encode::encode).collect(), state_version)
}

/// Compute the trie root of a list of extrinsics.
///
/// The merkle proof is using the same trie as runtime state with
/// `state_version` 0.
pub fn extrinsics_data_root<H: Hash>(xts: Vec<Vec<u8>>) -> H::Output {
H::ordered_trie_root(xts, sp_core::storage::StateVersion::V0)
pub fn extrinsics_data_root<H: Hash>(
xts: Vec<Vec<u8>>,
state_version: sp_core::storage::StateVersion,
) -> H::Output {
H::ordered_trie_root(xts, state_version)
}

/// An object to track the currently used extrinsic weight in a block.
Expand Down Expand Up @@ -205,6 +211,9 @@ pub mod pallet {

/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
pub mod config_preludes {
use sp_core::parameter_types;
use sp_core::storage::StateVersion;

use super::{inject_runtime_type, DefaultConfig};

/// Provides a viable default config that can be used with
Expand All @@ -215,6 +224,10 @@ pub mod pallet {
/// a downstream user of this particular `TestDefaultConfig`
pub struct TestDefaultConfig;

parameter_types! {
pub const ExtrinsicsRootStateVersion: StateVersion = StateVersion::V0;
}

#[frame_support::register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type Nonce = u32;
Expand Down Expand Up @@ -243,6 +256,7 @@ pub mod pallet {
type BaseCallFilter = frame_support::traits::Everything;
type BlockHashCount = frame_support::traits::ConstU64<10>;
type OnSetCode = ();
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}
}

Expand Down Expand Up @@ -401,6 +415,9 @@ pub mod pallet {

/// The maximum number of consumers allowed on a single account.
type MaxConsumers: ConsumerLimits;

/// State verison used to derive extrinsics root.
type ExtrinsicsRootStateVersion: Get<sp_core::storage::StateVersion>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -1447,7 +1464,8 @@ impl<T: Config> Pallet<T> {
let extrinsics = (0..ExtrinsicCount::<T>::take().unwrap_or_default())
.map(ExtrinsicData::<T>::take)
.collect();
let extrinsics_root = extrinsics_data_root::<T::Hashing>(extrinsics);
let extrinsics_root =
extrinsics_data_root::<T::Hashing>(extrinsics, T::ExtrinsicsRootStateVersion::get());

// move block hash pruning window by one block
let block_hash_count = T::BlockHashCount::get();
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ fn extrinsics_root_is_calculated_correctly() {
System::note_finished_extrinsics();
let header = System::finalize();

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

0 comments on commit d9bdb2b

Please sign in to comment.