Skip to content

Commit 58afd58

Browse files
Expire outbound payments after 3 blocks if no parts are pending
1 parent 5d59cdc commit 58afd58

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ enum PendingOutboundPayment {
413413
pending_amt_msat: u64,
414414
/// The total payment amount across all paths, used to verify that a retry is not overpaying.
415415
total_msat: u64,
416+
/// Our best known block height at the time this payment was initiated.
417+
starting_block_height: u32,
416418
},
417419
}
418420

@@ -1955,6 +1957,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
19551957
pending_amt_msat: 0,
19561958
payment_hash: *payment_hash,
19571959
payment_secret: *payment_secret,
1960+
starting_block_height: self.best_block.read().unwrap().height(),
19581961
total_msat: total_value,
19591962
});
19601963
assert!(payment.insert(session_priv_bytes, path.last().unwrap().fee_msat));
@@ -4543,6 +4546,15 @@ where
45434546
payment_secrets.retain(|_, inbound_payment| {
45444547
inbound_payment.expiry_time > header.time as u64
45454548
});
4549+
4550+
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
4551+
outbounds.retain(|_, payment| {
4552+
if payment.remaining_parts() != 0 { return true }
4553+
else if let PendingOutboundPayment::Retryable { starting_block_height, .. } = payment {
4554+
return *starting_block_height + 3 > height
4555+
}
4556+
true
4557+
});
45464558
}
45474559

45484560
fn get_relevant_txids(&self) -> Vec<Txid> {
@@ -5274,6 +5286,7 @@ impl_writeable_tlv_based_enum!(PendingOutboundPayment,
52745286
(4, payment_secret, option),
52755287
(6, total_msat, required),
52765288
(8, pending_amt_msat, required),
5289+
(10, starting_block_height, required),
52775290
},
52785291
;);
52795292

lightning/src/ln/functional_tests.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,6 +4326,58 @@ fn retry_single_path_payment() {
43264326
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
43274327
}
43284328

4329+
#[test]
4330+
fn retry_expired_payment() {
4331+
let chanmon_cfgs = create_chanmon_cfgs(3);
4332+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
4333+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
4334+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
4335+
4336+
let _chan_0 = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known());
4337+
let _chan_1 = create_announced_chan_between_nodes(&nodes, 2, 1, InitFeatures::known(), InitFeatures::known());
4338+
// Rebalance to find a route
4339+
send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
4340+
4341+
let logger = test_utils::TestLogger::new();
4342+
let (_payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
4343+
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
4344+
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[2].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &Vec::new(), 100_000, TEST_FINAL_CLTV, &logger).unwrap();
4345+
4346+
// Rebalance so that the first hop fails.
4347+
send_payment(&nodes[1], &vec!(&nodes[2])[..], 2_000_000);
4348+
4349+
// Make sure the payment fails on the first hop.
4350+
let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
4351+
check_added_monitors!(nodes[0], 1);
4352+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4353+
assert_eq!(events.len(), 1);
4354+
let mut payment_event = SendEvent::from_event(events.pop().unwrap());
4355+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
4356+
check_added_monitors!(nodes[1], 0);
4357+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
4358+
expect_pending_htlcs_forwardable!(nodes[1]);
4359+
expect_pending_htlcs_forwardable!(&nodes[1]);
4360+
let htlc_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
4361+
assert!(htlc_updates.update_add_htlcs.is_empty());
4362+
assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
4363+
assert!(htlc_updates.update_fulfill_htlcs.is_empty());
4364+
assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
4365+
check_added_monitors!(nodes[1], 1);
4366+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
4367+
commitment_signed_dance!(nodes[0], nodes[1], htlc_updates.commitment_signed, false);
4368+
expect_payment_failed!(nodes[0], payment_hash, false);
4369+
4370+
// Mine blocks so the payment will have expired.
4371+
connect_blocks(&nodes[0], 3);
4372+
4373+
// Retry the payment and make sure it errors as expected.
4374+
if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, payment_id) {
4375+
assert!(err.contains("not found"));
4376+
} else {
4377+
panic!("Unexpected error");
4378+
}
4379+
}
4380+
43294381
#[test]
43304382
fn test_dup_htlc_onchain_fails_on_reload() {
43314383
// When a Channel is closed, any outbound HTLCs which were relayed through it are simply

0 commit comments

Comments
 (0)