Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use Merkle Tree instead of Merkle Patricia Tree for execution trace
  • Loading branch information
liuchengxu committed Feb 9, 2022
commit 0098a20eaff255795cbbfe21c9d29345fe1dfdc1
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/sp-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_runtime::traits::{BlakeTwo256, Hash as HashT};
use sp_runtime::{OpaqueExtrinsic, RuntimeDebug};
use sp_std::vec::Vec;
use sp_trie::StorageProof;
use subspace_core_primitives::Randomness;
use subspace_core_primitives::{Randomness, Sha256Hash};
use subspace_runtime_primitives::AccountId;

/// Header of transaction bundle.
Expand Down Expand Up @@ -103,7 +103,7 @@ pub struct ExecutionReceipt<Hash> {
/// List of storage roots collected during the block execution.
pub trace: Vec<Hash>,
/// The merkle root of `trace`.
pub trace_root: H256,
pub trace_root: Sha256Hash,
}

impl<Hash: Encode> ExecutionReceipt<Hash> {
Expand Down
2 changes: 2 additions & 0 deletions cumulus/client/cirrus-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ futures = { version = "0.3.19", features = ["compat"] }
futures-timer = "3.0.1"
rand = "0.8.4"
rand_chacha = "0.3.1"
merkletree = "0.21.0"
parking_lot = "0.12.0"
sha2 = "0.10.0"
tracing = "0.1.25"
thiserror = "1.0.29"
tokio = "1.10"
Expand Down
1 change: 1 addition & 0 deletions cumulus/client/cirrus-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

mod aux_schema;
mod bundler;
mod merkle_tree;
mod processor;

use sc_client_api::{AuxStore, BlockBackend};
Expand Down
43 changes: 43 additions & 0 deletions cumulus/client/cirrus-executor/src/merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use merkletree::hash::Algorithm;
use sha2::{Digest, Sha256};
use std::hash::Hasher;
use subspace_core_primitives::Sha256Hash;

#[derive(Default, Clone)]
pub(super) struct Sha256Algorithm(Sha256);

impl Hasher for Sha256Algorithm {
#[inline]
fn write(&mut self, msg: &[u8]) {
self.0.update(msg);
}

#[inline]
fn finish(&self) -> u64 {
unimplemented!()
}
}

impl Algorithm<Sha256Hash> for Sha256Algorithm {
#[inline]
fn hash(&mut self) -> Sha256Hash {
self.0
.clone()
.finalize()
.as_slice()
.try_into()
.expect("Sha256 output is always 32 bytes; qed")
}

#[inline]
fn reset(&mut self) {
self.0.reset();
}
}

/// Merkle tree type for execution trace.
pub(super) type MerkleTree = merkletree::merkle::MerkleTree<
Sha256Hash,
Sha256Algorithm,
merkletree::store::VecStore<Sha256Hash>,
>;
11 changes: 8 additions & 3 deletions cumulus/client/cirrus-executor/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sp_consensus::BlockOrigin;
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT, Hash as HashT, Header as HeaderT},
traits::{Block as BlockT, Header as HeaderT},
};
use std::{
collections::{BTreeMap, VecDeque},
Expand Down Expand Up @@ -194,8 +194,13 @@ where
self.client.runtime_api().intermediate_roots(&BlockId::Hash(parent_hash))?;
roots.push(state_root.encode());

let trace_root =
BlakeTwo256::ordered_trie_root(roots.clone(), sp_core::storage::StateVersion::V1);
let trace_root = crate::merkle_tree::MerkleTree::new(
roots
.iter()
.map(|r| r.as_slice().try_into().expect("Storage root type is [u8; 32]; qed")),
)
.expect("Failed to construct merkle tree for execution trace")
.root();

let trace = roots
.into_iter()
Expand Down