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 25, 2023
1 parent fa4c672 commit 4ef2460
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 8 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 @@ -172,6 +172,7 @@ impl Contains<RuntimeCall> for IdentityCalls {
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 @@ -198,6 +199,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 @@ -163,6 +163,7 @@ impl Contains<RuntimeCall> for IdentityCalls {
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 @@ -189,6 +190,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 @@ -149,6 +149,7 @@ impl Contains<RuntimeCall> for IdentityCalls {
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 @@ -175,6 +176,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 @@ -156,6 +156,7 @@ impl Contains<RuntimeCall> for IdentityCalls {
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 @@ -182,6 +183,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 substrate/frame/bags-list/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ parameter_types! {
// Set the vote weight for any id who's weight has _not_ been set with `set_score_of`.
pub static NextVoteWeight: VoteWeight = 0;
pub static NextVoteWeightMap: HashMap<AccountId, VoteWeight> = Default::default();
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;
}

pub struct StakingMock;
Expand Down Expand Up @@ -72,6 +73,7 @@ impl frame_system::Config for Runtime {
type SystemWeightInfo = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions substrate/frame/nomination-pools/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ parameter_types! {
pub static MaxUnbonding: u32 = 8;
pub static StakingMinBond: Balance = 10;
pub storage Nominations: Option<Vec<AccountId>> = None;
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;
}

pub struct StakingMock;
Expand Down Expand Up @@ -205,6 +206,7 @@ impl frame_system::Config for Runtime {
type SystemWeightInfo = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

parameter_types! {
Expand Down
32 changes: 25 additions & 7 deletions substrate/frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,23 @@ const LOG_TARGET: &str = "runtime::system";
/// 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_root<H: Hash, E: codec::Encode>(extrinsics: &[E]) -> H::Output {
extrinsics_data_root::<H>(extrinsics.iter().map(codec::Encode::encode).collect())
/// `state_version`.
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)
/// `state_version`.
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
2 changes: 2 additions & 0 deletions substrate/test-utils/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ const MAXIMUM_BLOCK_WEIGHT: Weight =
parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
pub const Version: RuntimeVersion = VERSION;
pub const ExtrinsicsRootStateVersion: sp_core::storage::StateVersion = sp_core::storage::StateVersion::V0;

pub RuntimeBlockLength: BlockLength =
BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
Expand Down Expand Up @@ -367,6 +368,7 @@ impl frame_system::pallet::Config for Runtime {
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
type ExtrinsicsRootStateVersion = ExtrinsicsRootStateVersion;
}

pub mod currency {
Expand Down

0 comments on commit 4ef2460

Please sign in to comment.