Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

privatesend.h
Go to the documentation of this file.
1 // Copyright (c) 2014-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 PRIVATESEND_H
6 #define PRIVATESEND_H
7 
8 #include <bls/bls.h>
9 #include <chain.h>
10 #include <chainparams.h>
11 #include <primitives/transaction.h>
12 #include <pubkey.h>
13 #include <sync.h>
14 #include <spork.h>
15 #include <timedata.h>
16 #include <tinyformat.h>
17 
18 class CPrivateSend;
19 class CConnman;
20 
21 // timeouts
22 static const int PRIVATESEND_AUTO_TIMEOUT_MIN = 5;
23 static const int PRIVATESEND_AUTO_TIMEOUT_MAX = 15;
24 static const int PRIVATESEND_QUEUE_TIMEOUT = 30;
25 static const int PRIVATESEND_SIGNING_TIMEOUT = 15;
26 
28 static const int MIN_PRIVATESEND_PEER_PROTO_VERSION = 70213;
29 
30 static const size_t PRIVATESEND_ENTRY_MAX_SIZE = 9;
31 
32 // pool responses
33 enum PoolMessage : int32_t {
47  ERR_NOT_A_MN, // not used
59 };
60 template<> struct is_serializable_enum<PoolMessage> : std::true_type {};
61 
62 // pool states
63 enum PoolState : int32_t {
71 };
72 template<> struct is_serializable_enum<PoolState> : std::true_type {};
73 
74 // status update message constants
75 enum PoolStatusUpdate : int32_t {
78 };
79 template<> struct is_serializable_enum<PoolStatusUpdate> : std::true_type {};
80 
82 {
83 public:
86  int nEntriesCount; // deprecated, kept for backwards compatibility
89 
91  nSessionID(0),
93  nEntriesCount(0),
96 
99  nState(nState),
103 
105 
106  template <typename Stream, typename Operation>
107  inline void SerializationOp(Stream& s, Operation ser_action)
108  {
110  READWRITE(nState);
111  if (s.GetVersion() <= 702015) {
113  }
116  }
117 };
118 
121 class CTxDSIn : public CTxIn
122 {
123 public:
124  // memory only
126  bool fHasSig; // flag to indicate if signed
127 
128  CTxDSIn(const CTxIn& txin, const CScript& script) :
129  CTxIn(txin),
130  prevPubKey(script),
131  fHasSig(false)
132  {
133  }
134 
136  CTxIn(),
137  prevPubKey(),
138  fHasSig(false)
139  {
140  }
141 };
142 
144 {
145 public:
146  int nDenom;
148 
150  nDenom(0),
152 
154  nDenom(nDenom),
156 
158 
159  template <typename Stream, typename Operation>
160  inline void SerializationOp(Stream& s, Operation ser_action)
161  {
162  READWRITE(nDenom);
164  }
165 
166  friend bool operator==(const CPrivateSendAccept& a, const CPrivateSendAccept& b)
167  {
168  return a.nDenom == b.nDenom && a.txCollateral == b.txCollateral;
169  }
170 };
171 
172 // A client's transaction in the mixing pool
174 {
175 public:
176  std::vector<CTxDSIn> vecTxDSIn;
177  std::vector<CTxOut> vecTxOut;
179  // memory only
181 
183  vecTxDSIn(std::vector<CTxDSIn>()),
184  vecTxOut(std::vector<CTxOut>()),
186  addr(CService())
187  {
188  }
189 
190  CPrivateSendEntry(const std::vector<CTxDSIn>& vecTxDSIn, const std::vector<CTxOut>& vecTxOut, const CTransaction& txCollateral) :
194  addr(CService())
195  {
196  }
197 
199 
200  template <typename Stream, typename Operation>
201  inline void SerializationOp(Stream& s, Operation ser_action)
202  {
206  }
207 
208  bool AddScriptSig(const CTxIn& txin);
209 };
210 
211 
216 {
217 public:
218  int nDenom;
220  int64_t nTime;
221  bool fReady; //ready for submit
222  std::vector<unsigned char> vchSig;
223  // memory only
224  bool fTried;
225 
227  nDenom(0),
229  nTime(0),
230  fReady(false),
231  vchSig(std::vector<unsigned char>()),
232  fTried(false)
233  {
234  }
235 
236  CPrivateSendQueue(int nDenom, COutPoint outpoint, int64_t nTime, bool fReady) :
237  nDenom(nDenom),
238  masternodeOutpoint(outpoint),
239  nTime(nTime),
240  fReady(fReady),
241  vchSig(std::vector<unsigned char>()),
242  fTried(false)
243  {
244  }
245 
247 
248  template <typename Stream, typename Operation>
249  inline void SerializationOp(Stream& s, Operation ser_action)
250  {
251  READWRITE(nDenom);
253  READWRITE(nTime);
254  READWRITE(fReady);
255  if (!(s.GetType() & SER_GETHASH)) {
256  READWRITE(vchSig);
257  }
258  }
259 
260  uint256 GetSignatureHash() const;
268  bool Sign();
270  bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
271 
272  bool Relay(CConnman& connman);
273 
275  bool IsTimeOutOfBounds() const;
276 
277  std::string ToString() const
278  {
279  return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s",
280  nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", masternodeOutpoint.ToStringShort());
281  }
282 
283  friend bool operator==(const CPrivateSendQueue& a, const CPrivateSendQueue& b)
284  {
285  return a.nDenom == b.nDenom && a.masternodeOutpoint == b.masternodeOutpoint && a.nTime == b.nTime && a.fReady == b.fReady;
286  }
287 };
288 
292 {
293 private:
294  // memory only
295  // when corresponding tx is 0-confirmed or conflicted, nConfirmedHeight is -1
297 
298 public:
301  std::vector<unsigned char> vchSig;
302  int64_t sigTime;
303 
305  nConfirmedHeight(-1),
308  vchSig(),
309  sigTime(0)
310  {
311  }
312 
313  CPrivateSendBroadcastTx(const CTransactionRef& _tx, COutPoint _outpoint, int64_t _sigTime) :
314  nConfirmedHeight(-1),
315  tx(_tx),
316  masternodeOutpoint(_outpoint),
317  vchSig(),
318  sigTime(_sigTime)
319  {
320  }
321 
323 
324  template <typename Stream, typename Operation>
325  inline void SerializationOp(Stream& s, Operation ser_action)
326  {
327  READWRITE(tx);
329  if (!(s.GetType() & SER_GETHASH)) {
330  READWRITE(vchSig);
331  }
333  }
334 
336  {
337  return *a.tx == *b.tx;
338  }
340  {
341  return !(a == b);
342  }
343  explicit operator bool() const
344  {
345  return *this != CPrivateSendBroadcastTx();
346  }
347 
348  uint256 GetSignatureHash() const;
349 
350  bool Sign();
351  bool CheckSignature(const CBLSPublicKey& blsPubKey) const;
352 
353  void SetConfirmedHeight(int nConfirmedHeightIn) { nConfirmedHeight = nConfirmedHeightIn; }
354  bool IsExpired(const CBlockIndex* pindex);
355  bool IsValidStructure();
356 };
357 
358 // base class
360 {
361 protected:
363 
364  std::vector<CPrivateSendEntry> vecEntries; // Masternode/clients entries
365 
366  PoolState nState; // should be one of the POOL_STATE_XXX values
367  int64_t nTimeLastSuccessfulStep; // the time when last successful mixing step was performed
368 
369  int nSessionID; // 0 if no mixing session is active
370 
371  CMutableTransaction finalMutableTransaction; // the finalized transaction ready for signing
372 
373  void SetNull();
374 
375  bool IsValidInOuts(const std::vector<CTxIn>& vin, const std::vector<CTxOut>& vout, PoolMessage& nMessageIDRet, bool* fConsumeCollateralRet) const;
376 
377 public:
378  int nSessionDenom; // Users must submit a denom matching this
379 
381  vecEntries(),
384  nSessionID(0),
386  nSessionDenom(0)
387  {
388  }
389 
390  int GetState() const { return nState; }
391  std::string GetStateString() const;
392 
393  int GetEntriesCount() const { return vecEntries.size(); }
394 };
395 
396 // base class
398 {
399 protected:
401 
402  // The current mixing sessions in progress on the network
403  std::vector<CPrivateSendQueue> vecPrivateSendQueue;
404 
405  void SetNull();
406  void CheckQueue();
407 
408 public:
411 
412  int GetQueueSize() const { return vecPrivateSendQueue.size(); }
414 };
415 
416 // helper class
418 {
419 private:
420  // make constructor, destructor and copying not available
423  CPrivateSend(CPrivateSend const&) = delete;
424  CPrivateSend& operator=(CPrivateSend const&) = delete;
425 
426  // static members
427  static std::vector<CAmount> vecStandardDenominations;
428  static std::map<uint256, CPrivateSendBroadcastTx> mapDSTX;
429 
431 
432  static void CheckDSTXes(const CBlockIndex* pindex);
433 
434 public:
435  static void InitStandardDenominations();
436  static std::vector<CAmount> GetStandardDenominations() { return vecStandardDenominations; }
438 
439  static bool IsDenominatedAmount(CAmount nInputAmount);
440  static bool IsValidDenomination(int nDenom);
441 
442  static int AmountToDenomination(CAmount nInputAmount);
443  static CAmount DenominationToAmount(int nDenom);
444  static std::string DenominationToString(int nDenom);
445 
446  static std::string GetMessageByID(PoolMessage nMessageID);
447 
455 
457 
459  static bool IsCollateralValid(const CTransaction& txCollateral);
462 
463  static bool IsCollateralAmount(CAmount nInputAmount);
464 
465  static void AddDSTX(const CPrivateSendBroadcastTx& dstx);
466  static CPrivateSendBroadcastTx GetDSTX(const uint256& hash);
467 
468  static void UpdatedBlockTip(const CBlockIndex* pindex);
469  static void NotifyChainLock(const CBlockIndex* pindex);
470 
471  static void UpdateDSTXConfirmedHeight(const CTransactionRef& tx, int nHeight);
472  static void TransactionAddedToMempool(const CTransactionRef& tx);
473  static void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted);
474  static void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected);
475 
476 };
477 
478 #endif
static void UpdatedBlockTip(const CBlockIndex *pindex)
std::vector< unsigned char > vchSig
Definition: privatesend.h:222
PoolStatusUpdate nStatusUpdate
Definition: privatesend.h:87
static CAmount GetSmallestDenomination()
Definition: privatesend.h:437
int PoolMaxParticipants() const
Definition: chainparams.h:93
static void CheckDSTXes(const CBlockIndex *pindex)
static bool IsDenominatedAmount(CAmount nInputAmount)
uint256 GetSignatureHash() const
Definition: privatesend.cpp:90
#define READWRITE(obj)
Definition: serialize.h:165
bool CheckSignature(const CBLSPublicKey &blsPubKey) const
Check if we have a valid Masternode address.
Definition: privatesend.cpp:60
ADD_SERIALIZE_METHODS
Definition: privatesend.h:198
static CAmount GetCollateralAmount()
Definition: privatesend.h:460
CTxDSIn(const CTxIn &txin, const CScript &script)
Definition: privatesend.h:128
If none of the specialized versions above matched and T is an enum, default to calling Serialize/Unse...
Definition: params.h:197
CScript prevPubKey
Definition: privatesend.h:125
std::vector< CPrivateSendEntry > vecEntries
Definition: privatesend.h:364
static void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindex, const std::vector< CTransactionRef > &vtxConflicted)
friend bool operator!=(const CPrivateSendBroadcastTx &a, const CPrivateSendBroadcastTx &b)
Definition: privatesend.h:339
#define strprintf
Definition: tinyformat.h:1066
CPrivateSendAccept(int nDenom, const CMutableTransaction &txCollateral)
Definition: privatesend.h:153
std::vector< CTxDSIn > vecTxDSIn
Definition: privatesend.h:176
A currently in progress mixing merge and denomination information.
Definition: privatesend.h:215
static int GetMaxPoolParticipants()
Definition: privatesend.h:452
CPrivateSendEntry(const std::vector< CTxDSIn > &vecTxDSIn, const std::vector< CTxOut > &vecTxOut, const CTransaction &txCollateral)
Definition: privatesend.h:190
CService addr
Definition: privatesend.h:180
PoolMessage
Definition: privatesend.h:33
bool Relay(CConnman &connman)
Definition: privatesend.cpp:74
Definition: box.hpp:161
static CCriticalSection cs_mapdstx
Definition: privatesend.h:430
static int AmountToDenomination(CAmount nInputAmount)
CMutableTransaction finalMutableTransaction
Definition: privatesend.h:371
static void NotifyChainLock(const CBlockIndex *pindex)
static const int PRIVATESEND_QUEUE_TIMEOUT
Definition: privatesend.h:24
static std::vector< CAmount > GetStandardDenominations()
Definition: privatesend.h:436
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:345
friend bool operator==(const CPrivateSendAccept &a, const CPrivateSendAccept &b)
Definition: privatesend.h:166
std::vector< CTxOut > vecTxOut
Definition: privatesend.h:177
static void BlockDisconnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexDisconnected)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static std::vector< CAmount > vecStandardDenominations
Definition: privatesend.h:427
COutPoint masternodeOutpoint
Definition: privatesend.h:300
static void UpdateDSTXConfirmedHeight(const CTransactionRef &tx, int nHeight)
static CTransactionRef MakeTransactionRef()
Definition: transaction.h:346
false
Definition: bls_dkg.cpp:168
PoolState
Definition: privatesend.h:63
static int GetMinPoolParticipants()
Get the minimum/maximum number of participants for the pool.
Definition: privatesend.h:449
static void InitStandardDenominations()
CTransactionRef txCollateral
Definition: privatesend.h:178
COutPoint masternodeOutpoint
Definition: privatesend.h:219
static bool IsCollateralValid(const CTransaction &txCollateral)
If the collateral is valid given by a client.
bool GetQueueItemAndTry(CPrivateSendQueue &dsqRet)
An input of a transaction.
Definition: transaction.h:70
static CPrivateSendBroadcastTx GetDSTX(const uint256 &hash)
uint256 GetSignatureHash() const
Definition: privatesend.cpp:40
CCriticalSection cs_vecqueue
Definition: privatesend.h:400
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
int PoolNewMinParticipants() const
Definition: chainparams.h:92
Definition: privatesend.h:173
int GetQueueSize() const
Definition: privatesend.h:412
std::vector< CPrivateSendQueue > vecPrivateSendQueue
Definition: privatesend.h:403
CPrivateSendBroadcastTx(const CTransactionRef &_tx, COutPoint _outpoint, int64_t _sigTime)
Definition: privatesend.h:313
CPrivateSendEntry()
Definition: privatesend.h:182
Definition: net.h:136
static void TransactionAddedToMempool(const CTransactionRef &tx)
std::vector< unsigned char > vchSig
Definition: privatesend.h:301
An output of a transaction.
Definition: transaction.h:144
static void AddDSTX(const CPrivateSendBroadcastTx &dstx)
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
bool IsExpired(const CBlockIndex *pindex)
void SerializationOp(Stream &s, Operation ser_action)
Definition: privatesend.h:249
CPrivateSend & operator=(CPrivateSend const &)=delete
PoolStatusUpdate
Definition: privatesend.h:75
static const int PRIVATESEND_SIGNING_TIMEOUT
Definition: privatesend.h:25
bool IsTimeOutOfBounds() const
Check if a queue is too old or too far into the future.
Definition: privatesend.cpp:85
void SerializationOp(Stream &s, Operation ser_action)
Definition: privatesend.h:201
CCriticalSection cs_privatesend
Definition: privatesend.h:362
CSporkManager sporkManager
Definition: spork.cpp:29
void SerializationOp(Stream &s, Operation ser_action)
Definition: privatesend.h:325
256-bit opaque blob.
Definition: uint256.h:123
std::string GetStateString() const
static bool IsValidDenomination(int nDenom)
Helper class to store mixing transaction (tx) information.
Definition: privatesend.h:291
int GetEntriesCount() const
Definition: privatesend.h:393
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
const CChainParams & Params()
Return the currently selected parameters.
static CAmount GetMaxPoolAmount()
Definition: privatesend.h:456
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:389
CPrivateSendQueue(int nDenom, COutPoint outpoint, int64_t nTime, bool fReady)
Definition: privatesend.h:236
static const int PRIVATESEND_AUTO_TIMEOUT_MAX
Definition: privatesend.h:23
static const int MIN_PRIVATESEND_PEER_PROTO_VERSION
minimum peer version accepted by mixing pool
Definition: privatesend.h:28
CPrivateSendStatusUpdate(int nSessionID, PoolState nState, int nEntriesCount, PoolStatusUpdate nStatusUpdate, PoolMessage nMessageID)
Definition: privatesend.h:97
bool Sign()
Sign this mixing transaction.
Definition: privatesend.cpp:45
static std::string GetMessageByID(PoolMessage nMessageID)
static std::map< uint256, CPrivateSendBroadcastTx > mapDSTX
Definition: privatesend.h:428
static CAmount GetMaxCollateralAmount()
Definition: privatesend.h:461
bool IsValidInOuts(const std::vector< CTxIn > &vin, const std::vector< CTxOut > &vout, PoolMessage &nMessageIDRet, bool *fConsumeCollateralRet) const
void SerializationOp(Stream &s, Operation ser_action)
Definition: privatesend.h:107
static const int PRIVATESEND_AUTO_TIMEOUT_MIN
Definition: privatesend.h:22
bool CheckSignature(const CBLSPublicKey &blsPubKey) const
static bool IsCollateralAmount(CAmount nInputAmount)
void SerializationOp(Stream &s, Operation ser_action)
Definition: privatesend.h:160
A mutable version of CTransaction.
Definition: transaction.h:291
bool AddScriptSig(const CTxIn &txin)
Definition: privatesend.cpp:24
bool fHasSig
Definition: privatesend.h:126
friend bool operator==(const CPrivateSendBroadcastTx &a, const CPrivateSendBroadcastTx &b)
Definition: privatesend.h:335
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
bool IsSporkActive(SporkId nSporkID)
IsSporkActive returns a bool for time-based sporks, and should be used to determine whether the spork...
Definition: spork.cpp:211
CMutableTransaction txCollateral
Definition: privatesend.h:147
CTransactionRef tx
Definition: privatesend.h:299
std::string ToString() const
Definition: privatesend.h:277
Holds a mixing input.
Definition: privatesend.h:121
static CAmount DenominationToAmount(int nDenom)
static std::string DenominationToString(int nDenom)
int PoolNewMaxParticipants() const
Definition: chainparams.h:94
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
int PoolMinParticipants() const
Definition: chainparams.h:91
friend bool operator==(const CPrivateSendQueue &a, const CPrivateSendQueue &b)
Definition: privatesend.h:283
void SetConfirmedHeight(int nConfirmedHeightIn)
Definition: privatesend.h:353
static const size_t PRIVATESEND_ENTRY_MAX_SIZE
Definition: privatesend.h:30
std::string ToStringShort() const
Definition: transaction.cpp:17
Released under the MIT license