Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

privatesend-client.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2019 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 PRIVATESENDCLIENT_H
6 #define PRIVATESENDCLIENT_H
7 
10 #include <wallet/wallet.h>
11 
12 #include <evo/deterministicmns.h>
13 
15 class CConnman;
16 class CNode;
17 class UniValue;
18 
19 static const int MIN_PRIVATESEND_SESSIONS = 1;
20 static const int MIN_PRIVATESEND_ROUNDS = 2;
21 static const int MIN_PRIVATESEND_AMOUNT = 2;
22 static const int MIN_PRIVATESEND_DENOMS_GOAL = 10;
23 static const int MIN_PRIVATESEND_DENOMS_HARDCAP = 10;
24 static const int MAX_PRIVATESEND_SESSIONS = 10;
25 static const int MAX_PRIVATESEND_ROUNDS = 16;
26 static const int MAX_PRIVATESEND_DENOMS_GOAL = 100000;
27 static const int MAX_PRIVATESEND_DENOMS_HARDCAP = 100000;
28 static const int MAX_PRIVATESEND_AMOUNT = MAX_MONEY / COIN;
29 static const int DEFAULT_PRIVATESEND_SESSIONS = 4;
30 static const int DEFAULT_PRIVATESEND_ROUNDS = 4;
31 static const int DEFAULT_PRIVATESEND_AMOUNT = 1000;
32 static const int DEFAULT_PRIVATESEND_DENOMS_GOAL = 50;
33 static const int DEFAULT_PRIVATESEND_DENOMS_HARDCAP = 300;
34 
35 static const bool DEFAULT_PRIVATESEND_AUTOSTART = false;
36 static const bool DEFAULT_PRIVATESEND_MULTISESSION = false;
37 
38 // How many new denom outputs to create before we consider the "goal" loop in CreateDenominated
39 // a final one and start creating an actual tx. Same limit applies for the "hard cap" part of the algo.
40 // NOTE: We do not allow txes larger than 100kB, so we have to limit the number of outputs here.
41 // We still want to create a lot of outputs though.
42 // Knowing that each CTxOut is ~35b big, 400 outputs should take 400 x ~35b = ~17.5kb.
43 // More than 500 outputs starts to make qt quite laggy.
44 // Additionally to need all 500 outputs (assuming a max per denom of 50) you'd need to be trying to
45 // create denominations for over 3000 dash!
46 static const int PRIVATESEND_DENOM_OUTPUTS_THRESHOLD = 500;
47 
48 // Warn user if mixing in gui or try to create backup if mixing in daemon mode
49 // when we have only this many keys left
50 static const int PRIVATESEND_KEYS_THRESHOLD_WARNING = 100;
51 // Stop mixing completely, it's too dangerous to continue when we have only this many keys left
52 static const int PRIVATESEND_KEYS_THRESHOLD_STOP = 50;
53 // Pseudorandomly mix up to this many times in addition to base round count
54 static const int PRIVATESEND_RANDOM_ROUNDS = 3;
55 
56 // The main object for accessing mixing
58 
60 {
61 private:
62  static const int TIMEOUT = 15;
63 
66  int64_t nTimeCreated;
67 
68 public:
70  addr(CService()),
72  nTimeCreated(0)
73  {
74  }
75 
76  CPendingDsaRequest(const CService& addr_, const CPrivateSendAccept& dsa_) :
77  addr(addr_),
78  dsa(dsa_),
80  {
81  }
82 
83  CService GetAddr() { return addr; }
85  bool IsExpired() { return GetTime() - nTimeCreated > TIMEOUT; }
86 
87  friend bool operator==(const CPendingDsaRequest& a, const CPendingDsaRequest& b)
88  {
89  return a.addr == b.addr && a.dsa == b.dsa;
90  }
91  friend bool operator!=(const CPendingDsaRequest& a, const CPendingDsaRequest& b)
92  {
93  return !(a == b);
94  }
95  explicit operator bool() const
96  {
97  return *this != CPendingDsaRequest();
98  }
99 };
100 
102 {
103 private:
104  std::vector<COutPoint> vecOutPointLocked;
105 
106  std::string strLastMessage;
107  std::string strAutoDenomResult;
108 
110  CMutableTransaction txMyCollateral; // client side collateral
112 
113  CKeyHolderStorage keyHolderStorage; // storage for keys used in PrepareDenominate
114 
116  bool CreateDenominated(CAmount nBalanceToDenominate);
117  bool CreateDenominated(CAmount nBalanceToDenominate, const CompactTallyItem& tallyItem, bool fCreateMixingCollaterals);
118 
120  bool MakeCollateralAmounts();
121  bool MakeCollateralAmounts(const CompactTallyItem& tallyItem, bool fTryDenominated);
122 
123  bool JoinExistingQueue(CAmount nBalanceNeedsAnonymized, CConnman& connman);
124  bool StartNewQueue(CAmount nBalanceNeedsAnonymized, CConnman& connman);
125 
127  bool SelectDenominate(std::string& strErrorRet, std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsRet);
129  bool PrepareDenominate(int nMinRounds, int nMaxRounds, std::string& strErrorRet, const std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsIn, std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsRet, bool fDryRun = false);
131  bool SendDenominate(const std::vector<std::pair<CTxDSIn, CTxOut> >& vecPSInOutPairsIn, CConnman& connman);
132 
135  // Set the 'state' value, with some logging and capturing when the state changed
136  void SetState(PoolState nStateNew);
137 
138  void CompletedTransaction(PoolMessage nMessageID);
139 
141  bool SignFinalTransaction(const CTransaction& finalTransactionNew, CNode* pnode, CConnman& connman);
142 
143  void RelayIn(const CPrivateSendEntry& entry, CConnman& connman);
144 
145  void SetNull();
146 
147 public:
150  strLastMessage(),
153  txMyCollateral(),
156  {
157  }
158 
159  void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
160 
161  void UnlockCoins();
162 
163  void ResetPool();
164 
165  std::string GetStatus(bool fWaitForBlock);
166 
168 
170  bool DoAutomaticDenominating(CConnman& connman, bool fDryRun = false);
171 
173  bool SubmitDenominate(CConnman& connman);
174 
175  bool ProcessPendingDsaRequest(CConnman& connman);
176 
177  bool CheckTimeout();
178 
179  void GetJsonInfo(UniValue& obj) const;
180 };
181 
185 {
186 private:
187  // Keep track of the used Masternodes
188  std::vector<COutPoint> vecMasternodesUsed;
189 
190  // TODO: or map<denom, CPrivateSendClientSession> ??
191  std::deque<CPrivateSendClientSession> deqSessions;
193 
195  int nMinBlocksToWait; // how many blocks to wait after one successful mixing tx in non-multisession mode
196  std::string strAutoDenomResult;
197 
198  // Keep track of current block height
200 
201  bool WaitForAnotherBlock();
202 
203  // Make sure we have enough keys since last backup
204  bool CheckAutomaticBackup();
205 
206 public:
216 
217  int nCachedNumBlocks; // used for the overview screen
218  bool fCreateAutoBackups; // builtin support for automatic backups
219 
222  deqSessions(),
224  nMinBlocksToWait(1),
235  nCachedNumBlocks(std::numeric_limits<int>::max()),
237  {
238  }
239 
240  void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);
241 
242  void ResetPool();
243 
244  std::string GetStatuses();
245  std::string GetSessionDenoms();
246 
247  bool GetMixingMasternodesInfo(std::vector<CDeterministicMNCPtr>& vecDmnsRet) const;
248 
250  bool DoAutomaticDenominating(CConnman& connman, bool fDryRun = false);
251 
252  void CheckTimeout();
253 
254  void ProcessPendingDsaRequest(CConnman& connman);
255 
256  void AddUsedMasternode(const COutPoint& outpointMn);
258 
259  void UpdatedSuccessBlock();
260 
261  void UpdatedBlockTip(const CBlockIndex* pindex);
262 
263  void DoMaintenance(CConnman& connman);
264 
265  void GetJsonInfo(UniValue& obj) const;
266 };
267 
268 #endif
bool SubmitDenominate(CConnman &connman)
As a client, submit part of a future mixing transaction to a Masternode to start the process...
CPendingDsaRequest(const CService &addr_, const CPrivateSendAccept &dsa_)
static const bool DEFAULT_PRIVATESEND_AUTOSTART
Used to keep track of current status of mixing pool.
void SetState(PoolState nStateNew)
void UpdatedBlockTip(const CBlockIndex *pindex)
static const int DEFAULT_PRIVATESEND_DENOMS_HARDCAP
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
bool PrepareDenominate(int nMinRounds, int nMaxRounds, std::string &strErrorRet, const std::vector< std::pair< CTxDSIn, CTxOut > > &vecPSInOutPairsIn, std::vector< std::pair< CTxDSIn, CTxOut > > &vecPSInOutPairsRet, bool fDryRun=false)
step 1: prepare denominated inputs and outputs
static const CAmount COIN
Definition: amount.h:14
bool SendDenominate(const std::vector< std::pair< CTxDSIn, CTxOut > > &vecPSInOutPairsIn, CConnman &connman)
step 2: send denominated inputs and outputs prepared in step 1
static const int DEFAULT_PRIVATESEND_AMOUNT
static const int TIMEOUT
void ProcessPoolStateUpdate(CPrivateSendStatusUpdate psssup)
Process Masternode updates about the progress of mixing.
CPrivateSendClientManager privateSendClient
static const int PRIVATESEND_KEYS_THRESHOLD_WARNING
PoolMessage
Definition: privatesend.h:33
void GetJsonInfo(UniValue &obj) const
Definition: box.hpp:161
false true true true
Definition: bls_dkg.cpp:176
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
std::shared_ptr< const CDeterministicMN > CDeterministicMNCPtr
friend bool operator==(const CPendingDsaRequest &a, const CPendingDsaRequest &b)
bool GetMixingMasternodeInfo(CDeterministicMNCPtr &ret) const
static const int MAX_PRIVATESEND_DENOMS_HARDCAP
CDeterministicMNCPtr mixingMasternode
static const int PRIVATESEND_RANDOM_ROUNDS
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: utiltime.cpp:22
false
Definition: bls_dkg.cpp:168
PoolState
Definition: privatesend.h:63
static const int DEFAULT_PRIVATESEND_ROUNDS
bool StartNewQueue(CAmount nBalanceNeedsAnonymized, CConnman &connman)
bool DoAutomaticDenominating(CConnman &connman, bool fDryRun=false)
Passively run mixing in the background according to the configuration in settings.
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:143
Definition: privatesend.h:173
bool ProcessPendingDsaRequest(CConnman &connman)
static const int DEFAULT_PRIVATESEND_SESSIONS
CPrivateSendAccept GetDSA()
std::vector< COutPoint > vecMasternodesUsed
bool SignFinalTransaction(const CTransaction &finalTransactionNew, CNode *pnode, CConnman &connman)
As a client, check and sign the final transaction.
Definition: net.h:136
friend bool operator!=(const CPendingDsaRequest &a, const CPendingDsaRequest &b)
static const int MAX_PRIVATESEND_ROUNDS
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CDeterministicMNCPtr GetRandomNotUsedMasternode()
void CompletedTransaction(PoolMessage nMessageID)
bool SelectDenominate(std::string &strErrorRet, std::vector< std::pair< CTxDSIn, CTxOut > > &vecPSInOutPairsRet)
step 0: select denominated inputs and txouts
bool CreateDenominated(CAmount nBalanceToDenominate)
Create denominations.
static const int MAX_PRIVATESEND_AMOUNT
CKeyHolderStorage keyHolderStorage
void ProcessPendingDsaRequest(CConnman &connman)
static const int MAX_PRIVATESEND_SESSIONS
static const int PRIVATESEND_DENOM_OUTPUTS_THRESHOLD
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
CPendingDsaRequest pendingDsaRequest
static const int DEFAULT_PRIVATESEND_DENOMS_GOAL
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
static const int MIN_PRIVATESEND_DENOMS_GOAL
void DoMaintenance(CConnman &connman)
static const int MIN_PRIVATESEND_DENOMS_HARDCAP
void GetJsonInfo(UniValue &obj) const
static const int MIN_PRIVATESEND_ROUNDS
void RelayIn(const CPrivateSendEntry &entry, CConnman &connman)
static const int PRIVATESEND_KEYS_THRESHOLD_STOP
std::string GetStatus(bool fWaitForBlock)
bool GetMixingMasternodesInfo(std::vector< CDeterministicMNCPtr > &vecDmnsRet) const
static const int MAX_PRIVATESEND_DENOMS_GOAL
static const int MIN_PRIVATESEND_SESSIONS
A mutable version of CTransaction.
Definition: transaction.h:291
std::vector< COutPoint > vecOutPointLocked
bool DoAutomaticDenominating(CConnman &connman, bool fDryRun=false)
Passively run mixing in the background according to the configuration in settings.
bool JoinExistingQueue(CAmount nBalanceNeedsAnonymized, CConnman &connman)
std::deque< CPrivateSendClientSession > deqSessions
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:198
bool MakeCollateralAmounts()
Split up large inputs or make fee sized inputs.
Information about a peer.
Definition: net.h:800
static const int MIN_PRIVATESEND_AMOUNT
CPrivateSendAccept dsa
void AddUsedMasternode(const COutPoint &outpointMn)
CMutableTransaction txMyCollateral
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
static const bool DEFAULT_PRIVATESEND_MULTISESSION
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
Released under the MIT license