Skip to content

Commit

Permalink
core: Defer extracting the script until we're sure we'll use it
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cdecker committed Jan 16, 2024
1 parent 09fa7c4 commit 2cba7de
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,11 @@ static void topo_update_spends(struct chain_topology *topo, struct block *b)

static void topo_add_utxos(struct chain_topology *topo, struct block *b)
{
const u8 *script;
struct bitcoin_outpoint outpoint;
const struct bitcoin_tx *tx;
for (size_t i = 0; i < tal_count(b->full_txs); i++) {
const struct bitcoin_tx *tx = b->full_txs[i];
struct bitcoin_outpoint outpoint = { b->txids[i], 0 };
tx = b->full_txs[i];

for (size_t n = 0; n < tx->wtx->num_outputs; n++) {
if (tx->wtx->outputs[n].features
Expand All @@ -955,9 +957,11 @@ static void topo_add_utxos(struct chain_topology *topo, struct block *b)
if (!amount_asset_is_main(&amt))
continue;

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)) {
outpoint.txid = b->txids[i];
outpoint.n = n;
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));
Expand Down

0 comments on commit 2cba7de

Please sign in to comment.