Browse Source

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.
bump-pyln-proto
Christian Decker 4 years ago
parent
commit
f2797f303a
  1. 2
      bitcoin/block.c
  2. 1
      bitcoin/block.h
  3. 5
      lightningd/chaintopology.c
  4. 1
      lightningd/chaintopology.h

2
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 */

1
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 *

5
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;
}

1
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 */

Loading…
Cancel
Save