Skip to content

Commit 8307cc6

Browse files
authored
Merge pull request #3531 from valentinewallace/2025-01-fix-buggy-route-err
2 parents 8257cc3 + e479317 commit 8307cc6

File tree

5 files changed

+150
-40
lines changed

5 files changed

+150
-40
lines changed

lightning/src/ln/channelmanager.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14863,6 +14863,7 @@ mod tests {
1486314863
},
1486414864
_ => panic!("unexpected error")
1486514865
}
14866+
assert!(nodes[0].node.list_recent_payments().is_empty());
1486614867
}
1486714868

1486814869
#[test]

lightning/src/ln/functional_tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6483,6 +6483,7 @@ fn test_payment_route_reaching_same_channel_twice() {
64836483
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
64846484
), false, APIError::InvalidRoute { ref err },
64856485
assert_eq!(err, &"Path went through the same channel twice"));
6486+
assert!(nodes[0].node.list_recent_payments().is_empty());
64866487
}
64876488

64886489
// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.

lightning/src/ln/outbound_payment.rs

+68-39
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl OutboundPayments {
811811
{
812812
let onion_session_privs = self.add_new_pending_payment(payment_hash, recipient_onion.clone(), payment_id, None, route, None, None, entropy_source, best_block_height)?;
813813
self.pay_route_internal(route, payment_hash, &recipient_onion, None, None, payment_id, None,
814-
onion_session_privs, node_signer, best_block_height, &send_payment_along_path)
814+
&onion_session_privs, node_signer, best_block_height, &send_payment_along_path)
815815
.map_err(|e| { self.remove_outbound_if_all_failed(payment_id, &e); e })
816816
}
817817

@@ -991,7 +991,7 @@ impl OutboundPayments {
991991

992992
let result = self.pay_route_internal(
993993
&route, payment_hash, &recipient_onion, keysend_preimage, invoice_request, payment_id,
994-
Some(route_params.final_value_msat), onion_session_privs, node_signer, best_block_height,
994+
Some(route_params.final_value_msat), &onion_session_privs, node_signer, best_block_height,
995995
&send_payment_along_path
996996
);
997997
log_info!(
@@ -1000,9 +1000,9 @@ impl OutboundPayments {
10001000
);
10011001
if let Err(e) = result {
10021002
self.handle_pay_route_err(
1003-
e, payment_id, payment_hash, route, route_params, router, first_hops,
1004-
&inflight_htlcs, entropy_source, node_signer, best_block_height, logger,
1005-
pending_events, &send_payment_along_path
1003+
e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1004+
&inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1005+
&send_payment_along_path
10061006
);
10071007
}
10081008
Ok(())
@@ -1276,12 +1276,16 @@ impl OutboundPayments {
12761276
})?;
12771277

12781278
let res = self.pay_route_internal(&route, payment_hash, &recipient_onion,
1279-
keysend_preimage, None, payment_id, None, onion_session_privs, node_signer,
1279+
keysend_preimage, None, payment_id, None, &onion_session_privs, node_signer,
12801280
best_block_height, &send_payment_along_path);
12811281
log_info!(logger, "Sending payment with id {} and hash {} returned {:?}",
12821282
payment_id, payment_hash, res);
12831283
if let Err(e) = res {
1284-
self.handle_pay_route_err(e, payment_id, payment_hash, route, route_params, router, first_hops, &inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, &send_payment_along_path);
1284+
self.handle_pay_route_err(
1285+
e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1286+
&inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1287+
&send_payment_along_path
1288+
);
12851289
}
12861290
Ok(())
12871291
}
@@ -1433,19 +1437,25 @@ impl OutboundPayments {
14331437
}
14341438
};
14351439
let res = self.pay_route_internal(&route, payment_hash, &recipient_onion, keysend_preimage,
1436-
invoice_request.as_ref(), payment_id, Some(total_msat), onion_session_privs, node_signer,
1440+
invoice_request.as_ref(), payment_id, Some(total_msat), &onion_session_privs, node_signer,
14371441
best_block_height, &send_payment_along_path);
14381442
log_info!(logger, "Result retrying payment id {}: {:?}", &payment_id, res);
14391443
if let Err(e) = res {
1440-
self.handle_pay_route_err(e, payment_id, payment_hash, route, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path);
1444+
self.handle_pay_route_err(
1445+
e, payment_id, payment_hash, route, route_params, onion_session_privs, router, first_hops,
1446+
inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events,
1447+
send_payment_along_path
1448+
);
14411449
}
14421450
}
14431451

