@@ -336,7 +336,7 @@ static void convert_replay_txs(struct channel *channel)
336
336
}
337
337
}
338
338
339
- static UNNEEDED void replay_block (struct bitcoind * bitcoind ,
339
+ static void replay_block (struct bitcoind * bitcoind ,
340
340
u32 height ,
341
341
struct bitcoin_blkid * blkid ,
342
342
struct bitcoin_block * blk ,
@@ -1781,51 +1781,56 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
1781
1781
feerate_min (ld , NULL ));
1782
1782
subd_send_msg (channel -> owner , take (msg ));
1783
1783
1784
- watch_tx_and_outputs (channel , tx );
1784
+ /* If we're replaying, we just watch this */
1785
+ if (channel -> onchaind_replay_watches ) {
1786
+ replay_watch_tx (channel , blockheight , tx );
1787
+ } else {
1788
+ watch_tx_and_outputs (channel , tx );
1789
+ }
1785
1790
1786
1791
/* We keep watching until peer finally deleted, for reorgs. */
1787
1792
return KEEP_WATCHING ;
1788
1793
}
1789
1794
1790
1795
void onchaind_replay_channels (struct lightningd * ld )
1791
1796
{
1792
- u32 * onchaind_ids ;
1793
- struct channeltx * txs ;
1794
- struct channel * chan ;
1797
+ struct peer * peer ;
1798
+ struct peer_node_id_map_iter it ;
1795
1799
1800
+ /* We don't hold a db tx for all of init */
1796
1801
db_begin_transaction (ld -> wallet -> db );
1797
- onchaind_ids = wallet_onchaind_channels (tmpctx , ld -> wallet );
1798
-
1799
- for (size_t i = 0 ; i < tal_count (onchaind_ids ); i ++ ) {
1800
- log_info (ld -> log , "Restarting onchaind for channel %d" ,
1801
- onchaind_ids [i ]);
1802
-
1803
- txs = wallet_channeltxs_get (onchaind_ids , ld -> wallet ,
1804
- onchaind_ids [i ]);
1805
- chan = channel_by_dbid (ld , onchaind_ids [i ]);
1806
-
1807
- for (size_t j = 0 ; j < tal_count (txs ); j ++ ) {
1808
- if (txs [j ].type == WIRE_ONCHAIND_INIT ) {
1809
- onchaind_funding_spent (chan , txs [j ].tx ,
1810
- txs [j ].blockheight );
1811
-
1812
- } else if (txs [j ].type == WIRE_ONCHAIND_SPENT ) {
1813
- onchain_txo_spent (chan , txs [j ].tx ,
1814
- txs [j ].input_num ,
1815
- txs [j ].blockheight );
1816
-
1817
- } else if (txs [j ].type == WIRE_ONCHAIND_DEPTH ) {
1818
- onchain_tx_depth (chan , & txs [j ].txid ,
1819
- txs [j ].depth );
1820
-
1821
- } else {
1822
- fatal ("unknown message of type %d during "
1823
- "onchaind replay" ,
1824
- txs [j ].type );
1825
- }
1802
+
1803
+ /* For each channel, if we've recorded a spend, it's onchaind time! */
1804
+ for (peer = peer_node_id_map_first (ld -> peers , & it );
1805
+ peer ;
1806
+ peer = peer_node_id_map_next (ld -> peers , & it )) {
1807
+ struct channel * channel ;
1808
+
1809
+ list_for_each (& peer -> channels , channel , list ) {
1810
+ struct bitcoin_tx * tx ;
1811
+ u32 blockheight ;
1812
+
1813
+ if (channel_state_uncommitted (channel -> state ))
1814
+ continue ;
1815
+
1816
+ tx = wallet_get_funding_spend (tmpctx , ld -> wallet , channel -> dbid ,
1817
+ & blockheight );
1818
+ if (!tx )
1819
+ continue ;
1820
+
1821
+ log_info (channel -> log ,
1822
+ "Restarting onchaind (%s): closed in block %u" ,
1823
+ channel_state_name (channel ), blockheight );
1824
+
1825
+ /* We're in replay mode */
1826
+ channel -> onchaind_replay_watches = tal (channel , struct replay_tx_hash );
1827
+ replay_tx_hash_init (channel -> onchaind_replay_watches );
1828
+
1829
+ 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 );
1826
1833
}
1827
- tal_free (txs );
1828
1834
}
1829
-
1830
1835
db_commit_transaction (ld -> wallet -> db );
1831
1836
}
0 commit comments