@@ -4,14 +4,13 @@ use cfx_rpc_eth_types::{AccountState, StateDump, EOA_STORAGE_ROOT_H256};
44use cfx_rpc_primitives:: Bytes ;
55use cfx_statedb:: { StateDbExt , StateDbGeneric } ;
66use cfx_storage:: state_manager:: StateManagerTrait ;
7- use cfx_types:: { Address , Space , H256 } ;
7+ use cfx_types:: { Address , Space , H256 , U256 } ;
88use cfxcore:: NodeType ;
99use chrono:: Utc ;
1010use keccak_hash:: { keccak, KECCAK_EMPTY } ;
1111use parking_lot:: { Condvar , Mutex } ;
1212use primitives:: {
13- Account , SkipInputCheck , SpaceStorageFilter , StorageKey ,
14- StorageKeyWithSpace , StorageValue ,
13+ Account , SkipInputCheck , StorageKey , StorageKeyWithSpace , StorageValue ,
1514} ;
1615use rlp:: Rlp ;
1716use std:: {
@@ -260,7 +259,7 @@ pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
260259 let mut core_space_key_count: u64 = 0 ;
261260 let mut total_key_count: u64 = 0 ;
262261
263- for i in 0 ..=255 {
262+ for i in 198 ..=255 {
264263 let prefix = [ i] ;
265264 let start_key = StorageKey :: AddressPrefixKey ( & prefix) . with_space ( space) ;
266265
@@ -295,18 +294,15 @@ pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
295294 }
296295 } ;
297296
298- state. read_all_with_callback (
299- start_key,
300- & mut inner_callback,
301- Some ( SpaceStorageFilter :: from ( space) ) ,
302- ) ?;
297+ state. read_all_with_callback ( start_key, & mut inner_callback, true ) ?;
303298
304299 if account_states. len ( ) > 0 {
305300 println ( "Start to read account code and storage data..." ) ;
306301 }
307302
308303 for ( _address, account) in account_states {
309- let account_state = get_account_state ( state, & account, config) ?;
304+ let account_state =
305+ get_account_state ( state, & account, config, space) ?;
310306 callback ( account_state) ;
311307 found_accounts += 1 ;
312308 if config. limit > 0 && found_accounts >= config. limit as usize {
@@ -321,6 +317,7 @@ pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
321317#[ allow( unused) ]
322318fn get_account_state (
323319 state : & mut StateDbGeneric , account : & Account , config : & StateDumpConfig ,
320+ space : Space ,
324321) -> Result < AccountState , Box < dyn std:: error:: Error > > {
325322 let address = account. address ( ) ;
326323
@@ -335,7 +332,7 @@ fn get_account_state(
335332 } ;
336333
337334 let storage = if is_contract && !config. no_storage {
338- let storage = state . get_account_storage_entries ( & address, None ) ?;
335+ let storage = get_contract_storage ( state , & address. address , space ) ?;
339336 Some ( storage)
340337 } else {
341338 None
@@ -358,6 +355,36 @@ fn get_account_state(
358355 } )
359356}
360357
358+ fn get_contract_storage (
359+ state : & mut StateDbGeneric , address : & Address , space : Space ,
360+ ) -> Result < BTreeMap < H256 , U256 > , Box < dyn std:: error:: Error > > {
361+ let mut storage: BTreeMap < H256 , U256 > = Default :: default ( ) ;
362+
363+ let mut inner_callback = |( key, value) : ( Vec < u8 > , Box < [ u8 ] > ) | {
364+ let storage_key_with_space =
365+ StorageKeyWithSpace :: from_key_bytes :: < SkipInputCheck > ( & key) ;
366+ if storage_key_with_space. space != space {
367+ return ;
368+ }
369+
370+ if let StorageKey :: StorageKey {
371+ address_bytes : _,
372+ storage_key,
373+ } = storage_key_with_space. key
374+ {
375+ let h256_storage_key = H256 :: from_slice ( storage_key) ;
376+ let storage_value_with_owner: StorageValue =
377+ rlp:: decode ( & value) . expect ( "Failed to decode storage value" ) ;
378+ storage. insert ( h256_storage_key, storage_value_with_owner. value ) ;
379+ } ;
380+ } ;
381+
382+ let start_key = StorageKey :: new_storage_root_key ( address) . with_space ( space) ;
383+ state. read_all_with_callback ( start_key, & mut inner_callback, false ) ?;
384+
385+ Ok ( storage)
386+ }
387+
361388fn println ( message : & str ) {
362389 println ! ( "[{}] {}" , Utc :: now( ) . format( "%Y-%m-%d %H:%M:%S" ) , message) ;
363390}
0 commit comments