From aa696370affb09792aef74b1b4d3239027c84072 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 9 Apr 2018 15:20:54 +0200 Subject: [PATCH] txwatch: Switch to passing only txid into the depth callbacks All of the callback functions were only using the tx to generate the txid again, so we just pass that in directly and save passing the tx itself. This is a simplification to move to the DB backed depth callbacks. It'd be rather wasteful to read the rawtx and deserialize just to serialize right away again to find the txid, when we already searched the DB for exactly that txid. Signed-off-by: Christian Decker --- lightningd/onchain_control.c | 6 ++---- lightningd/peer_control.c | 14 ++++++-------- lightningd/watch.c | 14 +++++++------- lightningd/watch.h | 4 ++-- wallet/test/run-wallet.c | 2 +- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 10a97a9d0..293af7c8a 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -57,11 +57,10 @@ static void handle_onchain_init_reply(struct channel *channel, const u8 *msg) } static enum watch_result onchain_tx_watched(struct channel *channel, - const struct bitcoin_tx *tx, + const struct bitcoin_txid *txid, unsigned int depth) { u8 *msg; - struct bitcoin_txid txid; if (depth == 0) { log_unusual(channel->log, "Chain reorganization!"); @@ -75,8 +74,7 @@ static enum watch_result onchain_tx_watched(struct channel *channel, return KEEP_WATCHING; } - bitcoin_txid(tx, &txid); - msg = towire_onchain_depth(channel, &txid, depth); + msg = towire_onchain_depth(channel, txid, depth); subd_send_msg(channel->owner, take(msg)); return KEEP_WATCHING; } diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 08387018b..819a8bc8b 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -478,7 +478,7 @@ send_error: } static enum watch_result funding_announce_cb(struct channel *channel, - const struct bitcoin_tx *tx UNUSED, + const struct bitcoin_txid *txid UNUSED, unsigned int depth) { if (depth < ANNOUNCE_MIN_DEPTH) { @@ -500,17 +500,15 @@ static enum watch_result funding_announce_cb(struct channel *channel, } static enum watch_result funding_lockin_cb(struct channel *channel, - const struct bitcoin_tx *tx, + const struct bitcoin_txid *txid, unsigned int depth) { - struct bitcoin_txid txid; const char *txidstr; struct txlocator *loc; bool channel_ready; struct lightningd *ld = channel->peer->ld; - bitcoin_txid(tx, &txid); - txidstr = type_to_string(channel, struct bitcoin_txid, &txid); + txidstr = type_to_string(channel, struct bitcoin_txid, txid); log_debug(channel->log, "Funding tx %s depth %u of %u", txidstr, depth, channel->minimum_depth); tal_free(txidstr); @@ -518,7 +516,7 @@ static enum watch_result funding_lockin_cb(struct channel *channel, if (depth < channel->minimum_depth) return KEEP_WATCHING; - loc = locate_tx(channel, ld->topology, &txid); + loc = locate_tx(channel, ld->topology, txid); /* If we restart, we could already have peer->scid from database */ if (!channel->scid) { @@ -558,9 +556,9 @@ static enum watch_result funding_lockin_cb(struct channel *channel, * before. If we are at the right depth, call the callback * directly, otherwise schedule a callback */ if (depth >= ANNOUNCE_MIN_DEPTH) - funding_announce_cb(channel, tx, depth); + funding_announce_cb(channel, txid, depth); else - watch_txid(channel, ld->topology, channel, &txid, + watch_txid(channel, ld->topology, channel, txid, funding_announce_cb); return DELETE_WATCH; } diff --git a/lightningd/watch.c b/lightningd/watch.c index 1b642c856..f0fe8c124 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -68,7 +68,7 @@ struct txwatch { /* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */ enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_tx *tx, + const struct bitcoin_txid *txid, unsigned int depth); }; @@ -123,7 +123,7 @@ struct txwatch *watch_txid(const tal_t *ctx, struct channel *channel, const struct bitcoin_txid *txid, enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_tx *, + const struct bitcoin_txid *, unsigned int depth)) { struct txwatch *w; @@ -170,7 +170,7 @@ struct txwatch *watch_tx(const tal_t *ctx, struct channel *channel, const struct bitcoin_tx *tx, enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_tx *, + const struct bitcoin_txid *, unsigned int depth)) { struct bitcoin_txid txid; @@ -205,7 +205,7 @@ struct txowatch *watch_txo(const tal_t *ctx, /* Returns true if we fired a callback */ static bool txw_fire(struct txwatch *txw, - const struct bitcoin_tx *tx, + const struct bitcoin_txid *txid, unsigned int depth) { enum watch_result r; @@ -217,7 +217,7 @@ static bool txw_fire(struct txwatch *txw, txw->depth, depth, type_to_string(tmpctx, struct bitcoin_txid, &txw->txid)); txw->depth = depth; - r = txw->cb(txw->channel, tx, txw->depth); + r = txw->cb(txw->channel, txid, txw->depth); switch (r) { case DELETE_WATCH: tal_free(txw); @@ -239,7 +239,7 @@ void txwatch_fire(struct chain_topology *topo, txw = txwatch_hash_get(&topo->txwatches, &txid); if (txw) - txw_fire(txw, tx, depth); + txw_fire(txw, &txid, depth); } void txowatch_fire(const struct txowatch *txow, @@ -285,7 +285,7 @@ again: depth = get_tx_depth(topo, &w->txid, &tx); if (depth) - needs_rerun |= txw_fire(w, tx, depth); + needs_rerun |= txw_fire(w, &w->txid, depth); } if (needs_rerun) goto again; diff --git a/lightningd/watch.h b/lightningd/watch.h index b23d3f9a8..3edcc8547 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -44,7 +44,7 @@ struct txwatch *watch_txid(const tal_t *ctx, struct channel *channel, const struct bitcoin_txid *txid, enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_tx *, + const struct bitcoin_txid *, unsigned int depth)); struct txwatch *watch_tx(const tal_t *ctx, @@ -52,7 +52,7 @@ struct txwatch *watch_tx(const tal_t *ctx, struct channel *channel, const struct bitcoin_tx *tx, enum watch_result (*cb)(struct channel *channel, - const struct bitcoin_tx *, + const struct bitcoin_txid *, unsigned int depth)); struct txowatch *watch_txo(const tal_t *ctx, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index b2041625f..e132ea611 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -385,7 +385,7 @@ struct txwatch *watch_txid(const tal_t *ctx UNNEEDED, struct channel *channel UNNEEDED, const struct bitcoin_txid *txid UNNEEDED, enum watch_result (*cb)(struct channel *channel UNNEEDED, - const struct bitcoin_tx * UNNEEDED, + const struct bitcoin_txid * UNNEEDED, unsigned int depth)) { fprintf(stderr, "watch_txid called!\n"); abort(); } /* Generated stub for watch_txo */