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