Skip to content

Commit 3e6297a

Browse files
Expire outbound payments after 3 blocks if no parts are pending
1 parent 207479f commit 3e6297a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lightning/src/ln/channelmanager.rs

+14
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));
@@ -4546,6 +4549,16 @@ where
45464549
payment_secrets.retain(|_, inbound_payment| {
45474550
inbound_payment.expiry_time > header.time as u64
45484551
});
4552+
4553+
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
4554+
outbounds.retain(|_, payment| {
4555+
const PAYMENT_EXPIRY_BLOCKS: u32 = 3;
4556+
if payment.remaining_parts() != 0 { return true }
4557+
if let PendingOutboundPayment::Retryable { starting_block_height, .. } = payment {
4558+
return *starting_block_height + PAYMENT_EXPIRY_BLOCKS > height
4559+
}
4560+
true
4561+
});
45494562
}
45504563

45514564
fn get_relevant_txids(&self) -> Vec<Txid> {
@@ -5277,6 +5290,7 @@ impl_writeable_tlv_based_enum!(PendingOutboundPayment,
52775290
(4, payment_secret, option),
52785291
(6, total_msat, required),
52795292
(8, pending_amt_msat, required),
5293+
(10, starting_block_height, required),
52805294
},
52815295
;);
52825296

lightning/src/ln/functional_tests.rs

+55
Original file line numberDiff line numberDiff line change
@@ -4317,6 +4317,9 @@ fn retry_single_path_payment() {
43174317
// Rebalance the channel so the retry succeeds.
43184318
send_payment(&nodes[2], &vec!(&nodes[1])[..], 3_000_000);
43194319

4320+
// Mine two blocks (we expire retries after 3, so this will check that we don't expire early)
4321+
connect_blocks(&nodes[0], 2);
4322+
43204323
// Retry the payment and make sure it succeeds.
43214324
nodes[0].node.retry_payment(&route, payment_id).unwrap();
43224325
check_added_monitors!(nodes[0], 1);
@@ -4326,6 +4329,58 @@ fn retry_single_path_payment() {
43264329
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
43274330
}
43284331

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

0 commit comments

Comments
 (0)