Skip to content

Commit 5f9f1a3

Browse files
committed
optimize contract storage read method
1 parent 355829f commit 5f9f1a3

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

crates/client/src/state_dump.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use cfx_rpc_eth_types::{AccountState, StateDump, EOA_STORAGE_ROOT_H256};
44
use cfx_rpc_primitives::Bytes;
55
use cfx_statedb::{StateDbExt, StateDbGeneric};
66
use cfx_storage::state_manager::StateManagerTrait;
7-
use cfx_types::{Address, Space, H256};
7+
use cfx_types::{Address, Space, H256, U256};
88
use cfxcore::NodeType;
99
use chrono::Utc;
1010
use keccak_hash::{keccak, KECCAK_EMPTY};
@@ -260,7 +260,7 @@ pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
260260
let mut core_space_key_count: u64 = 0;
261261
let mut total_key_count: u64 = 0;
262262

263-
for i in 0..=255 {
263+
for i in 198..=255 {
264264
let prefix = [i];
265265
let start_key = StorageKey::AddressPrefixKey(&prefix).with_space(space);
266266

@@ -306,7 +306,8 @@ pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
306306
}
307307

308308
for (_address, account) in account_states {
309-
let account_state = get_account_state(state, &account, config)?;
309+
let account_state =
310+
get_account_state(state, &account, config, space)?;
310311
callback(account_state);
311312
found_accounts += 1;
312313
if config.limit > 0 && found_accounts >= config.limit as usize {
@@ -321,6 +322,7 @@ pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
321322
#[allow(unused)]
322323
fn get_account_state(
323324
state: &mut StateDbGeneric, account: &Account, config: &StateDumpConfig,
325+
space: Space,
324326
) -> Result<AccountState, Box<dyn std::error::Error>> {
325327
let address = account.address();
326328

@@ -335,7 +337,7 @@ fn get_account_state(
335337
};
336338

337339
let storage = if is_contract && !config.no_storage {
338-
let storage = state.get_account_storage_entries(&address, None)?;
340+
let storage = get_contract_storage(state, &address.address, space)?;
339341
Some(storage)
340342
} else {
341343
None
@@ -358,6 +360,36 @@ fn get_account_state(
358360
})
359361
}
360362

363+
fn get_contract_storage(
364+
state: &mut StateDbGeneric, address: &Address, space: Space,
365+
) -> Result<BTreeMap<H256, U256>, Box<dyn std::error::Error>> {
366+
let mut storage: BTreeMap<H256, U256> = Default::default();
367+
368+
let mut inner_callback = |(key, value): (Vec<u8>, Box<[u8]>)| {
369+
let storage_key_with_space =
370+
StorageKeyWithSpace::from_key_bytes::<SkipInputCheck>(&key);
371+
if storage_key_with_space.space != space {
372+
return;
373+
}
374+
375+
if let StorageKey::StorageKey {
376+
address_bytes: _,
377+
storage_key,
378+
} = storage_key_with_space.key
379+
{
380+
let h256_storage_key = H256::from_slice(storage_key);
381+
let storage_value_with_owner: StorageValue =
382+
rlp::decode(&value).expect("Failed to decode storage value");
383+
storage.insert(h256_storage_key, storage_value_with_owner.value);
384+
};
385+
};
386+
387+
let start_key = StorageKey::new_storage_root_key(address).with_space(space);
388+
state.read_all_with_callback(start_key, &mut inner_callback, None)?;
389+
390+
Ok(storage)
391+
}
392+
361393
fn println(message: &str) {
362394
println!("[{}] {}", Utc::now().format("%Y-%m-%d %H:%M:%S"), message);
363395
}

0 commit comments

Comments
 (0)