From 67b51a21427bdfc70535b479ac3c200f2457cb2f Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 31 Dec 2024 23:35:46 +0500 Subject: [PATCH 1/6] add bls address generator --- crates/rbuilder/src/utils/bls.rs | 13 +++++++++++++ crates/rbuilder/src/utils/mod.rs | 1 + 2 files changed, 14 insertions(+) create mode 100644 crates/rbuilder/src/utils/bls.rs diff --git a/crates/rbuilder/src/utils/bls.rs b/crates/rbuilder/src/utils/bls.rs new file mode 100644 index 00000000..76eebb45 --- /dev/null +++ b/crates/rbuilder/src/utils/bls.rs @@ -0,0 +1,13 @@ + +use blst::min_pk::SecretKey; +use rand::{self, RngCore}; + +pub fn generate_random_bls_address() -> [u8; 48] { + let mut rng = rand::thread_rng(); + let mut ikm = [0u8; 32]; + rng.fill_bytes(&mut ikm); + + let sk = SecretKey::key_gen(&ikm, &[]).unwrap(); + let pk = sk.sk_to_pk(); + return pk.compress(); +} \ No newline at end of file diff --git a/crates/rbuilder/src/utils/mod.rs b/crates/rbuilder/src/utils/mod.rs index 7ace6538..ea983db0 100644 --- a/crates/rbuilder/src/utils/mod.rs +++ b/crates/rbuilder/src/utils/mod.rs @@ -13,6 +13,7 @@ pub mod provider_head_state; #[cfg(test)] pub mod test_utils; pub mod tracing; +pub mod bls; use alloy_network::Ethereum; use alloy_primitives::{Address, Sign, I256, U256}; From bc6c357deb273e856a02e7a6650ccda29f504a1d Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 31 Dec 2024 23:36:31 +0500 Subject: [PATCH 2/6] hook method to cli --- Cargo.lock | 1 + crates/rbuilder/Cargo.toml | 1 + crates/rbuilder/src/live_builder/cli.rs | 12 ++++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fcef3c4d..1e20d3b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7743,6 +7743,7 @@ dependencies = [ "atoi", "beacon-api-client", "bigdecimal 0.4.6", + "blst", "built", "clap 4.5.21", "criterion 0.5.1", diff --git a/crates/rbuilder/Cargo.toml b/crates/rbuilder/Cargo.toml index fcac040d..04eaeb76 100644 --- a/crates/rbuilder/Cargo.toml +++ b/crates/rbuilder/Cargo.toml @@ -131,6 +131,7 @@ sysperf.workspace = true crossbeam = "0.8.4" parking_lot = "0.12.3" dashmap = "6.1.0" +blst = "0.3.13" [build-dependencies] built = { version = "0.7.1", features = ["git2", "chrono"] } diff --git a/crates/rbuilder/src/live_builder/cli.rs b/crates/rbuilder/src/live_builder/cli.rs index 9763b438..056bd428 100644 --- a/crates/rbuilder/src/live_builder/cli.rs +++ b/crates/rbuilder/src/live_builder/cli.rs @@ -4,6 +4,7 @@ use clap::Parser; use reth::revm::cached::CachedReads; use reth_db::Database; use reth_provider::{BlockReader, DatabaseProviderFactory, HeaderProvider, StateProviderFactory}; +use revm_primitives::hex; use serde::de::DeserializeOwned; use std::fmt::Debug; use sysperf::{format_results, gather_system_info, run_all_benchmarks}; @@ -16,7 +17,7 @@ use crate::{ base_config::load_config_toml_and_env, payload_events::MevBoostSlotDataGenerator, }, telemetry, - utils::build_info::Version, + utils::{bls::generate_random_bls_address, build_info::Version}, }; use super::{base_config::BaseConfig, LiveBuilder}; @@ -34,6 +35,8 @@ enum Cli { about = "Run system performance benchmarks (CPU, disk, memory)" )] SysPerf, + #[clap(name = "gen-bls", about = "Generate a BLS signature")] + GenBls, } #[derive(Parser, Debug)] @@ -106,6 +109,11 @@ where println!("{}", format_results(&result, &sysinfo)); return Ok(()); } + Cli::GenBls => { + let address = generate_random_bls_address(); + println!("0x{}", hex::encode(address)); + return Ok(()); + } }; let config: ConfigType = load_config_toml_and_env(cli.config)?; @@ -138,4 +146,4 @@ where ctrlc.await.unwrap_or_default(); Ok(()) -} +} \ No newline at end of file From d1bd6e7c91c11e1803bf818c03789f73dea0cb2c Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Tue, 31 Dec 2024 23:37:35 +0500 Subject: [PATCH 3/6] add test --- crates/rbuilder/src/utils/bls.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/rbuilder/src/utils/bls.rs b/crates/rbuilder/src/utils/bls.rs index 76eebb45..3194eb73 100644 --- a/crates/rbuilder/src/utils/bls.rs +++ b/crates/rbuilder/src/utils/bls.rs @@ -10,4 +10,16 @@ pub fn generate_random_bls_address() -> [u8; 48] { let sk = SecretKey::key_gen(&ikm, &[]).unwrap(); let pk = sk.sk_to_pk(); return pk.compress(); +} + + +#[cfg(test)] +mod tests { + use crate::utils::bls::generate_random_bls_address; + + #[test] + fn test_generate_random_bls_address() { + let bls_address = generate_random_bls_address(); + assert_eq!(bls_address.len(), 48, "BLS address should be of 48 length"); + } } \ No newline at end of file From 244bda9e44a61864e088f5125eab42295583bdd4 Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Wed, 1 Jan 2025 14:29:16 +0500 Subject: [PATCH 4/6] fix lint --- crates/rbuilder/src/live_builder/cli.rs | 2 +- crates/rbuilder/src/utils/bls.rs | 7 ++----- crates/rbuilder/src/utils/mod.rs | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/crates/rbuilder/src/live_builder/cli.rs b/crates/rbuilder/src/live_builder/cli.rs index 056bd428..0c6afdb3 100644 --- a/crates/rbuilder/src/live_builder/cli.rs +++ b/crates/rbuilder/src/live_builder/cli.rs @@ -146,4 +146,4 @@ where ctrlc.await.unwrap_or_default(); Ok(()) -} \ No newline at end of file +} diff --git a/crates/rbuilder/src/utils/bls.rs b/crates/rbuilder/src/utils/bls.rs index 3194eb73..4ff980fa 100644 --- a/crates/rbuilder/src/utils/bls.rs +++ b/crates/rbuilder/src/utils/bls.rs @@ -1,4 +1,3 @@ - use blst::min_pk::SecretKey; use rand::{self, RngCore}; @@ -6,13 +5,11 @@ pub fn generate_random_bls_address() -> [u8; 48] { let mut rng = rand::thread_rng(); let mut ikm = [0u8; 32]; rng.fill_bytes(&mut ikm); - let sk = SecretKey::key_gen(&ikm, &[]).unwrap(); let pk = sk.sk_to_pk(); - return pk.compress(); + pk.compress() } - #[cfg(test)] mod tests { use crate::utils::bls::generate_random_bls_address; @@ -22,4 +19,4 @@ mod tests { let bls_address = generate_random_bls_address(); assert_eq!(bls_address.len(), 48, "BLS address should be of 48 length"); } -} \ No newline at end of file +} diff --git a/crates/rbuilder/src/utils/mod.rs b/crates/rbuilder/src/utils/mod.rs index ea983db0..6635655b 100644 --- a/crates/rbuilder/src/utils/mod.rs +++ b/crates/rbuilder/src/utils/mod.rs @@ -1,4 +1,5 @@ //! a2r prefix = alloy to reth conversion +pub mod bls; pub mod build_info; pub mod constants; pub mod error_storage; @@ -13,7 +14,6 @@ pub mod provider_head_state; #[cfg(test)] pub mod test_utils; pub mod tracing; -pub mod bls; use alloy_network::Ethereum; use alloy_primitives::{Address, Sign, I256, U256}; From 24993c09556f2e04ddb68ce2645788043c363d19 Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Thu, 2 Jan 2025 01:33:45 +0500 Subject: [PATCH 5/6] refactor address generation --- Cargo.lock | 1 - crates/rbuilder/Cargo.toml | 1 - crates/rbuilder/src/live_builder/cli.rs | 3 +-- crates/rbuilder/src/utils/bls.rs | 22 +++++++++++++--------- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1e20d3b4..fcef3c4d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7743,7 +7743,6 @@ dependencies = [ "atoi", "beacon-api-client", "bigdecimal 0.4.6", - "blst", "built", "clap 4.5.21", "criterion 0.5.1", diff --git a/crates/rbuilder/Cargo.toml b/crates/rbuilder/Cargo.toml index 04eaeb76..fcac040d 100644 --- a/crates/rbuilder/Cargo.toml +++ b/crates/rbuilder/Cargo.toml @@ -131,7 +131,6 @@ sysperf.workspace = true crossbeam = "0.8.4" parking_lot = "0.12.3" dashmap = "6.1.0" -blst = "0.3.13" [build-dependencies] built = { version = "0.7.1", features = ["git2", "chrono"] } diff --git a/crates/rbuilder/src/live_builder/cli.rs b/crates/rbuilder/src/live_builder/cli.rs index 0c6afdb3..1e2a0150 100644 --- a/crates/rbuilder/src/live_builder/cli.rs +++ b/crates/rbuilder/src/live_builder/cli.rs @@ -4,7 +4,6 @@ use clap::Parser; use reth::revm::cached::CachedReads; use reth_db::Database; use reth_provider::{BlockReader, DatabaseProviderFactory, HeaderProvider, StateProviderFactory}; -use revm_primitives::hex; use serde::de::DeserializeOwned; use std::fmt::Debug; use sysperf::{format_results, gather_system_info, run_all_benchmarks}; @@ -111,7 +110,7 @@ where } Cli::GenBls => { let address = generate_random_bls_address(); - println!("0x{}", hex::encode(address)); + println!("0x{}", address); return Ok(()); } }; diff --git a/crates/rbuilder/src/utils/bls.rs b/crates/rbuilder/src/utils/bls.rs index 4ff980fa..fba47063 100644 --- a/crates/rbuilder/src/utils/bls.rs +++ b/crates/rbuilder/src/utils/bls.rs @@ -1,13 +1,13 @@ -use blst::min_pk::SecretKey; -use rand::{self, RngCore}; +use ethereum_consensus::crypto::SecretKey; +use rand; +use revm_primitives::hex; -pub fn generate_random_bls_address() -> [u8; 48] { +pub fn generate_random_bls_address() -> String { let mut rng = rand::thread_rng(); - let mut ikm = [0u8; 32]; - rng.fill_bytes(&mut ikm); - let sk = SecretKey::key_gen(&ikm, &[]).unwrap(); - let pk = sk.sk_to_pk(); - pk.compress() + let sk = SecretKey::random(&mut rng).unwrap(); + let pk = sk.public_key(); + let raw_bytes = pk.as_ref(); + hex::encode(raw_bytes) } #[cfg(test)] @@ -17,6 +17,10 @@ mod tests { #[test] fn test_generate_random_bls_address() { let bls_address = generate_random_bls_address(); - assert_eq!(bls_address.len(), 48, "BLS address should be of 48 length"); + assert_eq!( + bls_address.as_bytes().len(), + 96, + "BLS address should be of 96 length" + ); } } From 284aa6f7ce72f349d514a52b600af82e9f99b2f0 Mon Sep 17 00:00:00 2001 From: faheelsattar Date: Thu, 2 Jan 2025 01:35:53 +0500 Subject: [PATCH 6/6] fix test --- crates/rbuilder/src/utils/bls.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/rbuilder/src/utils/bls.rs b/crates/rbuilder/src/utils/bls.rs index fba47063..7e822df3 100644 --- a/crates/rbuilder/src/utils/bls.rs +++ b/crates/rbuilder/src/utils/bls.rs @@ -17,10 +17,6 @@ mod tests { #[test] fn test_generate_random_bls_address() { let bls_address = generate_random_bls_address(); - assert_eq!( - bls_address.as_bytes().len(), - 96, - "BLS address should be of 96 length" - ); + assert_eq!(bls_address.len(), 96, "BLS address should be of 96 length"); } }