Skip to content

Commit

Permalink
Merge pull request #143 from sr-gi/20231017-adds-tests
Browse files Browse the repository at this point in the history
sim-all: Adds tests coverage to `random_activity`
  • Loading branch information
sr-gi authored Oct 30, 2023
2 parents 1270a16 + 4ad2105 commit 6713c6f
Show file tree
Hide file tree
Showing 6 changed files with 347 additions and 25 deletions.
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ struct Cli {
#[clap(long, short, verbatim_doc_comment, default_value = "info")]
log_level: LevelFilter,
/// Expected payment amount for the random activity generator
#[clap(long, short, default_value_t = EXPECTED_PAYMENT_AMOUNT)]
#[clap(long, short, default_value_t = EXPECTED_PAYMENT_AMOUNT, value_parser = clap::builder::RangedU64ValueParser::<u64>::new().range(1..u64::MAX))]
expected_pmt_amt: u64,
/// Multiplier of the overall network capacity used by the random activity generator
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER)]
#[clap(long, short, default_value_t = ACTIVITY_MULTIPLIER, value_parser = clap::builder::RangedU64ValueParser::<u32>::new().range(1..u64::MAX))]
capacity_multiplier: f64,
/// Do not create an output file containing the simulations results
#[clap(long, default_value_t = false)]
Expand Down
3 changes: 3 additions & 0 deletions sim-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ hex = "0.4.3"
csv = "1.2.2"
serde_millis = "0.1.1"
rand_distr = "0.4.3"

[dev-dependencies]
ntest = "0.9.0"
32 changes: 21 additions & 11 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bitcoin::Network;
use csv::WriterBuilder;
use lightning::ln::features::NodeFeatures;
use lightning::ln::PaymentHash;
use random_activity::RandomActivityError;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fmt::{Display, Formatter};
Expand All @@ -23,6 +24,8 @@ pub mod cln;
pub mod lnd;
mod random_activity;
mod serializers;
#[cfg(test)]
mod test_utils;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
Expand Down Expand Up @@ -125,8 +128,8 @@ pub enum SimulationError {
CsvError(#[from] csv::Error),
#[error("File Error")]
FileError,
#[error("Random activity Error: {0}")]
RandomActivityError(String),
#[error("{0}")]
RandomActivityError(RandomActivityError),
}

// Phase 2: Event Queue
Expand Down Expand Up @@ -203,12 +206,15 @@ pub trait NetworkGenerator {
fn sample_node_by_capacity(&self, source: PublicKey) -> (NodeInfo, u64);
}

#[derive(Debug, Error)]
#[error("Payment generation error: {0}")]
pub struct PaymentGenerationError(String);
pub trait PaymentGenerator {
// Returns the number of seconds that a node should wait until firing its next payment.
fn next_payment_wait(&self) -> time::Duration;

// Returns a payment amount based on the capacity of the sending and receiving node.
fn payment_amount(&self, destination_capacity: u64) -> Result<u64, SimulationError>;
fn payment_amount(&self, destination_capacity: u64) -> Result<u64, PaymentGenerationError>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -642,9 +648,10 @@ impl Simulation {
producer_channels: HashMap<PublicKey, Sender<SimulationEvent>>,
tasks: &mut JoinSet<()>,
) -> Result<(), SimulationError> {
let network_generator = Arc::new(Mutex::new(NetworkGraphView::new(
node_capacities.values().cloned().collect(),
)?));
let network_generator = Arc::new(Mutex::new(
NetworkGraphView::new(node_capacities.values().cloned().collect())
.map_err(SimulationError::RandomActivityError)?,
));

log::info!(
"Created network generator: {}.",
Expand All @@ -655,18 +662,21 @@ impl Simulation {
let (info, source_capacity) = match node_capacities.get(&pk) {
Some((info, capacity)) => (info.clone(), *capacity),
None => {
return Err(SimulationError::RandomActivityError(format!(
"Random activity generator run for: {} with unknown capacity.",
pk
)));
return Err(SimulationError::RandomActivityError(
RandomActivityError::ValueError(format!(
"Random activity generator run for: {} with unknown capacity.",
pk
)),
));
}
};

let node_generator = PaymentActivityGenerator::new(
source_capacity,
self.expected_payment_msat,
self.activity_multiplier,
)?;
)
.map_err(SimulationError::RandomActivityError)?;

tasks.spawn(produce_random_events(
info,
Expand Down
Loading

0 comments on commit 6713c6f

Please sign in to comment.