From fb2fd5da02ca6bc86ef7445708c55292084fecd8 Mon Sep 17 00:00:00 2001 From: vedhavyas Date: Sat, 23 Sep 2023 09:02:01 +0200 Subject: [PATCH] introduce config param to fetch state version instead using a default one --- polkadot/runtime/kusama/src/lib.rs | 2 ++ polkadot/runtime/polkadot/src/lib.rs | 2 ++ polkadot/runtime/rococo/src/lib.rs | 2 ++ polkadot/runtime/westend/src/lib.rs | 2 ++ substrate/frame/system/src/lib.rs | 28 ++++++++++++++++++++----- substrate/frame/system/src/tests.rs | 2 +- substrate/test-utils/runtime/src/lib.rs | 2 ++ 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index 659a7052d2b73..f59fa3c597414 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -168,6 +168,7 @@ impl Contains 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 { @@ -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! { diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index 45ea561b33fa5..7c6036586c863 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -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 { @@ -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! { diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index e043852901f17..c595f2b256c3f 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -145,6 +145,7 @@ impl Contains 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 { @@ -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! { diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 7dfc781d2467e..a1f48090a2208 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -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 { @@ -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! { diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 84b6dc031457d..a17ef043d38c8 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -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(extrinsics: &[E]) -> H::Output { - extrinsics_data_root::(extrinsics.iter().map(codec::Encode::encode).collect()) +pub fn extrinsics_root( + extrinsics: &[E], + state_version: sp_core::storage::StateVersion, +) -> H::Output { + extrinsics_data_root::(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(xts: Vec>) -> H::Output { - H::ordered_trie_root(xts, sp_core::storage::StateVersion::V0) +pub fn extrinsics_data_root( + xts: Vec>, + 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. @@ -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 @@ -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; @@ -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; } } @@ -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; } #[pallet::pallet] @@ -1447,7 +1464,8 @@ impl Pallet { let extrinsics = (0..ExtrinsicCount::::take().unwrap_or_default()) .map(ExtrinsicData::::take) .collect(); - let extrinsics_root = extrinsics_data_root::(extrinsics); + let extrinsics_root = + extrinsics_data_root::(extrinsics, T::ExtrinsicsRootStateVersion::get()); // move block hash pruning window by one block let block_hash_count = T::BlockHashCount::get(); diff --git a/substrate/frame/system/src/tests.rs b/substrate/frame/system/src/tests.rs index 165df688b1c2c..54b3877a6abca 100644 --- a/substrate/frame/system/src/tests.rs +++ b/substrate/frame/system/src/tests.rs @@ -721,7 +721,7 @@ fn extrinsics_root_is_calculated_correctly() { System::note_finished_extrinsics(); let header = System::finalize(); - let ext_root = extrinsics_data_root::(vec![vec![1], vec![2]]); + let ext_root = extrinsics_data_root::(vec![vec![1], vec![2]], sp_core::storage::StateVersion::V0); assert_eq!(ext_root, *header.extrinsics_root()); }); } diff --git a/substrate/test-utils/runtime/src/lib.rs b/substrate/test-utils/runtime/src/lib.rs index b116c8556815f..7a9e78b3ef37b 100644 --- a/substrate/test-utils/runtime/src/lib.rs +++ b/substrate/test-utils/runtime/src/lib.rs @@ -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); @@ -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 {