2626#include <wally_psbt.h>
2727#include <wire/wire_sync.h>
2828
29+ /* If we're restarting, we keep a per-channel copy of watches, and replay */
30+ struct replay_tx {
31+ u32 blockheight ;
32+ struct bitcoin_txid txid ;
33+ struct bitcoin_tx * tx ;
34+ };
35+
36+ static const struct bitcoin_txid * replay_tx_keyof (const struct replay_tx * rtx )
37+ {
38+ return & rtx -> txid ;
39+ }
40+
41+ static bool replay_tx_eq_txid (const struct replay_tx * rtx ,
42+ const struct bitcoin_txid * txid )
43+ {
44+ return bitcoin_txid_eq (& rtx -> txid , txid );
45+ }
46+
47+ HTABLE_DEFINE_TYPE (struct replay_tx , replay_tx_keyof , txid_hash , replay_tx_eq_txid ,
48+ replay_tx_hash );
49+
2950/* We dump all the known preimages when onchaind starts up. */
3051static void onchaind_tell_fulfill (struct channel * channel )
3152{
@@ -280,6 +301,86 @@ static void handle_onchain_log_coin_move(struct channel *channel, const u8 *msg)
280301 tal_free (mvt );
281302}
282303
304+ static void replay_watch_tx (struct channel * channel ,
305+ u32 blockheight ,
306+ const struct bitcoin_tx * tx TAKES )
307+ {
308+ struct replay_tx * rtx = tal (channel -> onchaind_replay_watches , struct replay_tx );
309+ bitcoin_txid (tx , & rtx -> txid );
310+ rtx -> blockheight = blockheight ;
311+ rtx -> tx = clone_bitcoin_tx (rtx , tx );
312+
313+ replay_tx_hash_add (channel -> onchaind_replay_watches , rtx );
314+ }
315+
316+ static void replay_unwatch_txid (struct channel * channel ,
317+ const struct bitcoin_txid * txid )
318+ {
319+ replay_tx_hash_delkey (channel -> onchaind_replay_watches , txid );
320+ }
321+
322+ /* We've finished replaying, turn any txs left into live watches */
323+ static void convert_replay_txs (struct channel * channel )
324+ {
325+ struct replay_tx * rtx ;
326+ struct replay_tx_hash_iter rit ;
327+ struct replay_tx_hash * watches ;
328+
329+ /* Set to NULL so these are queued as real watches */
330+ watches = tal_steal (tmpctx , channel -> onchaind_replay_watches );
331+ channel -> onchaind_replay_watches = NULL ;
332+ for (rtx = replay_tx_hash_first (watches , & rit );
333+ rtx ;
334+ rtx = replay_tx_hash_next (watches , & rit )) {
335+ watch_tx_and_outputs (channel , rtx -> tx );
336+ }
337+ }
338+
339+ static UNNEEDED void replay_block (struct bitcoind * bitcoind ,
340+ u32 height ,
341+ struct bitcoin_blkid * blkid ,
342+ struct bitcoin_block * blk ,
343+ struct channel * channel )
344+ {
345+ struct replay_tx * rtx ;
346+ struct replay_tx_hash_iter rit ;
347+
348+ /* Tell onchaind that all existing txs have reached a new depth */
349+ for (rtx = replay_tx_hash_first (channel -> onchaind_replay_watches , & rit );
350+ rtx ;
351+ rtx = replay_tx_hash_next (channel -> onchaind_replay_watches , & rit )) {
352+ /* Note: if you're in this block, that's depth 1! */
353+ onchain_tx_depth (channel , & rtx -> txid , height - rtx -> blockheight + 1 );
354+ }
355+
356+ /* See if we add any new txs which spend a watched one */
357+ for (size_t i = 0 ; i < tal_count (blk -> tx ); i ++ ) {
358+ for (size_t j = 0 ; j < blk -> tx [i ]-> wtx -> num_inputs ; j ++ ) {
359+ struct bitcoin_txid spent ;
360+ bitcoin_tx_input_get_txid (blk -> tx [i ], j , & spent );
361+ rtx = replay_tx_hash_get (channel -> onchaind_replay_watches , & spent );
362+ if (rtx ) {
363+ /* Note: for efficiency, blk->tx's don't have
364+ * PSBTs, so add one now */
365+ if (!blk -> tx [i ]-> psbt )
366+ blk -> tx [i ]-> psbt = new_psbt (blk -> tx [i ], blk -> tx [i ]-> wtx );
367+ onchain_txo_spent (channel , blk -> tx [i ], j , height );
368+ /* Watch this and all the children too. */
369+ replay_watch_tx (channel , height , blk -> tx [i ]);
370+ }
371+ }
372+ }
373+
374+ /* Replay finished? Now we'll get fed real blocks */
375+ if (height == get_block_height (bitcoind -> ld -> topology )) {
376+ convert_replay_txs (channel );
377+ return ;
378+ }
379+
380+ /* Otherwise, loop on next block. */
381+ bitcoind_getrawblockbyheight (channel , bitcoind , height + 1 , replay_block , channel );
382+ }
383+
283384static void handle_onchain_unwatch_tx (struct channel * channel , const u8 * msg )
284385{
285386 struct bitcoin_txid txid ;
@@ -290,6 +391,12 @@ static void handle_onchain_unwatch_tx(struct channel *channel, const u8 *msg)
290391 return ;
291392 }
292393
394+ /* If we're doing replay: */
395+ if (channel -> onchaind_replay_watches ) {
396+ replay_unwatch_txid (channel , & txid );
397+ return ;
398+ }
399+
293400 /* Frees the txo watches, too: see watch_tx_and_outputs() */
294401 txw = find_txwatch (channel -> peer -> ld -> topology , & txid ,
295402 onchain_tx_watched , channel );
0 commit comments