Skip to content

Commit 8db9c50

Browse files
Add method to retry payments
1 parent bf527b0 commit 8db9c50

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,54 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21292129
}
21302130
}
21312131

2132+
/// Retries a payment along the given [`Route`].
2133+
///
2134+
/// Errors returned are a superset of those returned from [`send_payment`], so see
2135+
/// [`send_payment`] documentation for more details on errors. This method will also error if the
2136+
/// retry amount puts the payment more than 10% over the payment's total amount, or if the payment
2137+
/// for the given `payment_id` cannot be found (likely due to timeout or success).
2138+
///
2139+
/// [`send_payment`]: [`ChannelManager::send_payment`]
2140+
pub fn retry_payment(&self, route: &Route, payment_id: PaymentId) -> Result<(), PaymentSendFailure> {
2141+
const RETRY_OVERFLOW_PERCENTAGE: u64 = 10;
2142+
for path in route.paths.iter() {
2143+
if path.len() == 0 {
2144+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
2145+
err: "length-0 path in route".to_string()
2146+
}))
2147+
}
2148+
}
2149+
2150+
let (total_msat, payment_hash, payment_secret) = {
2151+
let outbounds = self.pending_outbound_payments.lock().unwrap();
2152+
if let Some(payment) = outbounds.get(&payment_id) {
2153+
match payment {
2154+
PendingOutboundPayment::Retryable {
2155+
total_msat, payment_hash, payment_secret, pending_amt_msat, ..
2156+
} => {
2157+
let retry_amt_msat: u64 = route.paths.iter().map(|path| path.last().unwrap().fee_msat).sum();
2158+
if retry_amt_msat + *pending_amt_msat > *total_msat * (100 + RETRY_OVERFLOW_PERCENTAGE) / 100 {
2159+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
2160+
err: format!("retry_amt_msat of {} will put pending_amt_msat (currently: {}) more than 10% over total_payment_amt_msat of {}", retry_amt_msat, pending_amt_msat, total_msat).to_string()
2161+
}))
2162+
}
2163+
(*total_msat, *payment_hash, *payment_secret)
2164+
},
2165+
PendingOutboundPayment::Legacy { .. } => {
2166+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
2167+
err: "Unable to retry payments that were initially sent on LDK versions prior to 0.0.102".to_string()
2168+
}))
2169+
}
2170+
}
2171+
} else {
2172+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
2173+
err: "Payment with ID {} not found".to_string()
2174+
}))
2175+
}
2176+
};
2177+
return self.send_payment_internal(route, payment_hash, &payment_secret, None, Some(payment_id), Some(total_msat)).map(|_| ())
2178+
}
2179+
21322180
/// Send a spontaneous payment, which is a payment that does not require the recipient to have
21332181
/// generated an invoice. Optionally, you may specify the preimage. If you do choose to specify
21342182
/// the preimage, it must be a cryptographically secure random value that no intermediate node

lightning/src/ln/functional_tests.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,6 +4183,96 @@ fn mpp_failure() {
41834183
fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
41844184
}
41854185

