diff --git a/libsigner/src/runloop.rs b/libsigner/src/runloop.rs index e7518371d..c3fa35768 100644 --- a/libsigner/src/runloop.rs +++ b/libsigner/src/runloop.rs @@ -67,7 +67,7 @@ pub trait SignerRunLoop { mut event_stop_signaler: EVST, ) -> Option { loop { - let poll_timeout = Duration::from_millis(128); //self.get_event_timeout(); + let poll_timeout = self.get_event_timeout(); let next_event_opt = match event_recv.recv_timeout(poll_timeout) { Ok(event) => Some(event), Err(RecvTimeoutError::Timeout) => None, diff --git a/stacks-signer/README.md b/stacks-signer/README.md index e8d4d719f..b3c287d9e 100644 --- a/stacks-signer/README.md +++ b/stacks-signer/README.md @@ -132,6 +132,7 @@ Generate the necessary files to run a collection of signers to communicate via s - `--private-keys:` A path to a file containing a list of hexadecimal representations of Stacks private keys. Required if `--num-keys` is not set. - `--network`: The network to use. One of "mainnet" or "testnet". - `--dir`: The directory to write files to. Defaults to the current directory. +- `--timeout`: Optional timeout in milliseconds to use when polling for updates in the StackerDB runloop. ## Contributing diff --git a/stacks-signer/src/cli.rs b/stacks-signer/src/cli.rs index 7409edc7a..5614c4219 100644 --- a/stacks-signer/src/cli.rs +++ b/stacks-signer/src/cli.rs @@ -142,6 +142,9 @@ pub struct GenerateFilesArgs { /// The directory to write the test data files to #[arg(long, default_value = ".")] pub dir: PathBuf, + /// The number of milliseconds to wait when polling for events from the stacker-db instance. + #[arg(long)] + pub timeout: Option, } /// Parse the contract ID diff --git a/stacks-signer/src/config.rs b/stacks-signer/src/config.rs index cf23adc00..5bfe5d002 100644 --- a/stacks-signer/src/config.rs +++ b/stacks-signer/src/config.rs @@ -27,7 +27,7 @@ use std::{ time::Duration, }; -const EVENT_TIMEOUT_SECS: u64 = 5; +const EVENT_TIMEOUT_MS: u64 = 5000; #[derive(thiserror::Error, Debug)] /// An error occurred parsing the provided configuration @@ -106,7 +106,7 @@ struct RawConfigFile { pub signers: Vec, /// The signer ID pub signer_id: u32, - /// The time to wait (in secs) for a response from the stacker-db instance + /// The time to wait (in millisecs) for a response from the stacker-db instance pub event_timeout: Option, } @@ -210,7 +210,7 @@ impl TryFrom for Config { signer_key_ids.insert(signer_key, s.key_ids.clone()); } let event_timeout = - Duration::from_secs(raw_data.event_timeout.unwrap_or(EVENT_TIMEOUT_SECS)); + Duration::from_millis(raw_data.event_timeout.unwrap_or(EVENT_TIMEOUT_MS)); Ok(Self { node_host, endpoint, diff --git a/stacks-signer/src/main.rs b/stacks-signer/src/main.rs index ff9d7840d..4c091c35d 100644 --- a/stacks-signer/src/main.rs +++ b/stacks-signer/src/main.rs @@ -55,6 +55,7 @@ use std::{ net::SocketAddr, path::PathBuf, sync::mpsc::{channel, Receiver}, + time::Duration, }; use wsts::Point; @@ -162,6 +163,7 @@ fn handle_generate_files(args: GenerateFilesArgs) { args.num_keys, &args.db_args.host.to_string(), &args.db_args.contract.to_string(), + args.timeout.map(Duration::from_millis), ); debug!("Built {:?} signer config tomls.", signer_config_tomls.len()); for (i, file_contents) in signer_config_tomls.iter().enumerate() { diff --git a/stacks-signer/src/utils.rs b/stacks-signer/src/utils.rs index e45ac56b5..d4b5f5126 100644 --- a/stacks-signer/src/utils.rs +++ b/stacks-signer/src/utils.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use p256k1::ecdsa; use rand_core::OsRng; use slog::slog_debug; @@ -15,6 +17,7 @@ pub fn build_signer_config_tomls( num_keys: u32, node_host: &str, contract_id: &str, + timeout: Option, ) -> Vec { let num_signers = signer_stacks_private_keys.len() as u32; let mut rng = OsRng; @@ -65,7 +68,7 @@ pub fn build_signer_config_tomls( let id = i; let message_private_key = signer_ecdsa_private_keys[i].to_string(); let stacks_private_key = stacks_private_key.to_hex(); - let signer_config_toml = format!( + let mut signer_config_toml = format!( r#" message_private_key = "{message_private_key}" stacks_private_key = "{stacks_private_key}" @@ -77,6 +80,16 @@ signer_id = {id} {signers_array} "# ); + + if let Some(timeout) = timeout { + let event_timeout_ms = timeout.as_millis(); + signer_config_toml = format!( + r#" +{signer_config_toml} +event_timeout = {event_timeout_ms} +"# + ) + } signer_config_tomls.push(signer_config_toml); } signer_config_tomls diff --git a/testnet/stacks-node/src/tests/signer.rs b/testnet/stacks-node/src/tests/signer.rs index 2928be753..571eb5f43 100644 --- a/testnet/stacks-node/src/tests/signer.rs +++ b/testnet/stacks-node/src/tests/signer.rs @@ -176,6 +176,7 @@ fn test_stackerdb_dkg() { num_keys, &conf.node.rpc_bind, &contract_id.to_string(), + Some(Duration::from_millis(128)), // Timeout defaults to 5 seconds. Let's override it to 128 milliseconds. ); // The test starts here