core: Defer extracting the script until we're sure we'll use it

We were extracting the output script for all outputs, and discarding
them immediately again if they were not P2WSH outputs which are the
ones of interest to us. This patch move the extraction until after we
have determined it is useful, and so we should save a couple thousand
`tal()` and `tal_free()` calls.

Changelog-Changed: lightningd: Speed up blocksync by not parsing unused parts of the transactions
This commit is contained in:
Christian Decker 2024-01-09 13:36:20 +01:00
parent c329756723
commit d185b0fa90

View File

@ -965,11 +965,12 @@ static void topo_add_utxos(struct chain_topology *topo, struct block *b)
if (!amount_asset_is_main(&amt))
continue; /* Ignore non-policy asset outputs */
const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, n);
if (!is_p2wsh(script, NULL))
if (!bitcoin_tx_output_script_is_p2wsh(tx, n))
continue; /* We only care about p2wsh utxos */
struct bitcoin_outpoint outpoint = { b->txids[i], n };
const u8 *script =
bitcoin_tx_output_get_script(tmpctx, tx, n);
wallet_utxoset_add(topo->ld->wallet, &outpoint,
b->height, i, script,
amount_asset_to_sat(&amt));