Skip to content

Commit

Permalink
block: Compute the txids only once
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdecker authored and rustyrussell committed Sep 2, 2020
1 parent 76ead7d commit 20cbf4d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions bitcoin/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions bitcoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
5 changes: 3 additions & 2 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions lightningd/chaintopology.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 20cbf4d

Please sign in to comment.