@@ -888,8 +888,8 @@ static void updates_complete(struct chain_topology *topo)
888888}
889889
890890static void record_wallet_spend (struct lightningd * ld ,
891- struct bitcoin_outpoint * outpoint ,
892- struct bitcoin_txid * txid ,
891+ const struct bitcoin_outpoint * outpoint ,
892+ const struct bitcoin_txid * txid ,
893893 u32 tx_blockheight )
894894{
895895 struct utxo * utxo ;
@@ -911,31 +911,34 @@ static void record_wallet_spend(struct lightningd *ld,
911911/**
912912 * topo_update_spends -- Tell the wallet about all spent outpoints
913913 */
914- static void topo_update_spends (struct chain_topology * topo , struct block * b )
914+ static void topo_update_spends (struct chain_topology * topo ,
915+ struct bitcoin_tx * * txs ,
916+ const struct bitcoin_txid * txids ,
917+ u32 blockheight )
915918{
916919 const struct short_channel_id * spent_scids ;
917- const size_t num_txs = tal_count (b -> full_txs );
920+ const size_t num_txs = tal_count (txs );
918921 for (size_t i = 0 ; i < num_txs ; i ++ ) {
919- const struct bitcoin_tx * tx = b -> full_txs [i ];
922+ const struct bitcoin_tx * tx = txs [i ];
920923
921924 for (size_t j = 0 ; j < tx -> wtx -> num_inputs ; j ++ ) {
922925 struct bitcoin_outpoint outpoint ;
923926
924927 bitcoin_tx_input_get_outpoint (tx , j , & outpoint );
925928
926929 if (wallet_outpoint_spend (tmpctx , topo -> ld -> wallet ,
927- b -> height , & outpoint ))
930+ blockheight , & outpoint ))
928931 record_wallet_spend (topo -> ld , & outpoint ,
929- & b -> txids [i ], b -> height );
932+ & txids [i ], blockheight );
930933
931934 }
932935 }
933936
934937 /* Retrieve all potential channel closes from the UTXO set and
935938 * tell gossipd about them. */
936939 spent_scids =
937- wallet_utxoset_get_spent (tmpctx , topo -> ld -> wallet , b -> height );
938- gossipd_notify_spends (topo -> bitcoind -> ld , b -> height , spent_scids );
940+ wallet_utxoset_get_spent (tmpctx , topo -> ld -> wallet , blockheight );
941+ gossipd_notify_spends (topo -> bitcoind -> ld , blockheight , spent_scids );
939942}
940943
941944static void topo_add_utxos (struct chain_topology * topo , struct block * b )
@@ -982,7 +985,7 @@ static void add_tip(struct chain_topology *topo, struct block *b)
982985 trace_span_end (b );
983986
984987 trace_span_start ("topo_update_spends" , b );
985- topo_update_spends (topo , b );
988+ topo_update_spends (topo , b -> full_txs , b -> txids , b -> height );
986989 trace_span_end (b );
987990
988991 /* Only keep the transactions we care about. */
@@ -1388,6 +1391,7 @@ void setup_topology(struct chain_topology *topo)
13881391 struct bitcoin_block * blk ;
13891392 bool blockscan_start_set ;
13901393 u32 blockscan_start ;
1394+ s64 fixup ;
13911395
13921396 /* This waits for bitcoind. */
13931397 bitcoind_check_commands (topo -> bitcoind );
@@ -1413,6 +1417,15 @@ void setup_topology(struct chain_topology *topo)
14131417 blockscan_start = blocknum_reduce (blockscan_start , topo -> ld -> config .rescan );
14141418 }
14151419
1420+ fixup = db_get_intvar (topo -> ld -> wallet -> db , "fixup_block_scan" , -1 );
1421+ if (fixup == -1 ) {
1422+ /* Never done fixup: this is set to non-zero if we have blocks. */
1423+ topo -> old_block_scan = wallet_blocks_minheight (topo -> ld -> wallet );
1424+ db_set_intvar (topo -> ld -> wallet -> db , "fixup_block_scan" ,
1425+ topo -> old_block_scan );
1426+ } else {
1427+ topo -> old_block_scan = fixup ;
1428+ }
14161429 db_commit_transaction (topo -> ld -> wallet -> db );
14171430
14181431 /* Sanity checks, then topology initialization. */
@@ -1509,6 +1522,36 @@ void setup_topology(struct chain_topology *topo)
15091522 tal_add_destructor (topo , destroy_chain_topology );
15101523}
15111524
1525+ static void fixup_scan_block (struct bitcoind * bitcoind ,
1526+ u32 height ,
1527+ struct bitcoin_blkid * blkid ,
1528+ struct bitcoin_block * blk ,
1529+ struct chain_topology * topo )
1530+ {
1531+ log_debug (topo -> ld -> log , "fixup_scan: block %u with %zu txs" , height , tal_count (blk -> tx ));
1532+ topo_update_spends (topo , blk -> tx , blk -> txids , height );
1533+
1534+ /* Caught up. */
1535+ if (height == get_block_height (topo )) {
1536+ log_info (topo -> ld -> log , "Scanning for missed UTXOs finished" );
1537+ db_set_intvar (topo -> ld -> wallet -> db , "fixup_block_scan" , 0 );
1538+ return ;
1539+ }
1540+
1541+ db_set_intvar (topo -> ld -> wallet -> db , "fixup_block_scan" , ++ topo -> old_block_scan );
1542+ bitcoind_getrawblockbyheight (topo , topo -> bitcoind ,
1543+ topo -> old_block_scan ,
1544+ fixup_scan_block , topo );
1545+ }
1546+
1547+ static void fixup_scan (struct chain_topology * topo )
1548+ {
1549+ log_info (topo -> ld -> log , "Scanning for missed UTXOs from block %u" , topo -> old_block_scan );
1550+ bitcoind_getrawblockbyheight (topo , topo -> bitcoind ,
1551+ topo -> old_block_scan ,
1552+ fixup_scan_block , topo );
1553+ }
1554+
15121555void begin_topology (struct chain_topology * topo )
15131556{
15141557 /* If we were not synced, start looping to check */
@@ -1518,6 +1561,9 @@ void begin_topology(struct chain_topology *topo)
15181561 start_fee_estimate (topo );
15191562 /* Regular block updates */
15201563 try_extend_tip (topo );
1564+
1565+ if (topo -> old_block_scan )
1566+ fixup_scan (topo );
15211567}
15221568
15231569void stop_topology (struct chain_topology * topo )
0 commit comments