From 05f12edf60dcf11809716feff8a7337c07912e9c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 13 Aug 2018 12:35:33 +0930 Subject: [PATCH] txwatch: hand ld to callback, don't assume channel is non-NULL. We're about to use the txwatch facility for UTXOs, where there's no channel, so allow that the be NULL, and hand the struct lightningd which callers want anyway. Signed-off-by: Rusty Russell --- lightningd/onchain_control.c | 7 ++++--- lightningd/peer_control.c | 4 ++-- lightningd/watch.c | 29 +++++++++++++++++++---------- lightningd/watch.h | 13 ++++++++----- wallet/test/run-wallet.c | 5 +++-- 5 files changed, 36 insertions(+), 22 deletions(-) diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 1065ae71e..35b5b0812 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -75,11 +75,12 @@ static void onchain_tx_depth(struct channel *channel, /** * Entrypoint for the txwatch callback, calls onchain_tx_depth. */ -static enum watch_result onchain_tx_watched(struct channel *channel, +static enum watch_result onchain_tx_watched(struct lightningd *ld, + struct channel *channel, const struct bitcoin_txid *txid, unsigned int depth) { - u32 blockheight = channel->peer->ld->topology->tip->height; + u32 blockheight = get_block_height(ld->topology); if (depth == 0) { log_unusual(channel->log, "Chain reorganization!"); channel_set_owner(channel, NULL, false); @@ -93,7 +94,7 @@ static enum watch_result onchain_tx_watched(struct channel *channel, } /* Store the channeltx so we can replay later */ - wallet_channeltxs_add(channel->peer->ld->wallet, channel, + wallet_channeltxs_add(ld->wallet, channel, WIRE_ONCHAIN_DEPTH, txid, 0, blockheight); onchain_tx_depth(channel, txid, depth); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index c8f0138df..8bc05aede 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -508,12 +508,12 @@ send_error: peer_start_openingd(peer, &cs, peer_fd, gossip_fd, error); } -static enum watch_result funding_lockin_cb(struct channel *channel, +static enum watch_result funding_lockin_cb(struct lightningd *ld, + struct channel *channel, const struct bitcoin_txid *txid, unsigned int depth) { const char *txidstr; - struct lightningd *ld = channel->peer->ld; txidstr = type_to_string(channel, struct bitcoin_txid, txid); log_debug(channel->log, "Funding tx %s depth %u of %u", diff --git a/lightningd/watch.c b/lightningd/watch.c index b79d0bb72..d4f1e4b3c 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -58,7 +58,7 @@ struct txowatch { struct txwatch { struct chain_topology *topo; - /* Channel who owns us. */ + /* Channel who owns us (or NULL, for wallet usage). */ struct channel *channel; /* Transaction to watch. */ @@ -66,7 +66,8 @@ struct txwatch { unsigned int depth; /* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */ - enum watch_result (*cb)(struct channel *channel, + enum watch_result (*cb)(struct lightningd *ld, + struct channel *channel, const struct bitcoin_txid *txid, unsigned int depth); }; @@ -121,9 +122,10 @@ struct txwatch *watch_txid(const tal_t *ctx, struct chain_topology *topo, struct channel *channel, const struct bitcoin_txid *txid, - enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_txid *, - unsigned int depth)) + enum watch_result (*cb)(struct lightningd *ld, + struct channel *channel, + const struct bitcoin_txid *, + unsigned int depth)) { struct txwatch *w; @@ -168,9 +170,10 @@ struct txwatch *watch_tx(const tal_t *ctx, struct chain_topology *topo, struct channel *channel, const struct bitcoin_tx *tx, - enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_txid *, - unsigned int depth)) + enum watch_result (*cb)(struct lightningd *ld, + struct channel *channel, + const struct bitcoin_txid *, + unsigned int depth)) { struct bitcoin_txid txid; @@ -208,15 +211,21 @@ static bool txw_fire(struct txwatch *txw, unsigned int depth) { enum watch_result r; + struct log *log; if (depth == txw->depth) return false; - log_debug(txw->channel->log, + if (txw->channel) + log = txw->channel->log; + else + log = txw->topo->log; + + log_debug(log, "Got depth change %u->%u for %s", txw->depth, depth, type_to_string(tmpctx, struct bitcoin_txid, &txw->txid)); txw->depth = depth; - r = txw->cb(txw->channel, txid, txw->depth); + r = txw->cb(txw->topo->bitcoind->ld, txw->channel, txid, txw->depth); switch (r) { case DELETE_WATCH: tal_free(txw); diff --git a/lightningd/watch.h b/lightningd/watch.h index f7dc211b5..a5e97ad9f 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -12,6 +12,7 @@ struct bitcoin_tx; struct block; struct channel; struct chain_topology; +struct lightningd; struct txowatch; struct txwatch; @@ -43,17 +44,19 @@ struct txwatch *watch_txid(const tal_t *ctx, struct chain_topology *topo, struct channel *channel, const struct bitcoin_txid *txid, - enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_txid *, + enum watch_result (*cb)(struct lightningd *ld, + struct channel *channel, + const struct bitcoin_txid *, unsigned int depth)); struct txwatch *watch_tx(const tal_t *ctx, struct chain_topology *topo, struct channel *channel, const struct bitcoin_tx *tx, - enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_txid *, - unsigned int depth)); + enum watch_result (*cb)(struct lightningd *ld, + struct channel *channel, + const struct bitcoin_txid *, + unsigned int depth)); struct txowatch *watch_txo(const tal_t *ctx, struct chain_topology *topo, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 21da2de62..9c8cbf5c7 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -362,8 +362,9 @@ struct txwatch *watch_txid(const tal_t *ctx UNNEEDED, struct chain_topology *topo UNNEEDED, struct channel *channel UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, - enum watch_result (*cb)(struct channel *channel UNNEEDED, - const struct bitcoin_txid * UNNEEDED, + enum watch_result (*cb)(struct lightningd *ld UNNEEDED, + struct channel *channel UNNEEDED, + const struct bitcoin_txid * UNNEEDED, unsigned int depth)) { fprintf(stderr, "watch_txid called!\n"); abort(); } /* Generated stub for watch_txo */