Skip to content

Commit

Permalink
fix: spammer-helder signing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Jul 18, 2024
1 parent 487d06a commit 4790102
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 49 deletions.
3 changes: 0 additions & 3 deletions bolt-spammer-helder/src/lib.rs

This file was deleted.

40 changes: 25 additions & 15 deletions bolt-spammer-helder/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
use std::{path::PathBuf, str::FromStr};

use alloy::{
eips::eip2718::Encodable2718,
hex,
network::EthereumWallet,
network::{EthereumWallet, TransactionBuilder},
primitives::{keccak256, Address},
signers::{local::PrivateKeySigner, Signer},
};
use beacon_api_client::mainnet::Client as BeaconApiClient;
use bolt_spammer_helder::{
constants::SLOTS_PER_EPOCH,
contract::BoltRegistry,
utils::{
current_slot, generate_random_blob_tx, generate_random_tx, prepare_rpc_request,
sign_transaction,
},
};
use clap::Parser;
use eyre::Result;
use reqwest::Url;
use reth_primitives::Address;
use tracing::info;

pub mod constants;
pub mod contract;
pub mod utils;

use crate::{
constants::SLOTS_PER_EPOCH,
contract::BoltRegistry,
utils::{current_slot, generate_random_blob_tx, generate_random_tx, prepare_rpc_request},
};

#[derive(Parser)]
struct Opts {
/// EL node URL to send transactions to
#[clap(short = 'p', long, default_value = "https://rpc.helder-devnets.xyz", env)]
el_provider_url: String,
/// CL node URL to fetch the beacon chain info from
#[clap(short = 'c', long, default_value = "http://localhost:4000", env)]
beacon_client_url: Url,
/// Address of the Bolt Registry smart contract
#[clap(short = 'r', long, default_value = "0xdF11D829eeC4C192774F3Ec171D822f6Cb4C14d9", env)]
registry_address: Address,
/// Private key to sign transactions with
#[clap(short = 'k', long, env)]
private_key: String,
/// Path to the ABI file of the Bolt Registry smart contract
#[clap(short = 'a', long, env, default_value = "./registry_abi.json")]
registry_abi_path: PathBuf,
// Flag for generating a blob tx instead of a regular tx
Expand All @@ -42,8 +50,7 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
tracing::info!("starting bolt-spammer-helder");

let path = dotenvy::dotenv()?;
tracing::info!("loaded environment variables from {:?}", path);
let _ = dotenvy::dotenv()?;
let opts = Opts::parse();

let wallet: PrivateKeySigner = opts.private_key.parse().expect("should parse private key");
Expand Down Expand Up @@ -85,16 +92,19 @@ async fn main() -> Result<()> {

let tx = if opts.blob { generate_random_blob_tx() } else { generate_random_tx() };

let (tx_hash, tx_rlp) = sign_transaction(&transaction_signer, tx).await?;
let tx_signed = tx.build(&transaction_signer).await?;
let tx_hash = tx_signed.tx_hash().to_string();
let tx_rlp = hex::encode(tx_signed.encoded_2718());

let message_digest = {
let mut data = Vec::new();
data.extend_from_slice(&next_preconfer_slot.to_le_bytes());
data.extend_from_slice(hex::decode(tx_hash.trim_start_matches("0x"))?.as_slice());
data
keccak256(data)
};

let signature = wallet.sign_message(message_digest.as_ref()).await?;
let signature = wallet.sign_hash(&message_digest).await?;
let signature = hex::encode(signature.as_bytes());

let request = prepare_rpc_request(
"bolt_inclusionPreconfirmation",
Expand Down
34 changes: 8 additions & 26 deletions bolt-spammer-helder/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ use std::str::FromStr;

use alloy::{
consensus::{BlobTransactionSidecar, SidecarBuilder, SimpleCoder},
hex,
network::{eip2718::Encodable2718, EthereumWallet, TransactionBuilder},
network::TransactionBuilder,
primitives::{Address, U256},
rpc::types::TransactionRequest,
};
use beacon_api_client::{mainnet::Client as BeaconApiClient, BlockId};
use eyre::Result;
use rand::{thread_rng, Rng};
use reth_primitives::TransactionSigned;
use serde_json::Value;

use crate::constants::{DEAD_ADDRESS, HELDER_TESTNET_CHAIN_ID, NOICE_GAS_PRICE};
Expand All @@ -21,6 +19,7 @@ pub fn generate_random_tx() -> TransactionRequest {
.with_to(Address::from_str(DEAD_ADDRESS).unwrap())
.with_chain_id(HELDER_TESTNET_CHAIN_ID)
.with_value(U256::from(thread_rng().gen_range(1..100)))
.with_gas_limit(1000000u128)
.with_gas_price(NOICE_GAS_PRICE)
}

Expand All @@ -31,32 +30,15 @@ pub fn generate_random_blob_tx() -> TransactionRequest {

let dead_address = Address::from_str(DEAD_ADDRESS).unwrap();

let tx: TransactionRequest = TransactionRequest::default()
TransactionRequest::default()
.with_to(dead_address)
.with_chain_id(HELDER_TESTNET_CHAIN_ID)
.with_value(U256::from(100))
.with_gas_price(NOICE_GAS_PRICE)
.with_blob_sidecar(sidecar);

tx
}

/// Signs a [TypedTransaction] with the given [Signer], returning a tuple
/// with the transaction hash and the RLP-encoded signed transaction.
pub async fn sign_transaction(
signer: &EthereumWallet,
tx: TransactionRequest,
) -> Result<(String, String)> {
let Ok(signed) = tx.build(signer).await else {
return Err(eyre::eyre!("Failed to sign transaction"));
};
let tx_signed_bytes = signed.encoded_2718();
let tx_signed = TransactionSigned::decode_enveloped(&mut tx_signed_bytes.as_slice()).unwrap();

let tx_hash = tx_signed.hash().to_string();
let hex_rlp_signed_tx = format!("0x{}", hex::encode(tx_signed_bytes));

Ok((tx_hash, hex_rlp_signed_tx))
.with_max_fee_per_blob_gas(100u128)
.max_fee_per_gas(100u128)
.max_priority_fee_per_gas(50u128)
.with_gas_limit(1_000_000u128)
.with_blob_sidecar(sidecar)
}

pub fn prepare_rpc_request(method: &str, params: Vec<Value>) -> Value {
Expand Down
2 changes: 1 addition & 1 deletion bolt-spammer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct Opts {
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
tracing::info!("starting bolt-spammer-helder");
tracing::info!("starting bolt-spammer");

let opts = Opts::parse();

Expand Down
6 changes: 2 additions & 4 deletions bolt-spammer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@ pub fn generate_random_blob_tx() -> TransactionRequest {

let dead_address = Address::from_str(DEAD_ADDRESS).unwrap();

let tx: TransactionRequest = TransactionRequest::default()
TransactionRequest::default()
.with_to(dead_address)
.with_chain_id(KURTOSIS_CHAIN_ID)
.with_value(U256::from(100))
.with_max_fee_per_blob_gas(100u128)
.max_fee_per_gas(100u128)
.max_priority_fee_per_gas(50u128)
.with_gas_limit(1_000_000u128)
.with_blob_sidecar(sidecar);

tx
.with_blob_sidecar(sidecar)
}

pub fn prepare_rpc_request(method: &str, params: Vec<Value>) -> Value {
Expand Down

0 comments on commit 4790102

Please sign in to comment.