Part of: #24303 Depends on: #24322
The GetUTXOStats function has 2 codepaths:
- One which queries the
CoinStatsIndexfor the UTXO hash - One which actually performs the hashing
For libbitcoinkernel, the only place where we call GetUTXOStats is in PopulateAndValidateSnapshots, which uses the SHA256D hash, and is therefore unable to use the CoinStatsIndex since that only provides MuHash hashes. Not that I think indices necessarily belong in libbitcoinkernel anyway.
This PR separates these 2 aforementioned codepaths of GetUTXOStats, uses the hashing codepath in PopulateAndValidateSnapshots, and removes the need to link in index/coinstatsindex.cpp and node/coinstats.cpp.
Logistically, this PR:
- Extracts out the
index_requestedandhash_typemembers ofCoinStats, which served as “in-params” toGetUTXOStatsembedded within theCoinStatsstruct. This allowsCoinStatsto only consist of “out-param” members, and be returned byGetUTXOStatswithout needing to be an “in-out” param - Introduce the purely virtual
UTXOHashersclass, with 3 implementations:SHA256DHasher,MuHashHasher, andNullHasher. These replace the existing template-based polymorphism. - Split
GetUTXOStatsinto:CalculateUTXOStatsWithHasher(UTXOHasher&, ...), andLookupUTXOStatsWithIndex(CoinStatsIndex&, ...)
- Use
CalculateUTXOStatsWithHasherdirectly where appropriate (src/validation.cppandsrc/fuzz) - Move
GetUTXOStatstorpc/blockchain, which is the only place that depends onGetUTXOStats’s weird fallback behaviour - Move
LookupUTXOStatsWithIndextoindex/coinstatsindex
Code organization:
src/kernel/→ only contains the hashing codepathcoinstats.cpp→ hashing codepath implementationscoinstats.h→ header forkernel/coinstats.cpp
index/→ only contains the index codepathcoinstatsindex.cpp→ index codepath implementationscoinstatsindex.h
validation.cpp→ only uses the hashing codepathrpc/blockchain.cpp→ uses both the hashing and index codepath, oldGetUTXOStatsfallback logic moved here as statictest/fuzz/coins_view.cpp→ only uses the hashing codepath
TODOs:
- Commit messages could be fleshed out more
Would love any feedback!