PIVX Core  5.6.99
P2P Digital Currency
quorums_chainlocks.h
Go to the documentation of this file.
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 
24 {
25 public:
26  int32_t nHeight{-1};
29 
30 public:
32  {
33  READWRITE(obj.nHeight);
34  READWRITE(obj.blockHash);
35  READWRITE(obj.sig);
36  }
37 
38  bool IsNull() const;
39  std::string ToString() const;
40 };
41 
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:
51 
54 
57 
58  int32_t lastSignedHeight{-1};
61 
62  std::map<uint256, int64_t> seenChainLocks;
63 
64  int64_t lastCleanupTime{0};
65 
66 public:
67  explicit CChainLocksHandler(CScheduler* _scheduler);
69  void Start();
70  void Stop();
71 
72 public:
73  bool AlreadyHave(const CInv& inv);
74  bool GetChainLockByHash(const uint256& hash, CChainLockSig& ret);
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
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
Definition: net.h:145
inv message data
Definition: protocol.h:466
Information about a peer.
Definition: net.h:669
std::string ToString() const
SERIALIZE_METHODS(CChainLockSig, obj)
virtual void HandleNewRecoveredSig(const CRecoveredSig &recoveredSig)
bool InternalHasConflictingChainLock(int nHeight, const uint256 &blockHash)
bool InternalHasChainLock(int nHeight, const uint256 &blockHash)
bool AlreadyHave(const CInv &inv)
bool GetChainLockByHash(const uint256 &hash, CChainLockSig &ret)
CChainLocksHandler(CScheduler *_scheduler)
CChainLockSig bestChainLockWithKnownBlock
bool HasChainLock(int nHeight, const uint256 &blockHash)
void DoInvalidateBlock(const CBlockIndex *pindex, bool activateBestChain)
std::map< uint256, int64_t > seenChainLocks
const CBlockIndex * bestChainLockBlockIndex
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
static const int64_t CLEANUP_SEEN_TIMEOUT
void AcceptedBlockHeader(const CBlockIndex *pindexNew)
void UpdatedBlockTip(const CBlockIndex *pindexNew)
static const int64_t CLEANUP_INTERVAL
void ProcessNewChainLock(NodeId from, const CChainLockSig &clsig, const uint256 &hash)
bool HasConflictingChainLock(int nHeight, const uint256 &blockHash)
256-bit opaque blob.
Definition: uint256.h:138
Definition: quorums.cpp:26
std::unique_ptr< CChainLocksHandler > chainLocksHandler
int NodeId
Definition: net.h:109
#define READWRITE(...)
Definition: serialize.h:183