Skip to content

Commit

Permalink
sim-lib: only use start delay if explicitly specified
Browse files Browse the repository at this point in the history
As is, we'll run with a 0 second start delay for:
- Random activities
- Defined activities with no `start_secs` specified

This very likely isn't the intent for defined activities (people should
set `start_secs=0` if they want their payments to fire immediately),
and doesn't work for random activity because we just fire all payments
on start.

Instead, we switch over to using our payment wait as the start delay in
the absence of this field being set for defined activities and always
for random activities.
  • Loading branch information
carlaKC committed Jun 28, 2024
1 parent e369d61 commit 44bb6b6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
9 changes: 4 additions & 5 deletions sim-lib/src/defined_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tokio::time::Duration;
#[derive(Clone)]
pub struct DefinedPaymentActivity {
destination: NodeInfo,
start: Duration,
start: Option<Duration>,
count: Option<u64>,
wait: ValueOrRange<u16>,
amount: ValueOrRange<u64>,
Expand All @@ -16,7 +16,7 @@ pub struct DefinedPaymentActivity {
impl DefinedPaymentActivity {
pub fn new(
destination: NodeInfo,
start: Duration,
start: Option<Duration>,
count: Option<u64>,
wait: ValueOrRange<u16>,
amount: ValueOrRange<u64>,
Expand Down Expand Up @@ -48,7 +48,7 @@ impl DestinationGenerator for DefinedPaymentActivity {
}

impl PaymentGenerator for DefinedPaymentActivity {
fn payment_start(&self) -> Duration {
fn payment_start(&self) -> Option<Duration> {
self.start
}

Expand Down Expand Up @@ -77,7 +77,6 @@ impl PaymentGenerator for DefinedPaymentActivity {
#[cfg(test)]
mod tests {
use super::DefinedPaymentActivity;
use super::*;
use crate::test_utils::{create_nodes, get_random_keypair};
use crate::{DestinationGenerator, PaymentGenerationError, PaymentGenerator};

Expand All @@ -91,7 +90,7 @@ mod tests {

let generator = DefinedPaymentActivity::new(
node.clone(),
Duration::from_secs(0),
None,
None,
crate::ValueOrRange::Value(60),
crate::ValueOrRange::Value(payment_amt),
Expand Down
28 changes: 15 additions & 13 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ pub struct ActivityParser {
#[serde(with = "serializers::serde_node_id")]
pub destination: NodeId,
/// The time in the simulation to start the payment.
#[serde(default)]
pub start_secs: u16,
pub start_secs: Option<u16>,
/// The number of payments to send over the course of the simulation.
#[serde(default)]
pub count: Option<u64>,
Expand All @@ -204,7 +203,7 @@ pub struct ActivityDefinition {
/// The destination of the payment.
pub destination: NodeInfo,
/// The time in the simulation to start the payment.
pub start_secs: u16,
pub start_secs: Option<u16>,
/// The number of payments to send over the course of the simulation.
pub count: Option<u64>,
/// The interval of the event, as in every how many seconds the payment is performed.
Expand Down Expand Up @@ -316,7 +315,7 @@ pub struct PaymentGenerationError(String);

pub trait PaymentGenerator: Display + Send {
/// Returns the time that the payments should start
fn payment_start(&self) -> Duration;
fn payment_start(&self) -> Option<Duration>;

/// Returns the number of payments that should be made
fn payment_count(&self) -> Option<u64>;
Expand Down Expand Up @@ -773,7 +772,9 @@ impl Simulation {
for description in self.activity.iter() {
let activity_generator = DefinedPaymentActivity::new(
description.destination.clone(),
Duration::from_secs(description.start_secs.into()),
description
.start_secs
.map(|start| Duration::from_secs(start.into())),
description.count,
description.interval_secs,
description.amount_msat,
Expand Down Expand Up @@ -1037,14 +1038,15 @@ async fn produce_events<N: DestinationGenerator + ?Sized, A: PaymentGenerator +
}
}

let wait: Duration = if current_count == 0 {
let start = node_generator.payment_start();
if start != Duration::from_secs(0) {
log::debug!(
"Using a start delay. The first payment for {source} will be at {:?}.",
start
);
}
// On our first run, add a one-time start delay if set for our payment.
let opt_start = node_generator.payment_start();
let wait = if current_count == 0 && opt_start.is_some() {
let start = opt_start.unwrap(); // Safe due to is_some check above.
log::debug!(
"Using a start delay. The first payment for {source} will be at {:?}.",
start
);

start
} else {
log::debug!(
Expand Down
4 changes: 2 additions & 2 deletions sim-lib/src/random_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ fn events_per_month(source_capacity_msat: u64, multiplier: f64, expected_payment

impl PaymentGenerator for RandomPaymentActivity {
/// Returns the time that the payments should start. This will always be 0 for the RandomPaymentActivity type.
fn payment_start(&self) -> Duration {
Duration::from_secs(0)
fn payment_start(&self) -> Option<Duration> {
None
}

/// Returns the number of payments that should be made. This will always be None for the RandomPaymentActivity type.
Expand Down

0 comments on commit 44bb6b6

Please sign in to comment.