Skip to content

Commit f4e1846

Browse files
committed
trivial: minor housekeeping of wallet/coinjoin.cpp
Minor improvements for readability, separation of Dash-specific headers and converting nested ternary expression to lambda for easier reading.
1 parent a93f5cb commit f4e1846

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

src/wallet/coinjoin.cpp

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
#include <wallet/coinjoin.h>
77

8-
#include <coinjoin/common.h>
9-
#include <coinjoin/options.h>
10-
#include <evo/dmn_types.h>
118
#include <key_io.h>
129
#include <wallet/receive.h>
1310
#include <wallet/spend.h>
1411
#include <wallet/transaction.h>
1512
#include <wallet/wallet.h>
1613

14+
#include <coinjoin/common.h>
15+
#include <coinjoin/options.h>
16+
#include <evo/dmn_types.h>
17+
1718
namespace wallet {
1819
void CWallet::InitCJSaltFromDb()
1920
{
@@ -150,10 +151,10 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
150151
for (const auto& outpoint : setWalletUTXO) {
151152
if (!setWalletTxesCounted.emplace(outpoint.hash).second) continue;
152153

153-
const auto it = mapWallet.find(outpoint.hash);
154+
const auto it{mapWallet.find(outpoint.hash)};
154155
if (it == mapWallet.end()) continue;
155156

156-
const CWalletTx& wtx = (*it).second;
157+
const CWalletTx& wtx{(*it).second};
157158

158159
if (wtx.IsCoinBase() && GetTxBlocksToMaturity(wtx) > 0) continue;
159160
if (fSkipUnconfirmed && !CachedTxIsTrusted(*this, wtx)) continue;
@@ -168,8 +169,9 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
168169

169170
auto itTallyItem = mapTally.find(txdest);
170171
if (nMaxOupointsPerAddress != -1 && itTallyItem != mapTally.end() &&
171-
int64_t(itTallyItem->second.outpoints.size()) >= nMaxOupointsPerAddress)
172+
int64_t(itTallyItem->second.outpoints.size()) >= nMaxOupointsPerAddress) {
172173
continue;
174+
}
173175

174176
COutPoint target_outpoint(outpoint.hash, i);
175177
if (IsSpent(target_outpoint) || IsLockedCoin(target_outpoint)) continue;
@@ -219,8 +221,9 @@ std::vector<CompactTallyItem> CWallet::SelectCoinsGroupedByAddresses(bool fSkipD
219221
// debug
220222
if (LogAcceptDebug(BCLog::SELECTCOINS)) {
221223
std::string strMessage = "SelectCoinsGroupedByAddresses - vecTallyRet:\n";
222-
for (const auto& item : vecTallyRet)
224+
for (const auto& item : vecTallyRet) {
223225
strMessage += strprintf(" %s %f\n", EncodeDestination(item.txdest), float(item.nAmount) / COIN);
226+
}
224227
LogPrint(BCLog::SELECTCOINS, "%s", strMessage); /* Continued */
225228
}
226229

@@ -244,7 +247,7 @@ int CWallet::CountInputsWithAmount(CAmount nInputAmount) const
244247
LOCK(cs_wallet);
245248

246249
for (const auto& outpoint : setWalletUTXO) {
247-
const auto it = mapWallet.find(outpoint.hash);
250+
const auto it{mapWallet.find(outpoint.hash)};
248251
if (it == mapWallet.end()) continue;
249252
if (it->second.tx->vout[outpoint.n].nValue != nInputAmount) continue;
250253
if (GetTxDepthInMainChain(it->second) < 0) continue;
@@ -260,7 +263,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
260263
{
261264
LOCK(cs_wallet);
262265

263-
const int nRoundsMax = MAX_COINJOIN_ROUNDS + CCoinJoinClientOptions::GetRandomRounds();
266+
const int nRoundsMax{MAX_COINJOIN_ROUNDS + CCoinJoinClientOptions::GetRandomRounds()};
264267

265268
if (nRounds >= nRoundsMax) {
266269
// there can only be nRoundsMax rounds max
@@ -275,7 +278,7 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
275278
}
276279

277280
// TODO wtx should refer to a CWalletTx object, not a pointer, based on surrounding code
278-
const CWalletTx* wtx = GetWalletTx(outpoint.hash);
281+
const CWalletTx* wtx{GetWalletTx(outpoint.hash)};
279282

280283
if (wtx == nullptr || wtx->tx == nullptr) {
281284
// no such tx in this wallet
@@ -336,11 +339,14 @@ int CWallet::GetRealOutpointCoinJoinRounds(const COutPoint& outpoint, int nRound
336339
}
337340
}
338341
}
339-
*nRoundsRef = fDenomFound
340-
? (nShortest >= nRoundsMax - 1
341-
? nRoundsMax
342-
: nShortest + 1) // good, we a +1 to the shortest one but only nRoundsMax rounds max allowed
343-
: 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+
}();
344350
WalletCJLogPrint(this, "%s UPDATED %-70s %3d\n", __func__, outpoint.ToStringShort(), *nRoundsRef);
345351
return *nRoundsRef;
346352
}
@@ -367,7 +373,7 @@ bool CWallet::IsDenominated(const COutPoint& outpoint) const
367373
{
368374
LOCK(cs_wallet);
369375

370-
const auto it = mapWallet.find(outpoint.hash);
376+
const auto it{mapWallet.find(outpoint.hash)};
371377
if (it == mapWallet.end()) {
372378
return false;
373379
}
@@ -393,7 +399,7 @@ bool CWallet::IsFullyMixed(const COutPoint& outpoint) const
393399
CDataStream ss(SER_GETHASH, PROTOCOL_VERSION);
394400
ss << outpoint << nCoinJoinSalt;
395401
uint256 nHash;
396-
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());
397403
if (ReadLE64(nHash.begin()) % 2 == 0) {
398404
return false;
399405
}
@@ -434,8 +440,8 @@ CAmount CWallet::GetAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfi
434440

435441
CAmount nTotal = 0;
436442

437-
const CAmount nSmallestDenom = CoinJoin::GetSmallestDenomination();
438-
const CAmount nMixingCollateral = CoinJoin::GetCollateralAmount();
443+
const CAmount nSmallestDenom{CoinJoin::GetSmallestDenomination()};
444+
const CAmount nMixingCollateral{CoinJoin::GetCollateralAmount()};
439445
for (const auto& item : vecTally) {
440446
bool fIsDenominated = CoinJoin::IsDenominatedAmount(item.nAmount);
441447
if (fSkipDenominated && fIsDenominated) continue;
@@ -478,7 +484,7 @@ CAmount CWallet::GetNormalizedAnonymizedBalance() const
478484

479485
LOCK(cs_wallet);
480486
for (const auto& outpoint : setWalletUTXO) {
481-
const auto it = mapWallet.find(outpoint.hash);
487+
const auto it{mapWallet.find(outpoint.hash)};
482488
if (it == mapWallet.end()) continue;
483489

484490
CAmount nValue = it->second.tx->vout[outpoint.n].nValue;
@@ -502,18 +508,24 @@ CAmount CachedTxGetAnonymizedCredit(const CWallet& wallet, const CWalletTx& wtx,
502508
CAmount nCredit = 0;
503509
uint256 hashTx = wtx.GetHash();
504510
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
505-
const CTxOut& txout = wtx.tx->vout[i];
506-
const COutPoint outpoint = COutPoint(hashTx, i);
511+
const CTxOut& txout{wtx.tx->vout[i]};
512+
const COutPoint outpoint(hashTx, i);
507513

508514
if (coinControl.HasSelected() && !coinControl.IsSelected(outpoint)) {
509515
continue;
510516
}
511517

512-
if (wallet.IsSpent(outpoint) || !CoinJoin::IsDenominatedAmount(txout.nValue)) continue;
518+
if (wallet.IsSpent(outpoint) || !CoinJoin::IsDenominatedAmount(txout.nValue)) {
519+
continue;
520+
}
513521

514-
if (wallet.IsFullyMixed(outpoint)) {
515-
nCredit += OutputGetCredit(wallet, txout, ISMINE_SPENDABLE);
516-
if (!MoneyRange(nCredit)) 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");
517529
}
518530
}
519531

@@ -546,19 +558,25 @@ CoinJoinCredits CachedTxGetAvailableCoinJoinCredits(const CWallet& wallet, const
546558

547559
uint256 hashTx = wtx.GetHash();
548560
for (unsigned int i = 0; i < wtx.tx->vout.size(); i++) {
549-
const CTxOut& txout = wtx.tx->vout[i];
550-
const COutPoint outpoint = COutPoint(hashTx, i);
561+
const CTxOut& txout{wtx.tx->vout[i]};
562+
const COutPoint outpoint(hashTx, i);
551563

552-
if (wallet.IsSpent(outpoint) || !CoinJoin::IsDenominatedAmount(txout.nValue)) continue;
553-
const CAmount credit = OutputGetCredit(wallet, txout, ISMINE_SPENDABLE);
564+
if (wallet.IsSpent(outpoint) || !CoinJoin::IsDenominatedAmount(txout.nValue)) {
565+
continue;
566+
}
554567

568+
const CAmount credit{OutputGetCredit(wallet, txout, ISMINE_SPENDABLE)};
555569
if (wallet.IsFullyMixed(outpoint)) {
556570
ret.m_anonymized += credit;
557-
if (!MoneyRange(ret.m_anonymized)) throw std::runtime_error(std::string(__func__) + ": value out of range");
571+
if (!MoneyRange(ret.m_anonymized)) {
572+
throw std::runtime_error(std::string(__func__) + ": value out of range");
573+
}
558574
}
559575

560576
ret.m_denominated += credit;
561-
if (!MoneyRange(ret.m_denominated)) throw std::runtime_error(std::string(__func__) + ": value out of range");
577+
if (!MoneyRange(ret.m_denominated)) {
578+
throw std::runtime_error(std::string(__func__) + ": value out of range");
579+
}
562580
}
563581

564582
wtx.m_amounts[CWalletTx::ANON_CREDIT].Set(ISMINE_SPENDABLE, ret.m_anonymized);

0 commit comments

Comments
 (0)