Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Only support state version v0 from the runtime
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bkchr committed Jan 22, 2022
1 parent f318350 commit 6ee7b44
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 31 deletions.
2 changes: 1 addition & 1 deletion frame/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion frame/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ mod tests {

#[pallet::weight(0)]
pub fn calculate_storage_root(_origin: OriginFor<T>) -> 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(())
}
Expand Down
8 changes: 4 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
}

Expand All @@ -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());
};
}

Expand Down
4 changes: 2 additions & 2 deletions frame/support/src/storage/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
pub fn root(child_info: &ChildInfo, _: StateVersion) -> Vec<u8> {
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()),
}
}

Expand Down
3 changes: 1 addition & 2 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,8 +1329,7 @@ impl<T: Config> Pallet<T> {
<BlockHash<T>>::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");

<T::Header as traits::Header>::new(
Expand Down
3 changes: 1 addition & 2 deletions frame/transaction-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <frame_system::Pallet<T>>::extrinsic_index()
Expand Down Expand Up @@ -301,7 +301,6 @@ pub mod pallet {
&proof.proof,
&encode_index(chunk_index),
&proof.chunk,
sp_runtime::StateVersion::V1,
),
Error::<T>::InvalidProof
);
Expand Down
16 changes: 8 additions & 8 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub trait Storage {
/// The hashing algorithm is defined by the `Block`.
///
/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
#[version(2)]
#[version(2, register_only)]
fn root(&mut self, version: StateVersion) -> Vec<u8> {
self.storage_root(version)
}
Expand Down Expand Up @@ -394,7 +394,7 @@ pub trait DefaultChildStorage {
/// The hashing algorithm is defined by the `Block`.
///
/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
#[version(2)]
#[version(2, register_only)]
fn root(&mut self, storage_key: &[u8], version: StateVersion) -> Vec<u8> {
let child_info = ChildInfo::new_default(storage_key);
self.child_storage_root(&child_info, version)
Expand All @@ -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<u8>, Vec<u8>)>, version: StateVersion) -> H256 {
match version {
StateVersion::V0 => LayoutV0::<sp_core::Blake2Hasher>::trie_root(input),
Expand All @@ -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<Vec<u8>>, version: StateVersion) -> H256 {
match version {
StateVersion::V0 => LayoutV0::<sp_core::Blake2Hasher>::ordered_trie_root(input),
Expand All @@ -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<u8>, Vec<u8>)>, version: StateVersion) -> H256 {
match version {
StateVersion::V0 => LayoutV0::<sp_core::KeccakHasher>::trie_root(input),
Expand All @@ -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<Vec<u8>>, version: StateVersion) -> H256 {
match version {
StateVersion::V0 => LayoutV0::<sp_core::KeccakHasher>::ordered_trie_root(input),
Expand All @@ -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<u8>],
Expand Down Expand Up @@ -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<u8>],
Expand Down
16 changes: 8 additions & 8 deletions primitives/runtime/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,12 @@ impl Hasher for BlakeTwo256 {
impl Hash for BlakeTwo256 {
type Output = sp_core::H256;

fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_root(input, version)
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, _: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_root(input)
}

fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_ordered_root(input, version)
fn ordered_trie_root(input: Vec<Vec<u8>>, _: StateVersion) -> Self::Output {
sp_io::trie::blake2_256_ordered_root(input)
}
}

Expand All @@ -510,12 +510,12 @@ impl Hasher for Keccak256 {
impl Hash for Keccak256 {
type Output = sp_core::H256;

fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_root(input, version)
fn trie_root(input: Vec<(Vec<u8>, Vec<u8>)>, _: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_root(input)
}

fn ordered_trie_root(input: Vec<Vec<u8>>, version: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_ordered_root(input, version)
fn ordered_trie_root(input: Vec<Vec<u8>>, _: StateVersion) -> Self::Output {
sp_io::trie::keccak_256_ordered_root(input)
}
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/transaction-storage-proof/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub mod registration {
use sp_trie::TrieMut;

type Hasher = sp_core::Blake2Hasher;
type TrieLayout = sp_trie::LayoutV1<Hasher>;
type TrieLayout = sp_trie::LayoutV0<Hasher>;

/// Create a new inherent data provider instance for a given parent block hash.
pub fn new_data_provider<B, C>(
Expand Down
4 changes: 2 additions & 2 deletions test-utils/runtime/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <Number>::take().expect("Number is set by `initialize_block`");
let parent_hash = <ParentHash>::take();
let mut digest = <StorageDigest>::take().expect("StorageDigest is set by `initialize_block`");
Expand All @@ -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 {
Expand Down

0 comments on commit 6ee7b44

Please sign in to comment.