From f2797f303a47728db7436fcbde15466c8d2490b7 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 26 Aug 2020 13:31:41 +0200 Subject: [PATCH] block: Compute the txids only once We're not going to mutate transactions in a block, so computing the txids every time we need them is a waste, let's compute them upfront and use them afterwards. --- bitcoin/block.c | 2 ++ bitcoin/block.h | 1 + lightningd/chaintopology.c | 5 +++-- lightningd/chaintopology.h | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bitcoin/block.c b/bitcoin/block.c index 53df3e13e..e142d340e 100644 --- a/bitcoin/block.c +++ b/bitcoin/block.c @@ -208,9 +208,11 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams, num = pull_varint(&p, &len); b->tx = tal_arr(b, struct bitcoin_tx *, num); + b->txids = tal_arr(b, struct bitcoin_txid, num); for (i = 0; i < num; i++) { b->tx[i] = pull_bitcoin_tx(b->tx, &p, &len); b->tx[i]->chainparams = chainparams; + bitcoin_txid(b->tx[i], &b->txids[i]); } /* We should end up not overrunning, nor have extra */ diff --git a/bitcoin/block.h b/bitcoin/block.h index 5239f372d..227fc211f 100644 --- a/bitcoin/block.h +++ b/bitcoin/block.h @@ -36,6 +36,7 @@ struct bitcoin_block { struct bitcoin_block_hdr hdr; /* tal_count shows now many */ struct bitcoin_tx **tx; + struct bitcoin_txid *txids; }; struct bitcoin_block * diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index a09a27f70..5595cd6c8 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -91,7 +91,7 @@ static void filter_block_txs(struct chain_topology *topo, struct block *b) } owned = AMOUNT_SAT(0); - bitcoin_txid(tx, &txid); + txid = b->txids[i]; if (txfilter_match(topo->bitcoind->ld->owned_txfilter, tx)) { wallet_extract_owned_outputs(topo->bitcoind->ld->wallet, tx->wtx, &b->height, &owned); @@ -748,7 +748,7 @@ static void topo_update_spends(struct chain_topology *topo, struct block *b) struct bitcoin_txid txid; struct amount_sat inputs_total = AMOUNT_SAT(0); - bitcoin_txid(tx, &txid); + txid = b->txids[i]; for (size_t j = 0; j < tx->wtx->num_inputs; j++) { const struct wally_tx_input *input = &tx->wtx->inputs[j]; @@ -843,6 +843,7 @@ static struct block *new_block(struct chain_topology *topo, b->hdr = blk->hdr; b->full_txs = tal_steal(b, blk->tx); + b->txids = tal_steal(b, blk->txids); return b; } diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 8fd651283..8a5c61949 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -62,6 +62,7 @@ struct block { /* Full copy of txs (freed in filter_block_txs) */ struct bitcoin_tx **full_txs; + struct bitcoin_txid *txids; }; /* Hash blocks by sha */