diff --git a/src/Makefile.am b/src/Makefile.am index a26a5d980bdd..5934cd2fcfd6 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -296,6 +296,7 @@ BITCOIN_CORE_H = \ util/macros.h \ util/memory.h \ util/moneystr.h \ + util/ranges.h \ util/serfloat.h \ util/string.h \ util/time.h \ diff --git a/src/bls/bls.h b/src/bls/bls.h index 39b82c1e2f6d..c313be377e77 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -9,6 +9,7 @@ #include #include #include +#include // bls-dash uses relic, which may define DEBUG and ERROR, which leads to many warnings in some build setups #undef ERROR @@ -108,7 +109,7 @@ class CBLSWrapper return; } - if (std::all_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c == 0; })) { + if (ranges::all_of(vecBytes, [](uint8_t c) { return c == 0; })) { Reset(); } else { try { diff --git a/src/bls/bls_worker.cpp b/src/bls/bls_worker.cpp index 0bf5a6a34d93..8c6947c5cee8 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -111,7 +112,7 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, const BLSIdVector& i }; futures.emplace_back(workerPool.push(f)); } - return std::all_of(futures.begin(), futures.end(), [](auto& f){ + return ranges::all_of(futures, [](auto& f){ return f.get(); }); } @@ -802,13 +803,9 @@ void CBLSWorker::AsyncVerifySig(const CBLSSignature& sig, const CBLSPublicKey& p std::unique_lock l(sigVerifyMutex); - bool foundDuplicate = false; - for (const auto& s : sigVerifyQueue) { - if (s.msgHash == msgHash) { - foundDuplicate = true; - break; - } - } + bool foundDuplicate = ranges::any_of(sigVerifyQueue, [&msgHash](const auto& job){ + return job.msgHash == msgHash; + }); if (foundDuplicate) { // batched/aggregated verification does not allow duplicate hashes, so we push what we currently have and start diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 53fcda8b5264..5540eea47b6f 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -87,13 +88,10 @@ void CCoinJoinClientQueueManager::ProcessMessage(CNode* pfrom, const std::string } // if the queue is ready, submit if we can - if (dsq.fReady) { - for (const auto& pair : coinJoinClientManagers) { - if (pair.second->TrySubmitDenominate(dmn->pdmnState->addr, connman)) { - LogPrint(BCLog::COINJOIN, "DSQUEUE -- CoinJoin queue (%s) is ready on masternode %s\n", dsq.ToString(), dmn->pdmnState->addr.ToString()); - return; - } - } + if (dsq.fReady && ranges::any_of(coinJoinClientManagers, + [&dmn, &connman](const auto& pair){ return pair.second->TrySubmitDenominate(dmn->pdmnState->addr, connman); })) { + LogPrint(BCLog::COINJOIN, "DSQUEUE -- CoinJoin queue (%s) is ready on masternode %s\n", dsq.ToString(), dmn->pdmnState->addr.ToString()); + return; } else { int64_t nLastDsq = mmetaman.GetMetaInfo(dmn->proTxHash)->GetLastDsq(); int64_t nDsqThreshold = mmetaman.GetDsqThreshold(dmn->proTxHash, mnList.GetValidMNsCount()); @@ -108,11 +106,8 @@ void CCoinJoinClientQueueManager::ProcessMessage(CNode* pfrom, const std::string LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue (%s) from masternode %s\n", dsq.ToString(), dmn->pdmnState->addr.ToString()); - for (const auto& pair : coinJoinClientManagers) { - if (pair.second->MarkAlreadyJoinedQueueAsTried(dsq)) { - break; - } - } + ranges::any_of(coinJoinClientManagers, + [&dsq](const auto& pair){ return pair.second->MarkAlreadyJoinedQueueAsTried(dsq); }); TRY_LOCK(cs_vecqueue, lockRecv); if (!lockRecv) return; @@ -597,13 +592,9 @@ bool CCoinJoinClientSession::SignFinalTransaction(const CTransaction& finalTrans for (const auto &entry: vecEntries) { // Check that the final transaction has all our outputs for (const auto &txout: entry.vecTxOut) { - bool fFound = false; - for (const auto &txoutFinal: finalMutableTransaction.vout) { - if (txoutFinal == txout) { - fFound = true; - break; - } - } + bool fFound = ranges::any_of(finalMutableTransaction.vout, [&txout](const auto& txoutFinal){ + return txoutFinal == txout; + }); if (!fFound) { // Something went wrong and we'll refuse to sign. It's possible we'll be charged collateral. But that's // better than signing if the transaction doesn't look like what we wanted. diff --git a/src/coinjoin/coinjoin.cpp b/src/coinjoin/coinjoin.cpp index 4660fe1884dc..4c4a09d390eb 100644 --- a/src/coinjoin/coinjoin.cpp +++ b/src/coinjoin/coinjoin.cpp @@ -136,7 +136,7 @@ bool CCoinJoinBroadcastTx::IsValidStructure() const if (tx->vin.size() > CCoinJoin::GetMaxPoolParticipants() * COINJOIN_ENTRY_MAX_SIZE) { return false; } - return std::all_of(tx->vout.cbegin(), tx->vout.cend(), [] (const auto& txOut){ + return ranges::all_of(tx->vout, [] (const auto& txOut){ return CCoinJoin::IsDenominatedAmount(txOut.nValue) && txOut.scriptPubKey.IsPayToPublicKeyHash(); }); } @@ -389,11 +389,10 @@ bool CCoinJoin::IsCollateralAmount(CAmount nInputAmount) int CCoinJoin::CalculateAmountPriority(CAmount nInputAmount) { - for (const auto& d : GetStandardDenominations()) { - // large denoms have lower value - if (nInputAmount == d) { - return (float)COIN / d * 10000; - } + if (auto optDenom = ranges::find_if_opt(GetStandardDenominations(), [&nInputAmount](const auto& denom) { + return nInputAmount == denom; + })) { + return (float)COIN / *optDenom * 10000; } if (nInputAmount < COIN) { return 20000; diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 40497a2c1381..550eb2d7bf55 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -15,6 +15,7 @@ #include