Skip to content

Commit 6ba5c5e

Browse files
Add method to retry payments
1 parent fae7817 commit 6ba5c5e

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,35 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21152115
}
21162116
}
21172117

2118+
/// XXX
2119+
pub fn retry_payment(&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>, payment_id: PaymentId) -> Result<(), PaymentSendFailure> {
2120+
let outbounds = self.pending_outbound_payments.lock().unwrap();
2121+
let mut total_msat_opt = None;
2122+
if let Some(payment) = outbounds.get(&payment_id) {
2123+
match payment {
2124+
PendingOutboundPayment::Retryable { total_msat, payment_hash: hash, .. } => {
2125+
// XXX checks that total_msat_opt makes sense
2126+
total_msat_opt = Some(*total_msat);
2127+
if payment_hash != *hash {
2128+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
2129+
err: "Payment hash does not match given payment ID".to_string()
2130+
}))
2131+
}
2132+
},
2133+
PendingOutboundPayment::Legacy { .. } => {
2134+
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
2135+
err: "Unable to retry payments that were serialized as legacy payments".to_string()
2136+
}))
2137+
}
2138+
}
2139+
} else {
2140+
// XXX they could retry with a bogus amt/hash/secret/payment_id here and we wouldn't catch it atm
2141+
// should we keep payments in pending_outbound_payments until they succeed/expire/manually removed?
2142+
}
2143+
mem::drop(outbounds);
2144+
return self.send_payment_internal(route, payment_hash, payment_secret, None, Some(payment_id), total_msat_opt).map(|_| ())
2145+
}
2146+
21182147
/// Send a spontaneous payment, which is a payment that does not require the recipient to have
21192148
/// generated an invoice. Optionally, you may specify the preimage. If you do choose to specify
21202149
/// the preimage, it must be a cryptographically secure random value that no intermediate node

lightning/src/ln/functional_tests.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,6 +4183,85 @@ 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+
// Retry the second half of the payment and make sure it succeeds.
4256+
let mut path = route.clone();
4257+
path.paths.remove(0);
4258+
nodes[0].node.retry_payment(&path, payment_hash, &Some(payment_secret), payment_id).unwrap();
4259+
check_added_monitors!(nodes[0], 1);
4260+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
4261+
assert_eq!(events.len(), 1);
4262+
pass_along_path(&nodes[0], &[&nodes[2], &nodes[3]], 2_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
4263+
}
4264+
41864265
#[test]
41874266
fn test_dup_htlc_onchain_fails_on_reload() {
41884267
// When a Channel is closed, any outbound HTLCs which were relayed through it are simply

0 commit comments

Comments
 (0)