4186+
#[test]
4187+
fn mpp_retry() {
4188+
let chanmon_cfgs = create_chanmon_cfgs(4);
4189+
let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
4190+
let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
4191+
let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
4192+
4193+
let chan_1_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
4194+
let chan_2_id = create_announced_chan_between_nodes(&nodes, 0, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
4195+
let chan_3_id = create_announced_chan_between_nodes(&nodes, 1, 3, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
4196+
let chan_4_id = create_announced_chan_between_nodes(&nodes, 3, 2, InitFeatures::known(), InitFeatures::known()).0.contents.short_channel_id;
4197+
let logger = test_utils::TestLogger::new();
4198+
// Rebalance
4199+
send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
4200+
4201+
let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash!(&nodes[3]);
4202+
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
4203+
let mut route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph, &nodes[3].node.get_our_node_id(), Some(InvoiceFeatures::known()), None, &[], 1_000_000, TEST_FINAL_CLTV, &logger).unwrap();
4204+
let path = route.paths[0].clone();
4205+
route.paths.push(path);
4206+
route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
4207+
route.paths[0][0].short_channel_id = chan_1_id;
4208+
route.paths[0][1].short_channel_id = chan_3_id;
4209+
route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
4210+
route.paths[1][0].short_channel_id = chan_2_id;
4211+
route.paths[1][1].short_channel_id = chan_4_id;
4212+
4213+
// Initiate the MPP payment.
4214+
let payment_id = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
4215+
check_added_monitors!(nodes[0], 2); // one monitor per path
4216+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4217+
assert_eq!(events.len(), 2);
4218+
4219+
// Pass half of the payment along the success path.
4220+
let success_path_msgs = events.remove(0);
4221+
pass_along_path(&nodes[0], &[&nodes[1], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), success_path_msgs, false, None);
4222+
4223+
// Add the HTLC along the first hop.
4224+
let fail_path_msgs_1 = events.remove(0);
4225+
let (update_add, commitment_signed) = match fail_path_msgs_1 {
4226+
MessageSendEvent::UpdateHTLCs { node_id: _, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
4227+
assert_eq!(update_add_htlcs.len(), 1);
4228+
assert!(update_fail_htlcs.is_empty());
4229+
assert!(update_fulfill_htlcs.is_empty());
4230+
assert!(update_fail_malformed_htlcs.is_empty());
4231+
assert!(update_fee.is_none());
4232+
(update_add_htlcs[0].clone(), commitment_signed.clone())
4233+
},
4234+
_ => panic!("Unexpected event"),
4235+
};
4236+
nodes[2].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &update_add);
4237+
commitment_signed_dance!(nodes[2], nodes[0], commitment_signed, false);
4238+
4239+
// Attempt to forward the payment and complete the 2nd path's failure.
4240+
expect_pending_htlcs_forwardable!(&nodes[2]);
4241+
expect_pending_htlcs_forwardable!(&nodes[2]);
4242+
let htlc_updates = get_htlc_update_msgs!(nodes[2], nodes[0].node.get_our_node_id());
4243+
assert!(htlc_updates.update_add_htlcs.is_empty());
4244+
assert_eq!(htlc_updates.update_fail_htlcs.len(), 1);
4245+
assert!(htlc_updates.update_fulfill_htlcs.is_empty());
4246+
assert!(htlc_updates.update_fail_malformed_htlcs.is_empty());
4247+
check_added_monitors!(nodes[2], 1);
4248+
nodes[0].node.handle_update_fail_htlc(&nodes[2].node.get_our_node_id(), &htlc_updates.update_fail_htlcs[0]);
4249+
commitment_signed_dance!(nodes[0], nodes[2], htlc_updates.commitment_signed, false);
4250+
expect_payment_failed!(nodes[0], payment_hash, false);
4251+
4252+
// Rebalance the channel so the second half of the payment can succeed.
4253+
send_payment(&nodes[3], &vec!(&nodes[2])[..], 1_500_000);
4254+
4255+
// Make sure it errors as expected given a too-large amount.
4256+
if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, payment_id) {
4257+
assert!(err.contains("over total_payment_amt_msat"));
4258+
} else { panic!("Unexpected error"); }
4259+
4260+
// Make sure it errors as expected given the wrong payment_id.
4261+
if let Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError { err })) = nodes[0].node.retry_payment(&route, PaymentId([0; 32])) {
4262+
assert!(err.contains("not found"));
4263+
} else { panic!("Unexpected error"); }
4264+
4265+
// Retry the second half of the payment and make sure it succeeds.
4266+
let mut path = route.clone();
4267+
path.paths.remove(0);
4268+
nodes[0].node.retry_payment(&path, payment_id).unwrap();
4269+
check_added_monitors!(nodes[0], 1);
4270+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4271+
assert_eq!(events.len(), 1);
4272+
pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
4273+
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
4274+
}
4275+
41864276
#[test]
41874277
fn test_dup_htlc_onchain_fails_on_reload() {
41884278
// When a Channel is closed, any outbound HTLCs which were relayed through it are simply

0 commit comments

Comments
 (0)