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

Commit

Permalink
Merge pull request paritytech#232 from subspace/cirrus-executive
Browse files Browse the repository at this point in the history
Implement the state transition root in cirrus properly
  • Loading branch information
liuchengxu authored Jan 24, 2022
2 parents 689d87e + 438c569 commit fc0b661
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 21 deletions.
23 changes: 22 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"cumulus/client/consensus/common",
"cumulus/client/consensus/relay-chain",
"cumulus/client/executor-gossip",
"cumulus/pallets/executive",
"cumulus/parachain-template/node",
"cumulus/parachain-template/runtime",
"cumulus/primitives",
Expand Down
2 changes: 1 addition & 1 deletion crates/sp-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct ExecutionReceipt<Hash> {
/// State root after finishing the execution.
pub state_root: Hash,
/// Merkle root of the execution.
pub state_transition_root: Hash,
pub state_transition_root: H256,
}

impl<Hash: Encode> ExecutionReceipt<Hash> {
Expand Down
2 changes: 1 addition & 1 deletion cumulus/client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ where
header.extrinsics_root().clone(),
HashFor::<Block>::ordered_trie_root(
self.extrinsics.iter().map(Encode::encode).collect(),
sp_core::storage::StateVersion::V1
sp_core::storage::StateVersion::V0 // TODO: switch to V1 once the upstream substrate switches.
),
);

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 @@ -58,6 +58,7 @@ use tracing::Instrument;
const LOG_TARGET: &str = "cirrus::executor";

/// The implementation of the Cirrus `Executor`.
// TODO: merge `runtime_api` into `client`.
pub struct Executor<Block: BlockT, BS, RA, Client, TransactionPool, Backend, CIDP> {
block_status: Arc<BS>,
// TODO: no longer used in executor, revisit this with ParachainBlockImport together.
Expand Down
48 changes: 35 additions & 13 deletions cumulus/client/cirrus-executor/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use sc_consensus::{
use sp_api::ProvideRuntimeApi;
use sp_consensus::BlockOrigin;
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_runtime::{
generic::BlockId,
traits::{BlakeTwo256, Block as BlockT, Hash as HashT, Header as HeaderT},
};
use std::{
collections::{BTreeMap, VecDeque},
fmt::Debug,
Expand Down Expand Up @@ -133,11 +136,13 @@ where

tracing::trace!(target: LOG_TARGET, ?extrinsics, "Origin deduplicated extrinsics");

let block_number = self.client.info().best_number;
let parent_hash = self.client.info().best_hash;
let parent_number = self.client.info().best_number;

let extrinsics: Vec<_> = match self
.runtime_api
.runtime_api()
.extract_signer(&BlockId::Number(block_number), extrinsics)
.extract_signer(&BlockId::Hash(parent_hash), extrinsics)
{
Ok(res) => res,
Err(e) => {
Expand All @@ -153,9 +158,6 @@ where
let extrinsics =
shuffle_extrinsics::<<Block as BlockT>::Extrinsic>(extrinsics, shuffling_seed);

let parent_hash = self.client.info().best_hash;
let parent_number = self.client.info().best_number;

let mut block_builder = BlockBuilder::new(
&*self.runtime_api,
parent_hash,
Expand All @@ -173,7 +175,11 @@ where
block_builder.set_extrinsics(final_extrinsics);

let BuiltBlock { block, storage_changes, proof: _ } = block_builder.build()?;

let (header, body) = block.deconstruct();
let state_root = *header.state_root();
let header_hash = header.hash();

let block_import_params = {
let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
import_block.body = Some(body);
Expand All @@ -185,15 +191,31 @@ where
};
(&*self.client).import_block(block_import_params, Default::default()).await?;

// TODO: now we have the final transaction list:
// - apply each tx one by one.
// - compute the incremental state root and add to the execution trace
// - produce ExecutionReceipt
let mut roots =
self.runtime_api.runtime_api().intermediate_roots(&BlockId::Hash(parent_hash))?;
roots.push(state_root.encode());

tracing::debug!(
target: LOG_TARGET,
intermediate_roots = ?roots
.iter()
.map(|r| {
Block::Hash::decode(&mut r.clone().as_slice())
.expect("Intermediate root uses the same Block hash type; qed")
})
.collect::<Vec<_>>(),
final_state_root = ?state_root,
"Calculating the state transition root for #{}", header_hash
);

let state_transition_root =
BlakeTwo256::ordered_trie_root(roots, sp_core::storage::StateVersion::V1);

let execution_receipt = ExecutionReceipt {
primary_hash,
secondary_hash: Block::Hash::default(),
state_root: Block::Hash::default(),
state_transition_root: Block::Hash::default(),
secondary_hash: header_hash,
state_root,
state_transition_root,
};

// The applied txs can be fully removed from the transaction pool
Expand Down
50 changes: 50 additions & 0 deletions cumulus/pallets/executive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[package]
name = "cirrus-pallet-executive"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>", "Subspace Labs <admin@subspace.network>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace/"
description = "Cirrus executives engine"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive"] }
frame-executive = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }
sp-tracing = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333", default-features = false }

[dev-dependencies]
hex-literal = "0.3.4"
pallet-balances = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333" }
sp-core = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333" }
sp-inherents = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333" }
sp-io = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333" }
sp-version = { git = "https://github.com/paritytech/substrate", rev = "04c7c6abef1e0c6b4126427159090a44f3221333" }

[features]
default = ["std"]
with-tracing = ["sp-tracing/with-tracing"]
std = [
"codec/std",
"frame-executive/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-std/std",
"sp-tracing/std",
]
try-runtime = ["frame-support/try-runtime"]
Loading

0 comments on commit fc0b661

Please sign in to comment.