Skip to content

Commit b353fed

Browse files
authored
refactor: [phase2] remove mpt-zktrie (#45)
* remove mpt-zktrie * rename
1 parent 97a1900 commit b353fed

File tree

16 files changed

+235
-534
lines changed

16 files changed

+235
-534
lines changed

Cargo.lock

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

Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ thiserror = "1.0"
2020
tiny-keccak = "2.0"
2121

2222
# dependencies from scroll-tech
23-
mpt-zktrie = { git = "https://github.com/scroll-tech/zkevm-circuits", branch = "feat/rkyv" }
23+
poseidon-bn254 = { git = "https://github.com/scroll-tech/poseidon-bn254", branch = "master" }
2424
zktrie = { git = "https://github.com/scroll-tech/zktrie.git", branch = "main", features= ["rs_zktrie"] }
2525

2626
# binary dependencies
@@ -86,7 +86,6 @@ missing-debug-implementations = "deny"
8686

8787
[patch.crates-io]
8888
ethers-core = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "feat/rkyv" }
89-
ethers-signers = { git = "https://github.com/scroll-tech/ethers-rs.git", branch = "feat/rkyv" }
9089
primitive-types = { git = "https://github.com/scroll-tech/parity-common.git", branch = "feat/rkyv" }
9190
ethereum-types = { git = "https://github.com/scroll-tech/parity-common.git", branch = "feat/rkyv" }
9291
ff = { git = "https://github.com/scroll-tech/ff", branch = "feat/sp1" }
@@ -107,13 +106,10 @@ alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch =
107106
# ethers-providers = { path = "../ethers-rs/ethers-providers" }
108107
# ethers = { path = "../ethers-rs/ethers" }
109108
# ethers-etherscan = { path = "../ethers-rs/ethers-etherscan" }
110-
# ethers-signers = { path = "../ethers-rs/ethers-signers" }
111109
# primitive-types = { path = "../parity-common/primitive-types" }
112110
# ethereum-types = { path = "../parity-common/ethereum-types" }
113111

114112
# for local development
115-
# [patch."https://github.com/scroll-tech/zkevm-circuits"]
116-
# mpt-zktrie = { path = "../zkevm-circuits/zktrie" }
117113
# [patch."https://github.com/scroll-tech/revm"]
118114
# revm = { path = "../revm/crates/revm" }
119115

crates/bin/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ url.workspace = true
2727

2828
sbv.workspace = true
2929

30-
mpt-zktrie.workspace = true
31-
3230
pprof = { workspace = true, optional = true }
3331
tracing-subscriber = { workspace = true, optional = true }
3432

crates/bin/src/commands/run_file.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ impl RunFileCommand {
7474
}
7575

7676
let fork_config = fork_config(traces[0].chain_id());
77-
let (chunk_info, mut zktrie_state) = ChunkInfo::from_block_traces(&traces);
77+
let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces);
7878

7979
let tx_bytes_hasher = RefCell::new(Keccak::v256());
8080

