LCOV - code coverage report
Current view: top level - src/util - blockstatecatcher.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 22 22 100.0 %
Date: 2026-08-09 10:51:41 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2021 The PIVX Core developers
       2             : // Distributed under the MIT software license, see the accompanying
       3             : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
       4             : 
       5             : #ifndef PIVX_UTIL_BLOCKSTATECATCHER_H
       6             : #define PIVX_UTIL_BLOCKSTATECATCHER_H
       7             : 
       8             : #include "consensus/validation.h"
       9             : #include "validationinterface.h"
      10             : 
      11             : /**
      12             :  * Validation interface listener used to get feedback from ProcessNewBlock result.
      13             :  */
      14             : class BlockStateCatcher : public CValidationInterface
      15             : {
      16             : public:
      17             :     uint256 hash;
      18             :     bool found;
      19             :     CValidationState state;
      20        4831 :     explicit BlockStateCatcher(const uint256& hashIn) : hash(hashIn), found(false), state(){};
      21        8459 :     void setBlockHash(const uint256& _hash) { clear(); hash = _hash; }
      22        8460 :     void clear() { hash.SetNull(); found = false; state = CValidationState(); }
      23        7868 :     bool stateErrorFound() { return found && state.IsError(); }
      24             : 
      25             : protected:
      26       12867 :     virtual void BlockChecked(const CBlock& block, const CValidationState& stateIn) {
      27       12867 :         if (block.GetHash() != hash) return;
      28       11225 :         found = true;
      29       11225 :         state = stateIn;
      30             :     };
      31             : };
      32             : 
      33             : class BlockStateCatcherWrapper
      34             : {
      35             : private:
      36             :     std::shared_ptr<BlockStateCatcher> stateCatcher = nullptr;
      37             :     bool isRegistered = false;
      38             : 
      39             : public:
      40        4831 :     explicit BlockStateCatcherWrapper(const uint256& hashIn)
      41        4831 :     {
      42        4831 :         stateCatcher = std::make_shared<BlockStateCatcher>(hashIn);
      43        4831 :     }
      44        4831 :     ~BlockStateCatcherWrapper()
      45        4831 :     {
      46        9662 :         if (isRegistered) UnregisterSharedValidationInterface(stateCatcher);
      47        4831 :     }
      48        4831 :     void registerEvent()
      49             :     {
      50        4831 :         RegisterSharedValidationInterface(stateCatcher);
      51        4831 :         isRegistered = true;
      52        4831 :     }
      53       23046 :     BlockStateCatcher& get() const
      54             :     {
      55       20312 :         return *stateCatcher;
      56             :     }
      57             : };
      58             : 
      59             : #endif // PIVX_UTIL_BLOCKSTATECATCHER_H

Generated by: LCOV version 1.14