diff --git a/Cargo.lock b/Cargo.lock index d803f09..18746ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3104,6 +3104,15 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.0.0" @@ -3336,6 +3345,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.52.0", diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index f24f614..9900569 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -15,7 +15,7 @@ rand = { workspace = true } serde = { workspace = true, features = ["derive"] } futures = { workspace = true } async-trait = { workspace = true } -tokio = { workspace = true } +tokio = { workspace = true, features = ["signal"]} alloy-serde = { workspace = true } serde_json = { workspace = true } contender_bundle_provider = { workspace = true } diff --git a/crates/core/src/spammer/spammer_trait.rs b/crates/core/src/spammer/spammer_trait.rs index 8ac4519..c382e75 100644 --- a/crates/core/src/spammer/spammer_trait.rs +++ b/crates/core/src/spammer/spammer_trait.rs @@ -1,3 +1,4 @@ +use std::sync::Mutex; use std::{pin::Pin, sync::Arc}; use alloy::providers::Provider; @@ -39,6 +40,15 @@ where run_id: Option, sent_tx_callback: Arc, ) -> impl std::future::Future> { + let quit = Arc::new(Mutex::new(false)); + + let quit_clone = quit.clone(); + tokio::task::spawn(async move { + let _ = tokio::signal::ctrl_c().await; + let mut quit = quit_clone.lock().unwrap(); + *quit = true; + }); + async move { let tx_requests = scenario .load_txs(crate::generator::PlanType::Spam( @@ -82,6 +92,10 @@ where if cache_size == 0 { break; } + if quit.lock().expect("lock failure").clone() { + println!("CTRL-C received, stopping spam..."); + break; + } block_counter += 1; } println!("done spamming. run_id={}", run_id);