Dash Core  0.12.2.1
P2P Digital Currency
privatesend.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 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 "chainparams.h"
10 #include "pubkey.h"
11 #include "sync.h"
12 #include "tinyformat.h"
13 #include "timedata.h"
14 
15 class CPrivateSend;
16 class CConnman;
17 
18 // timeouts
19 static const int PRIVATESEND_AUTO_TIMEOUT_MIN = 5;
20 static const int PRIVATESEND_AUTO_TIMEOUT_MAX = 15;
21 static const int PRIVATESEND_QUEUE_TIMEOUT = 30;
22 static const int PRIVATESEND_SIGNING_TIMEOUT = 15;
23 
25 static const int MIN_PRIVATESEND_PEER_PROTO_VERSION = 70208;
26 
28 
29 // pool responses
44  ERR_NOT_A_MN, // not used
55 };
56 
57 // pool states
58 enum PoolState {
67 };
68 
69 // status update message constants
73 };
74 
77 class CTxDSIn : public CTxIn
78 {
79 public:
80  bool fHasSig; // flag to indicate if signed
81  int nSentTimes; //times we've sent this anonymously
82 
83  CTxDSIn(const CTxIn& txin) :
84  CTxIn(txin),
85  fHasSig(false),
86  nSentTimes(0)
87  {}
88 
89  CTxDSIn() :
90  CTxIn(),
91  fHasSig(false),
92  nSentTimes(0)
93  {}
94 };
95 
98 class CTxDSOut : public CTxOut
99 {
100 public:
101  int nSentTimes; //times we've sent this anonymously
102 
103  CTxDSOut(const CTxOut& out) :
104  CTxOut(out),
105  nSentTimes(0)
106  {}
107 
109  CTxOut(),
110  nSentTimes(0)
111  {}
112 };
113 
114 // A clients transaction in the mixing pool
116 {
117 public:
118  std::vector<CTxDSIn> vecTxDSIn;
119  std::vector<CTxDSOut> vecTxDSOut;
121  // memory only
123 
125  vecTxDSIn(std::vector<CTxDSIn>()),
126  vecTxDSOut(std::vector<CTxDSOut>()),
128  addr(CService())
129  {}
130 
131  CDarkSendEntry(const std::vector<CTxIn>& vecTxIn, const std::vector<CTxOut>& vecTxOut, const CTransaction& txCollateral);
132 
134 
135  template <typename Stream, typename Operation>
136  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
140  }
141 
142  bool AddScriptSig(const CTxIn& txin);
143 };
144 
145 
150 {
151 public:
152  int nDenom;
154  int64_t nTime;
155  bool fReady; //ready for submit
156  std::vector<unsigned char> vchSig;
157  // memory only
158  bool fTried;
159 
161  nDenom(0),
162  vin(CTxIn()),
163  nTime(0),
164  fReady(false),
165  vchSig(std::vector<unsigned char>()),
166  fTried(false)
167  {}
168 
169  CDarksendQueue(int nDenom, COutPoint outpoint, int64_t nTime, bool fReady) :
170  nDenom(nDenom),
171  vin(CTxIn(outpoint)),
172  nTime(nTime),
173  fReady(fReady),
174  vchSig(std::vector<unsigned char>()),
175  fTried(false)
176  {}
177 
179 
180  template <typename Stream, typename Operation>
181  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
182  READWRITE(nDenom);
183  READWRITE(vin);
184  READWRITE(nTime);
185  READWRITE(fReady);
186  READWRITE(vchSig);
187  }
188 
196  bool Sign();
198  bool CheckSignature(const CPubKey& pubKeyMasternode);
199 
200  bool Relay(CConnman &connman);
201 
204 
205  std::string ToString()
206  {
207  return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s",
208  nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", vin.prevout.ToStringShort());
209  }
210 
211  friend bool operator==(const CDarksendQueue& a, const CDarksendQueue& b)
212  {
213  return a.nDenom == b.nDenom && a.vin.prevout == b.vin.prevout && a.nTime == b.nTime && a.fReady == b.fReady;
214  }
215 };
216 
220 {
221 private:
222  // memory only
223  // when corresponding tx is 0-confirmed or conflicted, nConfirmedHeight is -1
225 
226 public:
229  std::vector<unsigned char> vchSig;
230  int64_t sigTime;
231 
233  nConfirmedHeight(-1),
234  tx(),
235  vin(),
236  vchSig(),
237  sigTime(0)
238  {}
239 
241  nConfirmedHeight(-1),
242  tx(tx),
243  vin(CTxIn(outpoint)),
244  vchSig(),
246  {}
247 
249 
250  template <typename Stream, typename Operation>
251  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
252  READWRITE(tx);
253  READWRITE(vin);
254  READWRITE(vchSig);
256  }
257 
258  friend bool operator==(const CDarksendBroadcastTx& a, const CDarksendBroadcastTx& b)
259  {
260  return a.tx == b.tx;
261  }
262  friend bool operator!=(const CDarksendBroadcastTx& a, const CDarksendBroadcastTx& b)
263  {
264  return !(a == b);
265  }
266  explicit operator bool() const
267  {
268  return *this != CDarksendBroadcastTx();
269  }
270 
271  bool Sign();
272  bool CheckSignature(const CPubKey& pubKeyMasternode);
273 
274  void SetConfirmedHeight(int nConfirmedHeightIn) { nConfirmedHeight = nConfirmedHeightIn; }
275  bool IsExpired(int nHeight);
276 };
277 
278 // base class
280 {
281 protected:
282  // The current mixing sessions in progress on the network
283  std::vector<CDarksendQueue> vecDarksendQueue;
284 
285  std::vector<CDarkSendEntry> vecEntries; // Masternode/clients entries
286 
287  PoolState nState; // should be one of the POOL_STATE_XXX values
288  int64_t nTimeLastSuccessfulStep; // the time when last successful mixing step was performed, in UTC milliseconds
289 
290  int nSessionID; // 0 if no mixing session is active
291 
292  CMutableTransaction finalMutableTransaction; // the finalized transaction ready for signing
293 
294  void SetNull();
295 
296 public:
297  int nSessionDenom; //Users must submit an denom matching this
298 
300 
301  int GetQueueSize() const { return vecDarksendQueue.size(); }
302  int GetState() const { return nState; }
303  std::string GetStateString() const;
304 
305  int GetEntriesCount() const { return vecEntries.size(); }
306 };
307 
308 // helper class
310 {
311 private:
312  // make constructor, destructor and copying not available
315  CPrivateSend(CPrivateSend const&) = delete;
316  CPrivateSend& operator= (CPrivateSend const&) = delete;
317 
318  static const CAmount COLLATERAL = 0.001 * COIN;
319 
320  // static members
321  static std::vector<CAmount> vecStandardDenominations;
322  static std::map<uint256, CDarksendBroadcastTx> mapDSTX;
323 
325 
326 public:
327  static void InitStandardDenominations();
328  static std::vector<CAmount> GetStandardDenominations() { return vecStandardDenominations; }
330 
332  static int GetDenominationsByAmounts(const std::vector<CAmount>& vecAmount);
333 
335  static int GetDenominations(const std::vector<CTxOut>& vecTxOut, bool fSingleRandomDenom = false);
336  static int GetDenominations(const std::vector<CTxDSOut>& vecTxDSOut);
337  static std::string GetDenominationsToString(int nDenom);
338  static bool GetDenominationsBits(int nDenom, std::vector<int> &vecBitsRet);
339 
340  static std::string GetMessageByID(PoolMessage nMessageID);
341 
343  static int GetMaxPoolTransactions() { return Params().PoolMaxTransactions(); }
344 
346 
348  static bool IsCollateralValid(const CTransaction& txCollateral);
351 
352  static void AddDSTX(const CDarksendBroadcastTx& dstx);
353  static CDarksendBroadcastTx GetDSTX(const uint256& hash);
354  static void CheckDSTXes(int nHeight);
355 
356  static void SyncTransaction(const CTransaction& tx, const CBlock* pblock);
357 };
358 
359 void ThreadCheckPrivateSend(CConnman& connman);
360 
361 #endif
static CAmount GetMaxCollateralAmount()
Definition: privatesend.h:350
std::string GetStateString() const
PoolState nState
Definition: privatesend.h:287
friend bool operator==(const CDarksendBroadcastTx &a, const CDarksendBroadcastTx &b)
Definition: privatesend.h:258
#define READWRITE(obj)
Definition: serialize.h:175
CService addr
Definition: privatesend.h:122
bool fHasSig
Definition: privatesend.h:80
#define strprintf
Definition: tinyformat.h:1011
CDarksendQueue(int nDenom, COutPoint outpoint, int64_t nTime, bool fReady)
Definition: privatesend.h:169
static const CAmount COIN
Definition: amount.h:16
static std::string GetDenominationsToString(int nDenom)
int PoolMaxTransactions() const
Definition: chainparams.h:81
CPrivateSend & operator=(CPrivateSend const &)=delete
int nSentTimes
Definition: privatesend.h:81
bool AddScriptSig(const CTxIn &txin)
Definition: privatesend.cpp:31
Definition: net.h:108
static CAmount GetCollateralAmount()
Definition: privatesend.h:349
static CAmount GetSmallestDenomination()
Definition: privatesend.h:329
std::vector< unsigned char > vchSig
Definition: privatesend.h:229
static std::vector< CAmount > vecStandardDenominations
Definition: privatesend.h:321
static const int PRIVATESEND_QUEUE_TIMEOUT
Definition: privatesend.h:21
static void SyncTransaction(const CTransaction &tx, const CBlock *pblock)
PoolMessage
Definition: privatesend.h:30
static int GetDenominations(const std::vector< CTxOut > &vecTxOut, bool fSingleRandomDenom=false)
Get the denominations for a list of outputs (returns a bitshifted integer)
static void AddDSTX(const CDarksendBroadcastTx &dstx)
bool CheckSignature(const CPubKey &pubKeyMasternode)
std::vector< CTxDSIn > vecTxDSIn
Definition: privatesend.h:118
int GetQueueSize() const
Definition: privatesend.h:301
int64_t CAmount
Definition: amount.h:14
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: privatesend.h:136
bool IsExpired(int nHeight)
CTransaction txCollateral
Definition: privatesend.h:120
std::string ToString()
Definition: privatesend.h:205
static const CAmount PRIVATESEND_ENTRY_MAX_SIZE
Definition: privatesend.h:27
COutPoint prevout
Definition: transaction.h:61
static CCriticalSection cs_mapdstx
Definition: privatesend.h:324
bool Relay(CConnman &connman)
Definition: privatesend.cpp:75
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: privatesend.h:181
friend bool operator!=(const CDarksendBroadcastTx &a, const CDarksendBroadcastTx &b)
Definition: privatesend.h:262
int64_t nTimeLastSuccessfulStep
Definition: privatesend.h:288
static std::string GetMessageByID(PoolMessage nMessageID)
CDarksendBroadcastTx(CTransaction tx, COutPoint outpoint, int64_t sigTime)
Definition: privatesend.h:240
static bool GetDenominationsBits(int nDenom, std::vector< int > &vecBitsRet)
static const CAmount COLLATERAL
Definition: privatesend.h:318
CMutableTransaction finalMutableTransaction
Definition: privatesend.h:292
friend bool operator==(const CDarksendQueue &a, const CDarksendQueue &b)
Definition: privatesend.h:211
static const int PRIVATESEND_SIGNING_TIMEOUT
Definition: privatesend.h:22
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: privatesend.h:251
static std::map< uint256, CDarksendBroadcastTx > mapDSTX
Definition: privatesend.h:322
void ThreadCheckPrivateSend(CConnman &connman)
std::vector< CDarkSendEntry > vecEntries
Definition: privatesend.h:285
PoolStatusUpdate
Definition: privatesend.h:70
static std::vector< CAmount > GetStandardDenominations()
Definition: privatesend.h:328
int GetEntriesCount() const
Definition: privatesend.h:305
const CChainParams & Params()
CTxDSOut(const CTxOut &out)
Definition: privatesend.h:103
static CAmount GetMaxPoolAmount()
Definition: privatesend.h:345
void SetConfirmedHeight(int nConfirmedHeightIn)
Definition: privatesend.h:274
static int GetMaxPoolTransactions()
Get the maximum number of transactions for the pool.
Definition: privatesend.h:343
PoolState
Definition: privatesend.h:58
static const int PRIVATESEND_AUTO_TIMEOUT_MAX
Definition: privatesend.h:20
int64_t GetAdjustedTime()
Definition: timedata.cpp:33
static const int MIN_PRIVATESEND_PEER_PROTO_VERSION
minimum peer version accepted by mixing pool
Definition: privatesend.h:25
bool IsExpired()
Is this queue expired?
Definition: privatesend.h:203
bool CheckSignature(const CPubKey &pubKeyMasternode)
Check if we have a valid Masternode address.
Definition: privatesend.cpp:62
static const int PRIVATESEND_AUTO_TIMEOUT_MIN
Definition: privatesend.h:19
static bool IsCollateralValid(const CTransaction &txCollateral)
If the collateral is valid given by a client.
Definition: pubkey.h:37
int GetState() const
Definition: privatesend.h:302
int nSentTimes
Definition: privatesend.h:101
static void InitStandardDenominations()
Definition: block.h:73
std::string ToStringShort() const
Definition: transaction.cpp:17
CTxDSIn(const CTxIn &txin)
Definition: privatesend.h:83
static int GetDenominationsByAmounts(const std::vector< CAmount > &vecAmount)
Get the denominations for a specific amount of dash.
static void CheckDSTXes(int nHeight)
static CDarksendBroadcastTx GetDSTX(const uint256 &hash)
std::vector< CTxDSOut > vecTxDSOut
Definition: privatesend.h:119
std::vector< unsigned char > vchSig
Definition: privatesend.h:156
std::vector< CDarksendQueue > vecDarksendQueue
Definition: privatesend.h:283