|
| 1 | +use cfx_types::Address; |
| 2 | +use clap::{ArgMatches, Args}; |
| 3 | +use client::{ |
| 4 | + configuration::Configuration, |
| 5 | + state_dump::{state_dump, StateDumpConfig}, |
| 6 | +}; |
| 7 | +use parking_lot::{Condvar, Mutex}; |
| 8 | +use std::{fs, path::Path, str::FromStr, sync::Arc}; |
| 9 | + |
| 10 | +#[derive(Args, Debug)] |
| 11 | +pub struct DumpCommand { |
| 12 | + /// Include accounts for which we don't have the address (missing preimage) |
| 13 | + // #[arg(id = "incompletes", long = "incompletes")] |
| 14 | + // incompletes: bool, |
| 15 | + /// Print streaming JSON iteratively, delimited by newlines |
| 16 | + // #[arg(id = "iterative", long = "iterative", default_value = "true")] |
| 17 | + // iterative: bool, |
| 18 | + /// Max number of elements (0 = no limit) |
| 19 | + #[arg( |
| 20 | + id = "limit", |
| 21 | + long = "limit", |
| 22 | + value_name = "NUM", |
| 23 | + default_value = "0" |
| 24 | + )] |
| 25 | + limit: u64, |
| 26 | + /// Target block number, if not specified, the latest block will be used |
| 27 | + #[arg(id = "block", long = "block", value_name = "NUM")] |
| 28 | + block: Option<u64>, |
| 29 | + /// Exclude contract code (save db lookups) |
| 30 | + #[arg(id = "nocode", long = "nocode")] |
| 31 | + no_code: bool, |
| 32 | + /// Exclude storage entries (save db lookups) |
| 33 | + #[arg(id = "nostorage", long = "nostorage")] |
| 34 | + no_storage: bool, |
| 35 | + /// Start position address |
| 36 | + #[arg( |
| 37 | + id = "start", |
| 38 | + long = "start", |
| 39 | + value_name = "String", |
| 40 | + default_value = "0x0000000000000000000000000000000000000000" |
| 41 | + )] |
| 42 | + start: String, |
| 43 | + /// Path to the output folder (default: ./dump) |
| 44 | + #[arg(id = "output", long = "output", value_name = "PATH")] |
| 45 | + output: Option<String>, |
| 46 | +} |
| 47 | + |
| 48 | +impl DumpCommand { |
| 49 | + pub fn parse(matches: &ArgMatches) -> Result<Self, String> { |
| 50 | + let output = matches.get_one::<String>("output").cloned(); |
| 51 | + Ok(Self { |
| 52 | + block: matches.get_one::<u64>("block").cloned(), |
| 53 | + // incompletes: matches.get_flag("incompletes"), |
| 54 | + // iterative: matches.get_flag("iterative"), |
| 55 | + limit: matches.get_one::<u64>("limit").cloned().unwrap_or(0), |
| 56 | + no_code: matches.get_flag("nocode"), |
| 57 | + no_storage: matches.get_flag("nostorage"), |
| 58 | + start: matches.get_one::<String>("start").cloned().unwrap_or( |
| 59 | + "0x0000000000000000000000000000000000000000".to_string(), |
| 60 | + ), |
| 61 | + output, |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | + fn get_state_dump_config(&self) -> Result<StateDumpConfig, String> { |
| 66 | + let address_str = self.start.strip_prefix("0x").unwrap_or(&self.start); |
| 67 | + let start_address = Address::from_str(address_str) |
| 68 | + .map_err(|e| format!("Invalid address: {}", e))?; |
| 69 | + Ok(StateDumpConfig { |
| 70 | + start_address, |
| 71 | + limit: self.limit, |
| 72 | + block: self.block, |
| 73 | + no_code: self.no_code, |
| 74 | + no_storage: self.no_storage, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + pub fn execute(&self, conf: &mut Configuration) -> Result<String, String> { |
| 79 | + // Determine output directory |
| 80 | + let output_path = match self.output { |
| 81 | + Some(ref path) => path, |
| 82 | + None => { |
| 83 | + "./dump" // Default to "./dump" if no output specified |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + // Ensure the directory exists |
| 88 | + if !Path::new(output_path).exists() { |
| 89 | + fs::create_dir_all(output_path).map_err(|e| { |
| 90 | + format!("Failed to create output directory: {}", e) |
| 91 | + })?; |
| 92 | + } |
| 93 | + |
| 94 | + let exit = Arc::new((Mutex::new(false), Condvar::new())); |
| 95 | + |
| 96 | + let config = self.get_state_dump_config()?; |
| 97 | + let accounts = state_dump(conf, exit, &config)?; |
| 98 | + |
| 99 | + // TODO export to json file |
| 100 | + |
| 101 | + println!("accounts: {:?}", accounts.len()); |
| 102 | + for account in accounts { |
| 103 | + println!("{:?}", account.address); |
| 104 | + } |
| 105 | + |
| 106 | + Ok(format!( |
| 107 | + "dump account state at block number: {:?} to output directory: {}", |
| 108 | + self.block, output_path |
| 109 | + )) |
| 110 | + } |
| 111 | +} |
0 commit comments