Skip to content

Commit f8263f5

Browse files
authored
feat: use zktrie-ng (#54)
* use zktrie-ng * fix * clean legacy code * add more metrics * range adjustment * update grafana * fix * update * fix
1 parent 8fe7b2b commit f8263f5

22 files changed

+494
-1218
lines changed

Cargo.lock

+162-178
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tiny-keccak = "2.0"
2121

2222
# dependencies from scroll-tech
2323
poseidon-bn254 = { git = "https://github.com/scroll-tech/poseidon-bn254", branch = "master", features = ["bn254"] }
24-
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", branch = "main", features= ["rs_zktrie"] }
24+
zktrie-ng = { git = "https://github.com/scroll-tech/zktrie-ng", branch = "master", features = ["scroll"] }
2525

2626
# binary dependencies
2727
anyhow = "1.0"

crates/bin/src/commands/run_file.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use anyhow::bail;
33
use clap::Args;
44
use sbv::{
55
core::{ChunkInfo, EvmExecutorBuilder, HardforkConfig},
6-
primitives::{types::BlockTrace, Block, B256},
6+
primitives::{types::BlockTrace, zk_trie::db::HashMapDb, Block, B256},
77
};
8+
use std::rc::Rc;
89
use std::{cell::RefCell, path::PathBuf};
910
use tiny_keccak::{Hasher, Keccak};
1011
use tokio::task::JoinSet;
@@ -75,25 +76,25 @@ impl RunFileCommand {
7576

7677
let fork_config = fork_config(traces[0].chain_id());
7778
let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces);
79+
let zktrie_db = Rc::new(RefCell::new(zktrie_db));
7880

7981
let tx_bytes_hasher = RefCell::new(Keccak::v256());
8082

81-
let mut executor = EvmExecutorBuilder::new(zktrie_db.clone())
83+
let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone())
8284
.hardfork_config(fork_config)
83-
.with_execute_hooks(|hooks| {
85+
.with_hooks(&traces[0], |hooks| {
8486
hooks.add_tx_rlp_handler(|_, rlp| {
8587
tx_bytes_hasher.borrow_mut().update(rlp);
8688
});
87-
})
88-
.build(&traces[0])?;
89+
})?;
8990
executor.handle_block(&traces[0])?;
9091

9192
for trace in traces[1..].iter() {
9293
executor.update_db(trace)?;
9394
executor.handle_block(trace)?;
9495
}
9596

96-
let post_state_root = executor.commit_changes(&zktrie_db);
97+
let post_state_root = executor.commit_changes(zktrie_db.clone())?;
9798
if post_state_root != chunk_info.post_state_root() {
9899
bail!("post state root mismatch");
99100
}

crates/bin/src/main.rs

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ extern crate sbv;
55

66
use clap::Parser;
77
use sbv::core::HardforkConfig;
8-
use sbv::primitives::init_hash_scheme;
98

109
#[cfg(feature = "dev")]
1110
use tracing_subscriber::EnvFilter;
@@ -43,8 +42,6 @@ async fn main() -> anyhow::Result<()> {
4342
)
4443
.init();
4544

46-
init_hash_scheme();
47-
4845
let cmd = Cli::parse();
4946

5047
#[cfg(feature = "metrics")]

crates/bin/src/utils.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use sbv::primitives::zk_trie::ZkMemoryDb;
21
use sbv::{
32
core::{EvmExecutorBuilder, HardforkConfig, VerificationError},
4-
primitives::Block,
3+
primitives::{zk_trie::db::HashMapDb, Block},
54
};
5+
use std::cell::RefCell;
66
use std::rc::Rc;
77

88
pub fn verify<T: Block + Clone>(
@@ -39,17 +39,17 @@ fn verify_inner<T: Block + Clone>(
3939

4040
let zktrie_db = cycle_track!(
4141
{
42-
let mut zktrie_db = ZkMemoryDb::new();
42+
let mut zktrie_db = HashMapDb::default();
4343
measure_duration_millis!(
4444
build_zktrie_db_duration_milliseconds,
45-
l2_trace.build_zktrie_db(&mut zktrie_db)
45+
l2_trace.build_zktrie_db(&mut zktrie_db).unwrap()
4646
);
47-
Rc::new(zktrie_db)
47+
Rc::new(RefCell::new(zktrie_db))
4848
},
4949
"build ZktrieState"
5050
);
5151

52-
let mut executor = EvmExecutorBuilder::new(zktrie_db.clone())
52+
let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone())
5353
.hardfork_config(*fork_config)
5454
.build(&l2_trace)?;
5555

@@ -66,7 +66,7 @@ fn verify_inner<T: Block + Clone>(
6666
update_metrics_counter!(verification_error);
6767
e
6868
})?;
69-
let revm_root_after = executor.commit_changes(&zktrie_db);
69+
let revm_root_after = executor.commit_changes(zktrie_db.clone())?;
7070

