Skip to content

Commit e2beaef

Browse files
Merge pull request #1916 from valentinewallace/2022-11-chanman-payment-retries
`ChannelManager` Payment Retries
2 parents abbd295 + 6d81979 commit e2beaef

File tree

4 files changed

+1256
-35
lines changed

4 files changed

+1256
-35
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, No
4545
#[cfg(any(feature = "_test_utils", test))]
4646
use crate::ln::features::InvoiceFeatures;
4747
use crate::routing::gossip::NetworkGraph;
48-
use crate::routing::router::{DefaultRouter, InFlightHtlcs, PaymentParameters, Route, RouteHop, RoutePath, Router};
48+
use crate::routing::router::{DefaultRouter, InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, RoutePath, Router};
4949
use crate::routing::scoring::ProbabilisticScorer;
5050
use crate::ln::msgs;
5151
use crate::ln::onion_utils;
5252
use crate::ln::onion_utils::HTLCFailReason;
5353
use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VALUE_MSAT};
5454
#[cfg(test)]
5555
use crate::ln::outbound_payment;
56-
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment};
56+
use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, Retry};
5757
use crate::ln::wire::Encode;
5858
use crate::chain::keysinterface::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner};
5959
use crate::util::config::{UserConfig, ChannelConfig};
@@ -407,7 +407,7 @@ impl MsgHandleErrInternal {
407407
/// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited).
408408
/// This provides some limited amount of privacy. Ideally this would range from somewhere like one
409409
/// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly.
410-
const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
410+
pub(super) const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100;
411411

412412
/// For events which result in both a RevokeAndACK and a CommitmentUpdate, by default they should
413413
/// be sent in the order they appear in the return value, however sometimes the order needs to be
@@ -2466,6 +2466,18 @@ where
24662466
self.send_payment_along_path(path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv))
24672467
}
24682468

2469+
/// Similar to [`ChannelManager::send_payment`], but will automatically find a route based on
2470+
/// `route_params` and retry failed payment paths based on `retry_strategy`.
2471+
pub fn send_payment_with_retry(&self, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>, payment_id: PaymentId, route_params: RouteParameters, retry_strategy: Retry) -> Result<(), PaymentSendFailure> {
2472+
let best_block_height = self.best_block.read().unwrap().height();
2473+
self.pending_outbound_payments
2474+
.send_payment(payment_hash, payment_secret, payment_id, retry_strategy, route_params,
2475+
&self.router, self.list_usable_channels(), self.compute_inflight_htlcs(),
2476+
&self.entropy_source, &self.node_signer, best_block_height,
2477+
|path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv|
2478+
self.send_payment_along_path(path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv))
2479+
}
2480+
24692481
#[cfg(test)]
24702482
fn test_send_payment_internal(&self, route: &Route, payment_hash: PaymentHash, payment_secret: &Option<PaymentSecret>, keysend_preimage: Option<PaymentPreimage>, payment_id: PaymentId, recv_value_msat: Option<u64>, onion_session_privs: Vec<[u8; 32]>) -> Result<(), PaymentSendFailure> {
24712483
let best_block_height = self.best_block.read().unwrap().height();
@@ -2477,7 +2489,7 @@ where
24772489
#[cfg(test)]
24782490
pub(crate) fn test_add_new_pending_payment(&self, payment_hash: PaymentHash, payment_secret: Option<PaymentSecret>, payment_id: PaymentId, route: &Route) -> Result<Vec<[u8; 32]>, PaymentSendFailure> {
24792491
let best_block_height = self.best_block.read().unwrap().height();
2480-
self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, payment_secret, payment_id, route, &self.entropy_source, best_block_height)
2492+
self.pending_outbound_payments.test_add_new_pending_payment(payment_hash, payment_secret, payment_id, route, Retry::Attempts(0), &self.entropy_source, best_block_height)
24812493
}
24822494

24832495

@@ -3280,6 +3292,12 @@ where
32803292
}
32813293
}
32823294

3295+
let best_block_height = self.best_block.read().unwrap().height();
3296+
self.pending_outbound_payments.check_retry_payments(&self.router, || self.list_usable_channels(),
3297+
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer, best_block_height, &self.logger,
3298+
|path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv|
3299+
self.send_payment_along_path(path, payment_params, payment_hash, payment_secret, total_value, cur_height, payment_id, keysend_preimage, session_priv));
3300+
32833301
for (htlc_source, payment_hash, failure_reason, destination) in failed_forwards.drain(..) {
32843302
self.fail_htlc_backwards_internal(&htlc_source, &payment_hash, &failure_reason, destination);
32853303
}
@@ -7339,6 +7357,9 @@ where
73397357
hash_map::Entry::Vacant(entry) => {
73407358
let path_fee = path.get_path_fees();
73417359
entry.insert(PendingOutboundPayment::Retryable {
7360+
retry_strategy: Retry::Attempts(0),
7361+
attempts: PaymentAttempts::new(),
7362+
route_params: None,
73427363
session_privs: [session_priv_bytes].iter().map(|a| *a).collect(),
73437364
payment_hash: htlc.payment_hash,
73447365
payment_secret,

0 commit comments

Comments
 (0)