Skip to content

Commit

Permalink
Update build_signer_config_tomls to have Option<Duration> for event_t…
Browse files Browse the repository at this point in the history
…imeout

Signed-off-by: Jacinta Ferrant <[email protected]>
  • Loading branch information
jferrant committed Sep 14, 2023
1 parent db51c04 commit 54eb560
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libsigner/src/runloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub trait SignerRunLoop<R, CMD: Send> {
mut event_stop_signaler: EVST,
) -> Option<R> {
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,
Expand Down
1 change: 1 addition & 0 deletions stacks-signer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions stacks-signer/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
}

/// Parse the contract ID
Expand Down
6 changes: 3 additions & 3 deletions stacks-signer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,7 +106,7 @@ struct RawConfigFile {
pub signers: Vec<RawSigners>,
/// 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<u64>,
}

Expand Down Expand Up @@ -210,7 +210,7 @@ impl TryFrom<RawConfigFile> 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,
Expand Down
2 changes: 2 additions & 0 deletions stacks-signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use std::{
net::SocketAddr,
path::PathBuf,
sync::mpsc::{channel, Receiver},
time::Duration,
};
use wsts::Point;

Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 14 additions & 1 deletion stacks-signer/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use p256k1::ecdsa;
use rand_core::OsRng;
use slog::slog_debug;
Expand All @@ -15,6 +17,7 @@ pub fn build_signer_config_tomls(
num_keys: u32,
node_host: &str,
contract_id: &str,
timeout: Option<Duration>,
) -> Vec<String> {
let num_signers = signer_stacks_private_keys.len() as u32;
let mut rng = OsRng;
Expand Down Expand Up @@ -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}"
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions testnet/stacks-node/src/tests/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 54eb560

Please sign in to comment.