From 1cd53ae53eb6c02caeea76ac55b7e85105bbbac5 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 2 Oct 2023 09:29:51 +1030 Subject: [PATCH] lightningd: fix watch on existing tx. We never do this, but we're about to (we always watch before we broadcast a tx). We use a `depth` member to avoid calling the callback multiple times for the same event, but we initialize it to 0. This means if we register a watch, and the first thing that happens is that it reorganizes out, we *don't* make the callback. Use an impossible value at initialization, instead. Signed-off-by: Rusty Russell --- lightningd/watch.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lightningd/watch.c b/lightningd/watch.c index f533044b5..32fab750c 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -59,7 +59,7 @@ struct txwatch { /* May be NULL if we haven't seen it yet. */ const struct bitcoin_tx *tx; - unsigned int depth; + int depth; /* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */ enum watch_result (*cb)(struct lightningd *ld, @@ -130,7 +130,7 @@ struct txwatch *watch_txid_(const tal_t *ctx, w = tal(ctx, struct txwatch); w->topo = topo; - w->depth = 0; + w->depth = -1; w->txid = *txid; w->tx = NULL; w->cb = cb; @@ -203,12 +203,19 @@ static bool txw_fire(struct txwatch *txw, if (depth == txw->depth) return false; - /* We assume zero depth signals a reorganization */ - log_debug(txw->topo->log, - "Got depth change %u->%u for %s%s", - txw->depth, depth, - type_to_string(tmpctx, struct bitcoin_txid, &txw->txid), - depth ? "" : " REORG"); + if (txw->depth == -1) { + log_debug(txw->topo->log, + "Got first depth change ->%u for %s", + txw->depth, + type_to_string(tmpctx, struct bitcoin_txid, &txw->txid)); + } else { + /* zero depth signals a reorganization */ + log_debug(txw->topo->log, + "Got depth change %u->%u for %s%s", + txw->depth, depth, + type_to_string(tmpctx, struct bitcoin_txid, &txw->txid), + depth ? "" : " REORG"); + } txw->depth = depth; r = txw->cb(txw->topo->bitcoind->ld, txid, txw->tx, txw->depth, txw->cbarg);