Skip to content

Commit 0536339

Browse files
committed
sim-lib: add request struct to propagate_payment
1 parent 64dd234 commit 0536339

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

simln-lib/src/sim_node.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,15 +1091,15 @@ impl SimNetwork for SimGraph {
10911091
},
10921092
};
10931093

1094-
self.tasks.spawn(propagate_payment(
1095-
self.channels.clone(),
1094+
self.tasks.spawn(propagate_payment(PropagatePaymentRequest {
1095+
nodes: Arc::clone(&self.channels),
10961096
source,
1097-
path.clone(),
1097+
route: path.clone(),
10981098
payment_hash,
10991099
sender,
1100-
self.interceptors.clone(),
1101-
self.shutdown_signal.clone(),
1102-
));
1100+
interceptors: self.interceptors.clone(),
1101+
shutdown_signal: self.shutdown_signal.clone(),
1102+
}));
11031103
}
11041104

11051105
/// lookup_node fetches a node's information and channel capacities.
@@ -1338,43 +1338,45 @@ async fn remove_htlcs(
13381338
Ok(())
13391339
}
13401340

1341-
/// Finds a payment path from the source to destination nodes provided, and propagates the appropriate htlcs through
1342-
/// the simulated network, notifying the sender channel provided of the payment outcome. If a critical error occurs,
1343-
/// ie a breakdown of our state machine, it will still notify the payment outcome and will use the shutdown trigger
1344-
/// to signal that we should exit.
1345-
async fn propagate_payment(
1341+
struct PropagatePaymentRequest {
13461342
nodes: Arc<Mutex<HashMap<ShortChannelID, SimulatedChannel>>>,
13471343
source: PublicKey,
13481344
route: Path,
13491345
payment_hash: PaymentHash,
13501346
sender: Sender<Result<PaymentResult, LightningError>>,
13511347
interceptors: Vec<Arc<dyn Interceptor>>,
13521348
shutdown_signal: (Trigger, Listener),
1353-
) {
1349+
}
1350+
1351+
/// Finds a payment path from the source to destination nodes provided, and propagates the appropriate htlcs through
1352+
/// the simulated network, notifying the sender channel provided of the payment outcome. If a critical error occurs,
1353+
/// ie a breakdown of our state machine, it will still notify the payment outcome and will use the shutdown trigger
1354+
/// to signal that we should exit.
1355+
async fn propagate_payment(request: PropagatePaymentRequest) {
13541356
let notify_result = match add_htlcs(
1355-
nodes.clone(),
1356-
source,
1357-
route.clone(),
1358-
payment_hash,
1359-
interceptors.clone(),
1360-
shutdown_signal.1,
1357+
request.nodes.clone(),
1358+
request.source,
1359+
request.route.clone(),
1360+
request.payment_hash,
1361+
request.interceptors.clone(),
1362+
request.shutdown_signal.1,
13611363
)
13621364
.await
13631365
{
13641366
Ok(Ok(_)) => {
13651367
// If we successfully added the htlc, go ahead and remove all the htlcs in the route with successful resolution.
13661368
if let Err(e) = remove_htlcs(
1367-
nodes,
1368-
route.hops.len() - 1,
1369-
source,
1370-
route,
1371-
payment_hash,
1369+
request.nodes,
1370+
request.route.hops.len() - 1,
1371+
request.source,
1372+
request.route,
1373+
request.payment_hash,
13721374
true,
1373-
interceptors,
1375+
request.interceptors,
13741376
)
13751377
.await
13761378
{
1377-
shutdown_signal.0.trigger();
1379+
request.shutdown_signal.0.trigger();
13781380
log::error!("Could not remove htlcs from channel: {e}.");
13791381
}
13801382
PaymentResult {
@@ -1387,35 +1389,35 @@ async fn propagate_payment(
13871389
// state. It's possible that we failed with the very first add, and then we don't need to clean anything up.
13881390
if let Some(resolution_idx) = fail_idx {
13891391
if remove_htlcs(
1390-
nodes,
1392+
request.nodes,
13911393
resolution_idx,
1392-
source,
1393-
route,
1394-
payment_hash,
1394+
request.source,
1395+
request.route,
1396+
request.payment_hash,
13951397
false,
1396-
interceptors,
1398+
request.interceptors,
13971399
)
13981400
.await
13991401
.is_err()
14001402
{
1401-
shutdown_signal.0.trigger();
1403+
request.shutdown_signal.0.trigger();
14021404
}
14031405
}
14041406

14051407
log::debug!(
14061408
"Forwarding failure for simulated payment {}: {fwd_err}",
1407-
hex::encode(payment_hash.0)
1409+
hex::encode(request.payment_hash.0)
14081410
);
14091411
PaymentResult {
14101412
htlc_count: 0,
14111413
payment_outcome: PaymentOutcome::Unknown,
14121414
}
14131415
},
14141416
Err(critical_err) => {
1415-
shutdown_signal.0.trigger();
1417+
request.shutdown_signal.0.trigger();
14161418
log::debug!(
14171419
"Critical error in simulated payment {}: {critical_err}",
1418-
hex::encode(payment_hash.0)
1420+
hex::encode(request.payment_hash.0)
14191421
);
14201422
PaymentResult {
14211423
htlc_count: 0,
@@ -1424,7 +1426,7 @@ async fn propagate_payment(
14241426
},
14251427
};
14261428

1427-
if let Err(e) = sender.send(Ok(notify_result)) {
1429+
if let Err(e) = request.sender.send(Ok(notify_result)) {
14281430
log::error!("Could not notify payment result: {:?}.", e);
14291431
}
14301432
}

0 commit comments

Comments
 (0)