Skip to content

Commit b50b4f0

Browse files
committed
Issue #168: Adding optional start and count values to sim.json file
1 parent c3292a8 commit b50b4f0

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

sim-cli/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ async fn main() -> anyhow::Result<()> {
185185
validated_activities.push(ActivityDefinition {
186186
source,
187187
destination,
188+
start_secs: act.start_secs,
189+
count: act.count,
188190
interval_secs: act.interval_secs,
189191
amount_msat: act.amount_msat,
190192
});

sim-lib/src/defined_activity.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@ use tokio::time::Duration;
55
#[derive(Clone)]
66
pub struct DefinedPaymentActivity {
77
destination: NodeInfo,
8+
start: Duration,
9+
count: Option<u64>,
810
wait: Duration,
911
amount: u64,
1012
}
1113

1214
impl DefinedPaymentActivity {
13-
pub fn new(destination: NodeInfo, wait: Duration, amount: u64) -> Self {
15+
pub fn new(
16+
destination: NodeInfo,
17+
start: Duration,
18+
count: Option<u64>,
19+
wait: Duration,
20+
amount: u64,
21+
) -> Self {
1422
DefinedPaymentActivity {
1523
destination,
24+
start,
25+
count,
1626
wait,
1727
amount,
1828
}
@@ -36,6 +46,14 @@ impl DestinationGenerator for DefinedPaymentActivity {
3646
}
3747

3848
impl PaymentGenerator for DefinedPaymentActivity {
49+
fn payment_start(&self) -> Duration {
50+
self.start
51+
}
52+
53+
fn payment_count(&self) -> Option<u64> {
54+
self.count
55+
}
56+
3957
fn next_payment_wait(&self) -> Duration {
4058
self.wait
4159
}
@@ -69,8 +87,13 @@ mod tests {
6987
let source = get_random_keypair();
7088
let payment_amt = 50;
7189

72-
let generator =
73-
DefinedPaymentActivity::new(node.clone(), Duration::from_secs(60), payment_amt);
90+
let generator = DefinedPaymentActivity::new(
91+
node.clone(),
92+
Duration::from_secs(0),
93+
None,
94+
Duration::from_secs(60),
95+
payment_amt,
96+
);
7497

7598
let (dest, dest_capacity) = generator.choose_destination(source.1);
7699
assert_eq!(node.pubkey, dest.pubkey);

sim-lib/src/lib.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ pub struct ActivityParser {
139139
/// The destination of the payment.
140140
#[serde(with = "serializers::serde_node_id")]
141141
pub destination: NodeId,
142+
/// The time in the simulation to start the payment
143+
#[serde(default)]
144+
pub start_secs: u16,
145+
/// The number of payments to send over the course of the simulation
146+
#[serde(default)]
147+
pub count: Option<u64>,
142148
/// The interval of the event, as in every how many seconds the payment is performed.
143149
pub interval_secs: u16,
144150
/// The amount of m_sat to used in this payment.
@@ -153,6 +159,10 @@ pub struct ActivityDefinition {
153159
pub source: NodeInfo,
154160
/// The destination of the payment.
155161
pub destination: NodeInfo,
162+
/// The time in the simulation to start the payment
163+
pub start_secs: u16,
164+
/// The number of payments to send over the course of the simulation
165+
pub count: Option<u64>,
156166
/// The interval of the event, as in every how many seconds the payment is performed.
157167
pub interval_secs: u16,
158168
/// The amount of m_sat to used in this payment.
@@ -261,6 +271,12 @@ pub trait DestinationGenerator: Send {
261271
pub struct PaymentGenerationError(String);
262272

263273
pub trait PaymentGenerator: Display + Send {
274+
/// Returns the time that the payments should start
275+
fn payment_start(&self) -> Duration;
276+
277+
/// Returns the number of payments that should be made
278+
fn payment_count(&self) -> Option<u64>;
279+
264280
/// Returns the number of seconds that a node should wait until firing its next payment.
265281
fn next_payment_wait(&self) -> time::Duration;
266282

@@ -667,6 +683,8 @@ impl Simulation {
667683
for description in self.activity.iter() {
668684
let activity_generator = DefinedPaymentActivity::new(
669685
description.destination.clone(),
686+
Duration::from_secs(description.start_secs.into()),
687+
description.count,
670688
Duration::from_secs(description.interval_secs.into()),
671689
description.amount_msat,
672690
);
@@ -918,8 +936,21 @@ async fn produce_events<N: DestinationGenerator + ?Sized, A: PaymentGenerator +
918936
sender: Sender<SimulationEvent>,
919937
listener: Listener,
920938
) -> Result<(), SimulationError> {
939+
let mut current_count = 0;
921940
loop {
922-
let wait = node_generator.next_payment_wait();
941+
if let Some(c) = node_generator.payment_count() {
942+
if c == current_count {
943+
log::info!("Payment count has been met: {c} payments. Stopping the activity.");
944+
return Ok(());
945+
}
946+
}
947+
948+
let wait: Duration = if current_count == 0 {
949+
node_generator.payment_start()
950+
} else {
951+
node_generator.next_payment_wait()
952+
};
953+
923954
log::debug!("Next payment for {source} in {:?} seconds.", wait);
924955

925956
select! {
@@ -956,6 +987,7 @@ async fn produce_events<N: DestinationGenerator + ?Sized, A: PaymentGenerator +
956987
return Err(SimulationError::MpscChannelError (format!("Stopped random producer for {amount}: {source} -> {destination}.")));
957988
}
958989

990+
current_count += 1;
959991
},
960992
}
961993
}

sim-lib/src/random_activity.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ fn events_per_month(source_capacity_msat: u64, multiplier: f64, expected_payment
194194
}
195195

196196
impl PaymentGenerator for RandomPaymentActivity {
197+
/// Returns the time that the payments should start
198+
fn payment_start(&self) -> Duration {
199+
Duration::from_secs(0)
200+
}
201+
202+
/// Returns the number of payments that should be made
203+
fn payment_count(&self) -> Option<u64> {
204+
None
205+
}
206+
197207
/// Returns the amount of time until the next payment should be scheduled for the node.
198208
fn next_payment_wait(&self) -> Duration {
199209
let mut rng = rand::thread_rng();

0 commit comments

Comments
 (0)