Skip to content

Commit 876b4ba

Browse files
Persist retryable HTLCs in ChannelManager
Used in upcoming commits to store retries when payment paths fail, then retry when processing pending HTLC forwards.
1 parent 97950b5 commit 876b4ba

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, No
4747
#[cfg(any(feature = "_test_utils", test))]
4848
use crate::ln::features::InvoiceFeatures;
4949
use crate::routing::gossip::NetworkGraph;
50-
use crate::routing::router::{DefaultRouter, InFlightHtlcs, PaymentParameters, Route, RouteHop, RoutePath, Router};
50+
use crate::routing::router::{DefaultRouter, InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, RoutePath, Router};
5151
use crate::routing::scoring::ProbabilisticScorer;
5252
use crate::ln::msgs;
5353
use crate::ln::onion_utils;
5454
use crate::ln::onion_utils::HTLCFailReason;
5555
use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError, MAX_VALUE_MSAT};
5656
#[cfg(test)]
57-
use crate::ln::outbound_payment;
58-
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, Retry};
57+
use crate::ln::outbound_payment::{self, Retry};
58+
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment};
5959
use crate::ln::wire::Encode;
6060
use crate::chain::keysinterface::{EntropySource, KeysInterface, KeysManager, NodeSigner, Recipient, Sign, SignerProvider};
6161
use crate::util::config::{UserConfig, ChannelConfig};
@@ -6530,12 +6530,19 @@ where
65306530
debug_assert!(false, "While we have code to serialize pending_claiming_payments, the map should always be empty until a later PR");
65316531
}
65326532

6533+
let mut retryable_htlcs = None;
6534+
let our_retryable_htlcs = self.pending_outbound_payments.retryable_htlcs.lock().unwrap();
6535+
if our_retryable_htlcs.len() != 0 {
6536+
retryable_htlcs = Some(our_retryable_htlcs);
6537+
}
6538+
65336539
write_tlv_fields!(writer, {
65346540
(1, pending_outbound_payments_no_retry, required),
65356541
(2, pending_intercepted_htlcs, option),
65366542
(3, pending_outbound_payments, required),
65376543
(4, pending_claiming_payments, option),
65386544
(5, self.our_network_pubkey, required),
6545+
(6, retryable_htlcs, option),
65396546
(7, self.fake_scid_rand_bytes, required),
65406547
(9, htlc_purposes, vec_type),
65416548
(11, self.probing_cookie_secret, required),
@@ -6870,6 +6877,7 @@ where
68706877
let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> = None;
68716878
let mut pending_outbound_payments = None;
68726879
let mut pending_intercepted_htlcs: Option<HashMap<InterceptId, PendingAddHTLCInfo>> = Some(HashMap::new());
6880+
let mut retryable_htlcs: Option<Vec<(PaymentId, RouteParameters)>> = Some(Vec::new());
68736881
let mut received_network_pubkey: Option<PublicKey> = None;
68746882
let mut fake_scid_rand_bytes: Option<[u8; 32]> = None;
68756883
let mut probing_cookie_secret: Option<[u8; 32]> = None;
@@ -6881,6 +6889,7 @@ where
68816889
(3, pending_outbound_payments, option),
68826890
(4, pending_claiming_payments, option),
68836891
(5, received_network_pubkey, option),
6892+
(6, retryable_htlcs, option),
68846893
(7, fake_scid_rand_bytes, option),
68856894
(9, claimable_htlc_purposes, vec_type),
68866895
(11, probing_cookie_secret, option),
@@ -7149,7 +7158,9 @@ where
71497158
}),
71507159
inbound_payment_key: expanded_inbound_key,
71517160
pending_inbound_payments: Mutex::new(pending_inbound_payments),
7152-
pending_outbound_payments: OutboundPayments { pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()) },
7161+
pending_outbound_payments: OutboundPayments::from_outbounds(
7162+
pending_outbound_payments.unwrap(),
7163+
retryable_htlcs.unwrap()),
71537164
pending_intercepted_htlcs: Mutex::new(pending_intercepted_htlcs.unwrap()),
71547165

71557166
forward_htlcs: Mutex::new(forward_htlcs),

lightning/src/ln/outbound_payment.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,25 @@ pub enum PaymentSendFailure {
341341

342342
pub(super) struct OutboundPayments {
343343
pub(super) pending_outbound_payments: Mutex<HashMap<PaymentId, PendingOutboundPayment>>,
344+
/// HTLCs that may be retried using the given `RouteParameters`.
345+
pub(super) retryable_htlcs: Mutex<Vec<(PaymentId, RouteParameters)>>,
344346
}
345347

346348
impl OutboundPayments {
347349
pub(super) fn new() -> Self {
348350
Self {
349-
pending_outbound_payments: Mutex::new(HashMap::new())
351+
pending_outbound_payments: Mutex::new(HashMap::new()),
352+
retryable_htlcs: Mutex::new(Vec::new()),
353+
}
354+
}
355+
356+
pub(super) fn from_outbounds(
357+
pending_outbound_payments: HashMap<PaymentId, PendingOutboundPayment>,
358+
retryable_htlcs: Vec<(PaymentId, RouteParameters)>)
359+
-> Self {
360+
Self {
361+
pending_outbound_payments: Mutex::new(pending_outbound_payments),
362+
retryable_htlcs: Mutex::new(retryable_htlcs),
350363
}
351364
}
352365

lightning/src/util/ser.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,30 @@ impl Readable for Vec<u8> {
665665
Ok(ret)
666666
}
667667
}
668+
669+
impl<A: Writeable, B: Writeable> Writeable for Vec<(A, B)> {
670+
#[inline]
671+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
672+
(self.len() as u16).write(w)?;
673+
for item in self.iter() {
674+
item.write(w)?;
675+
}
676+
Ok(())
677+
}
678+
}
679+
680+
impl<A: Readable, B: Readable> Readable for Vec<(A, B)> {
681+
#[inline]
682+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
683+
let len: u16 = Readable::read(r)?;
684+
let mut ret = Vec::with_capacity(len as usize);
685+
for _ in 0..len {
686+
ret.push(<(A, B) as Readable>::read(r)?);
687+
}
688+
Ok(ret)
689+
}
690+
}
691+
668692
impl Writeable for Vec<ecdsa::Signature> {
669693
#[inline]
670694
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {

0 commit comments

Comments
 (0)