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&, ...), and
- LookupUTXOStatsWithIndex(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 codepath- coinstats.cpp→ hashing codepath implementations
- coinstats.h→ header for- kernel/coinstats.cpp
 
- index/→ only contains the index codepath- coinstatsindex.cpp→ index codepath implementations
- coinstatsindex.h
 
- validation.cpp→ only uses the hashing codepath
- rpc/blockchain.cpp→ uses both the hashing and index codepath, old- GetUTXOStatsfallback logic moved here as static
- test/fuzz/coins_view.cpp→ only uses the hashing codepath
 
TODOs:
- Commit messages could be fleshed out more
Would love any feedback!