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: 2025-04-02 01:23:23 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        3446 :     explicit BlockStateCatcher(const uint256& hashIn) : hash(hashIn), found(false), state(){};
      21       13521 :     void setBlockHash(const uint256& _hash) { clear(); hash = _hash; }
      22       13522 :     void clear() { hash.SetNull(); found = false; state = CValidationState(); }
      23       12770 :     bool stateErrorFound() { return found && state.IsError(); }
      24             : 
      25             : protected:
      26       16011 :     virtual void BlockChecked(const CBlock& block, const CValidationState& stateIn) {
      27       16011 :         if (block.GetHash() != hash) return;
      28       13682 :         found = true;
      29       13682 :         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        3446 :     explicit BlockStateCatcherWrapper(const uint256& hashIn)
      41        3446 :     {
      42        3446 :         stateCatcher = std::make_shared<BlockStateCatcher>(hashIn);
      43        3446 :     }
      44        3446 :     ~BlockStateCatcherWrapper()
      45        3446 :     {
      46        6892 :         if (isRegistered) UnregisterSharedValidationInterface(stateCatcher);
      47        3446 :     }
      48        3446 :     void registerEvent()
      49             :     {
      50        3446 :         RegisterSharedValidationInterface(stateCatcher);
      51        3446 :         isRegistered = true;
      52        3446 :     }
      53       28086 :     BlockStateCatcher& get() const
      54             :     {
      55       27965 :         return *stateCatcher;
      56             :     }
      57             : };
      58             : 
      59             : #endif // PIVX_UTIL_BLOCKSTATECATCHER_H

Generated by: LCOV version 1.14