Skip to content

Commit 5afc95b

Browse files
STR-1308: EVM state diff(snapshot) prototype (#800)
* Implement a prototype of EE batch state diff. * Implement exex for EE state diff generation. * Implement an RPC to fetch block state diff. * Implement StateDiff traits for DB. * Use state diff as an exex in alpen-reth, guarded by the config flag. * Implement unit tests for state diff DB table. * Add basic fntest for state diff. * Fmt and other small fixes. * Add unit tests for state diff. * Introduce a separate BatchStateDiff builder for a cleaner interface. * Factor our mpt into its own separate crate. * Use strata-mpt in the evm-ee-stf. * Basic interative state reconstruction from batch state diffs. * Extend exex DB with number to hash mapping to facilitate easier testing. * Expose an additional RPC that reconstructs and returns the block state root based off state diffs. * Minor fixes * Prettify structure, factor out pieces. * Split out chainspec into a separate microcrate and use it in statediffs to construct the genesis state. * Add fntests for state reconstructure from state diffs using load gen env. * Minor review fixes. * Fix chain configs path in the fntests.
1 parent 8af258e commit 5afc95b

39 files changed

+1256
-103
lines changed

Cargo.lock

Lines changed: 50 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ members = [
1111
"crates/evmexec",
1212
"crates/key-derivation",
1313
"crates/l1tx",
14+
"crates/mpt",
1415
"crates/primitives",
1516
"crates/proof-impl/btc-blockspace",
1617
"crates/proof-impl/checkpoint",
1718
"crates/proof-impl/cl-stf",
1819
"crates/proof-impl/evm-ee-stf",
1920
"crates/zkvm/hosts",
21+
"crates/reth/chainspec",
2022
"crates/reth/db",
2123
"crates/reth/evm",
2224
"crates/reth/exex",
@@ -65,12 +67,14 @@ default-members = [
6567
resolver = "2"
6668

6769
[workspace.dependencies]
70+
alpen-chainspec = { path = "crates/reth/chainspec" }
6871
alpen-reth-db = { path = "crates/reth/db" }
6972
alpen-reth-evm = { path = "crates/reth/evm" }
7073
alpen-reth-exex = { path = "crates/reth/exex" }
7174
alpen-reth-node = { path = "crates/reth/node" }
7275
alpen-reth-primitives = { path = "crates/reth/primitives" }
7376
alpen-reth-rpc = { path = "crates/reth/rpc" }
77+
alpen-reth-statediff = { path = "crates/reth/statediff" }
7478
strata-btcio = { path = "crates/btcio" }
7579
strata-chaintsn = { path = "crates/chaintsn" }
7680
strata-common = { path = "crates/common" }
@@ -83,6 +87,7 @@ strata-evmexec = { path = "crates/evmexec" }
8387
strata-key-derivation = { path = "crates/key-derivation" }
8488
strata-l1tx = { path = "crates/l1tx" }
8589
strata-mmr = { path = "crates/util/mmr" }
90+
strata-mpt = { path = "crates/mpt" }
8691
strata-primitives = { path = "crates/primitives" }
8792
strata-proofimpl-btc-blockspace = { path = "crates/proof-impl/btc-blockspace" }
8893
strata-proofimpl-checkpoint = { path = "crates/proof-impl/checkpoint" }

bin/alpen-reth/Cargo.toml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,24 @@ name = "alpen-reth"
88
path = "src/main.rs"
99

1010
[dependencies]
11+
alpen-chainspec.workspace = true
1112
alpen-reth-db.workspace = true
1213
alpen-reth-exex.workspace = true
1314
alpen-reth-node.workspace = true
1415
alpen-reth-rpc.workspace = true
1516

16-
alloy-genesis.workspace = true
1717
alloy-rpc-types.workspace = true
1818
async-trait.workspace = true
1919
clap.workspace = true
2020
eyre.workspace = true
2121
hex.workspace = true
22-
jsonrpsee-types.workspace = true
23-
reqwest.workspace = true
2422
reth.workspace = true
2523
reth-chainspec.workspace = true
26-
reth-cli.workspace = true
2724
reth-cli-commands.workspace = true
2825
reth-cli-util.workspace = true
2926
reth-db.workspace = true
3027
reth-primitives.workspace = true
31-
reth-provider.workspace = true
32-
reth-rpc-eth-api.workspace = true
33-
reth-rpc-eth-types.workspace = true
3428
rockbound.workspace = true
3529
serde_json.workspace = true
36-
shellexpand = "3.0.0"
3730
thiserror.workspace = true
3831
tracing.workspace = true

bin/alpen-reth/src/main.rs

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
mod db;
22

3-
use std::{fs, future::Future, path::PathBuf, sync::Arc};
3+
use std::{future::Future, sync::Arc};
44

5-
use alloy_genesis::Genesis;
5+
use alpen_chainspec::{chain_value_parser, StrataChainSpecParser};
66
use alpen_reth_db::rocksdb::WitnessDB;
7-
use alpen_reth_exex::ProverWitnessGenerator;
7+
use alpen_reth_exex::{ProverWitnessGenerator, StateDiffGenerator};
88
use alpen_reth_node::{args::StrataNodeArgs, StrataEthereumNode};
99
use alpen_reth_rpc::{StrataRPC, StrataRpcApiServer};
1010
use clap::Parser;
@@ -14,14 +14,9 @@ use reth::{
1414
CliRunner,
1515
};
1616
use reth_chainspec::ChainSpec;
17-
use reth_cli::chainspec::ChainSpecParser;
1817
use reth_cli_commands::node::NodeCommand;
1918
use tracing::info;
2019

21-
const DEFAULT_CHAIN_SPEC: &str = include_str!("../res/testnet-chain.json");
22-
const DEVNET_CHAIN_SPEC: &str = include_str!("../res/devnet-chain.json");
23-
const DEV_CHAIN_SPEC: &str = include_str!("../res/alpen-dev-chain.json");
24-
2520
fn main() {
2621
reth_cli_util::sigsegv_handler::install();
2722

@@ -48,17 +43,27 @@ fn main() {
4843

4944
let mut extend_rpc = None;
5045

51-
// Install Prover Input ExEx, persist to DB, and add RPC for querying block witness.
52-
if ext.enable_witness_gen {
46+
if ext.enable_witness_gen || ext.enable_state_diff_gen {
5347
let rbdb = db::open_rocksdb_database(datadir.clone()).expect("open rocksdb");
5448
let db = Arc::new(WitnessDB::new(rbdb));
55-
let rpc_db = db.clone();
56-
57-
extend_rpc.replace(StrataRPC::new(rpc_db));
49+
// Add RPC for querying block witness and state diffs.
50+
extend_rpc.replace(StrataRPC::new(db.clone()));
51+
52+
// Install Prover Input ExEx and persist to DB
53+
if ext.enable_witness_gen {
54+
let witness_db = db.clone();
55+
node_builder = node_builder.install_exex("prover_input", |ctx| async {
56+
Ok(ProverWitnessGenerator::new(ctx, witness_db).start())
57+
});
58+
}
5859

59-
node_builder = node_builder.install_exex("prover_input", |ctx| async {
60-
Ok(ProverWitnessGenerator::new(ctx, db).start())
61-
});
60+
// Install State Diff ExEx and persist to DB
61+
if ext.enable_state_diff_gen {
62+
let state_diff_db = db.clone();
63+
node_builder = node_builder.install_exex("state_diffs", |ctx| async {
64+
Ok(StateDiffGenerator::new(ctx, state_diff_db).start())
65+
});
66+
}
6267
}
6368

6469
// Note: can only add single hook
@@ -100,59 +105,14 @@ pub struct AdditionalConfig {
100105
#[arg(long, default_value_t = false)]
101106
pub enable_witness_gen: bool,
102107

108+
#[arg(long, default_value_t = false)]
109+
pub enable_state_diff_gen: bool,
110+
103111
/// Rpc of sequener's reth node to forward transactions to.
104112
#[arg(long, required = false)]
105113
pub sequencer_http: Option<String>,
106114
}
107115

108-
#[derive(Debug, Clone, Default)]
109-
#[non_exhaustive]
110-
pub struct StrataChainSpecParser;
111-
112-
impl ChainSpecParser for StrataChainSpecParser {
113-
type ChainSpec = ChainSpec;
114-
115-
const SUPPORTED_CHAINS: &'static [&'static str] = &["dev", "devnet", "testnet"];
116-
117-
fn parse(s: &str) -> eyre::Result<Arc<Self::ChainSpec>> {
118-
chain_value_parser(s)
119-
}
120-
}
121-
122-
pub fn chain_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error> {
123-
Ok(match s {
124-
"testnet" => parse_chain_spec(DEFAULT_CHAIN_SPEC)?,
125-
"devnet" => parse_chain_spec(DEVNET_CHAIN_SPEC)?,
126-
"dev" => parse_chain_spec(DEV_CHAIN_SPEC)?,
127-
_ => {
128-
// try to read json from path first
129-
let raw = match fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned())) {
130-
Ok(raw) => raw,
131-
Err(io_err) => {
132-
// valid json may start with "\n", but must contain "{"
133-
if s.contains('{') {
134-
s.to_string()
135-
} else {
136-
return Err(io_err.into()); // assume invalid path
137-
}
138-
}
139-
};
140-
141-
// both serialized Genesis and ChainSpec structs supported
142-
let genesis: Genesis = serde_json::from_str(&raw)?;
143-
144-
Arc::new(genesis.into())
145-
}
146-
})
147-
}
148-
149-
fn parse_chain_spec(chain_json: &str) -> eyre::Result<Arc<ChainSpec>> {
150-
// both serialized Genesis and ChainSpec structs supported
151-
let genesis: Genesis = serde_json::from_str(chain_json)?;
152-
153-
Ok(Arc::new(genesis.into()))
154-
}
155-
156116
/// Run node with logging
157117
/// based on reth::cli::Cli::run
158118
fn run<L, Fut>(

bin/datatool/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ name = "strata-datatool"
88
path = "src/main.rs"
99

1010
[dependencies]
11+
alpen-chainspec.workspace = true
1112
strata-key-derivation.workspace = true
1213
strata-primitives.workspace = true
1314
strata-risc0-guest-builder = { path = "../../provers/risc0", optional = true }

bin/datatool/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const OPKEY_ENVVAR: &str = "STRATA_OP_KEY";
4848
const DEFAULT_NETWORK: Network = Network::Signet;
4949

5050
/// The default evm chainspec to use in params.
51-
const DEFAULT_CHAIN_SPEC: &str = include_str!("../../alpen-reth/res/alpen-dev-chain.json");
51+
const DEFAULT_CHAIN_SPEC: &str = alpen_chainspec::DEV_CHAIN_SPEC;
5252

5353
/// Resolves a [`Network`] from a string.
5454
pub(super) fn resolve_network(arg: Option<&str>) -> anyhow::Result<Network> {

crates/mpt/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
description = "Implementation of the Merkle-Patricia Trie"
3+
edition = "2021"
4+
name = "strata-mpt"
5+
version = "0.1.0"
6+
7+
[dependencies]
8+
alloy-rlp.workspace = true
9+
alloy-rlp-derive.workspace = true
10+
alloy-rpc-types-eth.workspace = true
11+
12+
anyhow.workspace = true
13+
revm-primitives.workspace = true
14+
rlp = "0.5.2"
15+
serde.workspace = true
16+
thiserror.workspace = true
17+
18+
[dev-dependencies]
19+
hex-literal = "0.4"
File renamed without changes.

crates/proof-impl/evm-ee-stf/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.3.0-alpha.1"
66
[dependencies]
77
alpen-reth-evm.workspace = true
88
alpen-reth-primitives.workspace = true
9+
strata-mpt.workspace = true
910
strata-primitives.workspace = true
1011
strata-state.workspace = true
1112
zkaleido.workspace = true

0 commit comments

Comments
 (0)