Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

quorums_instantsend.h
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef DASH_QUORUMS_INSTANTSEND_H
6 #define DASH_QUORUMS_INSTANTSEND_H
7 
8 #include <llmq/quorums_signing.h>
9 
10 #include <coins.h>
11 #include <unordered_lru_cache.h>
12 #include <primitives/transaction.h>
13 
14 #include <unordered_map>
15 #include <unordered_set>
16 
17 namespace llmq
18 {
19 
21 {
22 public:
23  std::vector<COutPoint> inputs;
26 
27 public:
29 
30  template<typename Stream, typename Operation>
31  inline void SerializationOp(Stream& s, Operation ser_action)
32  {
34  READWRITE(txid);
35  READWRITE(sig);
36  }
37 
38  uint256 GetRequestId() const;
39 };
40 
41 typedef std::shared_ptr<CInstantSendLock> CInstantSendLockPtr;
42 
44 {
45 private:
47 
51 
52 public:
53  explicit CInstantSendDb(CDBWrapper& _db) : db(_db) {}
54 
55  void WriteNewInstantSendLock(const uint256& hash, const CInstantSendLock& islock);
56  void RemoveInstantSendLock(CDBBatch& batch, const uint256& hash, CInstantSendLockPtr islock);
57 
58  void WriteInstantSendLockMined(const uint256& hash, int nHeight);
59  void RemoveInstantSendLockMined(const uint256& hash, int nHeight);
60  void WriteInstantSendLockArchived(CDBBatch& batch, const uint256& hash, int nHeight);
61  std::unordered_map<uint256, CInstantSendLockPtr> RemoveConfirmedInstantSendLocks(int nUntilHeight);
62  void RemoveArchivedInstantSendLocks(int nUntilHeight);
63  bool HasArchivedInstantSendLock(const uint256& islockHash);
64  size_t GetInstantSendLockCount();
65 
70 
71  std::vector<uint256> GetInstantSendLocksByParent(const uint256& parent);
72  std::vector<uint256> RemoveChainedInstantSendLocks(const uint256& islockHash, const uint256& txid, int nHeight);
73 };
74 
76 {
77 private:
80 
81  std::thread workThread;
83 
88  std::unordered_set<uint256, StaticSaltedHasher> inputRequestIds;
89 
95  std::unordered_map<uint256, CInstantSendLock, StaticSaltedHasher> creatingInstantSendLocks;
96  // maps from txid to the in-progress islock
97  std::unordered_map<uint256, CInstantSendLock*, StaticSaltedHasher> txToCreatingInstantSendLocks;
98 
99  // Incoming and not verified yet
100  std::unordered_map<uint256, std::pair<NodeId, CInstantSendLock>, StaticSaltedHasher> pendingInstantSendLocks;
101 
102  // TXs which are neither IS locked nor ChainLocked. We use this to determine for which TXs we need to retry IS locking
103  // of child TXs
105  const CBlockIndex* pindexMined{nullptr};
107  std::unordered_set<uint256, StaticSaltedHasher> children;
108  };
109  std::unordered_map<uint256, NonLockedTxInfo, StaticSaltedHasher> nonLockedTxs;
110  std::unordered_map<COutPoint, uint256, SaltedOutpointHasher> nonLockedTxsByOutpoints;
111 
112  std::unordered_set<uint256, StaticSaltedHasher> pendingRetryTxs;
113 
114 public:
115  explicit CInstantSendManager(CDBWrapper& _llmqDb);
117 
118  void Start();
119  void Stop();
120  void InterruptWorkerThread();
121 
122 public:
123  bool ProcessTx(const CTransaction& tx, bool allowReSigning, const Consensus::Params& params);
124  bool CheckCanLock(const CTransaction& tx, bool printDebug, const Consensus::Params& params);
125  bool CheckCanLock(const COutPoint& outpoint, bool printDebug, const uint256& txHash, CAmount* retValue, const Consensus::Params& params);
126  bool IsLocked(const uint256& txHash);
127  bool IsConflicted(const CTransaction& tx);
129 
130  virtual void HandleNewRecoveredSig(const CRecoveredSig& recoveredSig);
131  void HandleNewInputLockRecoveredSig(const CRecoveredSig& recoveredSig, const uint256& txid);
132  void HandleNewInstantSendLockRecoveredSig(const CRecoveredSig& recoveredSig);
133 
134  void TrySignInstantSendLock(const CTransaction& tx);
135 
136  void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
137  void ProcessMessageInstantSendLock(CNode* pfrom, const CInstantSendLock& islock, CConnman& connman);
138  bool PreVerifyInstantSendLock(NodeId nodeId, const CInstantSendLock& islock, bool& retBan);
140  std::unordered_set<uint256> ProcessPendingInstantSendLocks(int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLock>, StaticSaltedHasher>& pend, bool ban);
141  void ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLock& islock);
142  void UpdateWalletTransaction(const CTransactionRef& tx, const CInstantSendLock& islock);
143 
144  void ProcessNewTransaction(const CTransactionRef& tx, const CBlockIndex* pindex, bool allowReSigning);
146  void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted);
147  void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected);
148 
149  void AddNonLockedTx(const CTransactionRef& tx, const CBlockIndex* pindexMined);
150  void RemoveNonLockedTx(const uint256& txid, bool retryChildren);
151  void RemoveConflictedTx(const CTransaction& tx);
153 
154  void NotifyChainLock(const CBlockIndex* pindexChainLock);
155  void UpdatedBlockTip(const CBlockIndex* pindexNew);
156 
157  void HandleFullyConfirmedBlock(const CBlockIndex* pindex);
158 
159  void RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock);
160  void ResolveBlockConflicts(const uint256& islockHash, const CInstantSendLock& islock);
161  void RemoveChainLockConflictingLock(const uint256& islockHash, const CInstantSendLock& islock);
162  void AskNodesForLockedTx(const uint256& txid);
164 
165  bool AlreadyHave(const CInv& inv);
166  bool GetInstantSendLockByHash(const uint256& hash, CInstantSendLock& ret);
167  bool GetInstantSendLockHashByTxid(const uint256& txid, uint256& ret);
168 
169  size_t GetInstantSendLockCount();
170 
171  void WorkThreadMain();
172 };
173 
175 
176 bool IsInstantSendEnabled();
177 
178 } // namespace llmq
179 
180 #endif//DASH_QUORUMS_INSTANTSEND_H
void AddNonLockedTx(const CTransactionRef &tx, const CBlockIndex *pindexMined)
void UpdateWalletTransaction(const CTransactionRef &tx, const CInstantSendLock &islock)
void RemoveInstantSendLockMined(const uint256 &hash, int nHeight)
void TruncateRecoveredSigsForInputs(const CInstantSendLock &islock)
bool IsInstantSendEnabled()
void ResolveBlockConflicts(const uint256 &islockHash, const CInstantSendLock &islock)
#define READWRITE(obj)
Definition: serialize.h:165
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:49
void BlockDisconnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexDisconnected)
CInstantSendLockPtr GetInstantSendLockByInput(const COutPoint &outpoint)
void TransactionAddedToMempool(const CTransactionRef &tx)
inv message data
Definition: protocol.h:429
unordered_lru_cache< uint256, uint256, StaticSaltedHasher, 10000 > txidCache
bool AlreadyHave(const CInv &inv)
void ProcessNewTransaction(const CTransactionRef &tx, const CBlockIndex *pindex, bool allowReSigning)
CInstantSendDb(CDBWrapper &_db)
void HandleNewInputLockRecoveredSig(const CRecoveredSig &recoveredSig, const uint256 &txid)
void TrySignInstantSendLock(const CTransaction &tx)
void ProcessMessageInstantSendLock(CNode *pfrom, const CInstantSendLock &islock, CConnman &connman)
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
std::unordered_map< uint256, std::pair< NodeId, CInstantSendLock >, StaticSaltedHasher > pendingInstantSendLocks
void RemoveConflictedTx(const CTransaction &tx)
void AskNodesForLockedTx(const uint256 &txid)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:345
CInstantSendManager * quorumInstantSendManager
bool GetInstantSendLockHashByTxid(const uint256 &txid, uint256 &ret)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool HasArchivedInstantSendLock(const uint256 &islockHash)
std::unordered_set< uint256, StaticSaltedHasher > inputRequestIds
Request ids of inputs that we signed.
void RemoveChainLockConflictingLock(const uint256 &islockHash, const CInstantSendLock &islock)
#define ADD_SERIALIZE_METHODS
Implement three methods for serializable objects.
Definition: serialize.h:174
std::shared_ptr< CInstantSendLock > CInstantSendLockPtr
bool IsLocked(const uint256 &txHash)
void ProcessInstantSendLock(NodeId from, const uint256 &hash, const CInstantSendLock &islock)
void HandleFullyConfirmedBlock(const CBlockIndex *pindex)
std::unordered_map< uint256, NonLockedTxInfo, StaticSaltedHasher > nonLockedTxs
CInstantSendLockPtr GetConflictingLock(const CTransaction &tx)
int64_t NodeId
Definition: net.h:109
Definition: net.h:136
Parameters that influence chain consensus.
Definition: params.h:130
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
void WriteNewInstantSendLock(const uint256 &hash, const CInstantSendLock &islock)
bool GetInstantSendLockByHash(const uint256 &hash, CInstantSendLock &ret)
void UpdatedBlockTip(const CBlockIndex *pindexNew)
CInstantSendManager(CDBWrapper &_llmqDb)
void NotifyChainLock(const CBlockIndex *pindexChainLock)
bool PreVerifyInstantSendLock(NodeId nodeId, const CInstantSendLock &islock, bool &retBan)
void WriteInstantSendLockMined(const uint256 &hash, int nHeight)
256-bit opaque blob.
Definition: uint256.h:123
CInstantSendLockPtr GetInstantSendLockByHash(const uint256 &hash)
std::vector< uint256 > GetInstantSendLocksByParent(const uint256 &parent)
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
void RemoveNonLockedTx(const uint256 &txid, bool retryChildren)
std::unordered_map< uint256, CInstantSendLockPtr > RemoveConfirmedInstantSendLocks(int nUntilHeight)
CInstantSendLockPtr GetInstantSendLockByTxid(const uint256 &txid)
uint256 GetInstantSendLockHashByTxid(const uint256 &txid)
void RemoveArchivedInstantSendLocks(int nUntilHeight)
void RemoveInstantSendLock(CDBBatch &batch, const uint256 &hash, CInstantSendLockPtr islock)
bool ProcessTx(const CTransaction &tx, bool allowReSigning, const Consensus::Params &params)
void HandleNewInstantSendLockRecoveredSig(const CRecoveredSig &recoveredSig)
void WriteInstantSendLockArchived(CDBBatch &batch, const uint256 &hash, int nHeight)
void RemoveMempoolConflictsForLock(const uint256 &hash, const CInstantSendLock &islock)
std::vector< COutPoint > inputs
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
Information about a peer.
Definition: net.h:800
std::unordered_map< uint256, CInstantSendLock, StaticSaltedHasher > creatingInstantSendLocks
These are the islocks that are currently in the middle of being created.
std::unordered_set< uint256, StaticSaltedHasher > children
bool CheckCanLock(const CTransaction &tx, bool printDebug, const Consensus::Params &params)
std::unordered_set< uint256, StaticSaltedHasher > pendingRetryTxs
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindex, const std::vector< CTransactionRef > &vtxConflicted)
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
ADD_SERIALIZE_METHODS void SerializationOp(Stream &s, Operation ser_action)
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
std::unordered_map< uint256, CInstantSendLock *, StaticSaltedHasher > txToCreatingInstantSendLocks
std::vector< uint256 > RemoveChainedInstantSendLocks(const uint256 &islockHash, const uint256 &txid, int nHeight)
virtual void HandleNewRecoveredSig(const CRecoveredSig &recoveredSig)
bool IsConflicted(const CTransaction &tx)
unordered_lru_cache< uint256, CInstantSendLockPtr, StaticSaltedHasher, 10000 > islockCache
unordered_lru_cache< COutPoint, uint256, SaltedOutpointHasher, 10000 > outpointCache
std::unordered_map< COutPoint, uint256, SaltedOutpointHasher > nonLockedTxsByOutpoints
Released under the MIT license