From 6ee7b4427434fb1b3f74909b26c4d91af2cc7946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Sat, 22 Jan 2022 23:16:16 +0100 Subject: [PATCH] Only support state version v0 from the runtime This ensures that we don't use the new host functions and just have them registered on the node. This will not require parachain teams to immediatley upgrade. --- frame/benchmarking/src/lib.rs | 2 +- frame/executive/src/lib.rs | 2 +- frame/support/src/lib.rs | 8 ++++---- frame/support/src/storage/child.rs | 4 ++-- frame/system/src/lib.rs | 3 +-- frame/transaction-storage/src/lib.rs | 3 +-- primitives/io/src/lib.rs | 16 ++++++++-------- primitives/runtime/src/traits.rs | 16 ++++++++-------- primitives/transaction-storage-proof/src/lib.rs | 2 +- test-utils/runtime/src/system.rs | 4 ++-- 10 files changed, 29 insertions(+), 31 deletions(-) diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index ca836e431e5ee..dcb6b0702995c 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -1080,7 +1080,7 @@ macro_rules! impl_benchmark { // Time the storage root recalculation. let start_storage_root = $crate::benchmarking::current_time(); - $crate::storage_root($crate::StateVersion::V1); + $crate::storage_root(); let finish_storage_root = $crate::benchmarking::current_time(); let elapsed_storage_root = finish_storage_root - start_storage_root; diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index d19ea8127bad3..520c3894690c1 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -665,7 +665,7 @@ mod tests { #[pallet::weight(0)] pub fn calculate_storage_root(_origin: OriginFor) -> DispatchResult { - let root = sp_io::storage::root(sp_runtime::StateVersion::V1); + let root = sp_io::storage::root(); sp_io::storage::set("storage_root".as_bytes(), &root); Ok(()) } diff --git a/frame/support/src/lib.rs b/frame/support/src/lib.rs index fc2754c5c555f..be5706e87aaec 100644 --- a/frame/support/src/lib.rs +++ b/frame/support/src/lib.rs @@ -751,9 +751,9 @@ macro_rules! assert_noop { $x:expr, $y:expr $(,)? ) => { - let h = $crate::storage_root($crate::StateVersion::V1); + let h = $crate::storage_root(); $crate::assert_err!($x, $y); - assert_eq!(h, $crate::storage_root($crate::StateVersion::V1)); + assert_eq!(h, $crate::storage_root()); }; } @@ -766,9 +766,9 @@ macro_rules! assert_storage_noop { ( $x:expr ) => { - let h = $crate::storage_root($crate::StateVersion::V1); + let h = $crate::storage_root(); $x; - assert_eq!(h, $crate::storage_root($crate::StateVersion::V1)); + assert_eq!(h, $crate::storage_root()); }; } diff --git a/frame/support/src/storage/child.rs b/frame/support/src/storage/child.rs index 949df84e7e768..cf4c0e0148746 100644 --- a/frame/support/src/storage/child.rs +++ b/frame/support/src/storage/child.rs @@ -168,10 +168,10 @@ pub fn put_raw(child_info: &ChildInfo, key: &[u8], value: &[u8]) { } /// Calculate current child root value. -pub fn root(child_info: &ChildInfo, version: StateVersion) -> Vec { +pub fn root(child_info: &ChildInfo, _: StateVersion) -> Vec { match child_info.child_type() { ChildType::ParentKeyId => - sp_io::default_child_storage::root(child_info.storage_key(), version), + sp_io::default_child_storage::root(child_info.storage_key()), } } diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index 800ffbfd20dc2..9857e2ac806f1 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -1329,8 +1329,7 @@ impl Pallet { >::remove(to_remove); } - let version = T::Version::get().state_version(); - let storage_root = T::Hash::decode(&mut &sp_io::storage::root(version)[..]) + let storage_root = T::Hash::decode(&mut &sp_io::storage::root()[..]) .expect("Node is configured to use the same hash; qed"); ::new( diff --git a/frame/transaction-storage/src/lib.rs b/frame/transaction-storage/src/lib.rs index d95a60b495121..96c247c393a78 100644 --- a/frame/transaction-storage/src/lib.rs +++ b/frame/transaction-storage/src/lib.rs @@ -189,7 +189,7 @@ pub mod pallet { // Chunk data and compute storage root let chunk_count = num_chunks(data.len() as u32); let chunks = data.chunks(CHUNK_SIZE).map(|c| c.to_vec()).collect(); - let root = sp_io::trie::blake2_256_ordered_root(chunks, sp_runtime::StateVersion::V1); + let root = sp_io::trie::blake2_256_ordered_root(chunks); let content_hash = sp_io::hashing::blake2_256(&data); let extrinsic_index = >::extrinsic_index() @@ -301,7 +301,6 @@ pub mod pallet { &proof.proof, &encode_index(chunk_index), &proof.chunk, - sp_runtime::StateVersion::V1, ), Error::::InvalidProof ); diff --git a/primitives/io/src/lib.rs b/primitives/io/src/lib.rs index 76ced407090c3..a677f418bec2d 100644 --- a/primitives/io/src/lib.rs +++ b/primitives/io/src/lib.rs @@ -202,7 +202,7 @@ pub trait Storage { /// The hashing algorithm is defined by the `Block`. /// /// Returns a `Vec` that holds the SCALE encoded hash. - #[version(2)] + #[version(2, register_only)] fn root(&mut self, version: StateVersion) -> Vec { self.storage_root(version) } @@ -394,7 +394,7 @@ pub trait DefaultChildStorage { /// The hashing algorithm is defined by the `Block`. /// /// Returns a `Vec` that holds the SCALE encoded hash. - #[version(2)] + #[version(2, register_only)] fn root(&mut self, storage_key: &[u8], version: StateVersion) -> Vec { let child_info = ChildInfo::new_default(storage_key); self.child_storage_root(&child_info, version) @@ -418,7 +418,7 @@ pub trait Trie { } /// A trie root formed from the iterated items. - #[version(2)] + #[version(2, register_only)] fn blake2_256_root(input: Vec<(Vec, Vec)>, version: StateVersion) -> H256 { match version { StateVersion::V0 => LayoutV0::::trie_root(input), @@ -432,7 +432,7 @@ pub trait Trie { } /// A trie root formed from the enumerated items. - #[version(2)] + #[version(2, register_only)] fn blake2_256_ordered_root(input: Vec>, version: StateVersion) -> H256 { match version { StateVersion::V0 => LayoutV0::::ordered_trie_root(input), @@ -446,7 +446,7 @@ pub trait Trie { } /// A trie root formed from the iterated items. - #[version(2)] + #[version(2, register_only)] fn keccak_256_root(input: Vec<(Vec, Vec)>, version: StateVersion) -> H256 { match version { StateVersion::V0 => LayoutV0::::trie_root(input), @@ -460,7 +460,7 @@ pub trait Trie { } /// A trie root formed from the enumerated items. - #[version(2)] + #[version(2, register_only)] fn keccak_256_ordered_root(input: Vec>, version: StateVersion) -> H256 { match version { StateVersion::V0 => LayoutV0::::ordered_trie_root(input), @@ -479,7 +479,7 @@ pub trait Trie { } /// Verify trie proof - #[version(2)] + #[version(2, register_only)] fn blake2_256_verify_proof( root: H256, proof: &[Vec], @@ -516,7 +516,7 @@ pub trait Trie { } /// Verify trie proof - #[version(2)] + #[version(2, register_only)] fn keccak_256_verify_proof( root: H256, proof: &[Vec], diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index b2e218cb9db73..a6004d07c2ff7 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -483,12 +483,12 @@ impl Hasher for BlakeTwo256 { impl Hash for BlakeTwo256 { type Output = sp_core::H256; - fn trie_root(input: Vec<(Vec, Vec)>, version: StateVersion) -> Self::Output { - sp_io::trie::blake2_256_root(input, version) + fn trie_root(input: Vec<(Vec, Vec)>, _: StateVersion) -> Self::Output { + sp_io::trie::blake2_256_root(input) } - fn ordered_trie_root(input: Vec>, version: StateVersion) -> Self::Output { - sp_io::trie::blake2_256_ordered_root(input, version) + fn ordered_trie_root(input: Vec>, _: StateVersion) -> Self::Output { + sp_io::trie::blake2_256_ordered_root(input) } } @@ -510,12 +510,12 @@ impl Hasher for Keccak256 { impl Hash for Keccak256 { type Output = sp_core::H256; - fn trie_root(input: Vec<(Vec, Vec)>, version: StateVersion) -> Self::Output { - sp_io::trie::keccak_256_root(input, version) + fn trie_root(input: Vec<(Vec, Vec)>, _: StateVersion) -> Self::Output { + sp_io::trie::keccak_256_root(input) } - fn ordered_trie_root(input: Vec>, version: StateVersion) -> Self::Output { - sp_io::trie::keccak_256_ordered_root(input, version) + fn ordered_trie_root(input: Vec>, _: StateVersion) -> Self::Output { + sp_io::trie::keccak_256_ordered_root(input) } } diff --git a/primitives/transaction-storage-proof/src/lib.rs b/primitives/transaction-storage-proof/src/lib.rs index 2e5aa3b2b9c71..1efd9474b4fd8 100644 --- a/primitives/transaction-storage-proof/src/lib.rs +++ b/primitives/transaction-storage-proof/src/lib.rs @@ -143,7 +143,7 @@ pub mod registration { use sp_trie::TrieMut; type Hasher = sp_core::Blake2Hasher; - type TrieLayout = sp_trie::LayoutV1; + type TrieLayout = sp_trie::LayoutV0; /// Create a new inherent data provider instance for a given parent block hash. pub fn new_data_provider( diff --git a/test-utils/runtime/src/system.rs b/test-utils/runtime/src/system.rs index 6df35421d3614..c53bc6c69ad57 100644 --- a/test-utils/runtime/src/system.rs +++ b/test-utils/runtime/src/system.rs @@ -197,7 +197,7 @@ pub fn finalize_block() -> Header { use sp_core::storage::StateVersion; let extrinsic_index: u32 = storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX).unwrap(); let txs: Vec<_> = (0..extrinsic_index).map(ExtrinsicData::take).collect(); - let extrinsics_root = trie::blake2_256_ordered_root(txs, StateVersion::V0); + let extrinsics_root = trie::blake2_256_ordered_root(txs); let number = ::take().expect("Number is set by `initialize_block`"); let parent_hash = ::take(); let mut digest = ::take().expect("StorageDigest is set by `initialize_block`"); @@ -206,7 +206,7 @@ pub fn finalize_block() -> Header { // This MUST come after all changes to storage are done. Otherwise we will fail the // “Storage root does not match that calculated” assertion. - let storage_root = Hash::decode(&mut &storage_root(StateVersion::V1)[..]) + let storage_root = Hash::decode(&mut &storage_root()[..]) .expect("`storage_root` is a valid hash"); if let Some(new_authorities) = o_new_authorities {