WIP: txospenderindex: hash keys and pack positions to reduce disk usage #36480

Draft
arejula27 wants to merge 6 commits from txospenderindex-block-keys into master AGit
arejula27 commented 2026-10-08 20:46:11 +00:00 (Migrated from github.com)

This applies the txindex key scheme from #35531 to txospenderindex. The motivation is to save space and to allow replicating what #36002 does for txindex, by referencing blocks instead of local files.

The first commits refactor the index code so the #35531 scheme can be shared: BaseIndex::DB can opt in to the versioned sync locator, and the block sequence keys, sequence assignment and salted hasher move from txindex into index/block_seq.h, without changing txindex's on-disk format. The following commits switch txospenderindex to the new keys and add tests. As in txindex, entries of disconnected blocks are kept and lookups only count blocks in the active chain, so CustomRemove is gone. Entries written by v31 are still read as a fallback. The last commit makes FindSpender skip candidates it cannot read and return std::optional, mimicking TxIndex::FindTx, since with 5-byte prefixes an unreadable false positive could otherwise hide the real spender.

Each entry is about 33% smaller (key bytes):

  • before: 's' (1) + SipHash-2-4 of the outpoint (8) + CDiskTxPos, as varints nFile (2) + nPos (4) + nTxOffset (3) = 18 bytes
  • after: 'x' (1) + SipHash-1-3-UJ prefix (5) + block sequence varint (3) + tx offset (3) = 12 bytes

I did not have time to do a benchmark (my setup is really cheap, laptop where i work with an external SSD connected), will try to do on the next days. But same as #35531 i hope to reduce disk space and indexing time.

This applies the txindex key scheme from #35531 to `txospenderindex`. The motivation is to save space and to allow replicating what #36002 does for `txindex`, by referencing blocks instead of local files. The first commits refactor the index code so the #35531 scheme can be shared: `BaseIndex::DB` can opt in to the versioned sync locator, and the block sequence keys, sequence assignment and salted hasher move from txindex into `index/block_seq.h`, without changing txindex's on-disk format. The following commits switch txospenderindex to the new keys and add tests. As in txindex, entries of disconnected blocks are kept and lookups only count blocks in the active chain, so `CustomRemove` is gone. Entries written by v31 are still read as a fallback. The last commit makes `FindSpender` skip candidates it cannot read and return `std::optional`, mimicking `TxIndex::FindTx`, since with 5-byte prefixes an unreadable false positive could otherwise hide the real spender. Each entry is about 33% smaller (key bytes): - before: 's' (1) + SipHash-2-4 of the outpoint (8) + CDiskTxPos, as varints nFile (2) + nPos (4) + nTxOffset (3) = 18 bytes - after: 'x' (1) + SipHash-1-3-UJ prefix (5) + block sequence varint (3) + tx offset (3) = 12 bytes I did not have time to do a benchmark (my setup is really cheap, laptop where i work with an external SSD connected), will try to do on the next days. But same as #35531 i hope to reduce disk space and indexing time.
DrahtBot commented 2026-10-08 20:46:20 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK andrewtoth, l0rinc, w0xlt, sedited

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36629 (Add output script index and corresponding script history RPC by andrewtoth)
  • #36033 ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko)
  • #36002 (txindex: allow running in pruned mode by andrewtoth)
  • #35474 (node: move index ownership to NodeContext by w0xlt)
  • #34132 (coins,dbwrapper: make point-read and txindex lookup failures fatal by l0rinc)
  • #33324 (blocks: add resumable reobfuscation for existing block files by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36480 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36480.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36480#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | Concept ACK | [andrewtoth](https://github.com/bitcoin/bitcoin/pull/36480#issuecomment-6068799412), [l0rinc](https://github.com/bitcoin/bitcoin/pull/36480#issuecomment-6075248071), [w0xlt](https://github.com/bitcoin/bitcoin/pull/36480#issuecomment-6185688251), [sedited](https://github.com/bitcoin/bitcoin/pull/36480#issuecomment-6370740796) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36629](https://github.com/bitcoin/bitcoin/pull/36629) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36629.svg"></sub> (Add output script index and corresponding script history RPC by andrewtoth) * [#36033](https://github.com/bitcoin/bitcoin/pull/36033) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36033.svg"></sub> ([wip,nomerge,rfc] build: Require C++23 compiler by maflcko) * [#36002](https://github.com/bitcoin/bitcoin/pull/36002) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36002.svg"></sub> (txindex: allow running in pruned mode by andrewtoth) * [#35474](https://github.com/bitcoin/bitcoin/pull/35474) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35474.svg"></sub> (node: move index ownership to NodeContext by w0xlt) * [#34132](https://github.com/bitcoin/bitcoin/pull/34132) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34132.svg"></sub> (coins,dbwrapper: make point-read and txindex lookup failures fatal by l0rinc) * [#33324](https://github.com/bitcoin/bitcoin/pull/33324) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33324.svg"></sub> (blocks: add resumable reobfuscation for existing block files by l0rinc) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
