@@ -218,21 +218,64 @@ static enum watch_result onchain_tx_watched(struct lightningd *ld,
218218static void watch_tx_and_outputs (struct channel * channel ,
219219 const struct bitcoin_tx * tx );
220220
221+ static void replay_unwatch_txid (struct channel * channel ,
222+ const struct bitcoin_txid * txid )
223+ {
224+ replay_tx_hash_delkey (channel -> onchaind_replay_watches , txid );
225+ }
226+
227+ static void onchaind_spent_reply (struct subd * onchaind , const u8 * msg ,
228+ const int * fds ,
229+ struct bitcoin_txid * txid )
230+ {
231+ bool interested ;
232+ struct txwatch * txw ;
233+ struct channel * channel = onchaind -> channel ;
234+
235+ if (!fromwire_onchaind_spent_reply (msg , & interested ))
236+ channel_internal_error (channel , "Invalid onchaind_spent_reply %s" ,
237+ tal_hex (tmpctx , msg ));
238+
239+ /* Only delete watch if it says it doesn't care */
240+ if (interested )
241+ return ;
242+
243+ /* If we're doing replay: */
244+ if (channel -> onchaind_replay_watches ) {
245+ replay_unwatch_txid (channel , txid );
246+ return ;
247+ }
248+
249+ /* Frees the txo watches, too: see watch_tx_and_outputs() */
250+ txw = find_txwatch (channel -> peer -> ld -> topology , txid ,
251+ onchain_tx_watched , channel );
252+ if (!txw )
253+ log_unusual (channel -> log , "Can't unwatch txid %s" ,
254+ fmt_bitcoin_txid (tmpctx , txid ));
255+ tal_free (txw );
256+ }
257+
221258/**
222259 * Notify onchaind that an output was spent and register new watches.
223260 */
224261static void onchain_txo_spent (struct channel * channel , const struct bitcoin_tx * tx , size_t input_num , u32 blockheight )
225262{
226263 u8 * msg ;
264+ struct bitcoin_txid * txid ;
227265 /* Onchaind needs all inputs, since it uses those to compare
228266 * with existing spends (which can vary, with feerate changes). */
229267 struct tx_parts * parts = tx_parts_from_wally_tx (tmpctx , tx -> wtx ,
230268 -1 , -1 );
231269
232270 watch_tx_and_outputs (channel , tx );
233271
272+ /* Reply will need this if we want to unwatch */
273+ txid = tal (NULL , struct bitcoin_txid );
274+ bitcoin_txid (tx , txid );
275+
234276 msg = towire_onchaind_spent (channel , parts , input_num , blockheight );
235- subd_send_msg (channel -> owner , take (msg ));
277+ subd_req (channel -> owner , channel -> owner , take (msg ), -1 , 0 ,
278+ onchaind_spent_reply , take (txid ));
236279
237280}
238281
@@ -304,12 +347,6 @@ static void replay_watch_tx(struct channel *channel,
304347 replay_tx_hash_add (channel -> onchaind_replay_watches , rtx );
305348}
306349
307- static void replay_unwatch_txid (struct channel * channel ,
308- const struct bitcoin_txid * txid )
309- {
310- replay_tx_hash_delkey (channel -> onchaind_replay_watches , txid );
311- }
312-
313350/* We've finished replaying, turn any txs left into live watches */
314351static void convert_replay_txs (struct channel * channel )
315352{
@@ -372,31 +409,6 @@ static void replay_block(struct bitcoind *bitcoind,
372409 bitcoind_getrawblockbyheight (channel , bitcoind , height + 1 , replay_block , channel );
373410}
374411
375- static void handle_onchain_unwatch_tx (struct channel * channel , const u8 * msg )
376- {
377- struct bitcoin_txid txid ;
378- struct txwatch * txw ;
379-
380- if (!fromwire_onchaind_unwatch_tx (msg , & txid )) {
381- channel_internal_error (channel , "Invalid onchain_unwatch_tx" );
382- return ;
383- }
384-
385- /* If we're doing replay: */
386- if (channel -> onchaind_replay_watches ) {
387- replay_unwatch_txid (channel , & txid );
388- return ;
389- }
390-
391- /* Frees the txo watches, too: see watch_tx_and_outputs() */
392- txw = find_txwatch (channel -> peer -> ld -> topology , & txid ,
393- onchain_tx_watched , channel );
394- if (!txw )
395- log_unusual (channel -> log , "Can't unwatch txid %s" ,
396- fmt_bitcoin_txid (tmpctx , & txid ));
397- tal_free (txw );
398- }
399-
400412static void handle_extracted_preimage (struct channel * channel , const u8 * msg )
401413{
402414 struct preimage preimage ;
@@ -1545,10 +1557,6 @@ static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds U
15451557 handle_onchain_init_reply (sd -> channel , msg );
15461558 break ;
15471559
1548- case WIRE_ONCHAIND_UNWATCH_TX :
1549- handle_onchain_unwatch_tx (sd -> channel , msg );
1550- break ;
1551-
15521560 case WIRE_ONCHAIND_EXTRACTED_PREIMAGE :
15531561 handle_extracted_preimage (sd -> channel , msg );
15541562 break ;
@@ -1614,6 +1622,7 @@ static unsigned int onchain_msg(struct subd *sd, const u8 *msg, const int *fds U
16141622 case WIRE_ONCHAIND_SPEND_CREATED :
16151623 case WIRE_ONCHAIND_DEV_MEMLEAK :
16161624 case WIRE_ONCHAIND_DEV_MEMLEAK_REPLY :
1625+ case WIRE_ONCHAIND_SPENT_REPLY :
16171626 break ;
16181627 }
16191628
0 commit comments