14441452
fn handle_pay_route_err<R: Deref, NS: Deref, ES: Deref, IH, SP, L: Deref>(
14451453
&self, err: PaymentSendFailure, payment_id: PaymentId, payment_hash: PaymentHash, route: Route,
1446-
mut route_params: RouteParameters, router: &R, first_hops: Vec<ChannelDetails>,
1447-
inflight_htlcs: &IH, entropy_source: &ES, node_signer: &NS, best_block_height: u32, logger: &L,
1448-
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>, send_payment_along_path: &SP,
1454+
mut route_params: RouteParameters, onion_session_privs: Vec<[u8; 32]>, router: &R,
1455+
first_hops: Vec<ChannelDetails>, inflight_htlcs: &IH, entropy_source: &ES, node_signer: &NS,
1456+
best_block_height: u32, logger: &L,
1457+
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
1458+
send_payment_along_path: &SP,
14491459
)
14501460
where
14511461
R::Target: Router,
@@ -1457,10 +1467,24 @@ impl OutboundPayments {
14571467
{
14581468
match err {
14591469
PaymentSendFailure::AllFailedResendSafe(errs) => {
1470+
self.remove_session_privs(payment_id, route.paths.iter().zip(onion_session_privs.iter()));
14601471
Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut route_params, route.paths, errs.into_iter().map(|e| Err(e)), logger, pending_events);
14611472
self.find_route_and_send_payment(payment_hash, payment_id, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path);
14621473
},
14631474
PaymentSendFailure::PartialFailure { failed_paths_retry: Some(mut retry), results, .. } => {
1475+
debug_assert_eq!(results.len(), route.paths.len());
1476+
debug_assert_eq!(results.len(), onion_session_privs.len());
1477+
let failed_paths = results.iter().zip(route.paths.iter().zip(onion_session_privs.iter()))
1478+
.filter_map(|(path_res, (path, session_priv))| {
1479+
match path_res {
1480+
// While a MonitorUpdateInProgress is an Err(_), the payment is still
1481+
// considered "in flight" and we shouldn't remove it from the
1482+
// PendingOutboundPayment set.
1483+
Ok(_) | Err(APIError::MonitorUpdateInProgress) => None,
1484+
_ => Some((path, session_priv))
1485+
}
1486+
});
1487+
self.remove_session_privs(payment_id, failed_paths);
14641488
Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut retry, route.paths, results.into_iter(), logger, pending_events);
14651489
// Some paths were sent, even if we failed to send the full MPP value our recipient may
14661490
// misbehave and claim the funds, at which point we have to consider the payment sent, so
@@ -1474,11 +1498,13 @@ impl OutboundPayments {
14741498
},
14751499
PaymentSendFailure::PathParameterError(results) => {
14761500
log_error!(logger, "Failed to send to route due to parameter error in a single path. Your router is buggy");
1501+
self.remove_session_privs(payment_id, route.paths.iter().zip(onion_session_privs.iter()));
14771502
Self::push_path_failed_evs_and_scids(payment_id, payment_hash, &mut route_params, route.paths, results.into_iter(), logger, pending_events);
14781503
self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
14791504
},
14801505
PaymentSendFailure::ParameterError(e) => {
14811506
log_error!(logger, "Failed to send to route due to parameter error: {:?}. Your router is buggy", e);
1507+
self.remove_session_privs(payment_id, route.paths.iter().zip(onion_session_privs.iter()));
14821508
self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
14831509
},
14841510
PaymentSendFailure::DuplicatePayment => debug_assert!(false), // unreachable
@@ -1518,6 +1544,21 @@ impl OutboundPayments {
15181544
}
15191545
}
15201546

1547+
// If a payment fails after adding the pending payment but before any HTLCs are locked into
1548+
// channels, we need to clear the session_privs in order for abandoning the payment to succeed.
1549+
fn remove_session_privs<'a, I: Iterator<Item = (&'a Path, &'a [u8; 32])>>(
1550+
&self, payment_id: PaymentId, path_session_priv: I
1551+
) {
1552+
if let Some(payment) = self.pending_outbound_payments.lock().unwrap().get_mut(&payment_id) {
1553+
for (path, session_priv_bytes) in path_session_priv {
1554+
let removed = payment.remove(session_priv_bytes, Some(path));
1555+
debug_assert!(removed, "This can't happen as the payment has an entry for this path added by callers");
1556+
}
1557+
} else {
1558+
debug_assert!(false, "This can't happen as the payment was added by callers");
1559+
}
1560+
}
1561+
15211562
pub(super) fn send_probe<ES: Deref, NS: Deref, F>(
15221563
&self, path: Path, probing_cookie_secret: [u8; 32], entropy_source: &ES, node_signer: &NS,
15231564
best_block_height: u32, send_payment_along_path: F
@@ -1549,7 +1590,7 @@ impl OutboundPayments {
15491590

15501591
let recipient_onion_fields = RecipientOnionFields::spontaneous_empty();
15511592
match self.pay_route_internal(&route, payment_hash, &recipient_onion_fields,
1552-
None, None, payment_id, None, onion_session_privs, node_signer, best_block_height,
1593+
None, None, payment_id, None, &onion_session_privs, node_signer, best_block_height,
15531594
&send_payment_along_path
15541595
) {
15551596
Ok(()) => Ok((payment_hash, payment_id)),
@@ -1740,7 +1781,7 @@ impl OutboundPayments {
17401781
fn pay_route_internal<NS: Deref, F>(
17411782
&self, route: &Route, payment_hash: PaymentHash, recipient_onion: &RecipientOnionFields,
17421783
keysend_preimage: Option<PaymentPreimage>, invoice_request: Option<&InvoiceRequest>,
1743-
payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>,
1784+
payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: &Vec<[u8; 32]>,
17441785
node_signer: &NS, best_block_height: u32, send_payment_along_path: &F
17451786
) -> Result<(), PaymentSendFailure>
17461787
where
@@ -1791,30 +1832,12 @@ impl OutboundPayments {
17911832
let cur_height = best_block_height + 1;
17921833
let mut results = Vec::new();
17931834
debug_assert_eq!(route.paths.len(), onion_session_privs.len());
1794-
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.into_iter()) {
1795-
let mut path_res = send_payment_along_path(SendAlongPathArgs {
1835+
for (path, session_priv_bytes) in route.paths.iter().zip(onion_session_privs.iter()) {
1836+
let path_res = send_payment_along_path(SendAlongPathArgs {
17961837
path: &path, payment_hash: &payment_hash, recipient_onion, total_value,
17971838
cur_height, payment_id, keysend_preimage: &keysend_preimage, invoice_request,
1798-
session_priv_bytes
1839+
session_priv_bytes: *session_priv_bytes
17991840
});
1800-
match path_res {
1801-
Ok(_) => {},
1802-
Err(APIError::MonitorUpdateInProgress) => {
1803-
// While a MonitorUpdateInProgress is an Err(_), the payment is still
1804-
// considered "in flight" and we shouldn't remove it from the
1805-
// PendingOutboundPayment set.
1806-
},
1807-
Err(_) => {
1808-
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
1809-
if let Some(payment) = pending_outbounds.get_mut(&payment_id) {
1810-
let removed = payment.remove(&session_priv_bytes, Some(path));
1811-
debug_assert!(removed, "This can't happen as the payment has an entry for this path added by callers");
1812-
} else {
1813-
debug_assert!(false, "This can't happen as the payment was added by callers");
1814-
path_res = Err(APIError::APIMisuseError { err: "Internal error: payment disappeared during processing. Please report this bug!".to_owned() });
1815-
}
1816-
}
1817-
}
18181841
results.push(path_res);
18191842
}
18201843
let mut has_ok = false;
@@ -1879,17 +1902,23 @@ impl OutboundPayments {
18791902
F: Fn(SendAlongPathArgs) -> Result<(), APIError>,
18801903
{
18811904
self.pay_route_internal(route, payment_hash, &recipient_onion,
1882-
keysend_preimage, None, payment_id, recv_value_msat, onion_session_privs,
1905+
keysend_preimage, None, payment_id, recv_value_msat, &onion_session_privs,
18831906
node_signer, best_block_height, &send_payment_along_path)
18841907
.map_err(|e| { self.remove_outbound_if_all_failed(payment_id, &e); e })
18851908
}
18861909

18871910
// If we failed to send any paths, remove the new PaymentId from the `pending_outbound_payments`
18881911
// map as the payment is free to be resent.
18891912
fn remove_outbound_if_all_failed(&self, payment_id: PaymentId, err: &PaymentSendFailure) {
1890-
if let &PaymentSendFailure::AllFailedResendSafe(_) = err {
1891-
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
1892-
debug_assert!(removed, "We should always have a pending payment to remove here");
1913+
match err {
1914+
PaymentSendFailure::AllFailedResendSafe(_)
1915+
| PaymentSendFailure::ParameterError(_)
1916+
| PaymentSendFailure::PathParameterError(_) =>
1917+
{
1918+
let removed = self.pending_outbound_payments.lock().unwrap().remove(&payment_id).is_some();
1919+
debug_assert!(removed, "We should always have a pending payment to remove here");
1920+
},
1921+
PaymentSendFailure::DuplicatePayment | PaymentSendFailure::PartialFailure { .. } => {}
18931922
}
18941923
}
18951924

lightning/src/ln/payment_tests.rs

+76-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::types::payment::{PaymentHash, PaymentSecret, PaymentPreimage};
2424
use crate::ln::chan_utils;
2525
use crate::ln::msgs::ChannelMessageHandler;
2626
use crate::ln::onion_utils;
27-
use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, Retry, RetryableSendFailure};
27+
use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, ProbeSendFailure, Retry, RetryableSendFailure};
2828
use crate::routing::gossip::{EffectiveCapacity, RoutingFees};
2929
use crate::routing::router::{get_route, Path, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters};
3030
use crate::routing::scoring::ChannelUsage;
@@ -1249,6 +1249,7 @@ fn sent_probe_is_probe_of_sending_node() {
12491249
// First check we refuse to build a single-hop probe
12501250
let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[1], 100_000);
12511251
assert!(nodes[0].node.send_probe(route.paths[0].clone()).is_err());
1252+
assert!(nodes[0].node.list_recent_payments().is_empty());
12521253

12531254
// Then build an actual two-hop probing path
12541255
let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], 100_000);
@@ -4375,3 +4376,77 @@ fn test_non_strict_forwarding() {
43754376
let events = nodes[0].node.get_and_clear_pending_events();
43764377
expect_payment_failed_conditions_event(events, payment_hash, false, PaymentFailedConditions::new().blamed_scid(routed_scid));
43774378
}
4379+
4380+
#[test]
4381+
fn remove_pending_outbounds_on_buggy_router() {
4382+
// Ensure that if a payment errors due to a bogus route, we'll abandon the payment and remove the
4383+
// pending outbound from storage.
4384+
let chanmon_cfgs = create_chanmon_cfgs(2);
4385+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4386+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4387+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4388+
create_announced_chan_between_nodes(&nodes, 0, 1);
4389+
4390+
let amt_msat = 10_000;
4391+
let payment_id = PaymentId([42; 32]);
4392+
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
4393+
.with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
4394+
let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, amt_msat);
4395+
4396+
// Extend the path by itself, essentially simulating route going through same channel twice
4397+
let cloned_hops = route.paths[0].hops.clone();
4398+
route.paths[0].hops.extend_from_slice(&cloned_hops);
4399+
let route_params = route.route_params.clone().unwrap();
4400+
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
4401+
4402+
nodes[0].node.send_payment(
4403+
payment_hash, RecipientOnionFields::secret_only(payment_secret), payment_id, route_params,
4404+
Retry::Attempts(1) // Even though another attempt is allowed, the payment should fail
4405+
).unwrap();
4406+
let events = nodes[0].node.get_and_clear_pending_events();
4407+
assert_eq!(events.len(), 2);
4408+
match &events[0] {
4409+
Event::PaymentPathFailed { failure, payment_failed_permanently, .. } => {
4410+
assert_eq!(failure, &PathFailure::InitialSend {
4411+
err: APIError::InvalidRoute { err: "Path went through the same channel twice".to_string() }
4412+
});
4413+
assert!(!payment_failed_permanently);
4414+
},
4415+
_ => panic!()
4416+
}
4417+
match events[1] {
4418+
Event::PaymentFailed { reason, .. } => {
4419+
assert_eq!(reason.unwrap(), PaymentFailureReason::UnexpectedError);
4420+
},
4421+
_ => panic!()
4422+
}
4423+
assert!(nodes[0].node.list_recent_payments().is_empty());
4424+
}
4425+
4426+
#[test]
4427+
fn remove_pending_outbound_probe_on_buggy_path() {
4428+
// Ensure that if a probe errors due to a bogus route, we'll return an error and remove the
4429+
// pending outbound from storage.
4430+
let chanmon_cfgs = create_chanmon_cfgs(2);
4431+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
4432+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
4433+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
4434+
create_announced_chan_between_nodes(&nodes, 0, 1);
4435+
4436+
let amt_msat = 10_000;
4437+
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
4438+
.with_bolt11_features(nodes[1].node.bolt11_invoice_features()).unwrap();
4439+
let (mut route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, amt_msat);
4440+
4441+
// Extend the path by itself, essentially simulating route going through same channel twice
4442+
let cloned_hops = route.paths[0].hops.clone();
4443+
route.paths[0].hops.extend_from_slice(&cloned_hops);
4444+
4445+
assert_eq!(
4446+
nodes[0].node.send_probe(route.paths.pop().unwrap()).unwrap_err(),
4447+
ProbeSendFailure::ParameterError(
4448+
APIError::InvalidRoute { err: "Path went through the same channel twice".to_string() }
4449+
)
4450+
);
4451+
assert!(nodes[0].node.list_recent_payments().is_empty());
4452+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Bug Fixes
2+
3+
* Fixed a rare case where a custom router returning a buggy route could result in holding onto a
4+
pending payment forever and in some cases failing to generate a PaymentFailed event (#3531).

0 commit comments

Comments
 (0)