1
+ #![ deny( rustdoc:: broken_intra_doc_links) ]
2
+ #![ deny( missing_docs) ]
3
+
1
4
use async_trait:: async_trait;
2
5
use bitcoin:: secp256k1:: PublicKey ;
3
6
use bitcoin:: Network ;
@@ -39,14 +42,15 @@ mod test_utils;
39
42
/// Represents a node id, either by its public key or alias.
40
43
#[ derive( Serialize , Debug , Clone ) ]
41
44
pub enum NodeId {
42
- /// The node's public key
45
+ /// The node's public key.
43
46
PublicKey ( PublicKey ) ,
44
- /// The node's alias (human-readable name)
47
+ /// The node's alias (human-readable name).
45
48
Alias ( String ) ,
46
49
}
47
50
48
51
impl NodeId {
49
- /// Validates that the provided node id matches the one returned by the backend.
52
+ /// Validates that the provided node id matches the one returned by the backend. If the node id is an alias,
53
+ /// it will be updated to the one returned by the backend if there is a mismatch.
50
54
pub fn validate ( & self , node_id : & PublicKey , alias : & mut String ) -> Result < ( ) , LightningError > {
51
55
match self {
52
56
crate :: NodeId :: PublicKey ( pk) => {
@@ -98,21 +102,21 @@ impl std::fmt::Display for NodeId {
98
102
#[ derive( Debug , Clone , PartialEq , Eq , Hash , Copy ) ]
99
103
pub struct ShortChannelID ( u64 ) ;
100
104
101
- /// Utility function to easily convert from u64 to `ShortChannelID`
105
+ /// Utility function to easily convert from u64 to `ShortChannelID`.
102
106
impl From < u64 > for ShortChannelID {
103
107
fn from ( value : u64 ) -> Self {
104
108
ShortChannelID ( value)
105
109
}
106
110
}
107
111
108
- /// Utility function to easily convert `ShortChannelID` into u64
112
+ /// Utility function to easily convert `ShortChannelID` into u64.
109
113
impl From < ShortChannelID > for u64 {
110
114
fn from ( scid : ShortChannelID ) -> Self {
111
115
scid. 0
112
116
}
113
117
}
114
118
115
- /// See <https://github.com/lightning/bolts/blob/60de4a09727c20dea330f9ee8313034de6e50594/07-routing-gossip.md#definition-of-short_channel_id>.
119
+ /// See <https://github.com/lightning/bolts/blob/60de4a09727c20dea330f9ee8313034de6e50594/07-routing-gossip.md#definition-of-short_channel_id>
116
120
impl std:: fmt:: Display for ShortChannelID {
117
121
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
118
122
write ! (
@@ -129,9 +133,9 @@ impl std::fmt::Display for ShortChannelID {
129
133
#[ derive( Debug , Clone , Copy , Serialize , Deserialize ) ]
130
134
#[ serde( untagged) ]
131
135
pub enum ValueOrRange < T > {
132
- /// A single fixed value
136
+ /// A single fixed value.
133
137
Value ( T ) ,
134
- /// A range [min, max) from which values are randomly sampled
138
+ /// A range [min, max) from which values are randomly sampled.
135
139
Range ( T , T ) ,
136
140
}
137
141
@@ -189,85 +193,85 @@ pub struct ActivityDefinition {
189
193
/// Represents errors that can occur during simulation execution.
190
194
#[ derive( Debug , Error ) ]
191
195
pub enum SimulationError {
192
- /// Error that occurred during Lightning Network operations
196
+ /// Error that occurred during Lightning Network operations.
193
197
#[ error( "Lightning Error: {0:?}" ) ]
194
198
LightningError ( #[ from] LightningError ) ,
195
- /// Error that occurred during task execution
199
+ /// Error that occurred during task execution.
196
200
#[ error( "TaskError" ) ]
197
201
TaskError ,
198
- /// Error that occurred while writing CSV data
202
+ /// Error that occurred while writing CSV data.
199
203
#[ error( "CSV Error: {0:?}" ) ]
200
204
CsvError ( #[ from] csv:: Error ) ,
201
- /// Error that occurred during file operations
205
+ /// Error that occurred during file operations.
202
206
#[ error( "File Error" ) ]
203
207
FileError ,
204
- /// Error that occurred during random activity generation
208
+ /// Error that occurred during random activity generation.
205
209
#[ error( "{0}" ) ]
206
210
RandomActivityError ( RandomActivityError ) ,
207
- /// Error that occurred in the simulated network
211
+ /// Error that occurred in the simulated network.
208
212
#[ error( "Simulated Network Error: {0}" ) ]
209
213
SimulatedNetworkError ( String ) ,
210
- /// Error that occurred while accessing system time
214
+ /// Error that occurred while accessing system time.
211
215
#[ error( "System Time Error: {0}" ) ]
212
216
SystemTimeError ( #[ from] SystemTimeError ) ,
213
- /// Error that occurred when a required node was not found
217
+ /// Error that occurred when a required node was not found.
214
218
#[ error( "Missing Node Error: {0}" ) ]
215
219
MissingNodeError ( String ) ,
216
- /// Error that occurred in message passing channels
220
+ /// Error that occurred in message passing channels.
217
221
#[ error( "Mpsc Channel Error: {0}" ) ]
218
222
MpscChannelError ( String ) ,
219
- /// Error that occurred while generating payment parameters
223
+ /// Error that occurred while generating payment parameters.
220
224
#[ error( "Payment Generation Error: {0}" ) ]
221
225
PaymentGenerationError ( PaymentGenerationError ) ,
222
- /// Error that occurred while generating destination nodes
226
+ /// Error that occurred while generating destination nodes.
223
227
#[ error( "Destination Generation Error: {0}" ) ]
224
228
DestinationGenerationError ( DestinationGenerationError ) ,
225
229
}
226
230
227
231
/// Represents errors that can occur during Lightning Network operations.
228
232
#[ derive( Debug , Error ) ]
229
233
pub enum LightningError {
230
- /// Error that occurred while connecting to a Lightning node
234
+ /// Error that occurred while connecting to a Lightning node.
231
235
#[ error( "Node connection error: {0}" ) ]
232
236
ConnectionError ( String ) ,
233
- /// Error that occurred while retrieving node information
237
+ /// Error that occurred while retrieving node information.
234
238
#[ error( "Get info error: {0}" ) ]
235
239
GetInfoError ( String ) ,
236
- /// Error that occurred while sending a payment
240
+ /// Error that occurred while sending a payment.
237
241
#[ error( "Send payment error: {0}" ) ]
238
242
SendPaymentError ( String ) ,
239
- /// Error that occurred while tracking a payment
243
+ /// Error that occurred while tracking a payment.
240
244
#[ error( "Track payment error: {0}" ) ]
241
245
TrackPaymentError ( String ) ,
242
- /// Error that occurred when a payment hash is invalid
246
+ /// Error that occurred when a payment hash is invalid.
243
247
#[ error( "Invalid payment hash" ) ]
244
248
InvalidPaymentHash ,
245
- /// Error that occurred while retrieving information about a specific node
249
+ /// Error that occurred while retrieving information about a specific node.
246
250
#[ error( "Get node info error: {0}" ) ]
247
251
GetNodeInfoError ( String ) ,
248
- /// Error that occurred during configuration validation
252
+ /// Error that occurred during configuration validation.
249
253
#[ error( "Config validation failed: {0}" ) ]
250
254
ValidationError ( String ) ,
251
- /// Error that represents a permanent failure condition
255
+ /// Error that represents a permanent failure condition.
252
256
#[ error( "Permanent error: {0:?}" ) ]
253
257
PermanentError ( String ) ,
254
- /// Error that occurred while listing channels
258
+ /// Error that occurred while listing channels.
255
259
#[ error( "List channels error: {0}" ) ]
256
260
ListChannelsError ( String ) ,
257
261
}
258
262
259
263
/// Information about a Lightning Network node.
260
- /// - Alias: A human-readable name for the node
264
+ /// - Alias: A human-readable name for the node.
261
265
/// - Features: The node's supported protocol features and capabilities, used to determine compatibility
262
266
/// and available functionality. In CLN, features are stored as big-endian bytes, while in LND they are
263
267
/// stored as a set of feature flags converted to little-endian bytes.
264
268
#[ derive( Debug , Clone ) ]
265
269
pub struct NodeInfo {
266
- /// The node's public key
270
+ /// The node's public key.
267
271
pub pubkey : PublicKey ,
268
- /// A human-readable name for the node (may be empty)
272
+ /// A human-readable name for the node (may be empty).
269
273
pub alias : String ,
270
- /// The node's supported protocol features and capabilities
274
+ /// The node's supported protocol features and capabilities.
271
275
pub features : NodeFeatures ,
272
276
}
273
277
@@ -288,7 +292,7 @@ impl Display for NodeInfo {
288
292
pub trait LightningNode : Send {
289
293
/// Get information about the node.
290
294
fn get_info ( & self ) -> & NodeInfo ;
291
- /// Get the network this node is running at
295
+ /// Get the network this node is running at.
292
296
async fn get_network ( & mut self ) -> Result < Network , LightningError > ;
293
297
/// Keysend payment worth `amount_msat` from a source node to the destination node.
294
298
async fn send_payment (
@@ -302,7 +306,7 @@ pub trait LightningNode: Send {
302
306
hash : & PaymentHash ,
303
307
shutdown : Listener ,
304
308
) -> Result < PaymentResult , LightningError > ;
305
- /// Gets information on a specific node
309
+ /// Gets information on a specific node.
306
310
async fn get_node_info ( & mut self , node_id : & PublicKey ) -> Result < NodeInfo , LightningError > ;
307
311
/// Lists all channels, at present only returns a vector of channel capacities in msat because no further
308
312
/// information is required.
@@ -315,14 +319,6 @@ pub trait LightningNode: Send {
315
319
pub struct DestinationGenerationError ( String ) ;
316
320
317
321
/// A trait for selecting destination nodes for payments in the Lightning Network.
318
- ///
319
- /// This trait is responsible for choosing appropriate destination nodes from the existing network
320
- /// for payments to be sent to. The selection can be based on different strategies:
321
- /// - Predefined destinations (for deterministic payment patterns)
322
- /// - Random selection weighted by node capacity
323
- ///
324
- /// The trait works with existing nodes in the network and is used in conjunction with
325
- /// the `PaymentGenerator` trait to create complete payment flows.
326
322
pub trait DestinationGenerator : Send {
327
323
/// choose_destination picks a destination node within the network, returning the node's information and its
328
324
/// capacity (if available).
@@ -338,22 +334,13 @@ pub trait DestinationGenerator: Send {
338
334
pub struct PaymentGenerationError ( String ) ;
339
335
340
336
/// A trait for generating payment parameters in the Lightning Network.
341
- ///
342
- /// This trait is responsible for determining when and how payments should be made,
343
- /// including timing, amounts, and frequency. It can be used to implement various
344
- /// payment strategies, from simple periodic payments to more complex patterns
345
- /// with random timing and amounts.
346
- ///
347
337
pub trait PaymentGenerator : Display + Send {
348
- /// Returns the time that the payments should start
338
+ /// Returns the time that the payments should start.
349
339
fn payment_start ( & self ) -> Option < Duration > ;
350
340
351
341
/// Returns the number of payments that should be made.
352
- ///
353
- /// # Returns
354
- ///
355
- /// * `Some(u64)` - The exact number of payments to make
356
- /// * `None` - Payments should continue indefinitely
342
+ /// Returns `Some(n)` if there's a limit on the number of payments to dispatch,
343
+ /// or `None` otherwise.
357
344
fn payment_count ( & self ) -> Option < u64 > ;
358
345
359
346
/// Returns the number of seconds that a node should wait until firing its next payment.
@@ -381,12 +368,7 @@ pub struct PaymentResult {
381
368
impl PaymentResult {
382
369
/// Creates a new PaymentResult indicating that the payment was never dispatched.
383
370
/// This is used when there was an error during the initial payment dispatch attempt.
384
- ///(e.g., insufficient balance, invalid destination)
385
- ///
386
- /// # Returns
387
- /// A PaymentResult with:
388
- /// - htlc_count: 0 (no HTLCs used since payment wasn't dispatched)
389
- /// - payment_outcome: NotDispatched
371
+ /// (e.g., insufficient balance, invalid destination) with [`PaymentOutcome::NotDispatched`].
390
372
pub fn not_dispatched ( ) -> Self {
391
373
PaymentResult {
392
374
htlc_count : 0 ,
@@ -396,12 +378,7 @@ impl PaymentResult {
396
378
397
379
/// Creates a new PaymentResult indicating that tracking the payment failed.
398
380
/// This is used when the payment was dispatched but the system was unable to
399
- /// determine its final outcome (e.g., due to connection issues or timeouts).
400
- ///
401
- /// # Returns
402
- /// A PaymentResult with:
403
- /// - htlc_count: 0 (unknown since tracking failed)
404
- /// - payment_outcome: TrackPaymentFailed
381
+ /// determine its final outcome (e.g., due to connection issues or timeouts) with [`PaymentOutcome::TrackPaymentFailed`].
405
382
pub fn track_payment_failed ( ) -> Self {
406
383
PaymentResult {
407
384
htlc_count : 0 ,
@@ -423,29 +400,29 @@ impl Display for PaymentResult {
423
400
/// Represents all possible outcomes of a Lightning Network payment attempt.
424
401
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
425
402
pub enum PaymentOutcome {
426
- /// Payment completed successfully, reaching its intended recipient
403
+ /// Payment completed successfully, reaching its intended recipient.
427
404
Success ,
428
- /// The recipient rejected the payment
405
+ /// The recipient rejected the payment.
429
406
RecipientRejected ,
430
- /// The payment was cancelled by the sending user before completion
407
+ /// The payment was cancelled by the sending user before completion.
431
408
UserAbandoned ,
432
- /// The payment failed after exhausting all retry attempts
409
+ /// The payment failed after exhausting all retry attempts.
433
410
RetriesExhausted ,
434
- /// The payment expired before it could complete (e.g., HTLC timeout)
411
+ /// The payment expired before it could complete (e.g., HTLC timeout).
435
412
PaymentExpired ,
436
- /// No viable route could be found to the destination node
413
+ /// No viable route could be found to the destination node.
437
414
RouteNotFound ,
438
- /// An unexpected error occurred during payment processing
415
+ /// An unexpected error occurred during payment processing.
439
416
UnexpectedError ,
440
- /// The payment failed due to incorrect payment details (e.g., wrong invoice amount)
417
+ /// The payment failed due to incorrect payment details (e.g., wrong invoice amount).
441
418
IncorrectPaymentDetails ,
442
- /// The sending node has insufficient balance to complete the payment
419
+ /// The sending node has insufficient balance to complete/dispatch the payment.
443
420
InsufficientBalance ,
444
- /// The payment failed for an unknown reason
421
+ /// The payment failed for an unknown reason.
445
422
Unknown ,
446
- /// The payment was never dispatched due to an error during initial sending
423
+ /// The payment was never dispatched due to an error during initial sending.
447
424
NotDispatched ,
448
- /// The payment was dispatched but its final status could not be determined
425
+ /// The payment was dispatched but its final status could not be determined.
449
426
TrackPaymentFailed ,
450
427
}
451
428
@@ -570,7 +547,6 @@ impl SimulationCfg {
570
547
/// A Lightning Network payment simulator that manages payment flows between nodes.
571
548
/// The simulator can execute both predefined payment patterns and generate random payment activity
572
549
/// based on configuration parameters.
573
- ///
574
550
#[ derive( Clone ) ]
575
551
pub struct Simulation {
576
552
/// Config for the simulation itself.
0 commit comments