11use crate :: error:: ZkTrieError ;
2- use mpt_zktrie:: state:: StorageData ;
3- use mpt_zktrie:: { AccountData , ZktrieState } ;
42use once_cell:: sync:: Lazy ;
5- use revm:: db:: AccountState ;
63use 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+ } ;
1112use std:: rc:: Rc ;
1213use std:: { cell:: RefCell , collections:: HashMap , convert:: Infallible , fmt} ;
13- use zktrie:: { SharedMemoryDb , ZkMemoryDb , ZkTrie } ;
1414
1515type 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
3737impl 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
4646impl 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