Skip to content

Commit 7a5145d

Browse files
committed
Add PendingOutboundPayment::InvoiceReceived
When a BOLT 12 invoice has been received, a payment attempt is made and any errors result in abandoning the PendingOutboundPayment. This results in generating at PaymentFailed event, which has a PaymentHash. Thus, when receiving an invoice, transition from AwaitingInvoice to a new InvoiceReceived state, the latter of which contains a PaymentHash such the abandon_payment helper can still be used.
1 parent 9e1181f commit 7a5145d

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,13 @@ pub enum RecentPaymentDetails {
16971697
/// Identifier for the payment to ensure idempotency.
16981698
payment_id: PaymentId,
16991699
},
1700+
/// When a requested invoice has been received, but a payment has not yet been sent.
1701+
InvoiceReceived {
1702+
/// Identifier for the payment to ensure idempotency.
1703+
payment_id: PaymentId,
1704+
/// Hash of the payment that has not yet been sent.
1705+
payment_hash: PaymentHash,
1706+
},
17001707
/// When a payment is still being sent and awaiting successful delivery.
17011708
Pending {
17021709
/// Hash of the payment that is currently being sent but has yet to be fulfilled or
@@ -2432,6 +2439,11 @@ where
24322439
PendingOutboundPayment::AwaitingInvoice { .. } => {
24332440
Some(RecentPaymentDetails::AwaitingInvoice { payment_id: *payment_id })
24342441
},
2442+
PendingOutboundPayment::InvoiceReceived { payment_hash } => {
2443+
Some(RecentPaymentDetails::InvoiceReceived {
2444+
payment_id: *payment_id, payment_hash: *payment_hash
2445+
})
2446+
},
24352447
PendingOutboundPayment::Retryable { payment_hash, total_msat, .. } => {
24362448
Some(RecentPaymentDetails::Pending {
24372449
payment_hash: *payment_hash,
@@ -8358,6 +8370,7 @@ where
83588370
}
83598371
}
83608372
PendingOutboundPayment::AwaitingInvoice { .. } => {},
8373+
PendingOutboundPayment::InvoiceReceived { .. } => {},
83618374
PendingOutboundPayment::Fulfilled { .. } => {},
83628375
PendingOutboundPayment::Abandoned { .. } => {},
83638376
}

lightning/src/ln/outbound_payment.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub(crate) enum PendingOutboundPayment {
4141
AwaitingInvoice {
4242
timer_ticks_without_response: u8,
4343
},
44+
InvoiceReceived {
45+
payment_hash: PaymentHash,
46+
},
4447
Retryable {
4548
retry_strategy: Option<Retry>,
4649
attempts: PaymentAttempts,
@@ -140,6 +143,7 @@ impl PendingOutboundPayment {
140143
match self {
141144
PendingOutboundPayment::Legacy { .. } => None,
142145
PendingOutboundPayment::AwaitingInvoice { .. } => None,
146+
PendingOutboundPayment::InvoiceReceived { payment_hash } => Some(*payment_hash),
143147
PendingOutboundPayment::Retryable { payment_hash, .. } => Some(*payment_hash),
144148
PendingOutboundPayment::Fulfilled { payment_hash, .. } => *payment_hash,
145149
PendingOutboundPayment::Abandoned { payment_hash, .. } => Some(*payment_hash),
@@ -153,10 +157,8 @@ impl PendingOutboundPayment {
153157
PendingOutboundPayment::Retryable { session_privs, .. } |
154158
PendingOutboundPayment::Fulfilled { session_privs, .. } |
155159
PendingOutboundPayment::Abandoned { session_privs, .. } => session_privs,
156-
PendingOutboundPayment::AwaitingInvoice { .. } => {
157-
debug_assert!(false);
158-
return;
159-
},
160+
PendingOutboundPayment::AwaitingInvoice { .. } |
161+
PendingOutboundPayment::InvoiceReceived { .. } => { debug_assert!(false); return; },
160162
});
161163
let payment_hash = self.payment_hash();
162164
*self = PendingOutboundPayment::Fulfilled { session_privs, payment_hash, timer_ticks_without_htlcs: 0 };
@@ -171,6 +173,12 @@ impl PendingOutboundPayment {
171173
payment_hash: *payment_hash,
172174
reason: Some(reason)
173175
};
176+
} else if let PendingOutboundPayment::InvoiceReceived { payment_hash } = self {
177+
*self = PendingOutboundPayment::Abandoned {
178+
session_privs: HashSet::new(),
179+
payment_hash: *payment_hash,
180+
reason: Some(reason)
181+
};
174182
}
175183
}
176184

@@ -184,6 +192,7 @@ impl PendingOutboundPayment {
184192
session_privs.remove(session_priv)
185193
},
186194
PendingOutboundPayment::AwaitingInvoice { .. } => false,
195+
PendingOutboundPayment::InvoiceReceived { .. } => false,
187196
};
188197
if remove_res {
189198
if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
@@ -204,6 +213,7 @@ impl PendingOutboundPayment {
204213
session_privs.insert(session_priv)
205214
}
206215
PendingOutboundPayment::AwaitingInvoice { .. } => false,
216+
PendingOutboundPayment::InvoiceReceived { .. } => false,
207217
PendingOutboundPayment::Fulfilled { .. } => false,
208218
PendingOutboundPayment::Abandoned { .. } => false,
209219
};
@@ -227,6 +237,7 @@ impl PendingOutboundPayment {
227237
session_privs.len()
228238
},
229239
PendingOutboundPayment::AwaitingInvoice { .. } => 0,
240+
PendingOutboundPayment::InvoiceReceived { .. } => 0,
230241
}
231242
}
232243
}
@@ -862,7 +873,9 @@ impl OutboundPayments {
862873
log_error!(logger, "Unable to retry payments that were initially sent on LDK versions prior to 0.0.102");
863874
return
864875
},
865-
PendingOutboundPayment::AwaitingInvoice { .. } => {
876+
PendingOutboundPayment::AwaitingInvoice { .. } |
877+
PendingOutboundPayment::InvoiceReceived { .. } =>
878+
{
866879
log_error!(logger, "Payment not yet sent");
867880
return
868881
},
@@ -1550,6 +1563,9 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
15501563
(5, AwaitingInvoice) => {
15511564
(0, timer_ticks_without_response, required),
15521565
},
1566+
(7, InvoiceReceived) => {
1567+
(0, payment_hash, required),
1568+
},
15531569
);
15541570

15551571
#[cfg(test)]

0 commit comments

Comments
 (0)