client/
state_dump.rs

1use crate::common::initialize_not_light_node_modules;
2use cfx_config::Configuration;
3use cfx_rpc_eth_types::{AccountState, StateDump, EOA_STORAGE_ROOT_H256};
4use cfx_rpc_primitives::Bytes;
5use cfx_statedb::{StateDbExt, StateDbGeneric};
6use cfx_storage::state_manager::StateManagerTrait;
7use cfx_types::{Address, Space, H256, U256};
8use cfxcore::NodeType;
9use chrono::Utc;
10use keccak_hash::{keccak, KECCAK_EMPTY};
11use parking_lot::{Condvar, Mutex};
12use primitives::{
13    Account, SkipInputCheck, StorageKey, StorageKeyWithSpace, StorageValue,
14};
15use rlp::Rlp;
16use std::{
17    collections::{BTreeMap, HashMap},
18    fs,
19    ops::Deref,
20    path::Path,
21    sync::Arc,
22    thread,
23    time::Duration,
24};
25
26pub struct StateDumpConfig {
27    pub start_address: Address,
28    pub limit: u64,
29    pub block: Option<u64>,
30    pub no_code: bool,
31    pub no_storage: bool,
32    pub out_put_path: String,
33}
34
35// This method will read all data (k, v) from the Conflux state tree (including
36// core space and espace accounts, code, storage, deposit, vote_list) into
37// memory at once, then parse and assemble them and assemble all account states
38// into a StateDump struct and return it
39pub fn dump_whole_state(
40    conf: &mut Configuration, exit_cond_var: Arc<(Mutex<bool>, Condvar)>,
41    config: &StateDumpConfig,
42) -> Result<StateDump, String> {
43    let (mut state_db, state_root) =
44        prepare_state_db(conf, exit_cond_var, config)?;
45
46    let accounts =
47        export_space_accounts(&mut state_db, Space::Ethereum, config)
48            .map_err(|e| e.to_string())?;
49
50    let state_dump = StateDump {
51        root: state_root,
52        accounts,
53        next: None,
54    };
55
56    Ok(state_dump)
57}
58
59// This method will iterate through the entire state tree, storing each found
60// account in a temporary map After iterating through all accounts, it will
61// retrieve the code and storage for each account, then call the callback method
62// Pass the AccountState as a parameter to the callback method, which will
63// handle the AccountState
64pub fn iterate_dump_whole_state<F: Fn(AccountState)>(
65    conf: &mut Configuration, exit_cond_var: Arc<(Mutex<bool>, Condvar)>,
66    config: &StateDumpConfig, callback: F,
67) -> Result<H256, String> {
68    let (mut state_db, state_root) =
69        prepare_state_db(conf, exit_cond_var, config)?;
70
71    export_space_accounts_with_callback(
72        &mut state_db,
73        Space::Ethereum,
74        config,
75        callback,
76    )
77    .map_err(|e| e.to_string())?;
78
79    Ok(state_root)
80}
81
82fn prepare_state_db(
83    conf: &mut Configuration, exit_cond_var: Arc<(Mutex<bool>, Condvar)>,
84    config: &StateDumpConfig,
85) -> Result<(StateDbGeneric, H256), String> {
86    println("Preparing state...");
87    let (
88        data_man,
89        _,
90        _,
91        consensus,
92        sync_service,
93        _,
94        _,
95        _,
96        _,
97        _,
98        _,
99        _,
100        _,
101        _,
102        _,
103        _,
104    ) = initialize_not_light_node_modules(
105        conf,
106        exit_cond_var,
107        NodeType::Archive,
108    )?;
109
110    while sync_service.catch_up_mode() {
111        thread::sleep(Duration::from_secs(1));
112    }
113
114    /*
115    1. Get the state at the target epoch, or the latest state if target_epoch is None
116    2. Iterate through the state, and dump the account state
117    */
118
119    let state_manager = data_man.storage_manager.clone();
120    let target_height = match config.block {
121        Some(epoch) => epoch,
122        None => consensus.latest_confirmed_epoch_number(),
123    };
124
125    let epoch_hash = consensus
126        .get_hash_from_epoch_number(target_height.into())
127        .map_err(|e| e.to_string())?;
128
129    let block = consensus
130        .get_phantom_block_by_hash(&epoch_hash, false)?
131        .expect("Failed to get block");
132
133    let state_root = block.pivot_header.deferred_state_root();
134
135    let state_index = data_man
136        .get_state_readonly_index(&epoch_hash)
137        .ok_or("Failed to get state index")?;
138
139    let state = state_manager
140        .get_state_no_commit(state_index, true, Some(Space::Ethereum))
141        .map_err(|e| e.to_string())?
142        .ok_or("Failed to get state")?;
143
144    let state_db = StateDbGeneric::new(state);
145
146    Ok((state_db, state_root.clone()))
147}
148
149fn export_space_accounts(
150    state: &mut StateDbGeneric, space: Space, config: &StateDumpConfig,
151) -> Result<BTreeMap<Address, AccountState>, Box<dyn std::error::Error>> {
152    println("Start to iterate state...");
153    let empty_key = StorageKey::EmptyKey.with_space(space);
154    let kv_pairs = state.read_all(empty_key, None)?;
155
156    let mut accounts_map = BTreeMap::new();
157    let mut codes_map = HashMap::new();
158    let mut storage_map = HashMap::new();
159
160    for (key, value) in kv_pairs {
161        let storage_key_with_space =
162            StorageKeyWithSpace::from_key_bytes::<SkipInputCheck>(&key);
163        if storage_key_with_space.space != space {
164            continue;
165        }
166        match storage_key_with_space.key {
167            StorageKey::AccountKey(address_bytes) => {
168                let address = Address::from_slice(address_bytes);
169                println(&format!("Find account: {:?}", address));
170                let account =
171                    Account::new_from_rlp(address, &Rlp::new(&value))?;
172                accounts_map.insert(address, account);
173            }
174            StorageKey::CodeKey {
175                address_bytes,
176                code_hash_bytes: _,
177            } => {
178                if config.no_code {
179                    continue;
180                }
181
182                let address = Address::from_slice(address_bytes);
183                let code = Bytes(value.to_vec());
184                codes_map.insert(address, code);
185            }
186            StorageKey::StorageKey {
187                address_bytes,
188                storage_key,
189            } => {
190                if config.no_storage {
191                    continue;
192                }
193
194                let address = Address::from_slice(address_bytes);
195                let h256_storage_key = H256::from_slice(storage_key);
196                let storage_value_with_owner: StorageValue =
197                    rlp::decode(&value)?;
198                let account_storage_map =
199                    storage_map.entry(address).or_insert(BTreeMap::new());
200                account_storage_map
201                    .insert(h256_storage_key, storage_value_with_owner.value);
202            }
203            _ => {
204                continue;
205            }
206        }
207    }
208
209    let mut accounts = BTreeMap::new();
210
211    for (address, account) in accounts_map {
212        let is_contract = account.code_hash != KECCAK_EMPTY;
213        // conflux state tree don't have storage root, so we use a fixed value
214        let root = EOA_STORAGE_ROOT_H256;
215        let address_hash = keccak(address);
216
217        let code = if is_contract {
218            codes_map.get(&address).cloned()
219        } else {
220            if let Some(code) = codes_map.get(&address) {
221                println(&format!("no-contract account have code: {:?}", code));
222            }
223            None
224        };
225
226        let storage = if is_contract {
227            storage_map.get(&address).cloned()
228        } else {
229            if let Some(_storage) = storage_map.get(&address) {
230                println(&format!("no-contract account have storage"));
231            }
232            None
233        };
234
235        let account_state = AccountState {
236            balance: account.balance,
237            nonce: account.nonce.as_u64(),
238            root,
239            code_hash: account.code_hash,
240            code,
241            storage,
242            address: Some(address),
243            address_hash: Some(address_hash),
244        };
245
246        accounts.insert(address, account_state);
247
248        if config.limit > 0 && accounts.len() >= config.limit as usize {
249            break;
250        }
251    }
252
253    Ok(accounts)
254}
255
256pub fn export_space_accounts_with_callback<F: Fn(AccountState)>(
257    state: &mut StateDbGeneric, space: Space, config: &StateDumpConfig,
258    callback: F,
259) -> Result<(), Box<dyn std::error::Error>> {
260    println("Start to iterate state...");
261    let mut found_accounts = 0;
262    let mut core_space_key_count: u64 = 0;
263    let mut total_key_count: u64 = 0;
264
265    for i in 0..=255 {
266        let prefix = [i];
267        let start_key = StorageKey::AddressPrefixKey(&prefix).with_space(space);
268
269        let mut account_states = BTreeMap::new();
270
271        let mut inner_callback = |(key, value): (Vec<u8>, Box<[u8]>)| {
272            total_key_count += 1;
273
274            if total_key_count % 10000 == 0 {
275                println(&format!(
276                    "total_key_count: {}, core_space_key_count: {}",
277                    total_key_count, core_space_key_count
278                ));
279            }
280
281            let storage_key_with_space =
282                StorageKeyWithSpace::from_key_bytes::<SkipInputCheck>(&key);
283            if storage_key_with_space.space != space {
284                core_space_key_count += 1;
285                return;
286            }
287
288            if let StorageKey::AccountKey(address_bytes) =
289                storage_key_with_space.key
290            {
291                let address = Address::from_slice(address_bytes);
292                println(&format!("Find account: {:?}", address));
293                let account = Account::new_from_rlp(address, &Rlp::new(&value))
294                    .expect("Failed to decode account");
295
296                account_states.insert(address, account);
297            }
298        };
299
300        state.read_all_with_callback(start_key, &mut inner_callback, true)?;
301
302        if account_states.len() > 0 {
303            println("Start to read account code and storage data...");
304        }
305
306        for (_address, account) in account_states {
307            let account_state =
308                get_account_state(state, &account, config, space)?;
309            callback(account_state);
310            found_accounts += 1;
311            if config.limit > 0 && found_accounts >= config.limit as usize {
312                break;
313            }
314        }
315    }
316
317    Ok(())
318}
319
320#[allow(unused)]
321fn get_account_state(
322    state: &mut StateDbGeneric, account: &Account, config: &StateDumpConfig,
323    space: Space,
324) -> Result<AccountState, Box<dyn std::error::Error>> {
325    let address = account.address();
326
327    let is_contract = account.code_hash != KECCAK_EMPTY;
328    // get code
329    let code = if is_contract && !config.no_code {
330        state
331            .get_code(address, &account.code_hash)?
332            .map(|code_info| Bytes(code_info.code.deref().to_vec()))
333    } else {
334        None
335    };
336
337    let storage = if is_contract && !config.no_storage {
338        let storage =
339            get_contract_storage(state, &address.address, space, config)?;
340        Some(storage)
341    } else {
342        None
343    };
344
345    // conflux state tree don't have storage root, so we use a fixed value
346    let root = EOA_STORAGE_ROOT_H256;
347
348    let address_hash = keccak(address.address);
349
350    Ok(AccountState {
351        balance: account.balance,
352        nonce: account.nonce.as_u64(),
353        root,
354        code_hash: account.code_hash,
355        code,
356        storage,
357        address: Some(address.address),
358        address_hash: Some(address_hash),
359    })
360}
361
362fn get_contract_storage(
363    state: &mut StateDbGeneric, address: &Address, space: Space,
364    config: &StateDumpConfig,
365) -> Result<BTreeMap<H256, U256>, Box<dyn std::error::Error>> {
366    let mut storage: BTreeMap<H256, U256> = Default::default();
367    let mut chunk_count = 0;
368
369    let mut inner_callback = |(key, value): (Vec<u8>, Box<[u8]>)| {
370        let storage_key_with_space =
371            StorageKeyWithSpace::from_key_bytes::<SkipInputCheck>(&key);
372        if storage_key_with_space.space != space {
373            return;
374        }
375
376        if let StorageKey::StorageKey {
377            address_bytes: _,
378            storage_key,
379        } = storage_key_with_space.key
380        {
381            let h256_storage_key = H256::from_slice(storage_key);
382            let storage_value_with_owner: StorageValue =
383                rlp::decode(&value).expect("Failed to decode storage value");
384            storage.insert(h256_storage_key, storage_value_with_owner.value);
385
386            if storage.len() == 5000_000 {
387                chunk_count += 1;
388                let name = format!("{:?}-chunk{}.json", address, chunk_count);
389                let file_path = Path::new(&config.out_put_path).join(&name);
390                let json_content = serde_json::to_string_pretty(&storage)
391                    .expect("Failed to serialize storage");
392                fs::write(&file_path, json_content)
393                    .expect("Failed to write storage file");
394                storage.clear();
395            }
396        };
397    };
398
399    let start_key = StorageKey::new_storage_root_key(address).with_space(space);
400    state.read_all_with_callback(start_key, &mut inner_callback, false)?;
401
402    Ok(storage)
403}
404
405fn println(message: &str) {
406    println!("[{}] {}", Utc::now().format("%Y-%m-%d %H:%M:%S"), message);
407}