1
+ #![ deny( rustdoc:: broken_intra_doc_links) ]
2
+
1
3
use async_trait:: async_trait;
2
4
use bitcoin:: secp256k1:: PublicKey ;
3
5
use bitcoin:: Network ;
@@ -35,13 +37,18 @@ pub mod serializers;
35
37
pub mod sim_node;
36
38
mod test_utils;
37
39
40
+ /// Represents a node id, either by its public key or alias.
38
41
#[ derive( Serialize , Debug , Clone ) ]
39
42
pub enum NodeId {
43
+ /// The node's public key.
40
44
PublicKey ( PublicKey ) ,
45
+ /// The node's alias (human-readable name).
41
46
Alias ( String ) ,
42
47
}
43
48
44
49
impl NodeId {
50
+ /// Validates that the provided node id matches the one returned by the backend. If the node id is an alias,
51
+ /// it will be updated to the one returned by the backend if there is a mismatch.
45
52
pub fn validate ( & self , node_id : & PublicKey , alias : & mut String ) -> Result < ( ) , LightningError > {
46
53
match self {
47
54
crate :: NodeId :: PublicKey ( pk) => {
@@ -66,6 +73,7 @@ impl NodeId {
66
73
Ok ( ( ) )
67
74
}
68
75
76
+ /// Returns the public key of the node if it is a public key node id.
69
77
pub fn get_pk ( & self ) -> Result < & PublicKey , String > {
70
78
if let NodeId :: PublicKey ( pk) = self {
71
79
Ok ( pk)
@@ -92,21 +100,21 @@ impl std::fmt::Display for NodeId {
92
100
#[ derive( Debug , Clone , PartialEq , Eq , Hash , Copy ) ]
93
101
pub struct ShortChannelID ( u64 ) ;
94
102
95
- /// Utility function to easily convert from u64 to `ShortChannelID`
103
+ /// Utility function to easily convert from u64 to `ShortChannelID`.
96
104
impl From < u64 > for ShortChannelID {
97
105
fn from ( value : u64 ) -> Self {
98
106
ShortChannelID ( value)
99
107
}
100
108
}
101
109
102
- /// Utility function to easily convert `ShortChannelID` into u64
110
+ /// Utility function to easily convert `ShortChannelID` into u64.
103
111
impl From < ShortChannelID > for u64 {
104
112
fn from ( scid : ShortChannelID ) -> Self {
105
113
scid. 0
106
114
}
107
115
}
108
116
109
- /// See https://github.com/lightning/bolts/blob/60de4a09727c20dea330f9ee8313034de6e50594/07-routing-gossip.md#definition-of-short_channel_id.
117
+ /// See < https://github.com/lightning/bolts/blob/60de4a09727c20dea330f9ee8313034de6e50594/07-routing-gossip.md#definition-of-short_channel_id>
110
118
impl std:: fmt:: Display for ShortChannelID {
111
119
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
112
120
write ! (
@@ -123,7 +131,9 @@ impl std::fmt::Display for ShortChannelID {
123
131
#[ derive( Debug , Clone , Copy , Serialize , Deserialize ) ]
124
132
#[ serde( untagged) ]
125
133
pub enum ValueOrRange < T > {
134
+ /// A single fixed value.
126
135
Value ( T ) ,
136
+ /// A range [min, max) from which values are randomly sampled.
127
137
Range ( T , T ) ,
128
138
}
129
139
@@ -178,58 +188,87 @@ pub struct ActivityDefinition {
178
188
pub amount_msat : Amount ,
179
189
}
180
190
191
+ /// Represents errors that can occur during simulation execution.
181
192
#[ derive( Debug , Error ) ]
182
193
pub enum SimulationError {
194
+ /// Error that occurred during Lightning Network operations.
183
195
#[ error( "Lightning Error: {0:?}" ) ]
184
196
LightningError ( #[ from] LightningError ) ,
197
+ /// Error that occurred during task execution.
185
198
#[ error( "TaskError" ) ]
186
199
TaskError ,
200
+ /// Error that occurred while writing CSV data.
187
201
#[ error( "CSV Error: {0:?}" ) ]
188
202
CsvError ( #[ from] csv:: Error ) ,
203
+ /// Error that occurred during file operations.
189
204
#[ error( "File Error" ) ]
190
205
FileError ,
206
+ /// Error that occurred during random activity generation.
191
207
#[ error( "{0}" ) ]
192
208
RandomActivityError ( RandomActivityError ) ,
209
+ /// Error that occurred in the simulated network.
193
210
#[ error( "Simulated Network Error: {0}" ) ]
194
211
SimulatedNetworkError ( String ) ,
212
+ /// Error that occurred while accessing system time.
195
213
#[ error( "System Time Error: {0}" ) ]
196
214
SystemTimeError ( #[ from] SystemTimeError ) ,
215
+ /// Error that occurred when a required node was not found.
197
216
#[ error( "Missing Node Error: {0}" ) ]
198
217
MissingNodeError ( String ) ,
218
+ /// Error that occurred in message passing channels.
199
219
#[ error( "Mpsc Channel Error: {0}" ) ]
200
220
MpscChannelError ( String ) ,
221
+ /// Error that occurred while generating payment parameters.
201
222
#[ error( "Payment Generation Error: {0}" ) ]
202
223
PaymentGenerationError ( PaymentGenerationError ) ,
224
+ /// Error that occurred while generating destination nodes.
203
225
#[ error( "Destination Generation Error: {0}" ) ]
204
226
DestinationGenerationError ( DestinationGenerationError ) ,
205
227
}
206
228
229
+ /// Represents errors that can occur during Lightning Network operations.
207
230
#[ derive( Debug , Error ) ]
208
231
pub enum LightningError {
232
+ /// Error that occurred while connecting to a Lightning node.
209
233
#[ error( "Node connection error: {0}" ) ]
210
234
ConnectionError ( String ) ,
235
+ /// Error that occurred while retrieving node information.
211
236
#[ error( "Get info error: {0}" ) ]
212
237
GetInfoError ( String ) ,
238
+ /// Error that occurred while sending a payment.
213
239
#[ error( "Send payment error: {0}" ) ]
214
240
SendPaymentError ( String ) ,
241
+ /// Error that occurred while tracking a payment.
215
242
#[ error( "Track payment error: {0}" ) ]
216
243
TrackPaymentError ( String ) ,
244
+ /// Error that occurred when a payment hash is invalid.
217
245
#[ error( "Invalid payment hash" ) ]
218
246
InvalidPaymentHash ,
247
+ /// Error that occurred while retrieving information about a specific node.
219
248
#[ error( "Get node info error: {0}" ) ]
220
249
GetNodeInfoError ( String ) ,
250
+ /// Error that occurred during configuration validation.
221
251
#[ error( "Config validation failed: {0}" ) ]
222
252
ValidationError ( String ) ,
253
+ /// Error that represents a permanent failure condition.
223
254
#[ error( "Permanent error: {0:?}" ) ]
224
255
PermanentError ( String ) ,
256
+ /// Error that occurred while listing channels.
225
257
#[ error( "List channels error: {0}" ) ]
226
258
ListChannelsError ( String ) ,
227
259
}
228
260
261
+ /// Information about a Lightning Network node.
262
+ /// - Alias: A human-readable name for the node.
263
+ /// - Features: The node's supported protocol features and capabilities,
264
+ /// used to determine compatibility and available
229
265
#[ derive( Debug , Clone ) ]
230
266
pub struct NodeInfo {
267
+ /// The node's public key.
231
268
pub pubkey : PublicKey ,
269
+ /// A human-readable name for the node (may be empty).
232
270
pub alias : String ,
271
+ /// The node's supported protocol features and capabilities.
233
272
pub features : NodeFeatures ,
234
273
}
235
274
@@ -250,7 +289,7 @@ impl Display for NodeInfo {
250
289
pub trait LightningNode : Send {
251
290
/// Get information about the node.
252
291
fn get_info ( & self ) -> & NodeInfo ;
253
- /// Get the network this node is running at
292
+ /// Get the network this node is running at.
254
293
async fn get_network ( & mut self ) -> Result < Network , LightningError > ;
255
294
/// Keysend payment worth `amount_msat` from a source node to the destination node.
256
295
async fn send_payment (
@@ -264,17 +303,19 @@ pub trait LightningNode: Send {
264
303
hash : & PaymentHash ,
265
304
shutdown : Listener ,
266
305
) -> Result < PaymentResult , LightningError > ;
267
- /// Gets information on a specific node
306
+ /// Gets information on a specific node.
268
307
async fn get_node_info ( & mut self , node_id : & PublicKey ) -> Result < NodeInfo , LightningError > ;
269
308
/// Lists all channels, at present only returns a vector of channel capacities in msat because no further
270
309
/// information is required.
271
310
async fn list_channels ( & mut self ) -> Result < Vec < u64 > , LightningError > ;
272
311
}
273
312
313
+ /// Represents an error that occurs when generating a destination for a payment.
274
314
#[ derive( Debug , Error ) ]
275
315
#[ error( "Destination generation error: {0}" ) ]
276
316
pub struct DestinationGenerationError ( String ) ;
277
317
318
+ /// A trait for selecting destination nodes for payments in the Lightning Network.
278
319
pub trait DestinationGenerator : Send {
279
320
/// choose_destination picks a destination node within the network, returning the node's information and its
280
321
/// capacity (if available).
@@ -284,15 +325,18 @@ pub trait DestinationGenerator: Send {
284
325
) -> Result < ( NodeInfo , Option < u64 > ) , DestinationGenerationError > ;
285
326
}
286
327
328
+ /// Represents an error that occurs when generating payments.
287
329
#[ derive( Debug , Error ) ]
288
330
#[ error( "Payment generation error: {0}" ) ]
289
331
pub struct PaymentGenerationError ( String ) ;
290
332
333
+ /// A trait for generating payment parameters in the Lightning Network.
291
334
pub trait PaymentGenerator : Display + Send {
292
- /// Returns the time that the payments should start
335
+ /// Returns the time that the payments should start.
293
336
fn payment_start ( & self ) -> Option < Duration > ;
294
337
295
- /// Returns the number of payments that should be made
338
+ /// Returns the number of payments that should be made.
339
+ /// Returns `Some(n)` if there's a limit on the number of payments to dispatch, or `None` otherwise.
296
340
fn payment_count ( & self ) -> Option < u64 > ;
297
341
298
342
/// Returns the number of seconds that a node should wait until firing its next payment.
@@ -305,20 +349,32 @@ pub trait PaymentGenerator: Display + Send {
305
349
) -> Result < u64 , PaymentGenerationError > ;
306
350
}
307
351
352
+ /// Represents the result of a payment attempt.
308
353
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
309
354
pub struct PaymentResult {
355
+ /// The number of HTLCs (Hash Time Locked Contracts) used in the payment attempt.
356
+ /// Multiple HTLCs may be used for a single payment when using techniques like multi-part payments or when
357
+ /// retrying failed payment paths.
310
358
pub htlc_count : usize ,
359
+ /// The final outcome of the payment attempt, indicating whether it succeeded or failed
360
+ /// (and if failed, the reason for failure).
311
361
pub payment_outcome : PaymentOutcome ,
312
362
}
313
363
314
364
impl PaymentResult {
365
+ /// Creates a new PaymentResult indicating that the payment was never dispatched. This is used when there was an
366
+ /// error during the initial payment dispatch attempt (e.g., insufficient balance, invalid destination) with
367
+ /// [`PaymentOutcome::NotDispatched`].
315
368
pub fn not_dispatched ( ) -> Self {
316
369
PaymentResult {
317
370
htlc_count : 0 ,
318
371
payment_outcome : PaymentOutcome :: NotDispatched ,
319
372
}
320
373
}
321
374
375
+ /// Creates a new PaymentResult indicating that tracking the payment failed. This is used when the payment was
376
+ /// dispatched but the system was unable to determine its final outcome (e.g. due to connection issues or timeouts)
377
+ /// with [`PaymentOutcome::TrackPaymentFailed`].
322
378
pub fn track_payment_failed ( ) -> Self {
323
379
PaymentResult {
324
380
htlc_count : 0 ,
@@ -337,19 +393,32 @@ impl Display for PaymentResult {
337
393
}
338
394
}
339
395
396
+ /// Represents all possible outcomes of a Lightning Network payment attempt.
340
397
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
341
398
pub enum PaymentOutcome {
399
+ /// Payment completed successfully, reaching its intended recipient.
342
400
Success ,
401
+ /// The recipient rejected the payment.
343
402
RecipientRejected ,
403
+ /// The payment was cancelled by the sending user before completion.
344
404
UserAbandoned ,
405
+ /// The payment failed after exhausting all retry attempts.
345
406
RetriesExhausted ,
407
+ /// The payment expired before it could complete (e.g., HTLC timeout).
346
408
PaymentExpired ,
409
+ /// No viable route could be found to the destination node.
347
410
RouteNotFound ,
411
+ /// An unexpected error occurred during payment processing.
348
412
UnexpectedError ,
413
+ /// The payment failed due to incorrect payment details (e.g., wrong invoice amount).
349
414
IncorrectPaymentDetails ,
415
+ /// The sending node has insufficient balance to complete/dispatch the payment.
350
416
InsufficientBalance ,
417
+ /// The payment failed for an unknown reason.
351
418
Unknown ,
419
+ /// The payment was never dispatched due to an error during initial sending.
352
420
NotDispatched ,
421
+ /// The payment was dispatched but its final status could not be determined.
353
422
TrackPaymentFailed ,
354
423
}
355
424
@@ -471,6 +540,9 @@ impl SimulationCfg {
471
540
}
472
541
}
473
542
543
+ /// A Lightning Network payment simulator that manages payment flows between nodes.
544
+ /// The simulator can execute both predefined payment patterns and generate random payment activity
545
+ /// based on configuration parameters.
474
546
#[ derive( Clone ) ]
475
547
pub struct Simulation {
476
548
/// Config for the simulation itself.
@@ -489,6 +561,7 @@ pub struct Simulation {
489
561
shutdown_listener : Listener ,
490
562
}
491
563
564
+ /// Configuration for writing simulation results to CSV files.
492
565
#[ derive( Clone ) ]
493
566
pub struct WriteResults {
494
567
/// Data directory where CSV result files are written.
0 commit comments