81-
let mut executor = EvmExecutorBuilder::new(&zktrie_state)
81+
let mut executor = EvmExecutorBuilder::new(zktrie_db.clone())
8282
.hardfork_config(fork_config)
8383
.with_execute_hooks(|hooks| {
8484
hooks.add_tx_rlp_handler(|_, rlp| {
@@ -93,7 +93,7 @@ impl RunFileCommand {
9393
executor.handle_block(trace)?;
9494
}
9595

96-
let post_state_root = executor.commit_changes(&mut zktrie_state);
96+
let post_state_root = executor.commit_changes(&zktrie_db);
9797
if post_state_root != chunk_info.post_state_root() {
9898
bail!("post state root mismatch");
9999
}

crates/bin/src/utils.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use mpt_zktrie::ZktrieState;
1+
use sbv::primitives::zk_trie::ZkMemoryDb;
22
use sbv::{
33
core::{EvmExecutorBuilder, HardforkConfig, VerificationError},
44
primitives::Block,
55
};
6+
use std::rc::Rc;
67

78
pub fn verify<T: Block + Clone>(
89
l2_trace: T,
@@ -36,20 +37,19 @@ fn verify_inner<T: Block + Clone>(
3637
.build()
3738
.unwrap();
3839

39-
let mut zktrie_state = cycle_track!(
40+
let zktrie_db = cycle_track!(
4041
{
41-
let old_root = l2_trace.root_before();
42-
let mut zktrie_state = ZktrieState::construct(old_root.0.into());
42+
let mut zktrie_db = ZkMemoryDb::new();
4343
measure_duration_histogram!(
44-
build_zktrie_state_duration_microseconds,
45-
l2_trace.build_zktrie_state(&mut zktrie_state)
44+
build_zktrie_db_duration_microseconds,
45+
l2_trace.build_zktrie_db(&mut zktrie_db)
4646
);
47-
zktrie_state
47+
Rc::new(zktrie_db)
4848
},
4949
"build ZktrieState"
5050
);
5151

52-
let mut executor = EvmExecutorBuilder::new(&zktrie_state)
52+
let mut executor = EvmExecutorBuilder::new(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(&mut zktrie_state);
69+
let revm_root_after = executor.commit_changes(&zktrie_db);
7070

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

crates/core/Cargo.toml

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ rkyv.workspace = true
2020
thiserror.workspace = true
2121
tiny-keccak.workspace = true
2222

23-
# dependencies from scroll-tech
24-
mpt-zktrie.workspace = true
25-
zktrie.workspace = true
26-
2723
sbv-primitives.workspace = true
2824
sbv-utils.workspace = true
2925

crates/core/src/chunk.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use mpt_zktrie::ZktrieState;
21
use revm::primitives::B256;
3-
use sbv_primitives::Block;
2+
use sbv_primitives::{zk_trie::ZkMemoryDb, Block};
3+
use std::rc::Rc;
44
use tiny_keccak::{Hasher, Keccak};
55

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

2323
impl ChunkInfo {
2424
/// Construct by block traces
25-
pub fn from_block_traces<T: Block>(traces: &[T]) -> (Self, ZktrieState) {
25+
pub fn from_block_traces<T: Block>(traces: &[T]) -> (Self, Rc<ZkMemoryDb>) {
2626
let chain_id = traces.first().unwrap().chain_id();
2727
let prev_state_root = traces
2828
.first()
@@ -41,13 +41,14 @@ impl ChunkInfo {
4141
let mut data_hash = B256::ZERO;
4242
data_hasher.finalize(&mut data_hash.0);
4343

44-
let mut zktrie_state = ZktrieState::construct(prev_state_root.0.into());
44+
let mut zktrie_db = ZkMemoryDb::new();
4545
for trace in traces.iter() {
4646
measure_duration_histogram!(
47-
build_zktrie_state_duration_microseconds,
48-
trace.build_zktrie_state(&mut zktrie_state)
47+
build_zktrie_db_duration_microseconds,
48+
trace.build_zktrie_db(&mut zktrie_db)
4949
);
5050
}
51+
let zktrie_db = Rc::new(zktrie_db);
5152

5253
let info = ChunkInfo {
5354
chain_id,
@@ -57,7 +58,7 @@ impl ChunkInfo {
5758
data_hash,
5859
};
5960

60-
(info, zktrie_state)
61+
(info, zktrie_db)
6162
}
6263

6364
/// Public input hash for a given chunk is defined as
@@ -137,18 +138,17 @@ mod tests {
137138
});
138139

139140
let fork_config = HardforkConfig::default_from_chain_id(traces[0].chain_id());
140-
let (chunk_info, mut zktrie_state) = ChunkInfo::from_block_traces(&traces);
141+
let (chunk_info, zktrie_db) = ChunkInfo::from_block_traces(&traces);
141142

142143
let tx_bytes_hasher = RefCell::new(Keccak::v256());
143144

144-
let mut executor = EvmExecutorBuilder::new(&zktrie_state)
145+
let mut executor = EvmExecutorBuilder::new(zktrie_db.clone())
145146
.hardfork_config(fork_config)
146147
.with_execute_hooks(|hooks| {
147148
hooks.add_tx_rlp_handler(|_, rlp| {
148149
tx_bytes_hasher.borrow_mut().update(rlp);
149150
});
150151
})
151-
.zktrie_state(&zktrie_state)
152152
.build(&traces[0])
153153
.unwrap();
154154
executor.handle_block(&traces[0]).unwrap();
@@ -158,7 +158,7 @@ mod tests {
158158
executor.handle_block(trace).unwrap();
159159
}
160160

161-
let post_state_root = executor.commit_changes(&mut zktrie_state);
161+
let post_state_root = executor.commit_changes(&zktrie_db);
162162
assert_eq!(post_state_root, chunk_info.post_state_root);
163163
drop(executor); // drop executor to release Rc<Keccek>
164164

crates/core/src/database.rs

+47-34
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use crate::error::ZkTrieError;
2-
use mpt_zktrie::state::StorageData;
3-
use mpt_zktrie::{AccountData, ZktrieState};
42
use once_cell::sync::Lazy;
5-
use revm::db::AccountState;
63
use revm::{
7-
db::DatabaseRef,
4+
db::{AccountState, DatabaseRef},
85
primitives::{AccountInfo, Address, Bytecode, B256, U256},
96
};
10-
use sbv_primitives::Block;
7+
use sbv_primitives::{
8+
init_hash_scheme,
9+
zk_trie::{SharedMemoryDb, ZkMemoryDb, ZkTrie},
10+
Block,
11+
};
1112
use std::rc::Rc;
1213
use std::{cell::RefCell, collections::HashMap, convert::Infallible, fmt};
13-
use zktrie::{SharedMemoryDb, ZkMemoryDb, ZkTrie};
1414

1515
type Result<T, E = ZkTrieError> = std::result::Result<T, E>;
1616

@@ -26,36 +26,38 @@ pub struct ReadOnlyDB {
2626
/// Storage trie cache, avoid re-creating trie for the same account.
2727
/// Need to invalidate before `update`, otherwise the trie root may be outdated.
2828
storage_trie_refs: RefCell<HashMap<Address, Lazy<ZkTrie<SharedMemoryDb>, StorageTrieLazyFn>>>,
29-
/// Current zkTrie root based on the block trace.
30-
zktrie_root: B256,
29+
/// Current uncommitted zkTrie root based on the block trace.
30+
committed_zktrie_root: B256,
3131
/// The underlying zkTrie database.
3232
zktrie_db: Rc<ZkMemoryDb>,
33-
/// Current view of zkTrie database with `zktrie_root`.
33+
/// Current view of zkTrie database.
3434
zktrie_db_ref: ZkTrie<SharedMemoryDb>,
3535
}
3636

3737
impl fmt::Debug for ReadOnlyDB {
3838
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3939
f.debug_struct("ReadOnlyDB")
4040
.field("code_db", &self.code_db.len())
41-
.field("zktrie_root", &self.zktrie_root)
41+
.field("committed_zktrie_root", &self.committed_zktrie_root)
4242
.finish()
4343
}
4444
}
4545

4646
impl ReadOnlyDB {
4747
/// Initialize an EVM database from a block trace.
48-
pub fn new<T: Block>(l2_trace: T, zktrie_state: &ZktrieState) -> Result<Self> {
48+
pub fn new<T: Block>(l2_trace: T, zktrie_db: &Rc<ZkMemoryDb>) -> Result<Self> {
4949
let size_hint = l2_trace.codes().len();
50-
Self::new_with_size_hint(l2_trace, zktrie_state, size_hint)
50+
Self::new_with_size_hint(l2_trace, zktrie_db, size_hint)
5151
}
5252

5353
/// Initialize an EVM database from a block trace with size hint of code database.
5454
pub fn new_with_size_hint<T: Block>(
5555
l2_trace: T,
56-
zktrie_state: &ZktrieState,
56+
zktrie_db: &Rc<ZkMemoryDb>,
5757
size_hint: usize,
5858
) -> Result<Self> {
59+
init_hash_scheme();
60+
5961
cycle_tracker_start!("insert CodeDB");
6062
let mut code_db = HashMap::with_capacity(size_hint);
6163
for code in l2_trace.codes() {
@@ -67,17 +69,16 @@ impl ReadOnlyDB {
6769
}
6870
cycle_tracker_end!("insert CodeDB");
6971

70-
let zktrie_root = l2_trace.root_before().0.into();
72+
let uncommitted_zktrie_root = l2_trace.root_before();
7173

7274
Ok(ReadOnlyDB {
7375
code_db,
7476
prev_storage_roots: Default::default(),
7577
storage_trie_refs: Default::default(),
76-
zktrie_root,
77-
zktrie_db: zktrie_state.zk_db.clone(),
78-
zktrie_db_ref: zktrie_state
79-
.zk_db
80-
.new_ref_trie(&zktrie_root.0)
78+
committed_zktrie_root: uncommitted_zktrie_root,
79+
zktrie_db: zktrie_db.clone(),
80+
zktrie_db_ref: zktrie_db
81+
.new_ref_trie(&uncommitted_zktrie_root.0)
8182
.ok_or(ZkTrieError::ZkTrieRootNotFound)?,
8283
})
8384
}
@@ -106,6 +107,18 @@ impl ReadOnlyDB {
106107
.unwrap_or_default()
107108
}
108109

110+
/// Get the zkTrie root.
111+
#[inline]
112+
pub(crate) fn committed_zktrie_root(&self) -> B256 {
113+
self.committed_zktrie_root
114+
}
115+
116+
/// Get the zkTrie root.
117+
#[inline]
118+
pub(crate) fn updated_committed_zktrie_root(&mut self, new_root: B256) {
119+
self.committed_zktrie_root = new_root;
120+
}
121+
109122
/// Update the database with a new block trace.
110123
pub fn update<T: Block>(&mut self, l2_trace: T) -> Result<()> {
111124
measure_duration_histogram!(update_db_duration_microseconds, self.update_inner(l2_trace))
@@ -122,11 +135,9 @@ impl ReadOnlyDB {
122135
}
123136
cycle_tracker_end!("insert CodeDB");
124137

125-
self.zktrie_root = l2_trace.root_before().0.into();
126-
127138
self.zktrie_db_ref = self
128139
.zktrie_db
129-
.new_ref_trie(&self.zktrie_root.0)
140+
.new_ref_trie(&l2_trace.root_before().0)
130141
.ok_or(ZkTrieError::ZkTrieRootNotFound)?;
131142

132143
Ok(())
@@ -154,11 +165,15 @@ impl DatabaseRef for ReadOnlyDB {
154165
Ok(self
155166
.zktrie_db_ref
156167
.get_account(address.as_slice())
157-
.map(AccountData::from)
158168
.map(|account_data| {
159-
let code_hash = B256::from(account_data.keccak_code_hash.0);
160-
161-
let storage_root = account_data.storage_root;
169+
let code_size =
170+
u64::from_be_bytes((&account_data[0][16..24]).try_into().unwrap()) as usize;
171+
let nonce = u64::from_be_bytes((&account_data[0][24..]).try_into().unwrap());
172+
let balance = U256::from_be_bytes(account_data[1]);
173+
let code_hash = B256::from(account_data[3]);
174+
let poseidon_code_hash = B256::from(account_data[4]);
175+
176+
let storage_root = B256::from(account_data[2]);
162177
self.prev_storage_roots
163178
.borrow_mut()
164179
.entry(address)
@@ -174,11 +189,11 @@ impl DatabaseRef for ReadOnlyDB {
174189
})),
175190
);
176191
AccountInfo {
177-
balance: U256::from_limbs(account_data.balance.0),
178-
nonce: account_data.nonce,
179-
code_size: account_data.code_size as usize,
192+
balance,
193+
nonce,
194+
code_size,
180195
code_hash,
181-
poseidon_code_hash: B256::from(account_data.poseidon_code_hash.0),
196+
poseidon_code_hash,
182197
code: self.code_db.get(&code_hash).cloned(),
183198
}
184199
}))
@@ -208,8 +223,7 @@ impl DatabaseRef for ReadOnlyDB {
208223
let storage_root = self
209224
.zktrie_db_ref
210225
.get_account(address.as_slice())
211-
.map(AccountData::from)
212-
.map(|account_data| account_data.storage_root)
226+
.map(|account_data| B256::from(account_data[2]))
213227
.unwrap_or_default();
214228
let zktrie_db = self.zktrie_db.clone();
215229
Lazy::new(Box::new(move || {
@@ -222,8 +236,7 @@ impl DatabaseRef for ReadOnlyDB {
222236

223237
Ok(trie
224238
.get_store(&index.to_be_bytes::<32>())
225-
.map(StorageData::from)
226-
.map(|val| U256::from_limbs(val.as_ref().0))
239+
.map(|store_data| U256::from_be_bytes(store_data))
227240
.unwrap_or_default())
228241
}
229242

0 commit comments

Comments
 (0)