Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --dev-tx-interval-ms CLI option #2822

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions cli/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 @@ -124,6 +124,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>,
/// Specify the path to a directory containing the ledger
#[clap(long = "storage_path")]
pub storage_path: Option<PathBuf>,
Expand Down Expand Up @@ -454,8 +457,9 @@ impl Start {

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

use aleo_std::StorageMode;
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 @@ -50,6 +50,7 @@ impl<N: Network> Node<N> {
genesis: Block<N>,
cdn: Option<String>,
storage_mode: StorageMode,
dev_tx_interval: Option<Duration>,
) -> Result<Self> {
Ok(Self::Validator(Arc::new(
Validator::new(
Expand All @@ -63,6 +64,7 @@ impl<N: Network> Node<N> {
genesis,
cdn,
storage_mode,
dev_tx_interval,
)
.await?,
)))
Expand Down
25 changes: 19 additions & 6 deletions node/src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
genesis: Block<N>,
cdn: Option<String>,
storage_mode: StorageMode,
dev_tx_interval: Option<Duration>,
) -> Result<Self> {
// Prepare the shutdown flag.
let shutdown: Arc<AtomicBool> = Default::default();
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<N: Network, C: ConsensusStorage<N>> Validator<N, C> {
shutdown,
};
// Initialize the transaction pool.
node.initialize_transaction_pool(storage_mode)?;
node.initialize_transaction_pool(storage_mode, dev_tx_interval)?;

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

/// Initialize the transaction pool.
fn initialize_transaction_pool(&self, storage_mode: StorageMode) -> Result<()> {
fn initialize_transaction_pool(&self, storage_mode: StorageMode, 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 storage_mode {
let interval = match storage_mode {
// If the node is running in development mode, only generate if you are allowed.
StorageMode::Development(id) => {
// If the node is not the first node, do not start the loop.
if id != 0 {
if id != 0 && dev_tx_interval.is_none() {
return Ok(());
}

// Note: dev_tx_interval can be Some(Duration::from_millis(0)), which is handled after this
dev_tx_interval.unwrap_or(DEFAULT_INTERVAL)
}
_ => {
// Retrieve the genesis committee.
Expand All @@ -371,8 +378,13 @@ 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
}
};
if interval.as_millis() == 0 {
// if the tx interval was explicitly set to 0, do not start the loop
return Ok(());
}

let self_ = self.clone();
Expand All @@ -382,7 +394,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 @@ -496,6 +508,7 @@ mod tests {
genesis,
None,
storage_mode,
None,
)
.await
.unwrap();
Expand Down
1 change: 1 addition & 0 deletions node/tests/common/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub async fn validator() -> Validator<CurrentNetwork, ConsensusMemory<CurrentNet
sample_genesis_block(), // Should load the current network's genesis block.
None, // No CDN.
StorageMode::Production,
None,
)
.await
.expect("couldn't create validator instance")
Expand Down