@@ -22,6 +22,7 @@ use crate::ln::channelmanager::{EventCompletionAction, HTLCSource, PaymentId};
22
22
use crate :: ln:: onion_utils;
23
23
use crate :: ln:: onion_utils:: { DecodedOnionFailure , HTLCFailReason } ;
24
24
use crate :: offers:: invoice:: Bolt12Invoice ;
25
+ use crate :: offers:: invoice_request:: InvoiceRequest ;
25
26
use crate :: routing:: router:: { BlindedTail , InFlightHtlcs , Path , PaymentParameters , Route , RouteParameters , Router } ;
26
27
use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
27
28
use crate :: util:: errors:: APIError ;
@@ -33,6 +34,7 @@ use crate::util::ser::ReadableArgs;
33
34
34
35
use core:: fmt:: { self , Display , Formatter } ;
35
36
use core:: ops:: Deref ;
37
+ use core:: sync:: atomic:: { AtomicBool , Ordering } ;
36
38
use core:: time:: Duration ;
37
39
38
40
use crate :: prelude:: * ;
@@ -54,6 +56,7 @@ pub(crate) enum PendingOutboundPayment {
54
56
expiration : StaleExpiration ,
55
57
retry_strategy : Retry ,
56
58
max_total_routing_fee_msat : Option < u64 > ,
59
+ invoice_request : Option < InvoiceRequest > ,
57
60
} ,
58
61
InvoiceReceived {
59
62
payment_hash : PaymentHash ,
@@ -679,13 +682,15 @@ pub(super) struct SendAlongPathArgs<'a> {
679
682
680
683
pub ( super ) struct OutboundPayments {
681
684
pub ( super ) pending_outbound_payments : Mutex < HashMap < PaymentId , PendingOutboundPayment > > ,
685
+ pub ( super ) awaiting_invoice_flag : AtomicBool ,
682
686
pub ( super ) retry_lock : Mutex < ( ) > ,
683
687
}
684
688
685
689
impl OutboundPayments {
686
690
pub ( super ) fn new ( ) -> Self {
687
691
Self {
688
692
pending_outbound_payments : Mutex :: new ( new_hash_map ( ) ) ,
693
+ awaiting_invoice_flag : AtomicBool :: new ( false ) ,
689
694
retry_lock : Mutex :: new ( ( ) ) ,
690
695
}
691
696
}
@@ -1337,7 +1342,7 @@ impl OutboundPayments {
1337
1342
1338
1343
pub ( super ) fn add_new_awaiting_invoice (
1339
1344
& self , payment_id : PaymentId , expiration : StaleExpiration , retry_strategy : Retry ,
1340
- max_total_routing_fee_msat : Option < u64 >
1345
+ max_total_routing_fee_msat : Option < u64 > , invoice_request : Option < InvoiceRequest >
1341
1346
) -> Result < ( ) , ( ) > {
1342
1347
let mut pending_outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
1343
1348
match pending_outbounds. entry ( payment_id) {
@@ -1347,6 +1352,7 @@ impl OutboundPayments {
1347
1352
expiration,
1348
1353
retry_strategy,
1349
1354
max_total_routing_fee_msat,
1355
+ invoice_request,
1350
1356
} ) ;
1351
1357
1352
1358
Ok ( ( ) )
@@ -1813,6 +1819,30 @@ impl OutboundPayments {
1813
1819
pub fn clear_pending_payments ( & self ) {
1814
1820
self . pending_outbound_payments . lock ( ) . unwrap ( ) . clear ( )
1815
1821
}
1822
+
1823
+ pub fn set_awaiting_invoice_flag ( & self ) {
1824
+ let pending_outbound_payments = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
1825
+ let has_invoice_requests = pending_outbound_payments. values ( ) . any ( |payment| {
1826
+ matches ! ( payment, PendingOutboundPayment :: AwaitingInvoice { invoice_request: Some ( _) , .. } )
1827
+ } ) ;
1828
+ self . awaiting_invoice_flag . store ( has_invoice_requests, Ordering :: SeqCst ) ;
1829
+ }
1830
+
1831
+ pub fn get_invoice_request_awaiting_invoice ( & self ) -> Vec < InvoiceRequest > {
1832
+ if !self . awaiting_invoice_flag . load ( Ordering :: SeqCst ) {
1833
+ return vec ! [ ] ;
1834
+ }
1835
+ let mut pending_outbound_payments = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
1836
+ let invoice_requests = pending_outbound_payments. values_mut ( )
1837
+ . filter_map ( |payment| match payment {
1838
+ PendingOutboundPayment :: AwaitingInvoice { invoice_request, .. } => invoice_request. take ( ) ,
1839
+ _ => None ,
1840
+ } )
1841
+ . collect ( ) ;
1842
+
1843
+ self . awaiting_invoice_flag . store ( false , Ordering :: SeqCst ) ;
1844
+ invoice_requests
1845
+ }
1816
1846
}
1817
1847
1818
1848
/// Returns whether a payment with the given [`PaymentHash`] and [`PaymentId`] is, in fact, a
@@ -1868,6 +1898,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
1868
1898
( 0 , expiration, required) ,
1869
1899
( 2 , retry_strategy, required) ,
1870
1900
( 4 , max_total_routing_fee_msat, option) ,
1901
+ ( 5 , invoice_request, option) ,
1871
1902
} ,
1872
1903
( 7 , InvoiceReceived ) => {
1873
1904
( 0 , payment_hash, required) ,
@@ -2106,7 +2137,7 @@ mod tests {
2106
2137
assert ! ( !outbound_payments. has_pending_payments( ) ) ;
2107
2138
assert ! (
2108
2139
outbound_payments. add_new_awaiting_invoice(
2109
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2140
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2110
2141
) . is_ok( )
2111
2142
) ;
2112
2143
assert ! ( outbound_payments. has_pending_payments( ) ) ;
@@ -2132,14 +2163,14 @@ mod tests {
2132
2163
2133
2164
assert ! (
2134
2165
outbound_payments. add_new_awaiting_invoice(
2135
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2166
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2136
2167
) . is_ok( )
2137
2168
) ;
2138
2169
assert ! ( outbound_payments. has_pending_payments( ) ) ;
2139
2170
2140
2171
assert ! (
2141
2172
outbound_payments. add_new_awaiting_invoice(
2142
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2173
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2143
2174
) . is_err( )
2144
2175
) ;
2145
2176
}
@@ -2155,7 +2186,7 @@ mod tests {
2155
2186
assert ! ( !outbound_payments. has_pending_payments( ) ) ;
2156
2187
assert ! (
2157
2188
outbound_payments. add_new_awaiting_invoice(
2158
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2189
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2159
2190
) . is_ok( )
2160
2191
) ;
2161
2192
assert ! ( outbound_payments. has_pending_payments( ) ) ;
@@ -2181,14 +2212,14 @@ mod tests {
2181
2212
2182
2213
assert ! (
2183
2214
outbound_payments. add_new_awaiting_invoice(
2184
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2215
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2185
2216
) . is_ok( )
2186
2217
) ;
2187
2218
assert ! ( outbound_payments. has_pending_payments( ) ) ;
2188
2219
2189
2220
assert ! (
2190
2221
outbound_payments. add_new_awaiting_invoice(
2191
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2222
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2192
2223
) . is_err( )
2193
2224
) ;
2194
2225
}
@@ -2203,7 +2234,7 @@ mod tests {
2203
2234
assert ! ( !outbound_payments. has_pending_payments( ) ) ;
2204
2235
assert ! (
2205
2236
outbound_payments. add_new_awaiting_invoice(
2206
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2237
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2207
2238
) . is_ok( )
2208
2239
) ;
2209
2240
assert ! ( outbound_payments. has_pending_payments( ) ) ;
@@ -2237,7 +2268,7 @@ mod tests {
2237
2268
2238
2269
assert ! (
2239
2270
outbound_payments. add_new_awaiting_invoice(
2240
- payment_id, expiration, Retry :: Attempts ( 0 ) , None
2271
+ payment_id, expiration, Retry :: Attempts ( 0 ) , None , None
2241
2272
) . is_ok( )
2242
2273
) ;
2243
2274
assert ! ( outbound_payments. has_pending_payments( ) ) ;
@@ -2301,7 +2332,7 @@ mod tests {
2301
2332
assert ! (
2302
2333
outbound_payments. add_new_awaiting_invoice(
2303
2334
payment_id, expiration, Retry :: Attempts ( 0 ) ,
2304
- Some ( invoice. amount_msats( ) / 100 + 50_000 )
2335
+ Some ( invoice. amount_msats( ) / 100 + 50_000 ) , None
2305
2336
) . is_ok( )
2306
2337
) ;
2307
2338
assert ! ( outbound_payments. has_pending_payments( ) ) ;
@@ -2401,7 +2432,7 @@ mod tests {
2401
2432
2402
2433
assert ! (
2403
2434
outbound_payments. add_new_awaiting_invoice(
2404
- payment_id, expiration, Retry :: Attempts ( 0 ) , Some ( 1234 )
2435
+ payment_id, expiration, Retry :: Attempts ( 0 ) , Some ( 1234 ) , None
2405
2436
) . is_ok( )
2406
2437
) ;
2407
2438
assert ! ( outbound_payments. has_pending_payments( ) ) ;
0 commit comments