Skip to content

Commit

Permalink
create, save, and load user seed: ~/.contender/seed
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Jan 9, 2025
1 parent c364e1b commit 621aea7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ clap = { workspace = true, features = ["derive"] }
alloy = { workspace = true, features = ["full", "node-bindings"] }
csv = { workspace = true }
termcolor = "1.4.1"
rand.workspace = true
14 changes: 4 additions & 10 deletions crates/cli/src/commands/contender_subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ Requires --priv-key to be set for each 'from' address in the given testfile.",
#[arg(
short,
long,
long_help = "The seed to use for generating spam transactions",
default_value = "0xffffffffffffffffffffffffffffffff13131313131313131313131313131313"
long_help = "The seed to use for generating spam transactions"
)]
seed: String,
seed: Option<String>,

/// The private keys to use for blockwise spamming.
/// Required if `txs_per_block` is set.
Expand Down Expand Up @@ -112,13 +111,8 @@ May be specified multiple times."
min_balance: String,

/// The seed used to generate pool accounts.
#[arg(
short,
long,
long_help = "The seed used to generate pool accounts.",
default_value = "0xffffffffffffffffffffffffffffffff13131313131313131313131313131313"
)]
seed: String,
#[arg(short, long, long_help = "The seed used to generate pool accounts.")]
seed: Option<String>,

/// The number of signers to generate for each pool.
#[arg(
Expand Down
24 changes: 24 additions & 0 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ mod util;

use std::sync::LazyLock;

use alloy::hex;
use commands::{ContenderCli, ContenderSubcommand, SpamCommandArgs};
use contender_core::{db::DbOps, generator::RandSeed};
use contender_sqlite::SqliteDb;
use rand::Rng;

static DB: LazyLock<SqliteDb> = std::sync::LazyLock::new(|| {
let path = &format!(
Expand All @@ -26,6 +28,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let _ = DB.create_tables(); // ignore error; tables already exist
let db = DB.clone();

let home = std::env::var("HOME").expect("$HOME not found in environment");
let contender_path = format!("{}/.contender", home);
if !std::path::Path::new(&contender_path).exists() {
std::fs::create_dir_all(&contender_path).expect("failed to create contender directory");
}

let seed_path = format!("{}/seed", &contender_path);
if !std::path::Path::new(&seed_path).exists() {
println!("generating seed file at {}", &seed_path);
let mut rng = rand::thread_rng();
let seed: [u8; 32] = rng.gen();
let seed_hex = hex::encode(seed);
std::fs::write(&seed_path, seed_hex).expect("failed to write seed file");
}

let stored_seed = format!(
"0x{}",
std::fs::read_to_string(&seed_path).expect("failed to read seed file")
);

match args.command {
ContenderSubcommand::Setup {
testfile,
Expand All @@ -35,6 +57,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
seed,
num_signers_per_pool,
} => {
let seed = seed.unwrap_or(stored_seed);
commands::setup(
&db,
testfile,
Expand All @@ -59,6 +82,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
disable_reports,
min_balance,
} => {
let seed = seed.unwrap_or(stored_seed);
commands::spam(
&db,
SpamCommandArgs {
Expand Down

0 comments on commit 621aea7

Please sign in to comment.