@@ -217,6 +217,7 @@ static enum watch_result onchain_tx_watched(struct lightningd *ld,
217
217
218
218
static void watch_tx_and_outputs (struct channel * channel ,
219
219
const struct bitcoin_tx * tx );
220
+ static void onchaind_replay (struct channel * channel );
220
221
221
222
static void replay_unwatch_txid (struct channel * channel ,
222
223
const struct bitcoin_txid * txid )
@@ -236,14 +237,16 @@ static void onchaind_spent_reply(struct subd *onchaind, const u8 *msg,
236
237
channel_internal_error (channel , "Invalid onchaind_spent_reply %s" ,
237
238
tal_hex (tmpctx , msg ));
238
239
240
+ channel -> num_onchain_spent_calls -- ;
241
+
239
242
/* Only delete watch if it says it doesn't care */
240
243
if (interested )
241
- return ;
244
+ goto out ;
242
245
243
246
/* If we're doing replay: */
244
247
if (channel -> onchaind_replay_watches ) {
245
248
replay_unwatch_txid (channel , txid );
246
- return ;
249
+ goto out ;
247
250
}
248
251
249
252
/* Frees the txo watches, too: see watch_tx_and_outputs() */
@@ -253,6 +256,13 @@ static void onchaind_spent_reply(struct subd *onchaind, const u8 *msg,
253
256
log_unusual (channel -> log , "Can't unwatch txid %s" ,
254
257
fmt_bitcoin_txid (tmpctx , txid ));
255
258
tal_free (txw );
259
+
260
+ out :
261
+ /* If that's the last request, continue asking for blocks */
262
+ if (channel -> onchaind_replay_watches
263
+ && channel -> num_onchain_spent_calls == 0 ) {
264
+ onchaind_replay (channel );
265
+ }
256
266
}
257
267
258
268
/**
@@ -276,7 +286,7 @@ static void onchain_txo_spent(struct channel *channel, const struct bitcoin_tx *
276
286
msg = towire_onchaind_spent (channel , parts , input_num , blockheight );
277
287
subd_req (channel -> owner , channel -> owner , take (msg ), -1 , 0 ,
278
288
onchaind_spent_reply , take (txid ));
279
-
289
+ channel -> num_onchain_spent_calls ++ ;
280
290
}
281
291
282
292
/**
@@ -405,8 +415,24 @@ static void replay_block(struct bitcoind *bitcoind,
405
415
return ;
406
416
}
407
417
408
- /* Otherwise, loop on next block. */
409
- bitcoind_getrawblockbyheight (channel , bitcoind , height + 1 , replay_block , channel );
418
+ /* Ready for next block */
419
+ channel -> onchaind_replay_height = height + 1 ;
420
+
421
+ /* Otherwise, wait for those to be resolved (in case onchaind is slow,
422
+ * e.g. waiting for HSM). */
423
+ if (channel -> num_onchain_spent_calls == 0 )
424
+ onchaind_replay (channel );
425
+ }
426
+
427
+ static void onchaind_replay (struct channel * channel )
428
+ {
429
+ assert (channel -> onchaind_replay_watches );
430
+ assert (channel -> num_onchain_spent_calls == 0 );
431
+
432
+ bitcoind_getrawblockbyheight (channel ,
433
+ channel -> peer -> ld -> topology -> bitcoind ,
434
+ channel -> onchaind_replay_height ,
435
+ replay_block , channel );
410
436
}
411
437
412
438
static void handle_extracted_preimage (struct channel * channel , const u8 * msg )
@@ -1824,12 +1850,11 @@ void onchaind_replay_channels(struct lightningd *ld)
1824
1850
1825
1851
/* We're in replay mode */
1826
1852
channel -> onchaind_replay_watches = tal (channel , struct replay_tx_hash );
1853
+ channel -> onchaind_replay_height = blockheight ;
1827
1854
replay_tx_hash_init (channel -> onchaind_replay_watches );
1828
1855
1829
1856
onchaind_funding_spent (channel , tx , blockheight );
1830
- /* Ask bitcoind to start grabbing those blocks for replay */
1831
- bitcoind_getrawblockbyheight (channel , ld -> topology -> bitcoind , blockheight ,
1832
- replay_block , channel );
1857
+ onchaind_replay (channel );
1833
1858
}
1834
1859
}
1835
1860
db_commit_transaction (ld -> wallet -> db );
0 commit comments