Problem
Today, consensus validation is tightly coupled to the node implementation. As a result, unit tests require substantial test setup, and validation cannot be reused independently of the node's state-management implementation.
This issue proposes refactoring the validation code into a stateless layer with explicit inputs while preserving most of the existing code, behavior, and architecture.
Testing
The resulting validation interface will provide a dedicated unit-testing surface. Consensus validation can be tested by supplying only the required consensus inputs through lightweight implementations of ChainView and CoinIndex, without constructing ChainstateManager, CBlockIndex, or CCoinsViewCache.
Refactoring plan
The following actions have been identified to be necessary towards the goal of providing a standalone validation library. For a large part, those actions are independent of each other and can be done independently, in any order.
Expose the validation interface
Make the existing
CheckBlockHeader,ContextualCheckBlockHeader, andContextualCheckBlockfunctions part of the public validation interface by declaring them invalidation.h.Interface Segregation: Remove dependency on
ChainstateManagerContextualCheck*functions need aChainstateManageronly to access the consensus parameters. PassConsensus::Paramsinstead and access them at the call site.Make environmental dependencies explicit
Pass the validation time explicitly to
ContextualCheckBlockHeaderinstead of reading the system clock internally.Replace
CBlockIndexwithChainViewIntroduce a
ChainViewabstraction representing the chain history required by contextual validation. Wrap the existingCBlockIndex-based implementation to provide this interface.Introduce
CoinIndexIntroduce a read-only
CoinIndexabstraction for the UTXO lookups performed during validation. WrapCCoinsViewCacheto provide this interface.Extract validation from
ConnectBlockExtract the consensus-validation logic from
ConnectBlockinto a stateless operation depending only on the block and its explicit consensus inputs (Consensus::Params,ChainView,CoinIndex). Further abstractions may become necessary (async execution, caching).ConnectBlockremains responsible for orchestrating validation and applying the resulting state transition.
Relation to the Bitcoin Kernel library
The validation library provides stateless consensus validation. The Bitcoin Kernel library can build on it by providing concrete implementations of ChainView and CoinIndex while retaining responsibility for state management.