Line data Source code
1 : // Copyright (c) 2019 The Dash Core developers 2 : // Copyright (c) 2023 The PIVX Core developers 3 : // Distributed under the MIT/X11 software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_LLMQ_QUORUMS_CHAINLOCKS_H 7 : #define PIVX_LLMQ_QUORUMS_CHAINLOCKS_H 8 : 9 : #include "llmq/quorums.h" 10 : #include "llmq/quorums_signing.h" 11 : 12 : #include "net.h" 13 : #include "chainparams.h" 14 : 15 : #include <atomic> 16 : 17 : class CBlockIndex; 18 : class CScheduler; 19 : 20 : namespace llmq 21 : { 22 : 23 : class CChainLockSig 24 : { 25 : public: 26 : int32_t nHeight{-1}; 27 : uint256 blockHash; 28 : CBLSSignature sig; 29 : 30 : public: 31 3861 : SERIALIZE_METHODS(CChainLockSig, obj) 32 : { 33 2294 : READWRITE(obj.nHeight); 34 2294 : READWRITE(obj.blockHash); 35 2294 : READWRITE(obj.sig); 36 2294 : } 37 : 38 : bool IsNull() const; 39 : std::string ToString() const; 40 : }; 41 : 42 : class CChainLocksHandler : public CRecoveredSigsListener 43 : { 44 : static const int64_t CLEANUP_INTERVAL = 1000 * 30; 45 : static const int64_t CLEANUP_SEEN_TIMEOUT = 24 * 60 * 60 * 1000; 46 : 47 : private: 48 : CScheduler* scheduler; 49 : RecursiveMutex cs; 50 : bool tryLockChainTipScheduled{false}; 51 : 52 : uint256 bestChainLockHash; 53 : CChainLockSig bestChainLock; 54 : 55 : CChainLockSig bestChainLockWithKnownBlock; 56 : const CBlockIndex* bestChainLockBlockIndex{nullptr}; 57 : 58 : int32_t lastSignedHeight{-1}; 59 : uint256 lastSignedRequestId; 60 : uint256 lastSignedMsgHash; 61 : 62 : std::map<uint256, int64_t> seenChainLocks; 63 : 64 : int64_t lastCleanupTime{0}; 65 : 66 : public: 67 : explicit CChainLocksHandler(CScheduler* _scheduler); 68 : ~CChainLocksHandler(); 69 : void Start(); 70 : void Stop(); 71 : 72 : public: 73 : bool AlreadyHave(const CInv& inv); 74 : bool GetChainLockByHash(const uint256& hash, CChainLockSig& ret); 75 : CChainLockSig GetBestChainLock(); 76 : 77 : void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman); 78 : void ProcessNewChainLock(NodeId from, const CChainLockSig& clsig, const uint256& hash); 79 : void AcceptedBlockHeader(const CBlockIndex* pindexNew); 80 : void UpdatedBlockTip(const CBlockIndex* pindexNew); 81 : void TrySignChainTip(); 82 : void EnforceBestChainLock(); 83 : virtual void HandleNewRecoveredSig(const CRecoveredSig& recoveredSig); 84 : 85 : bool HasChainLock(int nHeight, const uint256& blockHash); 86 : bool HasConflictingChainLock(int nHeight, const uint256& blockHash); 87 : 88 : private: 89 : // these require locks to be held already 90 : bool InternalHasChainLock(int nHeight, const uint256& blockHash); 91 : bool InternalHasConflictingChainLock(int nHeight, const uint256& blockHash); 92 : 93 : void DoInvalidateBlock(const CBlockIndex* pindex, bool activateBestChain); 94 : 95 : void Cleanup(); 96 : }; 97 : 98 : extern std::unique_ptr<CChainLocksHandler> chainLocksHandler; 99 : } 100 : 101 : #endif // PIVX_LLMQ_QUORUMS_CHAINLOCKS_H