Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

quorums_chainlocks.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 The Dash Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef DASH_QUORUMS_CHAINLOCKS_H
6 #define DASH_QUORUMS_CHAINLOCKS_H
7 
8 #include <llmq/quorums.h>
9 #include <llmq/quorums_signing.h>
10 
11 #include <net.h>
12 #include <chainparams.h>
13 
14 #include <atomic>
15 #include <unordered_set>
16 
17 #include <boost/thread.hpp>
18 
19 class CBlockIndex;
20 class CScheduler;
21 
22 namespace llmq
23 {
24 
26 {
27 public:
28  int32_t nHeight{-1};
31 
32 public:
34 
35  template<typename Stream, typename Operation>
36  inline void SerializationOp(Stream& s, Operation ser_action)
37  {
40  READWRITE(sig);
41  }
42 
43  bool IsNull() const;
44  std::string ToString() const;
45 };
46 
48 {
49  static const int64_t CLEANUP_INTERVAL = 1000 * 30;
50  static const int64_t CLEANUP_SEEN_TIMEOUT = 24 * 60 * 60 * 1000;
51 
52  // how long to wait for ixlocks until we consider a block with non-ixlocked TXs to be safe to sign
53  static const int64_t WAIT_FOR_ISLOCK_TIMEOUT = 10 * 60;
54 
55 private:
57  boost::thread* scheduler_thread;
60  bool isSporkActive{false};
61  bool isEnforced{false};
62 
65 
69 
70  int32_t lastSignedHeight{-1};
73 
74  // We keep track of txids from recently received blocks so that we can check if all TXs got ixlocked
75  typedef std::unordered_map<uint256, std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>> BlockTxs;
77  std::unordered_map<uint256, int64_t> txFirstSeenTime;
78 
79  std::map<uint256, int64_t> seenChainLocks;
80 
81  int64_t lastCleanupTime{0};
82 
83 public:
84  explicit CChainLocksHandler();
86 
87  void Start();
88  void Stop();
89 
90  bool AlreadyHave(const CInv& inv);
91  bool GetChainLockByHash(const uint256& hash, CChainLockSig& ret);
93 
94  void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
95  void ProcessNewChainLock(NodeId from, const CChainLockSig& clsig, const uint256& hash);
96  void AcceptedBlockHeader(const CBlockIndex* pindexNew);
97  void UpdatedBlockTip(const CBlockIndex* pindexNew);
98  void TransactionAddedToMempool(const CTransactionRef& tx, int64_t nAcceptTime);
99  void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted);
100  void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected);
101  void CheckActiveState();
102  void TrySignChainTip();
103  void EnforceBestChainLock();
104  virtual void HandleNewRecoveredSig(const CRecoveredSig& recoveredSig);
105 
106  bool HasChainLock(int nHeight, const uint256& blockHash);
107  bool HasConflictingChainLock(int nHeight, const uint256& blockHash);
108 
109  bool IsTxSafeForMining(const uint256& txid);
110 
111 private:
112  // these require locks to be held already
113  bool InternalHasChainLock(int nHeight, const uint256& blockHash);
114  bool InternalHasConflictingChainLock(int nHeight, const uint256& blockHash);
115 
116  void DoInvalidateBlock(const CBlockIndex* pindex);
117 
118  BlockTxs::mapped_type GetBlockTxs(const uint256& blockHash);
119 
120  void Cleanup();
121 };
122 
123 extern CChainLocksHandler* chainLocksHandler;
124 
125 
126 } // namespace llmq
127 
128 #endif //DASH_QUORUMS_CHAINLOCKS_H
void UpdatedBlockTip(const CBlockIndex *pindexNew)
void TransactionAddedToMempool(const CTransactionRef &tx, int64_t nAcceptTime)
void ProcessNewChainLock(NodeId from, const CChainLockSig &clsig, const uint256 &hash)
CChainLockSig bestChainLockWithKnownBlock
#define READWRITE(obj)
Definition: serialize.h:165
bool HasChainLock(int nHeight, const uint256 &blockHash)
inv message data
Definition: protocol.h:429
BlockTxs::mapped_type GetBlockTxs(const uint256 &blockHash)
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:345
std::map< uint256, int64_t > seenChainLocks
std::unordered_map< uint256, int64_t > txFirstSeenTime
CChainLocksHandler * chainLocksHandler
static const int64_t WAIT_FOR_ISLOCK_TIMEOUT
bool IsTxSafeForMining(const uint256 &txid)
virtual void HandleNewRecoveredSig(const CRecoveredSig &recoveredSig)
boost::thread * scheduler_thread
bool InternalHasConflictingChainLock(int nHeight, const uint256 &blockHash)
#define ADD_SERIALIZE_METHODS
Implement three methods for serializable objects.
Definition: serialize.h:174
void BlockDisconnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexDisconnected)
static const int64_t CLEANUP_SEEN_TIMEOUT
int64_t NodeId
Definition: net.h:109
Definition: net.h:136
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
std::string ToString() const
static const int64_t CLEANUP_INTERVAL
256-bit opaque blob.
Definition: uint256.h:123
const CBlockIndex * lastNotifyChainLockBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindex, const std::vector< CTransactionRef > &vtxConflicted)
bool AlreadyHave(const CInv &inv)
bool HasConflictingChainLock(int nHeight, const uint256 &blockHash)
bool InternalHasChainLock(int nHeight, const uint256 &blockHash)
Information about a peer.
Definition: net.h:800
bool GetChainLockByHash(const uint256 &hash, CChainLockSig &ret)
std::unordered_map< uint256, std::shared_ptr< std::unordered_set< uint256, StaticSaltedHasher > > > BlockTxs
const CBlockIndex * bestChainLockBlockIndex
void DoInvalidateBlock(const CBlockIndex *pindex)
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
void AcceptedBlockHeader(const CBlockIndex *pindexNew)
Released under the MIT license