From 4d835c16d6b53503d2a25312c8c127838487b457 Mon Sep 17 00:00:00 2001 From: Pasta Date: Tue, 14 Dec 2021 23:19:51 -0500 Subject: [PATCH 1/9] refactor: introduce ranges.h for prettier std algo. Also use it in dash core --- src/Makefile.am | 1 + src/bls/bls_worker.cpp | 11 +++---- src/coinjoin/client.cpp | 17 +++++----- src/coinjoin/server.cpp | 63 +++++++++++++++++-------------------- src/llmq/instantsend.cpp | 10 ++---- src/llmq/utils.cpp | 8 ++--- src/masternode/payments.cpp | 9 ++---- src/masternode/utils.cpp | 9 ++---- src/util/ranges.h | 26 +++++++++++++++ 9 files changed, 79 insertions(+), 75 deletions(-) create mode 100644 src/util/ranges.h 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_worker.cpp b/src/bls/bls_worker.cpp index 0bf5a6a34d93..53ec2a9c159b 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -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..c817ffd071ef 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -108,6 +109,12 @@ 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()); +// if (std::any_of(coinJoinClientManagers.cbegin(), +// coinJoinClientManagers.cend(), +// [&dsq](const auto& pair){return pair.second->MarkAlreadyJoinedQueueAsTried(dsq);})) { +// break; +// } + for (const auto& pair : coinJoinClientManagers) { if (pair.second->MarkAlreadyJoinedQueueAsTried(dsq)) { break; @@ -597,13 +604,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/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