From c6ce35240413812a1dca6bc79f1872a2366b0fcc Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 29 Jan 2018 12:51:18 +1030 Subject: [PATCH] lightningd: actually unwatch txs when onchaind says we should. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 15 ++++++++++++++- lightningd/watch.c | 18 ++++++++++++++++++ lightningd/watch.h | 4 ++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 92ef7b23a..e84fb0202 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1193,7 +1193,20 @@ static void handle_onchain_broadcast_tx(struct peer *peer, const u8 *msg) static void handle_onchain_unwatch_tx(struct peer *peer, const u8 *msg) { - /* FIXME: unwatch tx and children here. */ + struct bitcoin_txid txid; + struct txwatch *txw; + + if (!fromwire_onchain_unwatch_tx(msg, NULL, &txid)) { + peer_internal_error(peer, "Invalid onchain_unwatch_tx"); + return; + } + + /* Frees the txo watches, too: see watch_tx_and_outputs() */ + txw = find_txwatch(peer->ld->topology, &txid, peer); + if (!txw) + log_unusual(peer->log, "Can't unwatch txid %s", + type_to_string(ltmp, struct bitcoin_txid, &txid)); + tal_free(txw); } static void handle_extracted_preimage(struct peer *peer, const u8 *msg) diff --git a/lightningd/watch.c b/lightningd/watch.c index d23499c6e..a843d9cae 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -112,6 +112,24 @@ struct txwatch *watch_txid_(const tal_t *ctx, return w; } +struct txwatch *find_txwatch(struct chain_topology *topo, + const struct bitcoin_txid *txid, + const struct peer *peer) +{ + struct txwatch_hash_iter i; + struct txwatch *w; + + /* We could have more than one peer watching same txid, though we + * don't for onchaind. */ + for (w = txwatch_hash_getfirst(&topo->txwatches, txid, &i); + w; + w = txwatch_hash_getnext(&topo->txwatches, txid, &i)) { + if (w->peer == peer) + break; + } + return w; +} + bool watching_txid(const struct chain_topology *topo, const struct bitcoin_txid *txid) { diff --git a/lightningd/watch.h b/lightningd/watch.h index e4ba2809a..06f8d76e6 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -134,6 +134,10 @@ struct txowatch *watch_txo_(const tal_t *ctx, const struct block *block), \ (cbdata)) +struct txwatch *find_txwatch(struct chain_topology *topo, + const struct bitcoin_txid *txid, + const struct peer *peer); + void txwatch_fire(struct chain_topology *topo, const struct bitcoin_tx *tx, unsigned int depth);