andrewtoth commented 2026-10-08 20:50:14 +00:00 (Migrated from github.com)

Concept ACK!

I did not have time to do a benchmark

I have done this before. IIRC the db size went from ~84GB -> ~62GB. The time to index is also much lower.

Do we want to also enable pruning in one go like we are with #36002? I don't think it would be controversial so we could do it all in 1 PR.

Concept ACK! > I did not have time to do a benchmark I have done this before. IIRC the db size went from ~84GB -> ~62GB. The time to index is also much lower. Do we want to also enable pruning in one go like we are with #36002? I don't think it would be controversial so we could do it all in 1 PR.
arejula27 commented 2026-10-08 20:56:00 +00:00 (Migrated from github.com)

I have done this before.

Sorry @andrewtoth, i wasnt aware of it 😅 , should i compare with ur code and add u as co author?

IIRC the db size went from ~84GB -> ~62GB.

sounds awesome!

Do we want to also enable pruning in one go like we are with https://github.com/bitcoin/bitcoin/pull/36002? I don't think it would be controversial so we could do it all in 1 PR.

Sure! i will add the commits, but it will take me some time o7

> I have done this before. Sorry @andrewtoth, i wasnt aware of it :sweat_smile: , should i compare with ur code and add u as co author? > IIRC the db size went from ~84GB -> ~62GB. sounds awesome! >Do we want to also enable pruning in one go like we are with https://github.com/bitcoin/bitcoin/pull/36002? I don't think it would be controversial so we could do it all in 1 PR. Sure! i will add the commits, but it will take me some time o7
First-time contributor

🟠 Minor · An unreadable spender becomes an ordinary unspent RPC result

When local block-storage damage makes the spender unreadable and neither format supplies a verified match, gettxspendingprevout now returns the normal unspent response instead of an error.

Location: src/index/txospenderindex.cpp:158 (head).

🔵 Suggestion · The unreadable-candidate test misses the read-failure branch

Clearing BLOCK_HAVE_DATA skips the forged candidate before ReadTransaction runs, leaving continuation after an actual read failure untested.

Location: src/test/txospenderindex_tests.cpp:221 (head).

🔵 Suggestion · The persistent outpoint hash mapping lacks fixed-vector coverage

The tests do not pin the salt-and-outpoint mapping, so a wrapper-only change to the hash inputs could silently make existing hashed rows unreachable.

Location: src/test/txospenderindex_tests.cpp:126 (head).

🔵 Suggestion · Hashed spender persistence is not checked after restart

The only positive post-reopen lookup uses legacy data; the hashed spender is checked only before restart, so losing hashed lookup state on reopening would escape this test.

Location: test/functional/feature_txospenderindex_compatibility.py:84 (head).

🔵 Suggestion · Legacy detection adds an avoidable LevelDB open

For an existing on-disk database without wipe, legacy detection opens LevelDB twice even though reading the legacy salt after the normal open preserves valid-database behavior.

Location: src/index/txospenderindex.cpp:52 (head).

🔵 Suggestion · Document the reorg-related RPC result change

txospenderindex.cpp:153 now filters out spends from disconnected blocks immediately. The base returned them until the index caught up. That visible RPC change has no doc/release-notes-36480.md, and the window is only transient.

Location: src/index/txospenderindex.cpp:152 (head).

Review coverage was incomplete.


Reviewed: fadf210822d8 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:8541d3fafd3ea45d3ad824914bf78a806527457ae4801671a2c4a580beddb288:1e657457416e20a18d8d2d136919c48e28e1bfed7a76c26939ab1ca10a232c6a --> <!-- ralph-base:ab7e668cc6af5d716ad40166d1f88235a2f6d25d --> <!-- ralph-head:fadf210822d84dcf278c6d6897c67e189a3c0065 --> <!-- ralph-case:5375c43bd3587b499c3f7408360c116351517868519fdddf7a1d667870d10794 --> **🟠 Minor · An unreadable spender becomes an ordinary unspent RPC result** When local block-storage damage makes the spender unreadable and neither format supplies a verified match, gettxspendingprevout now returns the normal unspent response instead of an error. Location: [`src/index/txospenderindex.cpp:158`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fadf210822d84dcf278c6d6897c67e189a3c0065/src/index/txospenderindex.cpp#L158>) (head). **🔵 Suggestion · The unreadable-candidate test misses the read-failure branch** Clearing BLOCK_HAVE_DATA skips the forged candidate before ReadTransaction runs, leaving continuation after an actual read failure untested. Location: [`src/test/txospenderindex_tests.cpp:221`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fadf210822d84dcf278c6d6897c67e189a3c0065/src/test/txospenderindex_tests.cpp#L221>) (head). **🔵 Suggestion · The persistent outpoint hash mapping lacks fixed-vector coverage** The tests do not pin the salt-and-outpoint mapping, so a wrapper-only change to the hash inputs could silently make existing hashed rows unreachable. Location: [`src/test/txospenderindex_tests.cpp:126`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fadf210822d84dcf278c6d6897c67e189a3c0065/src/test/txospenderindex_tests.cpp#L126>) (head). **🔵 Suggestion · Hashed spender persistence is not checked after restart** The only positive post-reopen lookup uses legacy data; the hashed spender is checked only before restart, so losing hashed lookup state on reopening would escape this test. Location: [`test/functional/feature_txospenderindex_compatibility.py:84`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fadf210822d84dcf278c6d6897c67e189a3c0065/test/functional/feature_txospenderindex_compatibility.py#L84>) (head). **🔵 Suggestion · Legacy detection adds an avoidable LevelDB open** For an existing on-disk database without wipe, legacy detection opens LevelDB twice even though reading the legacy salt after the normal open preserves valid-database behavior. Location: [`src/index/txospenderindex.cpp:52`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fadf210822d84dcf278c6d6897c67e189a3c0065/src/index/txospenderindex.cpp#L52>) (head). **🔵 Suggestion · Document the reorg-related RPC result change** txospenderindex.cpp:153 now filters out spends from disconnected blocks immediately. The base returned them until the index caught up. That visible RPC change has no doc/release-notes-36480.md, and the window is only transient. Location: [`src/index/txospenderindex.cpp:152`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/fadf210822d84dcf278c6d6897c67e189a3c0065/src/index/txospenderindex.cpp#L152>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`fadf210822d8`](<https://git.fish.foo/bitcoin/bitcoin/commit/fadf210822d84dcf278c6d6897c67e189a3c0065>) · [Full review report](<https://review.fish.foo/traces/36480-fadf210822d84dcf278c6d6897c67e189a3c0065-1884-3.html>) · [Previous reports](<https://review.fish.foo/traces/36480/>) [permalink](<https://review.fish.foo/traces/36480-fadf210822d84dcf278c6d6897c67e189a3c0065-1884-3.html>)
DrahtBot commented 2026-10-08 22:15:17 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/37841926848/job/113533161262
LLM reason (✨ experimental): CI failed because the IWYU lint check modified three files, leaving uncommitted include changes that caused git diff --exit-code to fail.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/37841926848/job/113533161262</sub> <sub>LLM reason (✨ experimental): CI failed because the IWYU lint check modified three files, leaving uncommitted include changes that caused `git diff --exit-code` to fail.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
andrewtoth commented 2026-10-08 22:15:21 +00:00 (Migrated from github.com)

Sorry @andrewtoth, i wasnt aware of it 😅 , should i compare with ur code and add u as co author?

No worries, it was nothing I published. Let's just keep going with this. I don't need to be added as co-author.

it will take me some time

On second thought, if we merge #36002 first we can reuse the TxLookupResult (sorry I have to push an update there)

> Sorry @andrewtoth, i wasnt aware of it 😅 , should i compare with ur code and add u as co author? No worries, it was nothing I published. Let's just keep going with this. I don't need to be added as co-author. > it will take me some time On second thought, if we merge #36002 first we can reuse the TxLookupResult (sorry I have to push an update there)
arejula27 commented 2026-10-08 22:53:54 +00:00 (Migrated from github.com)

Sounds good to me, i will wait an then add here TxLookupResult !

Sounds good to me, i will wait an then add here `TxLookupResult `!
l0rinc commented 2026-10-09 05:58:38 +00:00 (Migrated from github.com)

Concept ACK
👍 for pruning support and also for merging https://github.com/bitcoin/bitcoin/pull/36002 first - if you agree, please convert this to draft until then so reviewers know which PRs to prioritize.

Concept ACK 👍 for pruning support and also for merging https://github.com/bitcoin/bitcoin/pull/36002 first - if you agree, please convert this to draft until then so reviewers know which PRs to prioritize.
w0xlt commented 2026-10-15 06:07:50 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
sedited commented 2026-10-23 17:25:24 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
DrahtBot commented 2026-11-23 10:34:15 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36480
No description provided.