Skip to content

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

Draft
arejula27 wants to merge 6 commits into
bitcoin:masterfrom
arejula27:txospenderindex-block-keys
Draft

arejula27 wants to merge 6 commits into
bitcoin:masterfrom
arejula27:txospenderindex-block-keys

Conversation

@arejula27

Copy link
Copy Markdown
Contributor

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.

Move the best_block_v2 locator handling from TxIndex::DB into
BaseIndex::DB behind a constructor flag, so other indexes can reuse
it when they change their on-disk format.
Make the key prefix a template parameter so other indexes can reuse
them.

Not in db_key.h, which holds the height-based scheme of
blockfilterindex and coinstatsindex.
Move the logic that assigns a sequence number to a newly indexed block
and the creation of the salted hasher out of TxIndex::DB into free
functions in block_seq, so other indexes using the same scheme can
share them.
Use the txindex key scheme: a 5-byte salted hash of the spent outpoint,
followed by the block sequence and the offset of the spending tx.

As in txindex, entries of disconnected blocks are kept, so CustomRemove
goes away and FindSpender only returns spends in the active chain.

Fall back to the legacy entries if no spend is found.
Pin the encodings of the new and legacy keys, and add unit tests for
hash prefix false positives, for entries of disconnected blocks being
kept but not returned, and for the fallback to legacy entries. Add a
functional test that upgrades a v31 datadir, reorgs a block indexed in
the legacy format and downgrades again.
Mimic TxIndex::FindTx: skip a candidate that cannot be read and return
std::optional. With 5-byte hash prefixes collisions are no longer
negligible, and failing on an unreadable false positive would hide the
real spender.
@DrahtBot

DrahtBot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

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.

@andrewtoth

Copy link
Copy Markdown
Contributor

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

arejula27 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

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 #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

@DrahtBot

DrahtBot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

🚧 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.

@andrewtoth

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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

@l0rinc

l0rinc commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Concept ACK
👍 for pruning support and also for merging #36002 first - if you agree, please convert this to draft until then so reviewers know which PRs to prioritize.

@w0xlt

w0xlt commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@sedited

sedited commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@DrahtBot

Copy link
Copy Markdown
Contributor

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants