chaintopology: do less work when iterating blocks

- Avoid overhead from tal checks when iterating block txs
- Skip pegin txs as well as coinbase txs while iterating
- Early-exit if the txout cannot possibly be p2wsh
- Don't re-calculate the txid when we already have it
- Don't allocate a script for non-policy asset outputs
- Don't copy txids for non-interesting UTXOs

Note the below -Changed line covers the previous wally and PSBT commits
which also provide general block processing speedups.

Changelog-Changed: core: Processing blocks should now be faster

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
This commit is contained in:
Jon Griffiths 2024-01-09 21:06:42 +13:00 committed by Christian Decker
parent 40dd780ea7
commit d5808d921a

View file

@ -942,26 +942,29 @@ static void topo_update_spends(struct chain_topology *topo, struct block *b)
static void topo_add_utxos(struct chain_topology *topo, struct block *b) static void topo_add_utxos(struct chain_topology *topo, struct block *b)
{ {
for (size_t i = 0; i < tal_count(b->full_txs); i++) { /* Coinbase and pegin UTXOs can be ignored */
const uint32_t skip_features = WALLY_TX_IS_COINBASE | WALLY_TX_IS_PEGIN;
const size_t num_txs = tal_count(b->full_txs);
for (size_t i = 0; i < num_txs; i++) {
const struct bitcoin_tx *tx = b->full_txs[i]; const struct bitcoin_tx *tx = b->full_txs[i];
struct bitcoin_outpoint outpoint; for (size_t n = 0; n < tx->wtx->num_outputs; n++) {
if (tx->wtx->outputs[n].features & skip_features)
bitcoin_txid(tx, &outpoint.txid);
for (outpoint.n = 0;
outpoint.n < tx->wtx->num_outputs;
outpoint.n++) {
if (tx->wtx->outputs[outpoint.n].features
& WALLY_TX_IS_COINBASE)
continue; continue;
if (tx->wtx->outputs[n].script_len != BITCOIN_SCRIPTPUBKEY_P2WSH_LEN)
continue; /* Cannot possibly be a p2wsh utxo */
const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, outpoint.n); struct amount_asset amt = bitcoin_tx_output_get_amount(tx, n);
struct amount_asset amt = bitcoin_tx_output_get_amount(tx, outpoint.n); if (!amount_asset_is_main(&amt))
continue; /* Ignore non-policy asset outputs */
if (amount_asset_is_main(&amt) && is_p2wsh(script, NULL)) { const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, n);
wallet_utxoset_add(topo->ld->wallet, &outpoint, if (!is_p2wsh(script, NULL))
b->height, i, script, continue; /* We only care about p2wsh utxos */
amount_asset_to_sat(&amt));
} struct bitcoin_outpoint outpoint = { b->txids[i], n };
wallet_utxoset_add(topo->ld->wallet, &outpoint,
b->height, i, script,
amount_asset_to_sat(&amt));
} }
} }
} }