Skip to content

wallet: index addresses(keyidx) and bulk-load our_addresses ranges - #9778

Open
daywalker90 wants to merge 1 commit into
ElementsProject:masterfrom
daywalker90:wallet-addresses-index-range
Open

daywalker90 wants to merge 1 commit into
ElementsProject:masterfrom
daywalker90:wallet-addresses-index-range

Conversation

@daywalker90

Copy link
Copy Markdown
Collaborator

When expanding our_addresses after restart (e.g. on the first block scan via wallet_can_spend() or in init_wallet_scriptpubkey_watches()), our_addresses_add_for_index() called wallet_get_addrtype() once per key index. Because the addresses table had no index on keyidx, each lookup performed a full table scan, resulting in O(N^2) startup cost for nodes with many issued addresses (such as nodes with invoices-onchain-fallback).

Add a reversible index migration on addresses(keyidx) and introduce our_addresses_add_range() to load all (keyidx, addrtype) rows for [start, end] in a single ordered query before deriving keys, reducing database work from O(N^2) to O(N) while preserving existing missing-row (ADDR_ALL) and duplicate-row semantics.

Changelog-Changed: wallet: indexed addresses(keyidx) and bulk-loaded address types when populating our_addresses, avoiding O(N^2) startup scans on nodes with many issued addresses.

@daywalker90
daywalker90 requested a review from nGoline November 27, 2026 15:39
When expanding `our_addresses` after restart (e.g. on the first block scan via `wallet_can_spend()` or in `init_wallet_scriptpubkey_watches()`), `our_addresses_add_for_index()` called `wallet_get_addrtype()` once per key index. Because the `addresses` table had no index on `keyidx`, each lookup performed a full table scan, resulting in O(N^2) startup cost for nodes with many issued addresses (such as nodes with `invoices-onchain-fallback`).

Add a reversible index migration (`CREATE INDEX addresses_keyidx_idx ON addresses (keyidx)`) and introduce `our_addresses_add_range()` to load all `(keyidx, addrtype)` rows for `[start, end]` in a single ordered query before deriving keys, reducing database work from O(N^2) to O(N) while preserving existing missing-row (`ADDR_ALL`) and duplicate-row semantics.

Update the manual DB-version-rewind tests in `tests/test_splicing.py` and `tests/test_splicing_disconnect.py` to drop `addresses_keyidx_idx` before re-running migrations.

Changelog-Changed: wallet: indexed `addresses(keyidx)` and bulk-loaded address types when populating `our_addresses`, avoiding O(N^2) startup scans on nodes with many issued addresses.
@daywalker90
daywalker90 force-pushed the wallet-addresses-index-range branch from 48c7f3f to 3aa8fdd Compare November 27, 2026 16:33

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant