66#include < wallet/coinjoin.h>
77
88#include < key_io.h>
9- #include < coinjoin/common.h>
10- #include < coinjoin/options.h>
11- #include < evo/dmn_types.h>
129#include < wallet/receive.h>
1310#include < wallet/spend.h>
14- #include < wallet/wallet.h>
1511#include < wallet/transaction.h>
12+ #include < wallet/wallet.h>
13+
14+ #include < coinjoin/common.h>
15+ #include < coinjoin/options.h>
16+ #include < evo/dmn_types.h>
1617
1718namespace wallet {
1819void CWallet::InitCJSaltFromDb ()
@@ -78,21 +79,20 @@ bool CWallet::SelectTxDSInsByDenomination(int nDenom, CAmount nValueMax, std::ve
7879 nValueTotal += nValue;
7980 vecTxDSInRet.emplace_back (CTxDSIn (txin, scriptPubKey, nRounds));
8081 setRecentTxIds.emplace (txHash);
81- WalletCJLogPrint (this , " CWallet::%s -- hash: %s, nValue: %d.%08d\n " ,
82- __func__, txHash. ToString (), nValue / COIN, nValue % COIN);
82+ WalletCJLogPrint (this , " CWallet::%s -- hash: %s, nValue: %d.%08d\n " , __func__, txHash. ToString (), nValue / COIN,
83+ nValue % COIN);
8384 }
8485
8586 WalletCJLogPrint (this , " CWallet::%s -- setRecentTxIds.size(): %d\n " , __func__, setRecentTxIds.size ());
8687
8788 return nValueTotal > 0 ;
8889}
8990
90- struct CompareByPriority
91- {
92- bool operator ()(const COutput& t1,
93- const COutput& t2) const
91+ struct CompareByPriority {
92+ bool operator ()(const COutput& t1, const COutput& t2) const
9493 {
95- return CoinJoin::CalculateAmountPriority (t1.GetEffectiveValue ()) > CoinJoin::CalculateAmountPriority (t2.GetEffectiveValue ());
94+ return CoinJoin::CalculateAmountPriority (t1.GetEffectiveValue ()) >
95+ CoinJoin::CalculateAmountPriority (t2.GetEffectiveValue ());
9696 }
9797};
9898
@@ -121,21 +121,24 @@ bool CWallet::SelectDenominatedAmounts(CAmount nValueMax, std::set<CAmount>& set
121121 return nValueTotal >= CoinJoin::GetSmallestDenomination ();
122122}
123123
124- std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses (bool fSkipDenominated , bool fAnonymizable , bool fSkipUnconfirmed , int nMaxOupointsPerAddress) const
124+ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses (bool fSkipDenominated , bool fAnonymizable ,
125+ bool fSkipUnconfirmed , int nMaxOupointsPerAddress) const
125126{
126127 LOCK (cs_wallet);
127128
128129 isminefilter filter = ISMINE_SPENDABLE;
129130
130131 // Try using the cache for already confirmed mixable inputs.
131132 // This should only be used if nMaxOupointsPerAddress was NOT specified.
132- if (nMaxOupointsPerAddress == -1 && fAnonymizable && fSkipUnconfirmed ) {
133- if (fSkipDenominated && fAnonymizableTallyCachedNonDenom ) {
134- LogPrint (BCLog::SELECTCOINS, " SelectCoinsGroupedByAddresses - using cache for non-denom inputs %d\n " , vecAnonymizableTallyCachedNonDenom.size ());
133+ if (nMaxOupointsPerAddress == -1 && fAnonymizable && fSkipUnconfirmed ) {
134+ if (fSkipDenominated && fAnonymizableTallyCachedNonDenom ) {
135+ LogPrint (BCLog::SELECTCOINS, " SelectCoinsGroupedByAddresses - using cache for non-denom inputs %d\n " ,
136+ vecAnonymizableTallyCachedNonDenom.size ());
135137 return vecAnonymizableTallyCachedNonDenom;
136138 }
137- if (!fSkipDenominated && fAnonymizableTallyCached ) {
138- LogPrint (BCLog::SELECTCOINS, " SelectCoinsGroupedByAddresses - using cache for all inputs %d\n " , vecAnonymizableTallyCached.size ());
139+ if (!fSkipDenominated && fAnonymizableTallyCached ) {
140+ LogPrint (BCLog::SELECTCOINS, " SelectCoinsGroupedByAddresses - using cache for all inputs %d\n " ,
141+ vecAnonymizableTallyCached.size ());
139142 return vecAnonymizableTallyCached;
140143 }
141144 }
@@ -146,13 +149,12 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
146149 std::map<CTxDestination, CompactTallyItem> mapTally;
147150 std::set<uint256> setWalletTxesCounted;
148151 for (const auto & outpoint : setWalletUTXO) {
149-
150152 if (!setWalletTxesCounted.emplace (outpoint.hash ).second ) continue ;
151153
152- const auto it = mapWallet.find (outpoint.hash );
154+ const auto it{ mapWallet.find (outpoint.hash )} ;
153155 if (it == mapWallet.end ()) continue ;
154156
155- const CWalletTx& wtx = (*it).second ;
157+ const CWalletTx& wtx{ (*it).second } ;
156158
157159 if (wtx.IsCoinBase () && GetTxBlocksToMaturity (wtx) > 0 ) continue ;
158160 if (fSkipUnconfirmed && !CachedTxIsTrusted (*this , wtx)) continue ;
@@ -163,23 +165,26 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
163165 if (!ExtractDestination (wtx.tx ->vout [i].scriptPubKey , txdest)) continue ;
164166
165167 isminefilter mine = IsMine (txdest);
166- if (!(mine & filter)) continue ;
168+ if (!(mine & filter)) continue ;
167169
168170 auto itTallyItem = mapTally.find (txdest);
169- if (nMaxOupointsPerAddress != -1 && itTallyItem != mapTally.end () && int64_t (itTallyItem->second .outpoints .size ()) >= nMaxOupointsPerAddress) continue ;
171+ if (nMaxOupointsPerAddress != -1 && itTallyItem != mapTally.end () &&
172+ int64_t (itTallyItem->second .outpoints .size ()) >= nMaxOupointsPerAddress) {
173+ continue ;
174+ }
170175
171176 COutPoint target_outpoint (outpoint.hash , i);
172- if (IsSpent (target_outpoint) || IsLockedCoin (target_outpoint)) continue ;
177+ if (IsSpent (target_outpoint) || IsLockedCoin (target_outpoint)) continue ;
173178
174- if (fSkipDenominated && CoinJoin::IsDenominatedAmount (wtx.tx ->vout [i].nValue )) continue ;
179+ if (fSkipDenominated && CoinJoin::IsDenominatedAmount (wtx.tx ->vout [i].nValue )) continue ;
175180
176- if (fAnonymizable ) {
181+ if (fAnonymizable ) {
177182 // ignore collaterals
178- if (CoinJoin::IsCollateralAmount (wtx.tx ->vout [i].nValue )) continue ;
183+ if (CoinJoin::IsCollateralAmount (wtx.tx ->vout [i].nValue )) continue ;
179184 if (fMasternodeMode && dmn_types::IsCollateralAmount (wtx.tx ->vout [i].nValue )) continue ;
180185 // ignore outputs that are 10 times smaller then the smallest denomination
181186 // otherwise they will just lead to higher fee / lower priority
182- if (wtx.tx ->vout [i].nValue <= nSmallestDenom/ 10 ) continue ;
187+ if (wtx.tx ->vout [i].nValue <= nSmallestDenom / 10 ) continue ;
183188 // ignore mixed
184189 if (IsFullyMixed (target_outpoint)) continue ;
185190 }
@@ -197,14 +202,14 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
197202 // NOTE: vecTallyRet is "sorted" by txdest (i.e. address), just like mapTally
198203 std::vector<CompactTallyItem> vecTallyRet;
199204 for (const auto & item : mapTally) {
200- if (fAnonymizable && item.second .nAmount < nSmallestDenom) continue ;
205+ if (fAnonymizable && item.second .nAmount < nSmallestDenom) continue ;
201206 vecTallyRet.push_back (item.second );
202207 }
203208
204209 // Cache already confirmed mixable entries for later use.
205210 // This should only be used if nMaxOupointsPerAddress was NOT specified.
206- if (nMaxOupointsPerAddress == -1 && fAnonymizable && fSkipUnconfirmed ) {
207- if (fSkipDenominated ) {
211+ if (nMaxOupointsPerAddress == -1 && fAnonymizable && fSkipUnconfirmed ) {
212+ if (fSkipDenominated ) {
208213 vecAnonymizableTallyCachedNonDenom = vecTallyRet;
209214 fAnonymizableTallyCachedNonDenom = true ;
210215 } else {
@@ -216,8 +221,9 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
216221 // debug
217222 if (LogAcceptDebug (BCLog::SELECTCOINS)) {
218223 std::string strMessage = " SelectCoinsGroupedByAddresses - vecTallyRet:\n " ;
219- for (const auto & item : vecTallyRet)
220- strMessage += strprintf (" %s %f\n " , EncodeDestination (item.txdest ), float (item.nAmount )/COIN);
224+ for (const auto & item : vecTallyRet) {
225+ strMessage += strprintf (" %s %f\n " , EncodeDestination (item.txdest ), float (item.nAmount ) / COIN);
226+ }
221227 LogPrint (BCLog::SELECTCOINS, " %s" , strMessage); /* Continued */
222228 }
223229
@@ -241,7 +247,7 @@ int CWallet::CountInputsWithAmount(CAmount nInputAmount) const
241247 LOCK (cs_wallet);
242248
243249 for (const auto & outpoint : setWalletUTXO) {
244- const auto it = mapWallet.find (outpoint.hash );
250+ const auto it{ mapWallet.find (outpoint.hash )} ;
245251 if (it == mapWallet.end ()) continue ;
246252 if (it->second .tx ->vout [outpoint.n ].nValue != nInputAmount) continue ;
247253 if (GetTxDepthInMainChain (it->second ) < 0 ) continue ;
@@ -257,7 +263,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
257263{
258264 LOCK (cs_wallet);
259265
260- const int nRoundsMax = MAX_COINJOIN_ROUNDS + CCoinJoinClientOptions::GetRandomRounds ();
266+ const int nRoundsMax{ MAX_COINJOIN_ROUNDS + CCoinJoinClientOptions::GetRandomRounds ()} ;
261267
262268 if (nRounds >= nRoundsMax) {
263269 // there can only be nRoundsMax rounds max
@@ -272,7 +278,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
272278 }
273279
274280 // TODO wtx should refer to a CWalletTx object, not a pointer, based on surrounding code
275- const CWalletTx* wtx = GetWalletTx (outpoint.hash );
281+ const CWalletTx* wtx{ GetWalletTx (outpoint.hash )} ;
276282
277283 if (wtx == nullptr || wtx->tx == nullptr ) {
278284 // no such tx in this wallet
@@ -298,7 +304,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
298304 }
299305
300306 // make sure the final output is non-denominate
301- if (!CoinJoin::IsDenominatedAmount (txOutRef->nValue )) { // NOT DENOM
307+ if (!CoinJoin::IsDenominatedAmount (txOutRef->nValue )) { // NOT DENOM
302308 *nRoundsRef = -2 ;
303309 WalletCJLogPrint (this , " %s UPDATED %-70s %3d\n " , __func__, outpoint.ToStringShort (), *nRoundsRef);
304310 return *nRoundsRef;
@@ -327,15 +333,20 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
327333 if (InputIsMine (*this , txinNext)) {
328334 int n = GetRealOutpointCoinJoinRounds (txinNext.prevout , nRounds + 1 );
329335 // denom found, find the shortest chain or initially assign nShortest with the first found value
330- if (n >= 0 && (n < nShortest || nShortest == -10 )) {
336+ if (n >= 0 && (n < nShortest || nShortest == -10 )) {
331337 nShortest = n;
332338 fDenomFound = true ;
333339 }
334340 }
335341 }
336- *nRoundsRef = fDenomFound
337- ? (nShortest >= nRoundsMax - 1 ? nRoundsMax : nShortest + 1 ) // good, we a +1 to the shortest one but only nRoundsMax rounds max allowed
338- : 0 ; // too bad, we are the fist one in that chain
342+ *nRoundsRef = [&]() {
343+ if (fDenomFound ) {
344+ // good, we a +1 to the shortest one but only nRoundsMax rounds max allowed
345+ return nShortest >= nRoundsMax - 1 ? nRoundsMax : nShortest + 1 ;
346+ }
347+ // too bad, we are the first one in that chain
348+ return 0 ;
349+ }();
339350 WalletCJLogPrint (this , " %s UPDATED %-70s %3d\n " , __func__, outpoint.ToStringShort (), *nRoundsRef);
340351 return *nRoundsRef;
341352}
@@ -345,7 +356,8 @@ int CWallet::GetCappedOutpointCoinJoinRounds(const COutPoint& outpoint) const
345356{
346357 LOCK (cs_wallet);
347358 int realCoinJoinRounds = GetRealOutpointCoinJoinRounds (outpoint);
348- return realCoinJoinRounds > CCoinJoinClientOptions::GetRounds () ? CCoinJoinClientOptions::GetRounds () : realCoinJoinRounds;
359+ return realCoinJoinRounds > CCoinJoinClientOptions::GetRounds () ? CCoinJoinClientOptions::GetRounds ()
360+ : realCoinJoinRounds;
349361}
350362
351363void CWallet::ClearCoinJoinRoundsCache ()
@@ -361,7 +373,7 @@ bool CWallet::IsDenominated(const COutPoint& outpoint) const
361373{
362374 LOCK (cs_wallet);
363375
364- const auto it = mapWallet.find (outpoint.hash );
376+ const auto it{ mapWallet.find (outpoint.hash )} ;
365377 if (it == mapWallet.end ()) {
366378 return false ;
367379 }
@@ -387,7 +399,7 @@ bool CWallet::IsFullyMixed(const COutPoint& outpoint) const
387399 CDataStream ss (SER_GETHASH, PROTOCOL_VERSION);
388400 ss << outpoint << nCoinJoinSalt;
389401 uint256 nHash;
390- CSHA256 ().Write (( const unsigned char *) ss.data (), ss.size ()).Finalize (nHash.begin ());
402+ CSHA256 ().Write (reinterpret_cast < const uint8_t *>( ss.data () ), ss.size ()).Finalize (nHash.begin ());
391403 if (ReadLE64 (nHash.begin ()) % 2 == 0 ) {
392404 return false ;
393405 }
@@ -428,14 +440,13 @@ CAmount CWallet::GetAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfi
428440
429441 CAmount nTotal = 0 ;
430442
431- const CAmount nSmallestDenom = CoinJoin::GetSmallestDenomination ();
432- const CAmount nMixingCollateral = CoinJoin::GetCollateralAmount ();
443+ const CAmount nSmallestDenom{ CoinJoin::GetSmallestDenomination ()} ;
444+ const CAmount nMixingCollateral{ CoinJoin::GetCollateralAmount ()} ;
433445 for (const auto & item : vecTally) {
434446 bool fIsDenominated = CoinJoin::IsDenominatedAmount (item.nAmount );
435- if (fSkipDenominated && fIsDenominated ) continue ;
447+ if (fSkipDenominated && fIsDenominated ) continue ;
436448 // assume that the fee to create denoms should be mixing collateral at max
437- if (item.nAmount >= nSmallestDenom + (fIsDenominated ? 0 : nMixingCollateral))
438- nTotal += item.nAmount ;
449+ if (item.nAmount >= nSmallestDenom + (fIsDenominated ? 0 : nMixingCollateral)) nTotal += item.nAmount ;
439450 }
440451
441452 return nTotal;
@@ -452,15 +463,15 @@ float CWallet::GetAverageAnonymizedRounds() const
452463
453464 LOCK (cs_wallet);
454465 for (const auto & outpoint : setWalletUTXO) {
455- if (!IsDenominated (outpoint)) continue ;
466+ if (!IsDenominated (outpoint)) continue ;
456467
457468 nTotal += GetCappedOutpointCoinJoinRounds (outpoint);
458469 nCount++;
459470 }
460471
461- if (nCount == 0 ) return 0 ;
472+ if (nCount == 0 ) return 0 ;
462473
463- return (float )nTotal/ nCount;
474+ return (float )nTotal / nCount;
464475}
465476
466477// Note: calculated including unconfirmed,
@@ -473,7 +484,7 @@ CAmount CWallet::GetNormalizedAnonymizedBalance() const
473484
474485 LOCK (cs_wallet);
475486 for (const auto & outpoint : setWalletUTXO) {
476- const auto it = mapWallet.find (outpoint.hash );
487+ const auto it{ mapWallet.find (outpoint.hash )} ;
477488 if (it == mapWallet.end ()) continue ;
478489
479490 CAmount nValue = it->second .tx ->vout [outpoint.n ].nValue ;
@@ -492,26 +503,29 @@ CAmount CachedTxGetAnonymizedCredit(const CWallet& wallet, const CWalletTx& wtx,
492503 AssertLockHeld (wallet.cs_wallet );
493504
494505 // Exclude coinbase and conflicted txes
495- if (wtx.IsCoinBase () || wallet.GetTxDepthInMainChain (wtx) < 0 )
496- return 0 ;
506+ if (wtx.IsCoinBase () || wallet.GetTxDepthInMainChain (wtx) < 0 ) return 0 ;
497507
498508 CAmount nCredit = 0 ;
499509 uint256 hashTx = wtx.GetHash ();
500- for (unsigned int i = 0 ; i < wtx.tx ->vout .size (); i++)
501- {
502- const CTxOut &txout = wtx.tx ->vout [i];
503- const COutPoint outpoint = COutPoint (hashTx, i);
510+ for (unsigned int i = 0 ; i < wtx.tx ->vout .size (); i++) {
511+ const CTxOut& txout{wtx.tx ->vout [i]};
512+ const COutPoint outpoint (hashTx, i);
504513
505514 if (coinControl.HasSelected () && !coinControl.IsSelected (outpoint)) {
506515 continue ;
507516 }
508517
509- if (wallet.IsSpent (outpoint) || !CoinJoin::IsDenominatedAmount (txout.nValue )) continue ;
518+ if (wallet.IsSpent (outpoint) || !CoinJoin::IsDenominatedAmount (txout.nValue )) {
519+ continue ;
520+ }
510521
511- if (wallet.IsFullyMixed (outpoint)) {
512- nCredit += OutputGetCredit (wallet, txout, ISMINE_SPENDABLE);
513- if (!MoneyRange (nCredit))
514- throw std::runtime_error (std::string (__func__) + " : value out of range" );
522+ if (!wallet.IsFullyMixed (outpoint)) {
523+ continue ;
524+ }
525+
526+ nCredit += OutputGetCredit (wallet, txout, ISMINE_SPENDABLE);
527+ if (!MoneyRange (nCredit)) {
528+ throw std::runtime_error (std::string (__func__) + " : value out of range" );
515529 }
516530 }
517531
@@ -525,8 +539,7 @@ CoinJoinCredits CachedTxGetAvailableCoinJoinCredits(const CWallet& wallet, const
525539 AssertLockHeld (wallet.cs_wallet );
526540
527541 // Must wait until coinbase is safely deep enough in the chain before valuing it
528- if (wtx.IsCoinBase () && wallet.GetTxBlocksToMaturity (wtx) > 0 )
529- return ret;
542+ if (wtx.IsCoinBase () && wallet.GetTxBlocksToMaturity (wtx) > 0 ) return ret;
530543
531544 int nDepth = wallet.GetTxDepthInMainChain (wtx);
532545 if (nDepth < 0 ) return ret;
@@ -535,29 +548,35 @@ CoinJoinCredits CachedTxGetAvailableCoinJoinCredits(const CWallet& wallet, const
535548
536549 if (wtx.m_amounts [CWalletTx::ANON_CREDIT].m_cached [ISMINE_SPENDABLE]) {
537550 if (ret.is_unconfirmed && wtx.m_amounts [CWalletTx::DENOM_UCREDIT].m_cached [ISMINE_SPENDABLE]) {
538- return {wtx.m_amounts [CWalletTx::ANON_CREDIT].m_value [ISMINE_SPENDABLE], wtx.m_amounts [CWalletTx::DENOM_UCREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
551+ return {wtx.m_amounts [CWalletTx::ANON_CREDIT].m_value [ISMINE_SPENDABLE],
552+ wtx.m_amounts [CWalletTx::DENOM_UCREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
539553 } else if (!ret.is_unconfirmed && wtx.m_amounts [CWalletTx::DENOM_CREDIT].m_cached [ISMINE_SPENDABLE]) {
540- return {wtx.m_amounts [CWalletTx::ANON_CREDIT].m_value [ISMINE_SPENDABLE], wtx.m_amounts [CWalletTx::DENOM_CREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
554+ return {wtx.m_amounts [CWalletTx::ANON_CREDIT].m_value [ISMINE_SPENDABLE],
555+ wtx.m_amounts [CWalletTx::DENOM_CREDIT].m_value [ISMINE_SPENDABLE], ret.is_unconfirmed };
541556 }
542557 }
543558
544559 uint256 hashTx = wtx.GetHash ();
545560 for (unsigned int i = 0 ; i < wtx.tx ->vout .size (); i++) {
546- const CTxOut &txout = wtx.tx ->vout [i];
547- const COutPoint outpoint = COutPoint (hashTx, i);
561+ const CTxOut& txout{ wtx.tx ->vout [i]} ;
562+ const COutPoint outpoint (hashTx, i);
548563
549- if (wallet.IsSpent (outpoint) || !CoinJoin::IsDenominatedAmount (txout.nValue )) continue ;
550- const CAmount credit = OutputGetCredit (wallet, txout, ISMINE_SPENDABLE);
564+ if (wallet.IsSpent (outpoint) || !CoinJoin::IsDenominatedAmount (txout.nValue )) {
565+ continue ;
566+ }
551567
568+ const CAmount credit{OutputGetCredit (wallet, txout, ISMINE_SPENDABLE)};
552569 if (wallet.IsFullyMixed (outpoint)) {
553570 ret.m_anonymized += credit;
554- if (!MoneyRange (ret.m_anonymized ))
571+ if (!MoneyRange (ret.m_anonymized )) {
555572 throw std::runtime_error (std::string (__func__) + " : value out of range" );
573+ }
556574 }
557575
558576 ret.m_denominated += credit;
559- if (!MoneyRange (ret.m_denominated ))
577+ if (!MoneyRange (ret.m_denominated )) {
560578 throw std::runtime_error (std::string (__func__) + " : value out of range" );
579+ }
561580 }
562581
563582 wtx.m_amounts [CWalletTx::ANON_CREDIT].Set (ISMINE_SPENDABLE, ret.m_anonymized );
0 commit comments