Skip to content

Commit

Permalink
feat: add --dev-tx-interval-ms CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
vvp committed Nov 15, 2023
1 parent 3066d73 commit 7eb2b53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
9 changes: 7 additions & 2 deletions cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use colored::Colorize;
use core::str::FromStr;
use rand::SeedableRng;
use rand_chacha::ChaChaRng;
use std::{net::SocketAddr, path::PathBuf};
use std::{net::SocketAddr, path::PathBuf, time::Duration};
use tokio::runtime::{self, Runtime};

/// The recommended minimum number of 'open files' limit for a validator.
Expand Down Expand Up @@ -117,6 +117,9 @@ pub struct Start {
/// If development mode is enabled, specify the number of genesis validators (default: 4)
#[clap(long)]
pub dev_num_validators: Option<u16>,
/// If development mode is enabled, specify the interval of mock transaction generation in milliseconds (default: 500ms if dev=0, None otherwise)
#[clap(long)]
pub dev_tx_interval_ms: Option<u16>,
}

impl Start {
Expand Down Expand Up @@ -430,8 +433,10 @@ impl Start {

// Initialize the node.
let bft_ip = if self.dev.is_some() { self.bft } else { None };
// Treat dev_tx_interval_ms=0 as Option<Duration>::None
let dev_tx_interval = self.dev_tx_interval_ms.filter(|msec| *msec > 0).map(|msec| Duration::from_millis(msec as u64));
match node_type {
NodeType::Validator => Node::new_validator(self.node, rest_ip, bft_ip, account, &trusted_peers, &trusted_validators, genesis, cdn, self.dev).await,
NodeType::Validator => Node::new_validator(self.node, rest_ip, bft_ip, account, &trusted_peers, &trusted_validators, genesis, cdn, self.dev, dev_tx_interval).await,
NodeType::Prover => Node::new_prover(self.node, account, &trusted_peers, genesis, self.dev).await,
NodeType::Client => Node::new_client(self.node, rest_ip, account, &trusted_peers, genesis, cdn, self.dev).await,
}
Expand Down
18 changes: 15 additions & 3 deletions node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use snarkvm::prelude::{
};

use anyhow::Result;
use std::{net::SocketAddr, sync::Arc};
use std::{net::SocketAddr, sync::Arc, time::Duration};

pub enum Node<N: Network> {
/// A validator is a full node, capable of validating blocks.
Expand All @@ -48,10 +48,22 @@ impl<N: Network> Node<N> {
genesis: Block<N>,
cdn: Option<String>,
dev: Option<u16>,
dev_tx_interval: Option<Duration>,
) -> Result<Self> {
Ok(Self::Validator(Arc::new(
Validator::new(node_ip, rest_ip, bft_ip, account, trusted_peers, trusted_validators, genesis, cdn, dev)
.await?,
Validator::new(
node_ip,
rest_ip,
bft_ip,
account,
trusted_peers,
trusted_validators,
genesis,
cdn,
dev,
dev_tx_interval,
)
.await?,
)))
}

Expand Down
26 changes: 17 additions & 9 deletions node/src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
genesis: Block<N>,
cdn: Option<String>,
dev: Option<u16>,
dev_tx_interval: Option<Duration>,
) -> Result<Self> {
// Initialize the signal handler.
let signal_node = Self::handle_signals();
Expand Down Expand Up @@ -132,7 +133,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
shutdown: Default::default(),
};
// Initialize the transaction pool.
node.initialize_transaction_pool(dev)?;
node.initialize_transaction_pool(dev, dev_tx_interval)?;

// Initialize the REST server.
if let Some(rest_ip) = rest_ip {
Expand Down Expand Up @@ -331,24 +332,29 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
// }

/// Initialize the transaction pool.
fn initialize_transaction_pool(&self, dev: Option<u16>) -> Result<()> {
fn initialize_transaction_pool(&self, dev: Option<u16>, dev_tx_interval: Option<Duration>) -> Result<()> {
use snarkvm::console::{
program::{Identifier, Literal, ProgramID, Value},
types::U64,
};
use std::str::FromStr;

// The default transaction generation interval (overrideable with --dev-tx-interval-ms)
const DEFAULT_INTERVAL: Duration = Duration::from_millis(500);

// Initialize the locator.
let locator = (ProgramID::from_str("credits.aleo")?, Identifier::from_str("transfer_public")?);

// Determine whether to start the loop.
match dev {
// Determine whether to start the loop and resolve generation interval.
let interval = match dev {
// If the node is running in development mode, only generate if you are allowed.
Some(dev) => {
// If the node is not the first node, do not start the loop.
if dev != 0 {
// If the node is not the first node and tx interval is not explicitly set, do not start the loop.
if dev != 0 && dev_tx_interval.is_none() {
return Ok(());
}

dev_tx_interval.unwrap_or(DEFAULT_INTERVAL)
}
None => {
// Retrieve the genesis committee.
Expand All @@ -362,9 +368,10 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
// If the node is not the first member, do not start the loop.
if self.address() != *first_member {
return Ok(());
}
};
DEFAULT_INTERVAL
}
}
};

let self_ = self.clone();
self.spawn(async move {
Expand All @@ -373,7 +380,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {

// Start the transaction loop.
loop {
tokio::time::sleep(Duration::from_millis(500)).await;
tokio::time::sleep(interval).await;

// Prepare the inputs.
let inputs = [Value::from(Literal::Address(self_.address())), Value::from(Literal::U64(U64::new(1)))];
Expand Down Expand Up @@ -486,6 +493,7 @@ mod tests {
genesis,
None,
dev,
None,
)
.await
.unwrap();
Expand Down

0 comments on commit 7eb2b53

Please sign in to comment.