diff --git a/src/Makefile.am b/src/Makefile.am index 1c5fd1936399..d49bba590b53 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -392,7 +392,6 @@ BITCOIN_CORE_H = \ util/bytevectorhash.h \ util/check.h \ util/edge.h \ - util/enumerate.h \ util/epochguard.h \ util/error.h \ util/fastrange.h \ @@ -400,7 +399,7 @@ BITCOIN_CORE_H = \ util/golombrice.h \ util/hasher.h \ util/hash_type.h \ - util/irange.h \ + util/helpers.h \ util/asmap.h \ util/getuniquepath.h \ util/macros.h \ @@ -408,13 +407,11 @@ BITCOIN_CORE_H = \ util/moneystr.h \ util/overflow.h \ util/overloaded.h \ - util/pointer.h \ - util/ranges.h \ util/readwritefile.h \ util/result.h \ - util/underlying.h \ util/serfloat.h \ util/settings.h \ + util/std23.h \ util/ranges_set.h \ util/sock.h \ util/string.h \ diff --git a/src/active/dkgsession.cpp b/src/active/dkgsession.cpp index 15eb449ece7f..efcbfe46aef9 100644 --- a/src/active/dkgsession.cpp +++ b/src/active/dkgsession.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include @@ -81,7 +83,7 @@ void ActiveDKGSession::SendContributions(CDKGPendingMessages& pendingMessages, P qc.contributions = std::make_shared>(); qc.contributions->InitEncrypt(members.size()); - for (const auto i : irange::range(members.size())) { + for (const auto i : util::irange(members.size())) { const auto& m = members[i]; CBLSSecretKey skContrib = m_sk_contributions[i]; @@ -150,7 +152,7 @@ void ActiveDKGSession::VerifyPendingContributions() return; } - for (const auto i : irange::range(memberIndexes.size())) { + for (const auto i : util::irange(memberIndexes.size())) { if (!result[i]) { const auto& m = members[memberIndexes[i]]; logger.Batch("invalid contribution from %s. will complain later", m->dmn->proTxHash.ToString()); @@ -267,7 +269,7 @@ void ActiveDKGSession::SendComplaint(CDKGPendingMessages& pendingMessages, PeerM int badCount = 0; int complaintCount = 0; - for (const auto i : irange::range(members.size())) { + for (const auto i : util::irange(members.size())) { const auto& m = members[i]; if (m->bad || m->badConnection) { qc.badMembers[i] = true; @@ -352,9 +354,9 @@ void ActiveDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, P qj.proTxHash = myProTxHash; qj.contributions.reserve(forMembers.size()); - for (const uint32_t i : irange::range(members.size())) { + for (const uint32_t i : util::irange(members.size())) { const auto& m = members[i]; - if (forMembers.count(m->dmn->proTxHash) == 0) { + if (!std23::ranges::contains(forMembers, m->dmn->proTxHash)) { continue; } logger.Batch("justifying for %s", m->dmn->proTxHash.ToString()); @@ -444,7 +446,7 @@ void ActiveDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages, Peer qc.quorumHash = m_quorum_base_block_index->GetBlockHash(); qc.proTxHash = myProTxHash; - for (const auto i : irange::range(members.size())) { + for (const auto i : util::irange(members.size())) { const auto& m = members[i]; if (!m->bad) { qc.validMembers[i] = true; @@ -559,7 +561,7 @@ std::vector ActiveDKGSession::FinalizeCommitments() for (const auto& p : prematureCommitments) { const auto& qc = p.second; - if (validCommitments.count(p.first) == 0) { + if (!std23::ranges::contains(validCommitments, p.first)) { continue; } diff --git a/src/active/dkgsessionhandler.cpp b/src/active/dkgsessionhandler.cpp index c0ed65b75402..ad795befc498 100644 --- a/src/active/dkgsessionhandler.cpp +++ b/src/active/dkgsessionhandler.cpp @@ -61,12 +61,12 @@ void ActiveDKGSessionHandler::UpdatedBlockTip(const CBlockIndex* pindexNew) bool fNewPhase = (quorumStageInt % params.dkgPhaseBlocks) == 0; int phaseInt = quorumStageInt / params.dkgPhaseBlocks + 1; QuorumPhase oldPhase = phase; - if (fNewPhase && phaseInt >= ToUnderlying(QuorumPhase::Initialized) && phaseInt <= ToUnderlying(QuorumPhase::Idle)) { + if (fNewPhase && phaseInt >= std23::to_underlying(QuorumPhase::Initialized) && phaseInt <= std23::to_underlying(QuorumPhase::Idle)) { phase = static_cast(phaseInt); } LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] currentHeight=%d, pQuorumBaseBlockIndex->nHeight=%d, oldPhase=%d, newPhase=%d\n", __func__, - params.name, quorumIndex, currentHeight, pQuorumBaseBlockIndex->nHeight, ToUnderlying(oldPhase), ToUnderlying(phase)); + params.name, quorumIndex, currentHeight, pQuorumBaseBlockIndex->nHeight, std23::to_underlying(oldPhase), std23::to_underlying(phase)); } void ActiveDKGSessionHandler::StartThread(CConnman& connman, PeerManager& peerman) @@ -75,7 +75,7 @@ void ActiveDKGSessionHandler::StartThread(CConnman& connman, PeerManager& peerma throw std::runtime_error("Tried to start an already started ActiveDKGSessionHandler thread."); } - m_thread_name = strprintf("llmq-%d-%d", ToUnderlying(params.type), quorumIndex); + m_thread_name = strprintf("llmq-%d-%d", std23::to_underlying(params.type), quorumIndex); phaseHandlerThread = std::thread(&util::TraceThread, m_thread_name.c_str(), [this, &connman, &peerman] { PhaseHandlerThread(connman, peerman); }); } @@ -120,7 +120,7 @@ class AbortPhaseException : public std::exception { void ActiveDKGSessionHandler::WaitForNextPhase(std::optional curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, const WhileWaitFunc& shouldNotWait) const { - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - starting, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, curPhase.has_value() ? ToUnderlying(*curPhase) : -1, ToUnderlying(nextPhase)); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - starting, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, curPhase.has_value() ? std23::to_underlying(*curPhase) : -1, std23::to_underlying(nextPhase)); while (true) { if (stopRequested) { @@ -136,7 +136,7 @@ void ActiveDKGSessionHandler::WaitForNextPhase(std::optional curPha break; } if (curPhase.has_value() && _phase != curPhase) { - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - aborting due unexpected phase change, _phase=%d, curPhase=%d\n", __func__, params.name, quorumIndex, ToUnderlying(_phase), curPhase.has_value() ? ToUnderlying(*curPhase) : -1); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - aborting due unexpected phase change, _phase=%d, curPhase=%d\n", __func__, params.name, quorumIndex, std23::to_underlying(_phase), curPhase.has_value() ? std23::to_underlying(*curPhase) : -1); throw AbortPhaseException(); } if (!shouldNotWait()) { @@ -144,7 +144,7 @@ void ActiveDKGSessionHandler::WaitForNextPhase(std::optional curPha } } - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - done, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, curPhase.has_value() ? ToUnderlying(*curPhase) : -1, ToUnderlying(nextPhase)); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - done, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, curPhase.has_value() ? std23::to_underlying(*curPhase) : -1, std23::to_underlying(nextPhase)); if (nextPhase == QuorumPhase::Initialized) { m_dkgdbgman.ResetLocalSessionStatus(params.type, quorumIndex); @@ -206,7 +206,7 @@ void ActiveDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase, const uint2 int heightTmp{currentHeight.load()}; int heightStart{heightTmp}; - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - starting sleep for %d ms, curPhase=%d\n", __func__, params.name, quorumIndex, sleepTime, ToUnderlying(curPhase)); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - starting sleep for %d ms, curPhase=%d\n", __func__, params.name, quorumIndex, sleepTime, std23::to_underlying(curPhase)); while (TicksSinceEpoch(SystemClock::now()) < endTime) { if (stopRequested) { @@ -233,20 +233,20 @@ void ActiveDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase, const uint2 } } - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - done, curPhase=%d\n", __func__, params.name, quorumIndex, ToUnderlying(curPhase)); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - done, curPhase=%d\n", __func__, params.name, quorumIndex, std23::to_underlying(curPhase)); } void ActiveDKGSessionHandler::HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc& startPhaseFunc, const WhileWaitFunc& runWhileWaiting) { - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - starting, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, ToUnderlying(curPhase), ToUnderlying(nextPhase)); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - starting, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, std23::to_underlying(curPhase), std23::to_underlying(nextPhase)); SleepBeforePhase(curPhase, expectedQuorumHash, randomSleepFactor, runWhileWaiting); startPhaseFunc(); WaitForNextPhase(curPhase, nextPhase, expectedQuorumHash, runWhileWaiting); - LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - done, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, ToUnderlying(curPhase), ToUnderlying(nextPhase)); + LogPrint(BCLog::LLMQ_DKG, "ActiveDKGSessionHandler::%s -- %s qi[%d] - done, curPhase=%d, nextPhase=%d\n", __func__, params.name, quorumIndex, std23::to_underlying(curPhase), std23::to_underlying(nextPhase)); } // returns a set of NodeIds which sent invalid messages diff --git a/src/active/quorums.cpp b/src/active/quorums.cpp index 1885022d9c35..043d31fa0982 100644 --- a/src/active/quorums.cpp +++ b/src/active/quorums.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,8 @@ #include +#include + namespace llmq { QuorumParticipant::QuorumParticipant(CBLSWorker& bls_worker, CConnman& connman, CDeterministicMNManager& dmnman, QuorumObserverParent& qman, CQuorumSnapshotManager& qsnapman, @@ -47,13 +50,15 @@ void QuorumParticipant::CheckQuorumConnections(const Consensus::LLMQParams& llmq const uint256 proTxHash = m_mn_activeman.GetProTxHash(); const bool watchOtherISQuorums = llmqParams.type == Params().GetConsensus().llmqTypeDIP0024InstantSend && - ranges::any_of(lastQuorums, [&proTxHash](const auto& old_quorum){ return old_quorum->IsMember(proTxHash); }); + std::ranges::any_of(lastQuorums, [&proTxHash](const auto& old_quorum) { + return old_quorum->IsMember(proTxHash); + }); for (const auto& quorum : lastQuorums) { if (utils::EnsureQuorumConnections(llmqParams, m_connman, m_sporkman, {m_dmnman, m_qsnapman, m_chainman, quorum->m_quorum_base_block_index}, m_dmnman.GetListAtChainTip(), proTxHash, /*is_masternode=*/true, m_quorums_watch)) { if (deletableQuorums.erase(quorum->qc->quorumHash) > 0) { - LogPrint(BCLog::LLMQ, "QuorumParticipant::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); + LogPrint(BCLog::LLMQ, "QuorumParticipant::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, std23::to_underlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); } } else if (watchOtherISQuorums && !quorum->IsMember(proTxHash)) { Uint256HashSet connections; @@ -63,12 +68,12 @@ void QuorumParticipant::CheckQuorumConnections(const Consensus::LLMQParams& llmq } if (!connections.empty()) { if (!m_connman.HasMasternodeQuorumNodes(llmqParams.type, quorum->m_quorum_base_block_index->GetBlockHash())) { - LogPrint(BCLog::LLMQ, "QuorumParticipant::%s -- llmqType[%d] h[%d] adding mn inter-quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); + LogPrint(BCLog::LLMQ, "QuorumParticipant::%s -- llmqType[%d] h[%d] adding mn inter-quorum connections for quorum: [%d:%s]\n", __func__, std23::to_underlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); m_connman.SetMasternodeQuorumNodes(llmqParams.type, quorum->m_quorum_base_block_index->GetBlockHash(), connections); m_connman.SetMasternodeQuorumRelayMembers(llmqParams.type, quorum->m_quorum_base_block_index->GetBlockHash(), connections); } if (deletableQuorums.erase(quorum->qc->quorumHash) > 0) { - LogPrint(BCLog::LLMQ, "QuorumParticipant::%s -- llmqType[%d] h[%d] keeping mn inter-quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); + LogPrint(BCLog::LLMQ, "QuorumParticipant::%s -- llmqType[%d] h[%d] keeping mn inter-quorum connections for quorum: [%d:%s]\n", __func__, std23::to_underlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); } } } @@ -96,7 +101,7 @@ size_t QuorumParticipant::GetQuorumRecoveryStartOffset(const CQuorum& quorum, gs size_t nIndex{0}; { auto my_protx_hash = m_mn_activeman.GetProTxHash(); - for (const auto i : irange::range(vecProTxHashes.size())) { + for (const auto i : util::irange(vecProTxHashes.size())) { // cppcheck-suppress useStlAlgorithm if (my_protx_hash == vecProTxHashes[i]) { nIndex = i; @@ -156,7 +161,7 @@ MessageProcessingResult QuorumParticipant::ProcessContribQDATA(CNode& pfrom, CDa std::vector vecSecretKeys; vecSecretKeys.resize(vecEncrypted.size()); - for (const auto i : irange::range(vecEncrypted.size())) { + for (const auto i : util::irange(vecEncrypted.size())) { if (!m_mn_activeman.Decrypt(vecEncrypted[i], memberIdx, vecSecretKeys[i], PROTOCOL_VERSION)) { return MisbehavingError{10, "failed to decrypt"}; } @@ -209,7 +214,9 @@ void QuorumParticipant::TriggerQuorumDataRecoveryThreads(gsl::not_nullIsValidMember(proTxHash); }); + const bool fWeAreQuorumTypeMember = std::ranges::any_of(vecQuorums, [&proTxHash](const auto& pQuorum) { + return pQuorum->IsValidMember(proTxHash); + }); for (auto& pQuorum : vecQuorums) { if (pQuorum->IsValidMember(proTxHash)) { @@ -224,7 +231,7 @@ void QuorumParticipant::TriggerQuorumDataRecoveryThreads(gsl::not_nullqc->llmqType), pQuorum->qc->quorumHash.ToString(), block_index->nHeight); + std23::to_underlying(pQuorum->qc->llmqType), pQuorum->qc->quorumHash.ToString(), block_index->nHeight); } } else { TryStartVvecSyncThread(block_index, std::move(pQuorum), fWeAreQuorumTypeMember); diff --git a/src/addressindex.h b/src/addressindex.h index 55dbc8284a58..6ef68d473d80 100644 --- a/src/addressindex.h +++ b/src/addressindex.h @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -122,7 +122,7 @@ struct CAddressIndexKey { template void Serialize(Stream& s) const { - ser_writedata8(s, ToUnderlying(m_address_type)); + ser_writedata8(s, std23::to_underlying(m_address_type)); m_address_bytes.Serialize(s); // Heights are stored big-endian for key sorting in LevelDB ser_writedata32be(s, m_block_height); @@ -169,7 +169,7 @@ struct CAddressIndexIteratorKey { template void Serialize(Stream& s) const { - ser_writedata8(s, ToUnderlying(m_address_type)); + ser_writedata8(s, std23::to_underlying(m_address_type)); m_address_bytes.Serialize(s); } @@ -207,7 +207,7 @@ struct CAddressIndexIteratorHeightKey { template void Serialize(Stream& s) const { - ser_writedata8(s, ToUnderlying(m_address_type)); + ser_writedata8(s, std23::to_underlying(m_address_type)); m_address_bytes.Serialize(s); ser_writedata32be(s, m_block_height); } diff --git a/src/bench/bls_dkg.cpp b/src/bench/bls_dkg.cpp index 14e83810a100..dc157ca0ebd3 100644 --- a/src/bench/bls_dkg.cpp +++ b/src/bench/bls_dkg.cpp @@ -3,9 +3,11 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include + #include +#include + #include -#include struct Member { CBLSId id; @@ -45,7 +47,7 @@ class DKG void VerifyContributionShares(size_t whoAmI, const std::set& invalidIndexes, bool aggregated) { auto result = blsWorker.VerifyContributionShares(members[whoAmI].id, receivedVvecs, receivedSkShares, aggregated); - for (const size_t i : irange::range(receivedVvecs.size())) { + for (const size_t i : util::irange(receivedVvecs.size())) { if (invalidIndexes.count(i)) { assert(!result[i]); } else { @@ -60,7 +62,7 @@ class DKG members.reserve(quorumSize); ids.reserve(quorumSize); - for (const int i : irange::range(quorumSize)) { + for (const int i : util::irange(quorumSize)) { uint256 id; WriteLE64(id.begin(), i + 1); members.push_back({CBLSId(id), {}, {}}); @@ -94,7 +96,7 @@ class DKG ReceiveShares(memberIdx); std::set invalidIndexes; - for ([[maybe_unused]] const auto _ : irange::range(invalidCount)) { + for ([[maybe_unused]] const auto _ : util::irange(invalidCount)) { size_t shareIdx = GetRand(receivedSkShares.size()); receivedSkShares[shareIdx].MakeNewKey(); invalidIndexes.emplace(shareIdx); @@ -117,7 +119,7 @@ static void BLSDKG_GenerateContributions(benchmark::Bench& bench, uint32_t epoch epoch_iters = 1; quorumSize = 1; } - for (const int i : irange::range(quorumSize)) { + for (const int i : util::irange(quorumSize)) { uint256 id; WriteLE64(id.begin(), i + 1); members.push_back({CBLSId(id), {}, {}}); diff --git a/src/bls/bls.h b/src/bls/bls.h index a99cec3114cf..5377f5dafa40 100644 --- a/src/bls/bls.h +++ b/src/bls/bls.h @@ -9,7 +9,6 @@ #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 @@ -23,10 +22,9 @@ #undef SEED #include -#include -#include - #include +#include +#include namespace bls { extern std::atomic bls_legacy_scheme; @@ -106,7 +104,7 @@ class CBLSWrapper return; } - if (ranges::all_of(vecBytes, [](uint8_t c) { return c == 0; })) { + if (std::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 44f15e46c22b..7d8d0bd6e492 100644 --- a/src/bls/bls_worker.cpp +++ b/src/bls/bls_worker.cpp @@ -5,10 +5,10 @@ #include #include -#include #include #include +#include #include template @@ -108,9 +108,7 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, Span ids, BL }; futures.emplace_back(workerPool.push(f)); } - return ranges::all_of(futures, [](auto& f){ - return f.get(); - }); + return std::ranges::all_of(futures, [](auto& f) { return f.get(); }); } // aggregates a single vector of BLS objects in parallel @@ -775,9 +773,8 @@ void CBLSWorker::AsyncVerifySig(const CBLSSignature& sig, const CBLSPublicKey& p std::unique_lock l(sigVerifyMutex); - bool foundDuplicate = ranges::any_of(sigVerifyQueue, [&msgHash](const auto& job){ - return job.msgHash == msgHash; - }); + bool foundDuplicate = std::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/chainlock/chainlock.cpp b/src/chainlock/chainlock.cpp index bbfb3c9f83af..e5a62a43c54b 100644 --- a/src/chainlock/chainlock.cpp +++ b/src/chainlock/chainlock.cpp @@ -10,9 +10,9 @@ #include #include #include +#include #include #include -#include #include #include @@ -135,7 +135,7 @@ MessageProcessingResult CChainLocksHandler::ProcessNewChainLock(const NodeId fro if (const auto ret = VerifyChainLock(clsig); ret != VerifyRecSigStatus::Valid) { LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- invalid CLSIG (%s), status=%d peer=%d\n", __func__, - clsig.ToString(), ToUnderlying(ret), from); + clsig.ToString(), std23::to_underlying(ret), from); if (from != -1) { return MisbehavingError{10}; } diff --git a/src/chainparams.cpp b/src/chainparams.cpp index bf6e74023579..cc4355e5ab9d 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -6,18 +6,18 @@ #include +#include +#include + +#include #include #include #include -#include -#include #include -#include #include -#include - -#include +#include +#include static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward) { @@ -735,7 +735,7 @@ class CDevNetParams : public CChainParams { */ void UpdateLLMQDevnetParameters(int size, int threshold) { - auto params = ranges::find_if(consensus.llmqs, [](const auto& llmq){ return llmq.type == Consensus::LLMQType::LLMQ_DEVNET;}); + auto params = std::ranges::find_if(consensus.llmqs, [](const auto& llmq){ return llmq.type == Consensus::LLMQType::LLMQ_DEVNET;}); assert(params != consensus.llmqs.end()); params->size = size; params->minSize = threshold; @@ -974,7 +974,7 @@ class CRegTestParams : public CChainParams { */ void UpdateLLMQTestParameters(int size, int threshold, const Consensus::LLMQType llmqType) { - auto params = ranges::find_if(consensus.llmqs, [llmqType](const auto& llmq){ return llmq.type == llmqType;}); + auto params = std::ranges::find_if(consensus.llmqs, [llmqType](const auto& llmq){ return llmq.type == llmqType;}); assert(params != consensus.llmqs.end()); params->size = size; params->minSize = threshold; @@ -1192,7 +1192,7 @@ void CRegTestParams::UpdateLLMQInstantSendDIP0024FromArgs(const ArgsManager& arg if (llmqType == Consensus::LLMQType::LLMQ_NONE) { throw std::runtime_error("Invalid LLMQ type specified for -llmqtestinstantsenddip0024."); } - LogPrintf("Setting llmqtestinstantsenddip0024 to %ld\n", ToUnderlying(llmqType)); + LogPrintf("Setting llmqtestinstantsenddip0024 to %ld\n", std23::to_underlying(llmqType)); UpdateLLMQDIP0024InstantSend(llmqType); } diff --git a/src/coinjoin/client.cpp b/src/coinjoin/client.cpp index 8299cd9d86cb..be6727cc41b1 100644 --- a/src/coinjoin/client.cpp +++ b/src/coinjoin/client.cpp @@ -4,31 +4,33 @@ #include -#include -#include #include -#include #include #include #include +#include +#include +#include + +#include +#include +#include #include #include -#include #include #include -#include #include -#include #include #include #include #include -#include #include #include #include #include +#include + #include using wallet::CCoinControl; @@ -601,9 +603,8 @@ bool CCoinJoinClientSession::SignFinalTransaction(CNode& peer, CChainState& acti for (const auto &entry: vecEntries) { // Check that the final transaction has all our outputs for (const auto &txout: entry.vecTxOut) { - bool fFound = ranges::any_of(finalMutableTransaction.vout, [&txout](const auto& txoutFinal){ - return txoutFinal == txout; - }); + bool fFound = std::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. @@ -620,7 +621,7 @@ bool CCoinJoinClientSession::SignFinalTransaction(CNode& peer, CChainState& acti int nMyInputIndex = -1; CScript prevPubKey = CScript(); - for (const auto i : irange::range(finalMutableTransaction.vin.size())) { + for (const auto i : util::irange(finalMutableTransaction.vin.size())) { // cppcheck-suppress useStlAlgorithm if (finalMutableTransaction.vin[i] == txdsin) { nMyInputIndex = i; @@ -1302,7 +1303,7 @@ bool CCoinJoinClientSession::SubmitDenominate(CConnman& connman) std::vector > vecInputsByRounds; - for (const auto i : irange::range(CCoinJoinClientOptions::GetRounds() + CCoinJoinClientOptions::GetRandomRounds())) { + for (const auto i : util::irange(CCoinJoinClientOptions::GetRounds() + CCoinJoinClientOptions::GetRandomRounds())) { if (PrepareDenominate(i, i, strError, vecTxDSIn, vecPSInOutPairsTmp, true)) { WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::SubmitDenominate -- Running CoinJoin denominate for %d rounds, success\n", i); vecInputsByRounds.emplace_back(i, vecPSInOutPairsTmp.size()); @@ -1444,14 +1445,14 @@ bool CCoinJoinClientSession::MakeCollateralAmounts() }); // First try to use only non-denominated funds - if (ranges::any_of(vecTally, [&](const auto& item) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) { + if (std::ranges::any_of(vecTally, [&](const auto& item) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) { return MakeCollateralAmounts(item, false); })) { return true; } // There should be at least some denominated funds we should be able to break in pieces to continue mixing - if (ranges::any_of(vecTally, [&](const auto& item) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) { + if (std::ranges::any_of(vecTally, [&](const auto& item) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) { return MakeCollateralAmounts(item, true); })) { return true; @@ -1768,7 +1769,7 @@ bool CCoinJoinClientSession::CreateDenominated(CAmount nBalanceToDenominate, con WalletCJLogPrint(m_wallet, "CCoinJoinClientSession::%s -- 2 - nBalanceToDenominate: %f, nDenomValue: %f, denomsToCreateValue: %d, denomsToCreateBal: %d\n", __func__, (float) nBalanceToDenominate / COIN, (float) nDenomValue / COIN, denomsToCreateValue, denomsToCreateBal); auto it = mapDenomCount.find(nDenomValue); - for (const auto i : irange::range(denomsToCreate)) { + for (const auto i : util::irange(denomsToCreate)) { // Never go above the cap unless it's the largest denom if (nDenomValue != nLargestDenomValue && it->second >= CCoinJoinClientOptions::GetDenomsHardCap()) break; diff --git a/src/coinjoin/client.h b/src/coinjoin/client.h index 2b1933841c71..5ecf433010f1 100644 --- a/src/coinjoin/client.h +++ b/src/coinjoin/client.h @@ -12,12 +12,12 @@ #include #include -#include #include #include #include #include +#include #include class CCoinJoinClientManager; @@ -105,7 +105,7 @@ class CoinJoinWalletManager { bool ForAnyCJClientMan(Callable&& func) EXCLUSIVE_LOCKS_REQUIRED(!cs_wallet_manager_map) { LOCK(cs_wallet_manager_map); - return ranges::any_of(m_wallet_manager_map, [&](auto& pair) { return func(pair.second); }); + return std::ranges::any_of(m_wallet_manager_map, [&](auto& pair) { return func(pair.second); }); }; private: diff --git a/src/coinjoin/coinjoin.cpp b/src/coinjoin/coinjoin.cpp index 1d129d8521e6..6db358e6c3e6 100644 --- a/src/coinjoin/coinjoin.cpp +++ b/src/coinjoin/coinjoin.cpp @@ -4,6 +4,12 @@ #include +#include +#include +#include +#include +#include + #include #include #include @@ -13,11 +19,7 @@ #include #include -#include -#include -#include -#include - +#include #include constexpr static CAmount DEFAULT_MAX_RAW_TX_FEE{COIN / 10}; @@ -63,7 +65,7 @@ bool CCoinJoinQueue::IsTimeOutOfBounds(int64_t current_time) const [[nodiscard]] std::string CCoinJoinQueue::ToString() const { return strprintf("nDenom=%d, nTime=%lld, fReady=%s, fTried=%s, masternode=%s", - nDenom, nTime, fReady ? "true" : "false", fTried ? "true" : "false", masternodeOutpoint.ToStringShort()); + nDenom, nTime, util::to_string(fReady), util::to_string(fTried), masternodeOutpoint.ToStringShort()); } uint256 CCoinJoinBroadcastTx::GetSignatureHash() const @@ -104,7 +106,7 @@ bool CCoinJoinBroadcastTx::IsValidStructure() const if (tx->vin.size() > CoinJoin::GetMaxPoolParticipants() * COINJOIN_ENTRY_MAX_SIZE) { return false; } - return ranges::all_of(tx->vout, [] (const auto& txOut){ + return std::ranges::all_of(tx->vout, [](const auto& txOut) { return CoinJoin::IsDenominatedAmount(txOut.nValue) && txOut.scriptPubKey.IsPayToPublicKeyHash(); }); } diff --git a/src/coinjoin/coinjoin.h b/src/coinjoin/coinjoin.h index 9a15be8b6c03..0d190414de65 100644 --- a/src/coinjoin/coinjoin.h +++ b/src/coinjoin/coinjoin.h @@ -7,13 +7,14 @@ #include +#include + #include #include #include #include #include #include -#include #include #include @@ -22,6 +23,8 @@ #include #include +#include + class CChainState; class CBLSPublicKey; class CBlockIndex; @@ -346,7 +349,7 @@ class CCoinJoinBaseManager std::optional GetQueueFromHash(const uint256& queueHash) EXCLUSIVE_LOCKS_REQUIRED(!cs_vecqueue) { LOCK(cs_vecqueue); - return ranges::find_if_opt(vecCoinJoinQueue, [&queueHash](const auto& q) { return q.GetHash() == queueHash; }); + return util::find_if_opt(vecCoinJoinQueue, [&queueHash](const auto& q) { return q.GetHash() == queueHash; }); } }; diff --git a/src/coinjoin/common.h b/src/coinjoin/common.h index 7b7c939f1184..6c7abfba2266 100644 --- a/src/coinjoin/common.h +++ b/src/coinjoin/common.h @@ -5,12 +5,13 @@ #ifndef BITCOIN_COINJOIN_COMMON_H #define BITCOIN_COINJOIN_COMMON_H +#include + #include #include #include #include -#include /** Holds a mixing input */ @@ -117,9 +118,8 @@ constexpr bool IsCollateralAmount(CAmount nInputAmount) constexpr int CalculateAmountPriority(CAmount nInputAmount) { - if (auto optDenom = ranges::find_if_opt(GetStandardDenominations(), [&nInputAmount](const auto& denom) { - return nInputAmount == denom; - })) { + if (auto optDenom = util::find_if_opt(GetStandardDenominations(), + [&nInputAmount](const auto& denom) { return nInputAmount == denom; })) { return (float)COIN / *optDenom * 10000; } if (nInputAmount < COIN) { diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index c68e33224a56..86cb3ce01d5e 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -25,6 +24,8 @@ #include +#include + CCoinJoinServer::CCoinJoinServer(PeerManagerInternal* peer_manager, ChainstateManager& chainman, CConnman& _connman, CDeterministicMNManager& dmnman, CDSTXManager& dstxman, CMasternodeMetaMan& mn_metaman, CTxMemPool& mempool, const CActiveMasternodeManager& mn_activeman, @@ -91,8 +92,8 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) auto mnOutpoint = m_mn_activeman.GetOutPoint(); - if (ranges::any_of(vecCoinJoinQueue, - [&mnOutpoint](const auto& q){return q.masternodeOutpoint == mnOutpoint;})) { + if (std::ranges::any_of(vecCoinJoinQueue, + [&mnOutpoint](const auto& q) { return q.masternodeOutpoint == mnOutpoint; })) { // refuse to create another queue this often LogPrint(BCLog::COINJOIN, "DSACCEPT -- last dsq is still in queue, refuse to mix\n"); PushStatus(peer, STATUS_REJECTED, ERR_RECENT); @@ -408,7 +409,7 @@ void CCoinJoinServer::ChargeFees() const if (nState == POOL_STATE_ACCEPTING_ENTRIES) { LOCK(cs_coinjoin); for (const auto& txCollateral : vecSessionCollaterals) { - bool fFound = ranges::any_of(vecEntries, [&txCollateral](const auto& entry){ + bool fFound = std::ranges::any_of(vecEntries, [&txCollateral](const auto& entry) { return *entry.txCollateral == *txCollateral; }); @@ -610,10 +611,8 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- txin=%s\n", __func__, txin.ToString()); LOCK(cs_coinjoin); for (const auto& inner_entry : vecEntries) { - if (ranges::any_of(inner_entry.vecTxDSIn, - [&txin](const auto& txdsin){ - return txdsin.prevout == txin.prevout; - })) { + if (std::ranges::any_of(inner_entry.vecTxDSIn, + [&txin](const auto& txdsin) { return txdsin.prevout == txin.prevout; })) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR: already have this txin in entries\n", __func__); nMessageIDRet = ERR_ALREADY_HAVE; // Two peers sent the same input? Can't really say who is the malicious one here, @@ -650,8 +649,8 @@ bool CCoinJoinServer::AddScriptSig(const CTxIn& txinNew) LOCK(cs_coinjoin); for (const auto& entry : vecEntries) { - if (ranges::any_of(entry.vecTxDSIn, - [&txinNew](const auto& txdsin){ return txdsin.scriptSig == txinNew.scriptSig; })){ + if (std::ranges::any_of(entry.vecTxDSIn, + [&txinNew](const auto& txdsin) { return txdsin.scriptSig == txinNew.scriptSig; })) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::AddScriptSig -- already exists\n"); return false; } @@ -687,8 +686,8 @@ bool CCoinJoinServer::IsSignaturesComplete() const AssertLockNotHeld(cs_coinjoin); LOCK(cs_coinjoin); - return ranges::all_of(vecEntries, [](const auto& entry){ - return ranges::all_of(entry.vecTxDSIn, [](const auto& txdsin){return txdsin.fHasSig;}); + return std::ranges::all_of(vecEntries, [](const auto& entry) { + return std::ranges::all_of(entry.vecTxDSIn, [](const auto& txdsin) { return txdsin.fHasSig; }); }); } diff --git a/src/evo/cbtx.cpp b/src/evo/cbtx.cpp index 47144774d187..4e1b7986ad6c 100644 --- a/src/evo/cbtx.cpp +++ b/src/evo/cbtx.cpp @@ -2,20 +2,22 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include #include + #include #include #include #include #include #include -#include +#include #include #include #include +#include #include +#include using node::ReadBlockFromDisk; @@ -109,11 +111,7 @@ auto CachedGetQcHashesQcIndexedHashes(const CBlockIndex* pindexPrev, const llmq: auto CalcHashCountFromQCHashes(const QcHashMap& qcHashes) { - size_t hash_count{0}; - for (const auto& [_, vec_hashes] : qcHashes) { - hash_count += vec_hashes.size(); - } - return hash_count; + return std23::ranges::fold_left(qcHashes, size_t{0}, [](size_t s, const auto& p) { return s + p.second.size(); }); } bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPrev, diff --git a/src/evo/core_write.cpp b/src/evo/core_write.cpp index b3e414d7c779..72b3cb0b32b8 100644 --- a/src/evo/core_write.cpp +++ b/src/evo/core_write.cpp @@ -13,11 +13,11 @@ #include #include #include +#include #include #include #include -#include #include @@ -163,7 +163,7 @@ RPCResult CCbTx::GetJsonHelp(const std::string& key, bool optional) UniValue CCbTx::ToJson() const { UniValue ret(UniValue::VOBJ); - ret.pushKV("version", ToUnderlying(nVersion)); + ret.pushKV("version", std23::to_underlying(nVersion)); ret.pushKV("height", nHeight); ret.pushKV("merkleRootMNList", merkleRootMNList.ToString()); if (nVersion >= CCbTx::Version::MERKLE_ROOT_QUORUMS) { @@ -301,7 +301,7 @@ UniValue CProRegTx::ToJson() const { UniValue ret(UniValue::VOBJ); ret.pushKV("version", nVersion); - ret.pushKV("type", ToUnderlying(nType)); + ret.pushKV("type", std23::to_underlying(nType)); ret.pushKV("collateralHash", collateralOutpoint.hash.ToString()); ret.pushKV("collateralIndex", collateralOutpoint.n); ret.pushKV("service", netInfo->GetPrimary().ToStringAddrPort()); @@ -391,7 +391,7 @@ UniValue CProUpServTx::ToJson() const { UniValue ret(UniValue::VOBJ); ret.pushKV("version", nVersion); - ret.pushKV("type", ToUnderlying(nType)); + ret.pushKV("type", std23::to_underlying(nType)); ret.pushKV("proTxHash", proTxHash.ToString()); ret.pushKV("service", netInfo->GetPrimary().ToStringAddrPort()); ret.pushKV("addresses", GetNetInfoWithLegacyFields(*this, nType)); @@ -524,7 +524,7 @@ UniValue CSimplifiedMNListEntry::ToJson(bool extended) const { UniValue obj(UniValue::VOBJ); obj.pushKV("nVersion", nVersion); - obj.pushKV("nType", ToUnderlying(nType)); + obj.pushKV("nType", std23::to_underlying(nType)); obj.pushKV("proRegTxHash", proRegTxHash.ToString()); obj.pushKV("confirmedHash", confirmedHash.ToString()); obj.pushKV("service", netInfo->GetPrimary().ToStringAddrPort()); diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index aa97f67de072..494b0fa9326f 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -13,8 +13,7 @@ #include #include #include -#include -#include +#include #include #include @@ -29,6 +28,7 @@ #include #include #include +#include #include @@ -120,8 +120,9 @@ CDeterministicMNCPtr CDeterministicMNList::GetValidMN(const uint256& proTxHash) CDeterministicMNCPtr CDeterministicMNList::GetMNByOperatorKey(const CBLSPublicKey& pubKey) const { - const auto it = ranges::find_if(mnMap, - [&pubKey](const auto& p){return p.second->pdmnState->pubKeyOperator.Get() == pubKey;}); + const auto it = std::ranges::find_if(mnMap, [&pubKey](const auto& p) { + return p.second->pdmnState->pubKeyOperator.Get() == pubKey; + }); if (it == mnMap.end()) { return nullptr; } @@ -242,7 +243,7 @@ std::vector CDeterministicMNList::GetProjectedMNPayees(gsl // If the last payee is an EvoNode, we need to check its consecutive payments and pay him again if needed if (dmn->nType == MnType::Evo && dmn->pdmnState->nConsecutivePayments < dmn_types::Evo.voting_weight) { remaining_evo_payments = dmn_types::Evo.voting_weight - dmn->pdmnState->nConsecutivePayments; - for ([[maybe_unused]] auto _ : irange::range(remaining_evo_payments)) { + for ([[maybe_unused]] auto _ : util::irange(remaining_evo_payments)) { result.emplace_back(dmn); evo_to_be_skipped = dmn; } @@ -253,14 +254,14 @@ std::vector CDeterministicMNList::GetProjectedMNPayees(gsl ForEachMNShared(/*onlyValid=*/true, [&](const auto& dmn) { if (dmn == evo_to_be_skipped) return; - for ([[maybe_unused]] auto _ : irange::range(isMNRewardReallocation ? 1 : GetMnType(dmn->nType).voting_weight)) { + for ([[maybe_unused]] auto _ : util::irange(isMNRewardReallocation ? 1 : GetMnType(dmn->nType).voting_weight)) { result.emplace_back(dmn); } }); if (evo_to_be_skipped != nullptr) { // if EvoNode is in the middle of payments, add entries for already paid ones to the end of the list - for ([[maybe_unused]] auto _ : irange::range(evo_to_be_skipped->pdmnState->nConsecutivePayments)) { + for ([[maybe_unused]] auto _ : util::irange(evo_to_be_skipped->pdmnState->nConsecutivePayments)) { result.emplace_back(evo_to_be_skipped); } } @@ -853,13 +854,13 @@ CDeterministicMNList CDeterministicMNManager::GetListForBlockInternal(gsl::not_n mnListsCache.emplace(snapshot_hash, snapshot); } else { // keep snapshots for yet alive quorums - if (ranges::any_of(Params().GetConsensus().llmqs, - [&snapshot, this](const auto& params) EXCLUSIVE_LOCKS_REQUIRED(cs) { - AssertLockHeld(cs); - return (snapshot.GetHeight() % params.dkgInterval == 0) && - (snapshot.GetHeight() + params.dkgInterval * (params.keepOldConnections + 1) >= - tipIndex->nHeight); - })) { + if (std::ranges::any_of(Params().GetConsensus().llmqs, [&snapshot, this]( + const auto& params) EXCLUSIVE_LOCKS_REQUIRED(cs) { + AssertLockHeld(cs); + return (snapshot.GetHeight() % params.dkgInterval == 0) && + (snapshot.GetHeight() + params.dkgInterval * (params.keepOldConnections + 1) >= + tipIndex->nHeight); + })) { mnListsCache.emplace(snapshot_hash, snapshot); } } @@ -919,7 +920,7 @@ void CDeterministicMNManager::CleanupCache(int nHeight) // it's a snapshot for the tip, keep it continue; } - bool fQuorumCache = ranges::any_of(Params().GetConsensus().llmqs, [&nHeight, &p](const auto& params){ + bool fQuorumCache = std::ranges::any_of(Params().GetConsensus().llmqs, [&nHeight, &p](const auto& params) { return (p.second.GetHeight() % params.dkgInterval == 0) && (p.second.GetHeight() + params.dkgInterval * (params.keepOldConnections + 1) >= nHeight); }); @@ -1465,7 +1466,7 @@ bool CDeterministicMNManager::MigrateLegacyDiffs(const CBlockIndex* const tip_in CDeterministicMNList snapshot; const auto start_height{Params().GetConsensus().DeploymentHeight(Consensus::DEPLOYMENT_DIP0003)}; - for (auto current_height : irange::range(start_height, tip_index->nHeight + 1)) { + for (auto current_height : std::views::iota(start_height, tip_index->nHeight + 1)) { auto current_index = tip_index->GetAncestor(current_height); auto target_key{std::make_tuple(DB_LIST_DIFF_LEGACY, current_index->GetBlockHash())}; pcursor->Seek(target_key); diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index 1bdcf7343e96..8563076e9a9f 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -21,9 +21,11 @@ #include #include +#include #include #include #include +#include #include #include @@ -262,17 +264,17 @@ class CDeterministicMNList [[nodiscard]] size_t GetValidMNsCount() const { - return ranges::count_if(mnMap, [](const auto& p) { return !p.second->pdmnState->IsBanned(); }); + return std::ranges::count_if(mnMap, [](const auto& p) { return !p.second->pdmnState->IsBanned(); }); } [[nodiscard]] size_t GetAllEvoCount() const { - return ranges::count_if(mnMap, [](const auto& p) { return p.second->nType == MnType::Evo; }); + return std::ranges::count_if(mnMap, [](const auto& p) { return p.second->nType == MnType::Evo; }); } [[nodiscard]] size_t GetValidEvoCount() const { - return ranges::count_if(mnMap, [](const auto& p) { + return std::ranges::count_if(mnMap, [](const auto& p) { return p.second->nType == MnType::Evo && !p.second->pdmnState->IsBanned(); }); } diff --git a/src/evo/dmnstate.h b/src/evo/dmnstate.h index b2f93fad0ccd..cb1d3a12c8d0 100644 --- a/src/evo/dmnstate.h +++ b/src/evo/dmnstate.h @@ -11,7 +11,7 @@ #include #include #include