@@ -1138,16 +1138,19 @@ where
1138
1138
core:: mem:: take ( & mut self . pending_dns_onion_messages . lock ( ) . unwrap ( ) )
1139
1139
}
1140
1140
1141
- /// Sends out [`OfferPathsRequest`] onion messages if we are an often-offline recipient and are
1142
- /// configured to interactively build offers and static invoices with a static invoice server.
1141
+ /// Sends out [`OfferPathsRequest`] and [`ServeStaticInvoice`] onion messages if we are an
1142
+ /// often-offline recipient and are configured to interactively build offers and static invoices
1143
+ /// with a static invoice server.
1143
1144
///
1144
1145
/// Errors if we failed to create blinded reply paths when sending an [`OfferPathsRequest`] message.
1145
1146
#[ cfg( async_payments) ]
1146
- pub ( crate ) fn check_refresh_async_receive_offers < ES : Deref > (
1147
- & self , peers : Vec < MessageForwardNode > , entropy : ES ,
1147
+ pub ( crate ) fn check_refresh_async_receive_offers < ES : Deref , R : Deref > (
1148
+ & self , peers : Vec < MessageForwardNode > , usable_channels : Vec < ChannelDetails > , entropy : ES ,
1149
+ router : R ,
1148
1150
) -> Result < ( ) , ( ) >
1149
1151
where
1150
1152
ES :: Target : EntropySource ,
1153
+ R :: Target : Router ,
1151
1154
{
1152
1155
// Terminate early if this node does not intend to receive async payments.
1153
1156
if self . paths_to_static_invoice_server . is_empty ( ) {
@@ -1174,7 +1177,7 @@ where
1174
1177
path_absolute_expiry : duration_since_epoch
1175
1178
. saturating_add ( REPLY_PATH_RELATIVE_EXPIRY ) ,
1176
1179
} ) ;
1177
- let reply_paths = match self . create_blinded_paths ( peers, context) {
1180
+ let reply_paths = match self . create_blinded_paths ( peers. clone ( ) , context) {
1178
1181
Ok ( paths) => paths,
1179
1182
Err ( ( ) ) => {
1180
1183
return Err ( ( ) ) ;
@@ -1194,6 +1197,62 @@ where
1194
1197
) ;
1195
1198
}
1196
1199
1200
+ // If a static invoice server has persisted an offer for us but the corresponding invoice is
1201
+ // expiring soon, we need to refresh that invoice. Here we create the onion messages that will
1202
+ // be used to request invoice refresh, based on the offers provided by the cache.
1203
+ let mut serve_static_invoice_messages = Vec :: new ( ) ;
1204
+ {
1205
+ let cache = self . async_receive_offer_cache . lock ( ) . unwrap ( ) ;
1206
+ for offer_and_metadata in cache. offers_needing_invoice_refresh ( duration_since_epoch) {
1207
+ let ( offer, offer_nonce, offer_created_at, update_static_invoice_path) =
1208
+ offer_and_metadata;
1209
+ let offer_id = offer. id ( ) ;
1210
+
1211
+ let ( serve_invoice_msg, reply_path_ctx) = match self
1212
+ . create_serve_static_invoice_message (
1213
+ offer. clone ( ) ,
1214
+ offer_nonce,
1215
+ offer_created_at,
1216
+ peers. clone ( ) ,
1217
+ usable_channels. clone ( ) ,
1218
+ update_static_invoice_path. clone ( ) ,
1219
+ & * entropy,
1220
+ & * router,
1221
+ ) {
1222
+ Ok ( ( msg, ctx) ) => ( msg, ctx) ,
1223
+ Err ( ( ) ) => continue ,
1224
+ } ;
1225
+ serve_static_invoice_messages. push ( ( serve_invoice_msg, reply_path_ctx, offer_id) ) ;
1226
+ }
1227
+ }
1228
+
1229
+ // Enqueue the new serve_static_invoice messages in a separate loop to avoid holding the offer
1230
+ // cache lock and the pending_async_payments_messages lock at the same time.
1231
+ for ( serve_invoice_msg, reply_path_ctx, offer_id) in serve_static_invoice_messages {
1232
+ let context = MessageContext :: AsyncPayments ( reply_path_ctx) ;
1233
+ let reply_paths = match self . create_blinded_paths ( peers. clone ( ) , context) {
1234
+ Ok ( paths) => paths,
1235
+ Err ( ( ) ) => continue ,
1236
+ } ;
1237
+
1238
+ {
1239
+ // We can't fail past this point, so indicate to the cache that we've requested an invoice
1240
+ // update.
1241
+ let mut cache = self . async_receive_offer_cache . lock ( ) . unwrap ( ) ;
1242
+ if cache. increment_invoice_update_attempts ( offer_id) . is_err ( ) {
1243
+ continue ;
1244
+ }
1245
+ }
1246
+
1247
+ let message = AsyncPaymentsMessage :: ServeStaticInvoice ( serve_invoice_msg) ;
1248
+ enqueue_onion_message_with_reply_paths (
1249
+ message,
1250
+ & self . paths_to_static_invoice_server ,
1251
+ reply_paths,
1252
+ & mut self . pending_async_payments_messages . lock ( ) . unwrap ( ) ,
1253
+ ) ;
1254
+ }
1255
+
1197
1256
Ok ( ( ) )
1198
1257
}
1199
1258
0 commit comments