Skip to content
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

Fix: #138. Set temporary coinbase signer during backtesting if none found #215

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config-backtest-example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ backtest_fetch_eth_rpc_url = "http://127.0.0.1:8545"
backtest_fetch_eth_rpc_parallel = 400
backtest_fetch_output_file = "~/.rbuilder/backtest/main.sqlite"
backtest_fetch_mempool_data_dir = "~/.rbuilder/mempool-data"
coinbase_secret_key = "env:COINBASE_SECRET_KEY"

sbundle_mergeabe_signers = []

Expand Down
22 changes: 21 additions & 1 deletion crates/rbuilder/src/backtest/backtest_build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use ahash::HashMap;
use alloy_primitives::utils::format_ether;
use revm_primitives::hex;
use secp256k1::{rand::rngs::OsRng, SecretKey};

use crate::backtest::restore_landed_orders::{
restore_landed_orders, sim_historical_block, ExecutedBlockTx, ExecutedTxs, SimplifiedOrder,
Expand All @@ -23,7 +25,7 @@ use crate::{
utils::timestamp_as_u64,
};
use clap::Parser;
use std::path::PathBuf;
use std::{env, path::PathBuf};

#[derive(Parser, Debug)]
struct Cli {
Expand Down Expand Up @@ -101,6 +103,10 @@ pub async fn run_backtest_build_block<ConfigType: LiveBuilderConfig>() -> eyre::
print_onchain_block_data(tx_sim_results, &orders, &block_data);
}

if let Err(_) = config.base_config().coinbase_signer() {
add_env_coinbase_signer();
}

let BacktestBlockInput {
ctx, sim_orders, ..
} = backtest_prepare_ctx_for_block(
Expand Down Expand Up @@ -401,3 +407,17 @@ fn print_onchain_block_data(
}
}
}

// Generate a random private key
fn generate_private_key() -> String {
let mut rng = OsRng;
let secret_key = SecretKey::new(&mut rng);
let hex_secret_key = hex::encode(secret_key.secret_bytes());
hex_secret_key
}

// Add COINBASE_SECRET_KEY as an environment variable during runtime
fn add_env_coinbase_signer() {
let private_key = generate_private_key();
env::set_var("COINBASE_SECRET_KEY", private_key);
}
Loading