1
1
use crate :: error:: ZkTrieError ;
2
- use mpt_zktrie:: state:: StorageData ;
3
- use mpt_zktrie:: { AccountData , ZktrieState } ;
4
2
use once_cell:: sync:: Lazy ;
5
- use revm:: db:: AccountState ;
6
3
use revm:: {
7
- db:: DatabaseRef ,
4
+ db:: { AccountState , DatabaseRef } ,
8
5
primitives:: { AccountInfo , Address , Bytecode , B256 , U256 } ,
9
6
} ;
10
- use sbv_primitives:: Block ;
7
+ use sbv_primitives:: {
8
+ init_hash_scheme,
9
+ zk_trie:: { SharedMemoryDb , ZkMemoryDb , ZkTrie } ,
10
+ Block ,
11
+ } ;
11
12
use std:: rc:: Rc ;
12
13
use std:: { cell:: RefCell , collections:: HashMap , convert:: Infallible , fmt} ;
13
- use zktrie:: { SharedMemoryDb , ZkMemoryDb , ZkTrie } ;
14
14
15
15
type Result < T , E = ZkTrieError > = std:: result:: Result < T , E > ;
16
16
@@ -26,36 +26,38 @@ pub struct ReadOnlyDB {
26
26
/// Storage trie cache, avoid re-creating trie for the same account.
27
27
/// Need to invalidate before `update`, otherwise the trie root may be outdated.
28
28
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 ,
31
31
/// The underlying zkTrie database.
32
32
zktrie_db : Rc < ZkMemoryDb > ,
33
- /// Current view of zkTrie database with `zktrie_root` .
33
+ /// Current view of zkTrie database.
34
34
zktrie_db_ref : ZkTrie < SharedMemoryDb > ,
35
35
}
36
36
37
37
impl fmt:: Debug for ReadOnlyDB {
38
38
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
39
39
f. debug_struct ( "ReadOnlyDB" )
40
40
. field ( "code_db" , & self . code_db . len ( ) )
41
- . field ( "zktrie_root " , & self . zktrie_root )
41
+ . field ( "committed_zktrie_root " , & self . committed_zktrie_root )
42
42
. finish ( )
43
43
}
44
44
}
45
45
46
46
impl ReadOnlyDB {
47
47
/// 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 > {
49
49
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)
51
51
}
52
52
53
53
/// Initialize an EVM database from a block trace with size hint of code database.
54
54
pub fn new_with_size_hint < T : Block > (
55
55
l2_trace : T ,
56
- zktrie_state : & ZktrieState ,
56
+ zktrie_db : & Rc < ZkMemoryDb > ,
57
57
size_hint : usize ,
58
58
) -> Result < Self > {
59
+ init_hash_scheme ( ) ;
60
+
59
61
cycle_tracker_start ! ( "insert CodeDB" ) ;
60
62
let mut code_db = HashMap :: with_capacity ( size_hint) ;
61
63
for code in l2_trace. codes ( ) {
@@ -67,17 +69,16 @@ impl ReadOnlyDB {
67
69
}
68
70
cycle_tracker_end ! ( "insert CodeDB" ) ;
69
71
70
- let zktrie_root = l2_trace. root_before ( ) . 0 . into ( ) ;
72
+ let uncommitted_zktrie_root = l2_trace. root_before ( ) ;
71
73
72
74
Ok ( ReadOnlyDB {
73
75
code_db,
74
76
prev_storage_roots : Default :: default ( ) ,
75
77
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 )
81
82
. ok_or ( ZkTrieError :: ZkTrieRootNotFound ) ?,
82
83
} )
83
84
}
@@ -106,6 +107,18 @@ impl ReadOnlyDB {
106
107
. unwrap_or_default ( )
107
108
}
108
109
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
+
109
122
/// Update the database with a new block trace.
110
123
pub fn update < T : Block > ( & mut self , l2_trace : T ) -> Result < ( ) > {
111
124
measure_duration_histogram ! ( update_db_duration_microseconds, self . update_inner( l2_trace) )
@@ -122,11 +135,9 @@ impl ReadOnlyDB {
122
135
}
123
136
cycle_tracker_end ! ( "insert CodeDB" ) ;
124
137
125
- self . zktrie_root = l2_trace. root_before ( ) . 0 . into ( ) ;
126
-
127
138
self . zktrie_db_ref = self
128
139
. zktrie_db
129
- . new_ref_trie ( & self . zktrie_root . 0 )
140
+ . new_ref_trie ( & l2_trace . root_before ( ) . 0 )
130
141
. ok_or ( ZkTrieError :: ZkTrieRootNotFound ) ?;
131
142
132
143
Ok ( ( ) )
@@ -154,11 +165,15 @@ impl DatabaseRef for ReadOnlyDB {
154
165
Ok ( self
155
166
. zktrie_db_ref
156
167
. get_account ( address. as_slice ( ) )
157
- . map ( AccountData :: from)
158
168
. 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 ] ) ;
162
177
self . prev_storage_roots
163
178
. borrow_mut ( )
164
179
. entry ( address)
@@ -174,11 +189,11 @@ impl DatabaseRef for ReadOnlyDB {
174
189
} ) ) ,
175
190
) ;
176
191
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,
180
195
code_hash,
181
- poseidon_code_hash : B256 :: from ( account_data . poseidon_code_hash . 0 ) ,
196
+ poseidon_code_hash,
182
197
code : self . code_db . get ( & code_hash) . cloned ( ) ,
183
198
}
184
199
} ) )
@@ -208,8 +223,7 @@ impl DatabaseRef for ReadOnlyDB {
208
223
let storage_root = self
209
224
. zktrie_db_ref
210
225
. 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 ] ) )
213
227
. unwrap_or_default ( ) ;
214
228
let zktrie_db = self . zktrie_db . clone ( ) ;
215
229
Lazy :: new ( Box :: new ( move || {
@@ -222,8 +236,7 @@ impl DatabaseRef for ReadOnlyDB {
222
236
223
237
Ok ( trie
224
238
. 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) )
227
240
. unwrap_or_default ( ) )
228
241
}
229
242
0 commit comments