@@ -123,19 +123,15 @@ impl Bolt11Payment {
123123 Ok ( ( ) )
124124 }
125125
126- fn has_pending_inbound_payment ( & self , payment_hash : & PaymentHash ) -> bool {
127- self . pending_payment_store . get_pending_manual_bolt11_by_payment_hash ( payment_hash) . is_some ( )
128- }
129-
130- fn register_manual_claim_invoice (
131- & self , payment_hash : PaymentHash , amount_msat : Option < u64 > , payment_secret : PaymentSecret ,
126+ fn pending_manual_claim_invoice (
127+ payment_hash : PaymentHash , amount_msat : Option < u64 > , payment_secret : Option < PaymentSecret > ,
132128 expiry_secs : u32 ,
133- ) -> Result < ( ) , Error > {
129+ ) -> PendingPaymentDetails {
134130 let payment_id = PaymentId ( payment_hash. 0 ) ;
135131 let kind = PaymentKind :: Bolt11 {
136132 hash : payment_hash,
137133 preimage : None ,
138- secret : Some ( payment_secret) ,
134+ secret : payment_secret,
139135 counterparty_skimmed_fee_msat : None ,
140136 } ;
141137 let payment = PaymentDetails :: new (
@@ -147,22 +143,51 @@ impl Bolt11Payment {
147143 PaymentStatus :: Pending ,
148144 ) ;
149145 let expires_at = Some ( Self :: current_time_secs ( ) . saturating_add ( expiry_secs as u64 ) ) ;
146+ PendingPaymentDetails :: new_with_expiry ( payment, Vec :: new ( ) , expires_at)
147+ }
148+
149+ fn reserve_manual_claim_invoice (
150+ & self , payment_hash : PaymentHash , amount_msat : Option < u64 > , expiry_secs : u32 ,
151+ ) -> Result < ( ) , Error > {
150152 let pending_payment =
151- PendingPaymentDetails :: new_with_expiry ( payment, Vec :: new ( ) , expires_at) ;
153+ Self :: pending_manual_claim_invoice ( payment_hash, amount_msat, None , expiry_secs) ;
154+ if let Err ( e) =
155+ self . runtime . block_on ( self . pending_payment_store . insert_manual_bolt11 ( pending_payment) )
156+ {
157+ if e == Error :: DuplicatePayment {
158+ log_error ! ( self . logger, "Payment error: an invoice must not be paid twice." ) ;
159+ }
160+ return Err ( e) ;
161+ }
162+ Ok ( ( ) )
163+ }
164+
165+ fn register_manual_claim_invoice (
166+ & self , payment_hash : PaymentHash , amount_msat : Option < u64 > , payment_secret : PaymentSecret ,
167+ expiry_secs : u32 ,
168+ ) -> Result < ( ) , Error > {
169+ let pending_payment = Self :: pending_manual_claim_invoice (
170+ payment_hash,
171+ amount_msat,
172+ Some ( payment_secret) ,
173+ expiry_secs,
174+ ) ;
152175 self . runtime . block_on ( self . pending_payment_store . insert_or_update ( pending_payment) ) ?;
153176 Ok ( ( ) )
154177 }
155178
179+ fn remove_manual_claim_invoice ( & self , payment_hash : PaymentHash ) -> Result < ( ) , Error > {
180+ let payment_id = PaymentId ( payment_hash. 0 ) ;
181+ self . runtime . block_on ( self . pending_payment_store . remove ( & payment_id) )
182+ }
183+
156184 pub ( crate ) fn receive_inner (
157185 & self , amount_msat : Option < u64 > , invoice_description : & LdkBolt11InvoiceDescription ,
158186 expiry_secs : u32 , manual_claim_payment_hash : Option < PaymentHash > ,
159187 ) -> Result < LdkBolt11Invoice , Error > {
160188 if let Some ( payment_hash) = manual_claim_payment_hash {
161189 self . prune_expired_pending_payments ( ) ?;
162- if self . has_pending_inbound_payment ( & payment_hash) {
163- log_error ! ( self . logger, "Payment error: an invoice must not be paid twice." ) ;
164- return Err ( Error :: DuplicatePayment ) ;
165- }
190+ self . reserve_manual_claim_invoice ( payment_hash, amount_msat, expiry_secs) ?;
166191 }
167192
168193 let invoice = {
@@ -181,6 +206,9 @@ impl Bolt11Payment {
181206 } ,
182207 Err ( e) => {
183208 log_error ! ( self . logger, "Failed to create invoice: {}" , e) ;
209+ if let Some ( payment_hash) = manual_claim_payment_hash {
210+ self . remove_manual_claim_invoice ( payment_hash) ?;
211+ }
184212 return Err ( Error :: InvoiceCreationFailed ) ;
185213 } ,
186214 }
@@ -234,14 +262,11 @@ impl Bolt11Payment {
234262 ) -> Result < LdkBolt11Invoice , Error > {
235263 if let Some ( payment_hash) = payment_hash {
236264 self . prune_expired_pending_payments ( ) ?;
237- if self . has_pending_inbound_payment ( & payment_hash) {
238- log_error ! ( self . logger, "Payment error: an invoice must not be paid twice." ) ;
239- return Err ( Error :: DuplicatePayment ) ;
240- }
265+ self . reserve_manual_claim_invoice ( payment_hash, amount_msat, expiry_secs) ?;
241266 }
242267
243268 let connection_manager = Arc :: clone ( & self . connection_manager ) ;
244- let ( invoice , chosen_lsp ) = self . runtime . block_on ( async move {
269+ let res = self . runtime . block_on ( async move {
245270 if let Some ( amount_msat) = amount_msat {
246271 self . liquidity_source
247272 . lsps2_client ( )
@@ -266,7 +291,16 @@ impl Bolt11Payment {
266291 )
267292 . await
268293 }
269- } ) ?;
294+ } ) ;
295+ let ( invoice, chosen_lsp) = match res {
296+ Ok ( res) => res,
297+ Err ( e) => {
298+ if let Some ( payment_hash) = payment_hash {
299+ self . remove_manual_claim_invoice ( payment_hash) ?;
300+ }
301+ return Err ( e) ;
302+ } ,
303+ } ;
270304
271305 if let Some ( payment_hash) = payment_hash {
272306 self . register_manual_claim_invoice (
0 commit comments