From aa55db71749bafb84dce3f0f0e8867017fefd3eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Thu, 20 Feb 2025 17:21:47 -0300 Subject: [PATCH 01/11] sort and show top 10 accounts --- cmd/ethrex/ethrex.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index 62631a2680..23fe2c5ab9 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -1,7 +1,10 @@ use bytes::Bytes; use directories::ProjectDirs; use ethrex_blockchain::{add_block, fork_choice::apply_fork_choice}; -use ethrex_common::types::{Block, Genesis}; +use ethrex_common::{ + types::{Block, Genesis}, + H160, U256, +}; use ethrex_p2p::{ kademlia::KademliaTable, network::{node_id_from_signing_key, peer_table}, @@ -174,6 +177,23 @@ async fn main() { }; let genesis = read_genesis_file(&network); + + // Show top rich accounts + let mut top_accounts: Vec<(&H160, U256)> = genesis + .alloc + .iter() + .map(|(address, account)| (address, account.balance)) + .collect(); + top_accounts.sort_by(|a, b| b.1.cmp(&a.1)); // sort by greater balance + let number_of_top_accounts = 10; + top_accounts.truncate(number_of_top_accounts); + info!( + "Showing {number_of_top_accounts} top rich accounts\n {:<42} Balance", + "Address" + ); + for (address, balance) in top_accounts { + println!(" {address:?} {balance}"); + } store .add_initial_state(genesis.clone()) .expect("Failed to create genesis block"); From 43e550c9dfb391a5b201bdd280169e220f1d1440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 12:16:27 -0300 Subject: [PATCH 02/11] add cli to convert pk to address --- cmd/ethrex_l2/src/cli.rs | 5 ++++- cmd/ethrex_l2/src/commands/mod.rs | 1 + cmd/ethrex_l2/src/commands/utils.rs | 28 ++++++++++++++++++++++++++++ cmd/ethrex_l2/src/commands/wallet.rs | 1 - 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 cmd/ethrex_l2/src/commands/utils.rs diff --git a/cmd/ethrex_l2/src/cli.rs b/cmd/ethrex_l2/src/cli.rs index a4715524c2..65b762e4c1 100644 --- a/cmd/ethrex_l2/src/cli.rs +++ b/cmd/ethrex_l2/src/cli.rs @@ -1,5 +1,5 @@ use crate::{ - commands::{autocomplete, config, info, prove, stack, test, wallet}, + commands::{autocomplete, config, info, prove, stack, test, utils, wallet}, config::load_selected_config, }; use clap::{Parser, Subcommand}; @@ -33,6 +33,8 @@ enum EthrexL2Command { Info(info::Command), #[clap(about = "Read a test chain from disk and prove a block.")] Prove(prove::Command), + #[clap(subcommand, about = "Utils commands.")] + Utils(utils::Command), } pub async fn start() -> eyre::Result<()> { @@ -53,6 +55,7 @@ pub async fn start() -> eyre::Result<()> { EthrexL2Command::Test(cmd) => cmd.run(cfg).await?, EthrexL2Command::Info(cmd) => cmd.run(cfg).await?, EthrexL2Command::Prove(_) => unreachable!(), + EthrexL2Command::Utils(cmd) => cmd.run()?, }; Ok(()) } diff --git a/cmd/ethrex_l2/src/commands/mod.rs b/cmd/ethrex_l2/src/commands/mod.rs index 80fafb9a0e..b17b4bd9f4 100644 --- a/cmd/ethrex_l2/src/commands/mod.rs +++ b/cmd/ethrex_l2/src/commands/mod.rs @@ -4,4 +4,5 @@ pub(crate) mod info; pub(crate) mod prove; pub(crate) mod stack; pub(crate) mod test; +pub(crate) mod utils; pub(crate) mod wallet; diff --git a/cmd/ethrex_l2/src/commands/utils.rs b/cmd/ethrex_l2/src/commands/utils.rs new file mode 100644 index 0000000000..318878acad --- /dev/null +++ b/cmd/ethrex_l2/src/commands/utils.rs @@ -0,0 +1,28 @@ +use clap::Subcommand; +use keccak_hash::H256; +use secp256k1::SecretKey; + +#[derive(Subcommand)] +pub(crate) enum Command { + #[clap(about = "Convert private key to address.")] + PrivateKeyToAddress { + #[arg(long = "pk", help = "Private key in hex format.", required = true)] + private_key: String, + }, +} + +impl Command { + pub fn run(self) -> eyre::Result<()> { + match self { + Command::PrivateKeyToAddress { private_key } => { + let pk_str = private_key.strip_prefix("0x").unwrap_or(&private_key); + let pk_h256 = pk_str.parse::()?; + let pk_bytes = pk_h256.as_bytes(); + let secret_key = SecretKey::from_slice(pk_bytes)?; + let address = ethrex_l2_sdk::get_address_from_secret_key(&secret_key)?; + println!("Address: {:#x}", address); + } + } + Ok(()) + } +} diff --git a/cmd/ethrex_l2/src/commands/wallet.rs b/cmd/ethrex_l2/src/commands/wallet.rs index bfd2430d1c..4dc187ad58 100644 --- a/cmd/ethrex_l2/src/commands/wallet.rs +++ b/cmd/ethrex_l2/src/commands/wallet.rs @@ -2,7 +2,6 @@ use crate::config::EthrexL2Config; use bytes::Bytes; use clap::Subcommand; use ethereum_types::{Address, H256, U256}; -use ethrex_common::types::Transaction; use ethrex_l2_sdk::calldata::{encode_calldata, Value}; use ethrex_l2_sdk::merkle_tree::merkle_proof; use ethrex_l2_sdk::{get_withdrawal_hash, COMMON_BRIDGE_L2_ADDRESS, L2_WITHDRAW_SIGNATURE}; From bb3e9a7a0fe262032738d90835c2175ecb1a97b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 16:07:56 -0300 Subject: [PATCH 03/11] sort addresses --- Cargo.lock | 1 + cmd/ethrex/Cargo.toml | 1 + cmd/ethrex/ethrex.rs | 75 +++++++++++++++++++++++++++-------- test_data/private_keys_l1.txt | 21 ++++++++++ 4 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 test_data/private_keys_l1.txt diff --git a/Cargo.lock b/Cargo.lock index fc6b495a59..ff758aa347 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2561,6 +2561,7 @@ dependencies = [ "local-ip-address", "rand 0.8.5", "redb", + "secp256k1", "serde_json", "tokio", "tokio-util", diff --git a/cmd/ethrex/Cargo.toml b/cmd/ethrex/Cargo.toml index 9d33b04904..92c1813ba0 100644 --- a/cmd/ethrex/Cargo.toml +++ b/cmd/ethrex/Cargo.toml @@ -30,6 +30,7 @@ local-ip-address = "0.6" tokio-util.workspace = true redb = { workspace = true, optional = true } lazy_static.workspace = true +secp256k1.workspace = true cfg-if = "1.0.0" diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index 23fe2c5ab9..c4bc5615d3 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -3,7 +3,7 @@ use directories::ProjectDirs; use ethrex_blockchain::{add_block, fork_choice::apply_fork_choice}; use ethrex_common::{ types::{Block, Genesis}, - H160, U256, + H160, H256, U256, }; use ethrex_p2p::{ kademlia::KademliaTable, @@ -12,12 +12,16 @@ use ethrex_p2p::{ types::{Node, NodeRecord}, }; use ethrex_rlp::decode::RLPDecode; +use ethrex_rpc::clients::eth::get_address_from_secret_key; use ethrex_storage::{EngineType, Store}; use ethrex_vm::{backends::EVM, EVM_BACKEND}; use k256::ecdsa::SigningKey; +use secp256k1::SecretKey; + use local_ip_address::local_ip; use rand::rngs::OsRng; use std::{ + collections::HashMap, fs::{self, File}, future::IntoFuture, io, @@ -177,23 +181,8 @@ async fn main() { }; let genesis = read_genesis_file(&network); + show_rich_accounts(&genesis); - // Show top rich accounts - let mut top_accounts: Vec<(&H160, U256)> = genesis - .alloc - .iter() - .map(|(address, account)| (address, account.balance)) - .collect(); - top_accounts.sort_by(|a, b| b.1.cmp(&a.1)); // sort by greater balance - let number_of_top_accounts = 10; - top_accounts.truncate(number_of_top_accounts); - info!( - "Showing {number_of_top_accounts} top rich accounts\n {:<42} Balance", - "Address" - ); - for (address, balance) in top_accounts { - println!(" {address:?} {balance}"); - } store .add_initial_state(genesis.clone()) .expect("Failed to create genesis block"); @@ -517,3 +506,55 @@ fn read_known_peers(file_path: PathBuf) -> Result, serde_json::Error> serde_json::from_reader(file) } + +fn show_rich_accounts(genesis: &Genesis) { + let Ok(contents) = fs::read_to_string("../../test_data/private_keys.txt") else { + return; + }; + + let private_keys: Vec = contents + .lines() + .filter(|line| !line.trim().is_empty()) + .map(|line| line.trim().to_string()) + .collect(); + + let mut address_to_pk = HashMap::new(); + for pk in private_keys.iter() { + let pk_str = pk.strip_prefix("0x").unwrap_or(pk); + let Ok(pk_h256) = pk_str.parse::() else { + return; + }; + let pk_bytes = pk_h256.as_bytes(); + let Ok(secret_key) = SecretKey::from_slice(pk_bytes) else { + return; + }; + let Ok(address) = get_address_from_secret_key(&secret_key) else { + return; + }; // better to use the sdk function + address_to_pk.insert(address, pk); + } + + let mut top_accounts: Vec<(&H160, U256)> = genesis + .alloc + .iter() + .map(|(address, account)| (address, account.balance)) + .collect(); + top_accounts.sort_by(|a, b| b.1.cmp(&a.1)); // sort by greater balance + let number_of_top_accounts = 10; + top_accounts.truncate(number_of_top_accounts); + + println!("Showing first {} accounts", number_of_top_accounts); + println!("-------------------------------------------------------------------------------"); + for (address, balance) in top_accounts { + let Some(pk) = address_to_pk.get(address) else { + return; + }; + println!("Private Key: {}", pk); + println!( + "Address: {:?} (Ξ {})", + address, + balance.checked_div(U256::exp10(18)).unwrap_or(U256::zero()) + ); + println!("-------------------------------------------------------------------------------"); + } +} diff --git a/test_data/private_keys_l1.txt b/test_data/private_keys_l1.txt new file mode 100644 index 0000000000..9a1763ac8c --- /dev/null +++ b/test_data/private_keys_l1.txt @@ -0,0 +1,21 @@ +0xbcdf20249abf0ed6d944c0288fad489e33f66b3960d9e6229c1cd214ed3bbe31 +0x39725efee3fb28614de3bacaffe4cc4bd8c436257e2c8bb887c4b5c4be45e76d +0x53321db7c1e331d93a11a41d16f004d7ff63972ec8ec7c25db329728ceeb1710 +0xab63b23eb7941c1251757e24b3d2350d2bc05c3c388d06f8fe6feafefb1e8c70 +0x5d2344259f42259f82d2c140aa66102ba89b57b4883ee441a8b312622bd42491 +0x27515f805127bebad2fb9b183508bdacb8c763da16f54e0678b16e8f28ef3fff +0x7ff1a4c1d57e5e784d327c4c7651e952350bc271f156afb3d00d20f5ef924856 +0x3a91003acaf4c21b3953d94fa4a6db694fa69e5242b2e37be05dd82761058899 +0xbb1d0f125b4fb2bb173c318cdead45468474ca71474e2247776b2b4c0fa2d3f5 +0x850643a0224065ecce3882673c21f56bcf6eef86274cc21cadff15930b59fc8c +0x94eb3102993b41ec55c241060f47daa0f6372e2e3ad7e91612ae36c364042e44 +0xdaf15504c22a352648a71ef2926334fe040ac1d5005019e09f6c979808024dc7 +0xeaba42282ad33c8ef2524f07277c03a776d98ae19f581990ce75becb7cfa1c23 +0x3fd98b5187bf6526734efaa644ffbb4e3670d66f5d0268ce0323ec09124bff61 +0x5288e2f440c7f0cb61a9be8afdeb4295f786383f96f5e35eb0c94ef103996b64 +0xf296c7802555da2a5a662be70e078cbd38b44f96f8615ae529da41122ce8db05 +0xbf3beef3bd999ba9f2451e06936f0423cd62b815c9233dd3bc90f7e02a1e8673 +0x6ecadc396415970e91293726c3f5775225440ea0844ae5616135fd10d66b5954 +0xa492823c3e193d6c595f37a18e3c06650cf4c74558cc818b16130b293716106f +0xc5114526e042343c6d1899cad05e1c00ba588314de9b96929914ee0df18d46b2 +0x04b9f63ecf84210c5366c66d68fa1f5da1fa4f634fad6dfc86178e4d79ff9e59 From 591207771de4ce5b63554898cc40bc15622be311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 16:24:48 -0300 Subject: [PATCH 04/11] change calling function --- cmd/ethrex/ethrex.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index c4bc5615d3..c76288c398 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -181,7 +181,6 @@ async fn main() { }; let genesis = read_genesis_file(&network); - show_rich_accounts(&genesis); store .add_initial_state(genesis.clone()) @@ -301,6 +300,7 @@ async fn main() { error!("Cannot run with DEV_MODE if the `l2` feature is enabled."); panic!("Run without the --dev argument."); } + show_rich_accounts(&genesis, "../../test_data/private_keys.txt"); let l2_proposer = ethrex_l2::start_proposer(store).into_future(); tracker.spawn(l2_proposer); } else if #[cfg(feature = "dev")] { @@ -308,7 +308,8 @@ async fn main() { // Start the block_producer module if devmode was set if dev_mode { info!("Runnning in DEV_MODE"); - let authrpc_jwtsecret = + show_rich_accounts(&genesis, "test_data/private_keys_l1.txt"); + let authrpc_jwtsecret = std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); let head_block_hash = { let current_block_number = store.get_latest_block_number().unwrap(); @@ -507,8 +508,8 @@ fn read_known_peers(file_path: PathBuf) -> Result, serde_json::Error> serde_json::from_reader(file) } -fn show_rich_accounts(genesis: &Genesis) { - let Ok(contents) = fs::read_to_string("../../test_data/private_keys.txt") else { +fn show_rich_accounts(genesis: &Genesis, path: &str) { + let Ok(contents) = fs::read_to_string(path) else { return; }; From 2ab2eabe0cc6aaaf612e8d00d10f530cb760b6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 18:05:52 -0300 Subject: [PATCH 05/11] changes --- cmd/ethrex/ethrex.rs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index c76288c398..b6677df16a 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -40,6 +40,9 @@ mod decode; mod networks; const DEFAULT_DATADIR: &str = "ethrex"; +const L1_PK_PATH: &str = "test_data/private_keys_l1.txt"; +#[cfg(feature = "l2")] +const L2_PK_PATH: &str = "../../test_data/private_keys.txt"; #[tokio::main] async fn main() { let matches = cli::cli().get_matches(); @@ -291,7 +294,8 @@ async fn main() { let metrics_api = ethrex_metrics::api::start_prometheus_metrics_api(metrics_port); tracker.spawn(metrics_api); } - + let l2_pks = include_str!(L2_PK_PATH); + show_rich_accounts(&genesis, l2_pks); let dev_mode = *matches.get_one::("dev").unwrap_or(&false); // We do not want to start the networking module if the l2 feature is enabled. cfg_if::cfg_if! { @@ -300,7 +304,6 @@ async fn main() { error!("Cannot run with DEV_MODE if the `l2` feature is enabled."); panic!("Run without the --dev argument."); } - show_rich_accounts(&genesis, "../../test_data/private_keys.txt"); let l2_proposer = ethrex_l2::start_proposer(store).into_future(); tracker.spawn(l2_proposer); } else if #[cfg(feature = "dev")] { @@ -308,9 +311,9 @@ async fn main() { // Start the block_producer module if devmode was set if dev_mode { info!("Runnning in DEV_MODE"); - show_rich_accounts(&genesis, "test_data/private_keys_l1.txt"); - let authrpc_jwtsecret = - std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); + show_rich_accounts(&genesis, L1_PK_PATH); + let authrpc_jwtsecret = + std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); let head_block_hash = { let current_block_number = store.get_latest_block_number().unwrap(); store @@ -508,10 +511,11 @@ fn read_known_peers(file_path: PathBuf) -> Result, serde_json::Error> serde_json::from_reader(file) } -fn show_rich_accounts(genesis: &Genesis, path: &str) { - let Ok(contents) = fs::read_to_string(path) else { - return; - }; +fn show_rich_accounts(genesis: &Genesis, contents: &str) { + dbg!("entro"); + // let Ok(contents) = fs::read_to_string(path) else { + // return; + // }; let private_keys: Vec = contents .lines() @@ -548,14 +552,15 @@ fn show_rich_accounts(genesis: &Genesis, path: &str) { println!("-------------------------------------------------------------------------------"); for (address, balance) in top_accounts { let Some(pk) = address_to_pk.get(address) else { - return; + continue; }; println!("Private Key: {}", pk); - println!( - "Address: {:?} (Ξ {})", - address, - balance.checked_div(U256::exp10(18)).unwrap_or(U256::zero()) - ); + println!("Address: {:?} (Ξ {})", address, wei_to_eth(balance)); println!("-------------------------------------------------------------------------------"); } } + +fn wei_to_eth(wei: U256) -> U256 { + wei.checked_div(U256::from_dec_str("1000000000000000000").unwrap()) + .unwrap_or(U256::zero()) +} From b994a9be6c54b001d985b016177814257d3c7183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 18:16:09 -0300 Subject: [PATCH 06/11] use str macro --- cmd/ethrex/ethrex.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index b6677df16a..0789bb7ce1 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -40,9 +40,6 @@ mod decode; mod networks; const DEFAULT_DATADIR: &str = "ethrex"; -const L1_PK_PATH: &str = "test_data/private_keys_l1.txt"; -#[cfg(feature = "l2")] -const L2_PK_PATH: &str = "../../test_data/private_keys.txt"; #[tokio::main] async fn main() { let matches = cli::cli().get_matches(); @@ -294,8 +291,6 @@ async fn main() { let metrics_api = ethrex_metrics::api::start_prometheus_metrics_api(metrics_port); tracker.spawn(metrics_api); } - let l2_pks = include_str!(L2_PK_PATH); - show_rich_accounts(&genesis, l2_pks); let dev_mode = *matches.get_one::("dev").unwrap_or(&false); // We do not want to start the networking module if the l2 feature is enabled. cfg_if::cfg_if! { @@ -304,6 +299,8 @@ async fn main() { error!("Cannot run with DEV_MODE if the `l2` feature is enabled."); panic!("Run without the --dev argument."); } + let l2_pks = include_str!("../../test_data/private_keys.txt"); + show_rich_accounts(&genesis, l2_pks); let l2_proposer = ethrex_l2::start_proposer(store).into_future(); tracker.spawn(l2_proposer); } else if #[cfg(feature = "dev")] { @@ -311,7 +308,8 @@ async fn main() { // Start the block_producer module if devmode was set if dev_mode { info!("Runnning in DEV_MODE"); - show_rich_accounts(&genesis, L1_PK_PATH); + let l1_pks = include_str!("../../test_data/private_keys_l1.txt"); + show_rich_accounts(&genesis, l1_pks); let authrpc_jwtsecret = std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); let head_block_hash = { From e27d5d473fd5383f811ceafdc966663b993eff29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 18:26:24 -0300 Subject: [PATCH 07/11] remove old things --- cmd/ethrex/ethrex.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index 0789bb7ce1..c287b504e3 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -181,7 +181,6 @@ async fn main() { }; let genesis = read_genesis_file(&network); - store .add_initial_state(genesis.clone()) .expect("Failed to create genesis block"); @@ -291,6 +290,7 @@ async fn main() { let metrics_api = ethrex_metrics::api::start_prometheus_metrics_api(metrics_port); tracker.spawn(metrics_api); } + let dev_mode = *matches.get_one::("dev").unwrap_or(&false); // We do not want to start the networking module if the l2 feature is enabled. cfg_if::cfg_if! { @@ -311,7 +311,7 @@ async fn main() { let l1_pks = include_str!("../../test_data/private_keys_l1.txt"); show_rich_accounts(&genesis, l1_pks); let authrpc_jwtsecret = - std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); + std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); let head_block_hash = { let current_block_number = store.get_latest_block_number().unwrap(); store @@ -510,11 +510,6 @@ fn read_known_peers(file_path: PathBuf) -> Result, serde_json::Error> } fn show_rich_accounts(genesis: &Genesis, contents: &str) { - dbg!("entro"); - // let Ok(contents) = fs::read_to_string(path) else { - // return; - // }; - let private_keys: Vec = contents .lines() .filter(|line| !line.trim().is_empty()) From d34bfd0a2f89cb6eb3725d1a53429c0ae5ef52f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Fri, 21 Feb 2025 18:27:29 -0300 Subject: [PATCH 08/11] remove comment --- cmd/ethrex/ethrex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index c287b504e3..e63405f05e 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -528,7 +528,7 @@ fn show_rich_accounts(genesis: &Genesis, contents: &str) { }; let Ok(address) = get_address_from_secret_key(&secret_key) else { return; - }; // better to use the sdk function + }; address_to_pk.insert(address, pk); } From 37e20933e7c36bdb9842971b45314cb896671ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Mon, 24 Feb 2025 10:14:53 -0300 Subject: [PATCH 09/11] change to dev crate --- Cargo.lock | 4 ++- cmd/ethrex/Cargo.toml | 3 +- cmd/ethrex/ethrex.rs | 62 +++----------------------------- crates/blockchain/dev/Cargo.toml | 3 ++ crates/blockchain/dev/dev.rs | 1 + crates/blockchain/dev/utils.rs | 56 +++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 61 deletions(-) create mode 100644 crates/blockchain/dev/utils.rs diff --git a/Cargo.lock b/Cargo.lock index b3f4510ea2..cec7a432e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2562,7 +2562,6 @@ dependencies = [ "local-ip-address", "rand 0.8.5", "redb", - "secp256k1", "serde_json", "tokio", "tokio-util", @@ -2621,11 +2620,14 @@ dependencies = [ "bytes", "envy", "ethereum-types", + "ethrex-common", "ethrex-rpc", + "ethrex-sdk", "hex", "jsonwebtoken", "keccak-hash", "reqwest", + "secp256k1", "serde", "serde_json", "sha2 0.10.8", diff --git a/cmd/ethrex/Cargo.toml b/cmd/ethrex/Cargo.toml index 92c1813ba0..e5a6025a33 100644 --- a/cmd/ethrex/Cargo.toml +++ b/cmd/ethrex/Cargo.toml @@ -30,7 +30,6 @@ local-ip-address = "0.6" tokio-util.workspace = true redb = { workspace = true, optional = true } lazy_static.workspace = true -secp256k1.workspace = true cfg-if = "1.0.0" @@ -49,4 +48,4 @@ metrics = ["ethrex-blockchain/metrics", "ethrex-l2/metrics"] libmdbx = ["ethrex-storage/libmdbx"] redb = ["dep:redb", "ethrex-storage/redb"] blst = ["ethrex-vm/blst"] -l2 = ["dep:ethrex-l2", "ethrex-vm/l2"] +l2 = ["dep:ethrex-l2", "ethrex-vm/l2", "dep:ethrex-dev"] diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index e63405f05e..82b7d2acb5 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -1,10 +1,7 @@ use bytes::Bytes; use directories::ProjectDirs; use ethrex_blockchain::{add_block, fork_choice::apply_fork_choice}; -use ethrex_common::{ - types::{Block, Genesis}, - H160, H256, U256, -}; +use ethrex_common::types::{Block, Genesis}; use ethrex_p2p::{ kademlia::KademliaTable, network::{node_id_from_signing_key, peer_table}, @@ -12,16 +9,13 @@ use ethrex_p2p::{ types::{Node, NodeRecord}, }; use ethrex_rlp::decode::RLPDecode; -use ethrex_rpc::clients::eth::get_address_from_secret_key; use ethrex_storage::{EngineType, Store}; use ethrex_vm::{backends::EVM, EVM_BACKEND}; use k256::ecdsa::SigningKey; -use secp256k1::SecretKey; use local_ip_address::local_ip; use rand::rngs::OsRng; use std::{ - collections::HashMap, fs::{self, File}, future::IntoFuture, io, @@ -299,8 +293,9 @@ async fn main() { error!("Cannot run with DEV_MODE if the `l2` feature is enabled."); panic!("Run without the --dev argument."); } + use ethrex_dev; let l2_pks = include_str!("../../test_data/private_keys.txt"); - show_rich_accounts(&genesis, l2_pks); + ethrex_dev::utils::show_rich_accounts(&genesis, l2_pks); let l2_proposer = ethrex_l2::start_proposer(store).into_future(); tracker.spawn(l2_proposer); } else if #[cfg(feature = "dev")] { @@ -309,7 +304,7 @@ async fn main() { if dev_mode { info!("Runnning in DEV_MODE"); let l1_pks = include_str!("../../test_data/private_keys_l1.txt"); - show_rich_accounts(&genesis, l1_pks); + ethrex_dev::utils::show_rich_accounts(&genesis, l1_pks); let authrpc_jwtsecret = std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); let head_block_hash = { @@ -508,52 +503,3 @@ fn read_known_peers(file_path: PathBuf) -> Result, serde_json::Error> serde_json::from_reader(file) } - -fn show_rich_accounts(genesis: &Genesis, contents: &str) { - let private_keys: Vec = contents - .lines() - .filter(|line| !line.trim().is_empty()) - .map(|line| line.trim().to_string()) - .collect(); - - let mut address_to_pk = HashMap::new(); - for pk in private_keys.iter() { - let pk_str = pk.strip_prefix("0x").unwrap_or(pk); - let Ok(pk_h256) = pk_str.parse::() else { - return; - }; - let pk_bytes = pk_h256.as_bytes(); - let Ok(secret_key) = SecretKey::from_slice(pk_bytes) else { - return; - }; - let Ok(address) = get_address_from_secret_key(&secret_key) else { - return; - }; - address_to_pk.insert(address, pk); - } - - let mut top_accounts: Vec<(&H160, U256)> = genesis - .alloc - .iter() - .map(|(address, account)| (address, account.balance)) - .collect(); - top_accounts.sort_by(|a, b| b.1.cmp(&a.1)); // sort by greater balance - let number_of_top_accounts = 10; - top_accounts.truncate(number_of_top_accounts); - - println!("Showing first {} accounts", number_of_top_accounts); - println!("-------------------------------------------------------------------------------"); - for (address, balance) in top_accounts { - let Some(pk) = address_to_pk.get(address) else { - continue; - }; - println!("Private Key: {}", pk); - println!("Address: {:?} (Ξ {})", address, wei_to_eth(balance)); - println!("-------------------------------------------------------------------------------"); - } -} - -fn wei_to_eth(wei: U256) -> U256 { - wei.checked_div(U256::from_dec_str("1000000000000000000").unwrap()) - .unwrap_or(U256::zero()) -} diff --git a/crates/blockchain/dev/Cargo.toml b/crates/blockchain/dev/Cargo.toml index 85450b30c5..22c3677759 100644 --- a/crates/blockchain/dev/Cargo.toml +++ b/crates/blockchain/dev/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" [dependencies] ethrex-rpc.workspace = true +ethrex-common.workspace = true +ethrex-sdk.workspace = true tokio.workspace = true tracing.workspace = true @@ -21,6 +23,7 @@ reqwest = { version = "0.12.7", features = ["json"] } envy = "0.4.2" keccak-hash.workspace = true sha2 = "0.10.8" +secp256k1.workspace = true [lib] path = "./dev.rs" diff --git a/crates/blockchain/dev/dev.rs b/crates/blockchain/dev/dev.rs index fe770290b2..0ce0c5892a 100644 --- a/crates/blockchain/dev/dev.rs +++ b/crates/blockchain/dev/dev.rs @@ -1 +1,2 @@ pub mod block_producer; +pub mod utils; diff --git a/crates/blockchain/dev/utils.rs b/crates/blockchain/dev/utils.rs new file mode 100644 index 0000000000..eccbd5a0e1 --- /dev/null +++ b/crates/blockchain/dev/utils.rs @@ -0,0 +1,56 @@ +use std::collections::HashMap; + +use ethereum_types::{H160, U256}; +use ethrex_common::types::Genesis; +use ethrex_l2_sdk::get_address_from_secret_key; +use keccak_hash::H256; +use secp256k1::SecretKey; + +pub fn show_rich_accounts(genesis: &Genesis, contents: &str) { + let private_keys: Vec = contents + .lines() + .filter(|line| !line.trim().is_empty()) + .map(|line| line.trim().to_string()) + .collect(); + + let mut address_to_pk = HashMap::new(); + for pk in private_keys.iter() { + let pk_str = pk.strip_prefix("0x").unwrap_or(pk); + let Ok(pk_h256) = pk_str.parse::() else { + return; + }; + let pk_bytes = pk_h256.as_bytes(); + let Ok(secret_key) = SecretKey::from_slice(pk_bytes) else { + return; + }; + let Ok(address) = get_address_from_secret_key(&secret_key) else { + return; + }; + address_to_pk.insert(address, pk); + } + + let mut top_accounts: Vec<(&H160, U256)> = genesis + .alloc + .iter() + .map(|(address, account)| (address, account.balance)) + .collect(); + top_accounts.sort_by(|a, b| b.1.cmp(&a.1)); // sort by greater balance + let number_of_top_accounts = 10; + top_accounts.truncate(number_of_top_accounts); + + println!("Showing first {} accounts", number_of_top_accounts); + println!("-------------------------------------------------------------------------------"); + for (address, balance) in top_accounts { + let Some(pk) = address_to_pk.get(address) else { + continue; + }; + println!("Private Key: {}", pk); + println!("Address: {:?} (Ξ {})", address, wei_to_eth(balance)); + println!("-------------------------------------------------------------------------------"); + } +} + +pub fn wei_to_eth(wei: U256) -> U256 { + wei.checked_div(U256::from_dec_str("1000000000000000000").unwrap()) + .unwrap_or(U256::zero()) +} From 76297d107ffa3f2e4b275fa281adb3269c50bcc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Mon, 24 Feb 2025 10:30:20 -0300 Subject: [PATCH 10/11] changed to constant --- cmd/ethrex/ethrex.rs | 1 - crates/blockchain/dev/utils.rs | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index 82b7d2acb5..ea73d6ac97 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -12,7 +12,6 @@ use ethrex_rlp::decode::RLPDecode; use ethrex_storage::{EngineType, Store}; use ethrex_vm::{backends::EVM, EVM_BACKEND}; use k256::ecdsa::SigningKey; - use local_ip_address::local_ip; use rand::rngs::OsRng; use std::{ diff --git a/crates/blockchain/dev/utils.rs b/crates/blockchain/dev/utils.rs index eccbd5a0e1..507991f23d 100644 --- a/crates/blockchain/dev/utils.rs +++ b/crates/blockchain/dev/utils.rs @@ -6,6 +6,8 @@ use ethrex_l2_sdk::get_address_from_secret_key; use keccak_hash::H256; use secp256k1::SecretKey; +pub const NUMBER_OF_TOP_ACCOUNTS: usize = 10; + pub fn show_rich_accounts(genesis: &Genesis, contents: &str) { let private_keys: Vec = contents .lines() @@ -35,10 +37,9 @@ pub fn show_rich_accounts(genesis: &Genesis, contents: &str) { .map(|(address, account)| (address, account.balance)) .collect(); top_accounts.sort_by(|a, b| b.1.cmp(&a.1)); // sort by greater balance - let number_of_top_accounts = 10; - top_accounts.truncate(number_of_top_accounts); + top_accounts.truncate(NUMBER_OF_TOP_ACCOUNTS); - println!("Showing first {} accounts", number_of_top_accounts); + println!("Showing first {} accounts", NUMBER_OF_TOP_ACCOUNTS); println!("-------------------------------------------------------------------------------"); for (address, balance) in top_accounts { let Some(pk) = address_to_pk.get(address) else { From 6d137cc7c62d1a6cdbcdb0071ca9ea7fa35a96c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Paradelo?= Date: Mon, 24 Feb 2025 10:50:02 -0300 Subject: [PATCH 11/11] remove cli feat --- cmd/ethrex_l2/src/cli.rs | 5 +---- cmd/ethrex_l2/src/commands/mod.rs | 1 - cmd/ethrex_l2/src/commands/utils.rs | 28 ---------------------------- 3 files changed, 1 insertion(+), 33 deletions(-) delete mode 100644 cmd/ethrex_l2/src/commands/utils.rs diff --git a/cmd/ethrex_l2/src/cli.rs b/cmd/ethrex_l2/src/cli.rs index 65b762e4c1..a4715524c2 100644 --- a/cmd/ethrex_l2/src/cli.rs +++ b/cmd/ethrex_l2/src/cli.rs @@ -1,5 +1,5 @@ use crate::{ - commands::{autocomplete, config, info, prove, stack, test, utils, wallet}, + commands::{autocomplete, config, info, prove, stack, test, wallet}, config::load_selected_config, }; use clap::{Parser, Subcommand}; @@ -33,8 +33,6 @@ enum EthrexL2Command { Info(info::Command), #[clap(about = "Read a test chain from disk and prove a block.")] Prove(prove::Command), - #[clap(subcommand, about = "Utils commands.")] - Utils(utils::Command), } pub async fn start() -> eyre::Result<()> { @@ -55,7 +53,6 @@ pub async fn start() -> eyre::Result<()> { EthrexL2Command::Test(cmd) => cmd.run(cfg).await?, EthrexL2Command::Info(cmd) => cmd.run(cfg).await?, EthrexL2Command::Prove(_) => unreachable!(), - EthrexL2Command::Utils(cmd) => cmd.run()?, }; Ok(()) } diff --git a/cmd/ethrex_l2/src/commands/mod.rs b/cmd/ethrex_l2/src/commands/mod.rs index b17b4bd9f4..80fafb9a0e 100644 --- a/cmd/ethrex_l2/src/commands/mod.rs +++ b/cmd/ethrex_l2/src/commands/mod.rs @@ -4,5 +4,4 @@ pub(crate) mod info; pub(crate) mod prove; pub(crate) mod stack; pub(crate) mod test; -pub(crate) mod utils; pub(crate) mod wallet; diff --git a/cmd/ethrex_l2/src/commands/utils.rs b/cmd/ethrex_l2/src/commands/utils.rs deleted file mode 100644 index 318878acad..0000000000 --- a/cmd/ethrex_l2/src/commands/utils.rs +++ /dev/null @@ -1,28 +0,0 @@ -use clap::Subcommand; -use keccak_hash::H256; -use secp256k1::SecretKey; - -#[derive(Subcommand)] -pub(crate) enum Command { - #[clap(about = "Convert private key to address.")] - PrivateKeyToAddress { - #[arg(long = "pk", help = "Private key in hex format.", required = true)] - private_key: String, - }, -} - -impl Command { - pub fn run(self) -> eyre::Result<()> { - match self { - Command::PrivateKeyToAddress { private_key } => { - let pk_str = private_key.strip_prefix("0x").unwrap_or(&private_key); - let pk_h256 = pk_str.parse::()?; - let pk_bytes = pk_h256.as_bytes(); - let secret_key = SecretKey::from_slice(pk_bytes)?; - let address = ethrex_l2_sdk::get_address_from_secret_key(&secret_key)?; - println!("Address: {:#x}", address); - } - } - Ok(()) - } -}