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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2023-10-02 09:29:51 +10:30
parent eb1ef40f96
commit 1cd53ae53e

View File

@@ -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);