7171
#[cfg(feature = "profiling")]
7272
if let Ok(report) = guard.report().build() {

crates/core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ tracing-subscriber.workspace = true
3333
[features]
3434
debug-account = ["sbv-utils/debug-account"]
3535
debug-storage = ["sbv-utils/debug-storage"]
36-
dev = ["sbv-utils/dev"]
36+
dev = ["sbv-primitives/dev", "sbv-utils/dev"]
3737
metrics = ["sbv-utils/metrics"]
3838

3939
# sp1 related

crates/core/src/chunk.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use revm::primitives::B256;
2-
use sbv_primitives::{zk_trie::ZkMemoryDb, Block};
3-
use std::rc::Rc;
2+
use sbv_primitives::{zk_trie::db::HashMapDb, Block};
43
use tiny_keccak::{Hasher, Keccak};
54

65
/// A chunk is a set of continuous blocks.
@@ -22,7 +21,7 @@ pub struct ChunkInfo {
2221

2322
impl ChunkInfo {
2423
/// Construct by block traces
25-
pub fn from_block_traces<T: Block>(traces: &[T]) -> (Self, Rc<ZkMemoryDb>) {
24+
pub fn from_block_traces<T: Block>(traces: &[T]) -> (Self, HashMapDb) {
2625
let chain_id = traces.first().unwrap().chain_id();
2726
let prev_state_root = traces
2827
.first()
@@ -41,14 +40,13 @@ impl ChunkInfo {
4140
let mut data_hash = B256::ZERO;
4241
data_hasher.finalize(&mut data_hash.0);
4342

44-
let mut zktrie_db = ZkMemoryDb::new();
43+
let mut zktrie_db = HashMapDb::default();
4544
for trace in traces.iter() {
4645
measure_duration_millis!(
4746
build_zktrie_db_duration_milliseconds,
48-
trace.build_zktrie_db(&mut zktrie_db)
47+
trace.build_zktrie_db(&mut zktrie_db).unwrap()
4948
);
5049
}
51-
let zktrie_db = Rc::new(zktrie_db);
5250

5351
let info = ChunkInfo {
5452
chain_id,
@@ -118,6 +116,7 @@ mod tests {
118116
use revm::primitives::b256;
119117
use sbv_primitives::types::BlockTrace;
120118
use std::cell::RefCell;
119+
use std::rc::Rc;
121120

122121
const TRACES_STR: [&str; 4] = [
123122
include_str!("../../../testdata/mainnet_blocks/8370400.json"),
@@ -140,17 +139,17 @@ mod tests {
140139

141140
let fork_config = HardforkConfig::default_from_chain_id(traces[0].chain_id());
142141
let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces);
142+
let zktrie_db = Rc::new(RefCell::new(zktrie_db));
143143

144144
let tx_bytes_hasher = RefCell::new(Keccak::v256());
145145

146-
let mut executor = EvmExecutorBuilder::new(zktrie_db.clone())
146+
let mut executor = EvmExecutorBuilder::new(HashMapDb::default(), zktrie_db.clone())
147147
.hardfork_config(fork_config)
148-
.with_execute_hooks(|hooks| {
148+
.with_hooks(&traces[0], |hooks| {
149149
hooks.add_tx_rlp_handler(|_, rlp| {
150150
tx_bytes_hasher.borrow_mut().update(rlp);
151151
});
152152
})
153-
.build(&traces[0])
154153
.unwrap();
155154
executor.handle_block(&traces[0]).unwrap();
156155

@@ -159,7 +158,7 @@ mod tests {
159158
executor.handle_block(trace).unwrap();
160159
}
161160

162-
let post_state_root = executor.commit_changes(&zktrie_db);
161+
let post_state_root = executor.commit_changes(zktrie_db.clone()).unwrap();
163162
assert_eq!(post_state_root, chunk_info.post_state_root);
164163
drop(executor); // drop executor to release Rc<Keccek>
165164

0 commit comments

Comments
 (0)