-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(l1, l2): show rich wallets on startup #2045
Open
tomip01
wants to merge
9
commits into
main
Choose a base branch
from
l2/print-rich-wallets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa55db7
sort and show top 10 accounts
tomip01 43e550c
add cli to convert pk to address
tomip01 bb3e9a7
sort addresses
tomip01 5912077
change calling function
tomip01 2ab2eab
changes
tomip01 b994a9b
use str macro
tomip01 e27d5d4
remove old things
tomip01 d34bfd0
remove comment
tomip01 da1d4f7
Merge branch 'main' into l2/print-rich-wallets
tomip01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
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, H256, U256, | ||
}; | ||
use ethrex_p2p::{ | ||
kademlia::KademliaTable, | ||
network::{node_id_from_signing_key, peer_table}, | ||
sync::{SyncManager, SyncMode}, | ||
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, | ||
|
@@ -292,13 +299,17 @@ 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")] { | ||
use ethrex_dev; | ||
// Start the block_producer module if devmode was set | ||
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); | ||
let authrpc_jwtsecret = | ||
std::fs::read(authrpc_jwtsecret).expect("Failed to read JWT secret"); | ||
let head_block_hash = { | ||
|
@@ -497,3 +508,52 @@ fn read_known_peers(file_path: PathBuf) -> Result<Vec<Node>, serde_json::Error> | |
|
||
serde_json::from_reader(file) | ||
} | ||
|
||
fn show_rich_accounts(genesis: &Genesis, contents: &str) { | ||
let private_keys: Vec<String> = 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::<H256>() else { | ||
return; | ||
}; | ||
Comment on lines
+522
to
+524
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is any error, don't print the addresses so we don't stop the ethrex client |
||
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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::<H256>()?; | ||
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use this so we can run the print independent where we execute it