From f7568c7aefb521c4c3cebc0ef198487303ae4fff Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Sat, 31 Oct 2026 02:42:50 +0700 Subject: [PATCH 01/10] refactor: use SteadyClock for Dash-specific benchmark timers GetTimeMicros() reads the system clock, which can jump, and hides the unit in a plain int64_t. bitcoin#27233 removes it; the remaining Dash callers in cbtx.cpp, simplifiedmns.cpp, specialtxman.cpp, httprpc.cpp and DisconnectTip() all measure elapsed time, so switch them to SteadyClock durations and Ticks<>() the way ConnectBlock() already does. --- src/evo/cbtx.cpp | 21 ++++++------ src/evo/simplifiedmns.cpp | 11 +++---- src/evo/specialtxman.cpp | 69 ++++++++++++++++++++------------------- src/httprpc.cpp | 7 ++-- src/validation.cpp | 6 ++-- 5 files changed, 58 insertions(+), 56 deletions(-) diff --git a/src/evo/cbtx.cpp b/src/evo/cbtx.cpp index b059354fa955..a74c3f1da954 100644 --- a/src/evo/cbtx.cpp +++ b/src/evo/cbtx.cpp @@ -18,6 +18,7 @@ #include #include #include +#include using node::ReadBlockFromDisk; @@ -82,11 +83,11 @@ bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPre const llmq::CQuorumBlockProcessor& quorum_block_processor, uint256& merkleRootRet, BlockValidationState& state) { - static int64_t nTimeMined = 0; - static int64_t nTimeLoop = 0; - static int64_t nTimeMerkle = 0; + static SteadyClock::duration nTimeMined{}; + static SteadyClock::duration nTimeLoop{}; + static SteadyClock::duration nTimeMerkle{}; - int64_t nTime1 = GetTimeMicros(); + const auto nTime1{SteadyClock::now()}; auto retVal = quorum_block_processor.GetQcHashes(pindexPrev); if (!retVal) { @@ -95,8 +96,8 @@ bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPre // The returned quorums are in reversed order, so the most recent one is at index 0 auto [qcHashes, qcIndexedHashes] = retVal.value(); - int64_t nTime2 = GetTimeMicros(); nTimeMined += nTime2 - nTime1; - LogPrint(BCLog::BENCHMARK, " - GetQcHashes: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeMined * 0.000001); + const auto nTime2{SteadyClock::now()}; nTimeMined += nTime2 - nTime1; + LogPrint(BCLog::BENCHMARK, " - GetQcHashes: %.2fms [%.2fs]\n", Ticks(nTime2 - nTime1), Ticks(nTimeMined)); // now add the commitments from the current block, which are not returned by GetMinedAndActiveCommitmentsUntilBlock // due to the use of pindexPrev (we don't have the tip index here) @@ -155,14 +156,14 @@ bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPre } std::sort(vec_hashes_final.begin(), vec_hashes_final.end()); - int64_t nTime3 = GetTimeMicros(); nTimeLoop += nTime3 - nTime2; - LogPrint(BCLog::BENCHMARK, " - Loop: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeLoop * 0.000001); + const auto nTime3{SteadyClock::now()}; nTimeLoop += nTime3 - nTime2; + LogPrint(BCLog::BENCHMARK, " - Loop: %.2fms [%.2fs]\n", Ticks(nTime3 - nTime2), Ticks(nTimeLoop)); bool mutated = false; merkleRootRet = ComputeMerkleRoot(vec_hashes_final, &mutated); - int64_t nTime4 = GetTimeMicros(); nTimeMerkle += nTime4 - nTime3; - LogPrint(BCLog::BENCHMARK, " - ComputeMerkleRoot: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeMerkle * 0.000001); + const auto nTime4{SteadyClock::now()}; nTimeMerkle += nTime4 - nTime3; + LogPrint(BCLog::BENCHMARK, " - ComputeMerkleRoot: %.2fms [%.2fs]\n", Ticks(nTime4 - nTime3), Ticks(nTimeMerkle)); if (mutated) { return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "mutated-calc-cbtx-quorummerkleroot"); diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index dc63317d88f5..002c847441c8 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -103,11 +103,10 @@ bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, gsl::not_null nTimeMerkle = 0; - - int64_t nTime1 = GetTimeMicros(); + const auto nTime1{SteadyClock::now()}; static Mutex cached_mutex; + static SteadyClock::duration nTimeMerkle GUARDED_BY(cached_mutex){}; static gsl::not_null> cached_sml GUARDED_BY(cached_mutex){ std::make_shared()}; static uint256 merkleRootCached GUARDED_BY(cached_mutex); @@ -125,10 +124,10 @@ bool CalcCbTxMerkleRootMNList(uint256& merkleRootRet, gsl::not_nullCalcMerkleRoot(&mutated); - int64_t nTime2 = GetTimeMicros(); + const auto nTime2{SteadyClock::now()}; nTimeMerkle += nTime2 - nTime1; - LogPrint(BCLog::BENCHMARK, " - CalcMerkleRoot: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), - nTimeMerkle * 0.000001); + LogPrint(BCLog::BENCHMARK, " - CalcMerkleRoot: %.2fms [%.2fs]\n", Ticks(nTime2 - nTime1), + Ticks(nTimeMerkle)); cached_sml = sml; merkleRootCached = merkleRootRet; diff --git a/src/evo/specialtxman.cpp b/src/evo/specialtxman.cpp index d4f6e03e08d9..102df6408d2d 100644 --- a/src/evo/specialtxman.cpp +++ b/src/evo/specialtxman.cpp @@ -29,6 +29,7 @@ #include #include #include +#include CSpecialTxProcessor::CSpecialTxProcessor(CEvoDB& evodb, CDeterministicMNManager& dmnman, llmq::CQuorumBlockProcessor& qblockman, llmq::CQuorumSnapshotManager& qsnapman, @@ -910,14 +911,14 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const AssertLockHeld(::cs_main); try { - static int64_t nTimeLoop = 0; - static int64_t nTimeQuorum = 0; - static int64_t nTimeDMN = 0; - static int64_t nTimeMnehf = 0; - static int64_t nTimePayload = 0; - static int64_t nTimeCreditPool = 0; + static SteadyClock::duration nTimeLoop{}; + static SteadyClock::duration nTimeQuorum{}; + static SteadyClock::duration nTimeDMN{}; + static SteadyClock::duration nTimeMnehf{}; + static SteadyClock::duration nTimePayload{}; + static SteadyClock::duration nTimeCreditPool{}; - int64_t nTime1 = GetTimeMicros(); + const auto nTime1{SteadyClock::now()}; std::optional opt_cbTx{std::nullopt}; if (fCheckCbTxMerkleRoots && block.vtx.size() > 0 && block.vtx[0]->nType == TRANSACTION_COINBASE) { @@ -945,10 +946,10 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const } } - int64_t nTime2 = GetTimeMicros(); + const auto nTime2{SteadyClock::now()}; nTimePayload += nTime2 - nTime1; - LogPrint(BCLog::BENCHMARK, " - GetTxPayload: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), - nTimePayload * 0.000001); + LogPrint(BCLog::BENCHMARK, " - GetTxPayload: %.2fms [%.2fs]\n", Ticks(nTime2 - nTime1), + Ticks(nTimePayload)); CRangesSet indexes; if (DeploymentActiveAt(*pindex, m_consensus_params, Consensus::DEPLOYMENT_V20)) { @@ -982,9 +983,9 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const } } - int64_t nTime3 = GetTimeMicros(); + const auto nTime3{SteadyClock::now()}; nTimeLoop += nTime3 - nTime2; - LogPrint(BCLog::BENCHMARK, " - Loop: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeLoop * 0.000001); + LogPrint(BCLog::BENCHMARK, " - Loop: %.2fms [%.2fs]\n", Ticks(nTime3 - nTime2), Ticks(nTimeLoop)); if (opt_cbTx.has_value()) { if (!CheckCreditPoolDiffForBlock(block, pindex, *opt_cbTx, blockSubsidy, state)) { @@ -994,20 +995,20 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const } } - int64_t nTime4 = GetTimeMicros(); + const auto nTime4{SteadyClock::now()}; nTimeCreditPool += nTime4 - nTime3; - LogPrint(BCLog::BENCHMARK, " - CheckCreditPoolDiffForBlock: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), - nTimeCreditPool * 0.000001); + LogPrint(BCLog::BENCHMARK, " - CheckCreditPoolDiffForBlock: %.2fms [%.2fs]\n", Ticks(nTime4 - nTime3), + Ticks(nTimeCreditPool)); if (!m_qblockman.ProcessBlock(chainstate, block, pindex, state, fJustCheck, fCheckCbTxMerkleRoots)) { // pass the state returned by the function above return false; } - int64_t nTime5 = GetTimeMicros(); + const auto nTime5{SteadyClock::now()}; nTimeQuorum += nTime5 - nTime4; - LogPrint(BCLog::BENCHMARK, " - m_qblockman.ProcessBlock: %.2fms [%.2fs]\n", 0.001 * (nTime5 - nTime4), - nTimeQuorum * 0.000001); + LogPrint(BCLog::BENCHMARK, " - m_qblockman.ProcessBlock: %.2fms [%.2fs]\n", Ticks(nTime5 - nTime4), + Ticks(nTimeQuorum)); CDeterministicMNList mn_list; if (DeploymentActiveAt(*pindex, m_consensus_params, Consensus::DEPLOYMENT_DIP0003)) { @@ -1027,15 +1028,15 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const } } - int64_t nTime6 = GetTimeMicros(); + const auto nTime6{SteadyClock::now()}; nTimeDMN += nTime6 - nTime5; - LogPrint(BCLog::BENCHMARK, " - m_dmnman.ProcessBlock: %.2fms [%.2fs]\n", 0.001 * (nTime6 - nTime5), - nTimeDMN * 0.000001); + LogPrint(BCLog::BENCHMARK, " - m_dmnman.ProcessBlock: %.2fms [%.2fs]\n", Ticks(nTime6 - nTime5), + Ticks(nTimeDMN)); if (opt_cbTx.has_value()) { - static int64_t nTimeMerkleMNL = 0; - static int64_t nTimeMerkleQuorums = 0; - static int64_t nTimeCbTxCL = 0; + static SteadyClock::duration nTimeMerkleMNL{}; + static SteadyClock::duration nTimeMerkleQuorums{}; + static SteadyClock::duration nTimeCbTxCL{}; uint256 calculatedMerkleRootMNL; if (!CalcCbTxMerkleRootMNList(calculatedMerkleRootMNL, mn_list.to_sml(), state)) { @@ -1046,10 +1047,10 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-mnmerkleroot"); } - int64_t nTime6_1 = GetTimeMicros(); + const auto nTime6_1{SteadyClock::now()}; nTimeMerkleMNL += nTime6_1 - nTime6; LogPrint(BCLog::BENCHMARK, " - CalcCbTxMerkleRootMNList: %.2fms [%.2fs]\n", - 0.001 * (nTime6_1 - nTime6), nTimeMerkleMNL * 0.000001); + Ticks(nTime6_1 - nTime6), Ticks(nTimeMerkleMNL)); if (opt_cbTx->nVersion >= CCbTx::Version::MERKLE_ROOT_QUORUMS) { uint256 calculatedMerkleRootQuorums; @@ -1062,34 +1063,34 @@ bool CSpecialTxProcessor::ProcessSpecialTxsInBlock(Chainstate& chainstate, const } } - int64_t nTime6_2 = GetTimeMicros(); + const auto nTime6_2{SteadyClock::now()}; nTimeMerkleQuorums += nTime6_2 - nTime6_1; LogPrint(BCLog::BENCHMARK, " - CalcCbTxMerkleRootQuorums: %.2fms [%.2fs]\n", - 0.001 * (nTime6_2 - nTime6_1), nTimeMerkleQuorums * 0.000001); + Ticks(nTime6_2 - nTime6_1), Ticks(nTimeMerkleQuorums)); if (!CheckCbTxBestChainlock(*opt_cbTx, pindex, m_consensus_params, chain, m_qman, m_chainlocks, state)) { // pass the state returned by the function above return false; } - int64_t nTime6_3 = GetTimeMicros(); + const auto nTime6_3{SteadyClock::now()}; nTimeCbTxCL += nTime6_3 - nTime6_2; LogPrint(BCLog::BENCHMARK, " - CheckCbTxBestChainlock: %.2fms [%.2fs]\n", - 0.001 * (nTime6_3 - nTime6_2), nTimeCbTxCL * 0.000001); + Ticks(nTime6_3 - nTime6_2), Ticks(nTimeCbTxCL)); } - int64_t nTime7 = GetTimeMicros(); + const auto nTime7{SteadyClock::now()}; if (!m_mnhfman->ProcessBlock(block, pindex, fJustCheck, state)) { // pass the state returned by the function above return false; } - int64_t nTime8 = GetTimeMicros(); + const auto nTime8{SteadyClock::now()}; nTimeMnehf += nTime8 - nTime7; - LogPrint(BCLog::BENCHMARK, " - m_mnhfman->ProcessBlock: %.2fms [%.2fs]\n", 0.001 * (nTime8 - nTime7), - nTimeMnehf * 0.000001); + LogPrint(BCLog::BENCHMARK, " - m_mnhfman->ProcessBlock: %.2fms [%.2fs]\n", Ticks(nTime8 - nTime7), + Ticks(nTimeMnehf)); if (DeploymentActiveAfter(pindex, m_consensus_params, Consensus::DEPLOYMENT_V19) && bls::bls_legacy_scheme.load()) { // NOTE: The block next to the activation is the one that is using new rules. diff --git a/src/httprpc.cpp b/src/httprpc.cpp index 046b1683089f..295656ff73a7 100644 --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -77,20 +78,20 @@ class RpcHttpRequest { public: HTTPRequest* m_req; - int64_t m_startTime; + SteadyClock::time_point m_startTime; int m_status{0}; std::string user; std::string command; RpcHttpRequest(HTTPRequest* req) : m_req{req}, - m_startTime{GetTimeMicros()} + m_startTime{SteadyClock::now()} {} ~RpcHttpRequest() { const bool is_external = find(g_external_usernames.begin(), g_external_usernames.end(), user) != g_external_usernames.end(); - LogPrint(BCLog::BENCHMARK, "HTTP RPC request handled: user=%s command=%s external=%s status=%d elapsed_time_ms=%d\n", user, command, is_external, m_status, (GetTimeMicros() - m_startTime) / 1000); + LogPrint(BCLog::BENCHMARK, "HTTP RPC request handled: user=%s command=%s external=%s status=%d elapsed_time_ms=%d\n", user, command, is_external, m_status, Ticks(SteadyClock::now() - m_startTime)); } bool send_reply(int status, const std::string& response = "") diff --git a/src/validation.cpp b/src/validation.cpp index 3d0b419490a1..981eec95629b 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3473,7 +3473,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra AssertLockHeld(cs_main); if (m_mempool) AssertLockHeld(m_mempool->cs); - int64_t nTime1 = GetTimeMicros(); + const auto nTime1{SteadyClock::now()}; CBlockIndex *pindexDelete = m_chain.Tip(); assert(pindexDelete); @@ -3548,13 +3548,13 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra GetMainSignals().BlockDisconnected(pblock, pindexDelete); } - int64_t nTime2 = GetTimeMicros(); + const auto nTime2{SteadyClock::now()}; unsigned int nSigOps = 0; for (const auto& tx : block.vtx) { nSigOps += GetLegacySigOpCount(*tx); } - ::g_stats_client->timing("DisconnectTip_ms", (nTime2 - nTime1) / 1000, 1.0f); + ::g_stats_client->timing("DisconnectTip_ms", Ticks(nTime2 - nTime1), 1.0f); ::g_stats_client->gauge("blocks.tip.SizeBytes", ::GetSerializeSize(block, PROTOCOL_VERSION), 1.0f); ::g_stats_client->gauge("blocks.tip.Height", m_chain.Height(), 1.0f); ::g_stats_client->gauge("blocks.tip.Version", block.nVersion, 1.0f); From b3e3f8e9c461882ee4737f5de15f445a7703432a Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 23 Mar 2023 10:02:58 +0000 Subject: [PATCH 02/10] Merge bitcoin/bitcoin#27233: refactor: Replace GetTimeMicros by SystemClock faf3f12424fa8558e65fa3f1dd3aa1d0eea8604e refactor: Replace GetTimeMicros by SystemClock (MarcoFalke) Pull request description: It is unclear from the name that `GetTimeMicros` returns the system time. Also, it is not using the type-safe `std::chrono` types. Fix both issues by replacing it with `SystemClock` in the only place it is used. This refactor should not change behavior. ACKs for top commit: willcl-ark: tACK faf3f1242 john-moffett: ACK faf3f12424fa8558e65fa3f1dd3aa1d0eea8604e changes, but left a comment for the existing code. Tree-SHA512: 069e6ef26467a469f128b98a4aeb334f141742befd7880cb3a7d280480e9f0684dc0686fa6a828cdcb3d11943ae5c7f8ad5d9d9dab4c668be85e5d28c78cd489 Co-authored-by: fanquake --- src/logging.cpp | 7 ++++--- src/util/time.cpp | 5 ----- src/util/time.h | 8 +++----- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 72a877fd7518..2f0de91a72d1 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -395,11 +395,12 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str) return str; if (m_started_new_line) { - int64_t nTimeMicros = GetTimeMicros(); - strStamped = FormatISO8601DateTime(nTimeMicros/1000000); + const auto now{SystemClock::now()}; + const auto now_seconds{std::chrono::time_point_cast(now)}; + strStamped = FormatISO8601DateTime(TicksSinceEpoch(now_seconds)); if (m_log_time_micros && !strStamped.empty()) { strStamped.pop_back(); - strStamped += strprintf(".%06dZ", nTimeMicros%1000000); + strStamped += strprintf(".%06dZ", Ticks(now - now_seconds)); } std::chrono::seconds mocktime = GetMockTime(); if (mocktime > 0s) { diff --git a/src/util/time.cpp b/src/util/time.cpp index 8061a864ca1c..22d87d7835de 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -55,11 +55,6 @@ std::chrono::seconds GetMockTime() return g_mock_time.load(std::memory_order_relaxed); } -int64_t GetTimeMicros() -{ - return int64_t{GetSystemTime().count()}; -} - int64_t GetTime() { return GetTime().count(); } std::string FormatISO8601DateTime(int64_t nTime) diff --git a/src/util/time.h b/src/util/time.h index 42fdcd1f753c..0f7532cc0ebf 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -99,14 +99,12 @@ using MillisecondsDouble = std::chrono::duration() if a cast is needed. * ClockType is - * - std::chrono::steady_clock for steady time - * - std::chrono::system_clock for system time - * - NodeClock for mockable system time + * - SteadyClock/std::chrono::steady_clock for steady time + * - SystemClock/std::chrono::system_clock for system time + * - NodeClock for mockable system time */ int64_t GetTime(); -/** Returns the system time (not mockable) */ -int64_t GetTimeMicros(); /** * DEPRECATED * Use SetMockTime with chrono type From e96e71dd65b85bdc438051f4974c5211a8e524c7 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Sun, 2 Apr 2023 12:39:54 +0100 Subject: [PATCH 03/10] Merge bitcoin/bitcoin#27379: net processing: #26140 follow-ups 3fa4c54ac54b2d738e0c43b57b5c232ee02fe3b3 [net processing] Pass TxRelay to FindTxForGetData instead of Peer (dergoegge) c85ee76a3647a42d3a59c28444c5ca52e7db7c5e [net processin] Don't take cs_main in FindTxForGetData (dergoegge) Pull request description: Addresses left over feedback from #26140. * https://github.com/bitcoin/bitcoin/pull/26140#discussion_r1153498543 * https://github.com/bitcoin/bitcoin/pull/26140#discussion_r1153499627 `mapRelay` is only accessed from the message processing thread and does not need to be kept in sync with anything validation specific, it is therfore perfectly fine to have it guarded by `g_msgproc_mutex`. ACKs for top commit: jnewbery: utACK 3fa4c54ac54b2d738e0c43b57b5c232ee02fe3b3 hebasto: ACK 3fa4c54ac54b2d738e0c43b57b5c232ee02fe3b3, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: 3ef84bfe4abfa8d991a7e65d9184221294d80e0df0bbb47f0270ab6ca1593266c98abf83c610f9f86b4d16c7a4b62bcf83f8856c68d3c2e10894bff6ed3e88cd Co-authored-by: fanquake --- src/net_processing.cpp | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 017616c5a5a9..99502604080b 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -362,11 +362,6 @@ struct Peer { LOCK(m_tx_relay_mutex); return m_can_tx_relay ? m_tx_relay.get() : nullptr; }; - const TxRelay* GetTxRelay() const EXCLUSIVE_LOCKS_REQUIRED(!m_tx_relay_mutex) - { - LOCK(m_tx_relay_mutex); - return m_can_tx_relay ? m_tx_relay.get() : nullptr; - }; /** A vector of addresses to send to the peer, limited to MAX_ADDR_TO_SEND. */ std::vector m_addrs_to_send GUARDED_BY(NetEventsInterface::g_msgproc_mutex); @@ -723,7 +718,7 @@ class PeerManagerImpl final : public PeerManager /** * Private implementation of IsInvInFilter which does not call GetPeerRef; to be prefered when the PeerRef is available. */ - bool IsInvInFilter(const Peer& peer, const uint256& hash) const; + bool IsInvInFilter(Peer& peer, const uint256& hash) const; /** Get a shared pointer to the Peer object. * May return an empty shared_ptr if the Peer object can't be found. */ @@ -1107,8 +1102,8 @@ class PeerManagerImpl final : public PeerManager std::atomic m_last_tip_update{0s}; /** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */ - CTransactionRef FindTxForGetData(const Peer& peer, const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) - LOCKS_EXCLUDED(cs_main) EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex); + CTransactionRef FindTxForGetData(const Peer::TxRelay& tx_relay, const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) + EXCLUSIVE_LOCKS_REQUIRED(NetEventsInterface::g_msgproc_mutex); void ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex, peer.m_getdata_requests_mutex, NetEventsInterface::g_msgproc_mutex) @@ -1123,9 +1118,9 @@ class PeerManagerImpl final : public PeerManager /** Relay map (txid -> CTransactionRef) */ typedef std::map MapRelay; - MapRelay mapRelay GUARDED_BY(cs_main); + MapRelay mapRelay GUARDED_BY(NetEventsInterface::g_msgproc_mutex); /** Expiration-time ordered list of (expire time, relay map entry) pairs. */ - std::deque> g_relay_expiration GUARDED_BY(cs_main); + std::deque> g_relay_expiration GUARDED_BY(NetEventsInterface::g_msgproc_mutex); /** * When a peer sends us a valid block, instruct it to announce blocks to us @@ -2547,7 +2542,7 @@ void PeerManagerImpl::AskPeersForTransaction(const uint256& txid) } } -bool PeerManagerImpl::IsInvInFilter(const Peer& peer, const uint256& hash) const +bool PeerManagerImpl::IsInvInFilter(Peer& peer, const uint256& hash) const { if (auto tx_relay = peer.GetTxRelay(); tx_relay != nullptr) { LOCK(tx_relay->m_tx_inventory_mutex); @@ -2932,7 +2927,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv& } //! Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). -CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer& peer, const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) +CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer::TxRelay& tx_relay, const uint256& txid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) { auto txinfo = m_mempool.info(txid); if (!txinfo.tx) { @@ -2950,17 +2945,13 @@ CTransactionRef PeerManagerImpl::FindTxForGetData(const Peer& peer, const uint25 } } - { - LOCK(cs_main); - - // Otherwise, the transaction must have been announced recently. - if (Assume(peer.GetTxRelay())->m_recently_announced_invs.contains(txid)) { - // If it was, it can be relayed from either the mempool... - if (txinfo.tx) return std::move(txinfo.tx); - // ... or the relay pool. - auto mi = mapRelay.find(txid); - if (mi != mapRelay.end()) return mi->second; - } + // Otherwise, the transaction must have been announced recently. + if (tx_relay.m_recently_announced_invs.contains(txid)) { + // If it was, it can be relayed from either the mempool... + if (txinfo.tx) return std::move(txinfo.tx); + // ... or the relay pool. + auto mi = mapRelay.find(txid); + if (mi != mapRelay.end()) return mi->second; } return {}; @@ -3008,7 +2999,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic bool push = false; if (inv.IsGenTxMsg()) { - CTransactionRef tx = FindTxForGetData(peer, inv.hash, mempool_req, now); + CTransactionRef tx = FindTxForGetData(*tx_relay, inv.hash, mempool_req, now); if (tx) { CCoinJoinBroadcastTx dstx; if (inv.IsMsgDstx()) { From 72ec2d7e0a168fe091fca9bd24cb1704cbf6c294 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Sat, 11 Mar 2023 11:13:48 +0100 Subject: [PATCH 04/10] Merge bitcoin/bitcoin#27239: refactor: Consistently use context args over gArgs in node/interfaces fa3e9b420fbc1ce9b20218f03356502e1b79d5ff refactor: Consistently use args over gArgs in init.cpp (MarcoFalke) fa891120c81d3ef81bf2b42510c83e879d026944 refactor: Consistently use context args over gArgs in node/interfaces (MarcoFalke) Pull request description: Currently `node/interfaces.cpp` uses a mix of `gArgs` vs `m_context->args`. This is fine, because outside of tests those should be identical. However, it makes the code inconsistent and harder to use in tests. Fix that by using `args` from the context consistently. Do the same in `init.cpp`, where `gArgs` and `args` are inconsistently used in the same scope or even line. ACKs for top commit: ryanofsky: Code review ACK fa3e9b420fbc1ce9b20218f03356502e1b79d5ff Tree-SHA512: bcd52f176794ebb1ecb9e1922411f7b84d212ae13bad314a1961b85f3077f645fca71fb0124c0889ebfdd8b59a0903b99b9985b1a4fb8f152aa6d7f0126fe5c7 Co-authored-by: fanquake --- src/init.cpp | 22 ++++++++++----------- src/node/interfaces.cpp | 42 +++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 0cce8815ae67..05b243aa8d1d 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1220,7 +1220,7 @@ bool AppInitParameterInteraction(const ArgsManager& args) InitWarning(warnings); } - if (!fs::is_directory(gArgs.GetBlocksDirPath())) { + if (!fs::is_directory(args.GetBlocksDirPath())) { return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist."), args.GetArg("-blocksdir", ""))); } @@ -1646,7 +1646,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) if (args.IsArgSet("-asmap")) { fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME); if (!asmap_path.is_absolute()) { - asmap_path = gArgs.GetDataDirNet() / asmap_path; + asmap_path = args.GetDataDirNet() / asmap_path; } if (!fs::exists(asmap_path)) { InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path)))); @@ -1693,7 +1693,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) } assert(!node.banman); - node.banman = std::make_unique(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME)); + node.banman = std::make_unique(args.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME)); assert(!node.connman); node.connman = std::make_unique(GetRand(), GetRand(), @@ -1962,7 +1962,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) // ********************************************************* Step 7a: Load sporks if (!node.sporkman->LoadCache()) { - auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "sporks.dat"); + auto file_path = fs::PathToString(args.GetDataDirNet() / "sporks.dat"); return InitError(strprintf(_("Failed to load sporks cache from %s"), file_path)); } @@ -2268,7 +2268,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) bool fLoadCacheFiles = !(fReindex || fReindexChainState) && WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip() != nullptr); if (!node.netfulfilledman->LoadCache(fLoadCacheFiles)) { - auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "netfulfilled.dat"); + auto file_path = fs::PathToString(args.GetDataDirNet() / "netfulfilled.dat"); if (fLoadCacheFiles) { return InitError(strprintf(_("Failed to load fulfilled requests cache from %s"), file_path)); } @@ -2276,7 +2276,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) } if (!node.mn_metaman->LoadCache(fLoadCacheFiles)) { - auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "mncache.dat"); + auto file_path = fs::PathToString(args.GetDataDirNet() / "mncache.dat"); if (fLoadCacheFiles) { return InitError(strprintf(_("Failed to load masternode cache from %s"), file_path)); } @@ -2285,7 +2285,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) if (is_governance_enabled) { if (!node.govman->LoadCache(fLoadCacheFiles)) { - auto file_path = fs::PathToString(gArgs.GetDataDirNet() / "governance.dat"); + auto file_path = fs::PathToString(args.GetDataDirNet() / "governance.dat"); if (fLoadCacheFiles) { return InitError(strprintf(_("Failed to load governance cache from %s"), file_path)); } @@ -2413,12 +2413,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) // ********************************************************* Step 11: import blocks - if (!CheckDiskSpace(gArgs.GetDataDirNet())) { - InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(gArgs.GetDataDirNet())))); + if (!CheckDiskSpace(args.GetDataDirNet())) { + InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(args.GetDataDirNet())))); return false; } - if (!CheckDiskSpace(gArgs.GetBlocksDirPath())) { - InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(gArgs.GetBlocksDirPath())))); + if (!CheckDiskSpace(args.GetBlocksDirPath())) { + InitError(strprintf(_("Error: Disk space is low for %s"), fs::quoted(fs::PathToString(args.GetBlocksDirPath())))); return false; } diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index b330c2590c84..b794c88c210f 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -889,15 +889,15 @@ class NodeImpl : public Node CoinJoinOptionsImpl m_coinjoin; explicit NodeImpl(NodeContext& context) { setContext(&context); } - void initLogging() override { InitLogging(*Assert(m_context->args)); } - void initParameterInteraction() override { InitParameterInteraction(*Assert(m_context->args)); } + void initLogging() override { InitLogging(args()); } + void initParameterInteraction() override { InitParameterInteraction(args()); } bilingual_str getWarnings() override { return GetWarnings(true); } int getExitStatus() override { return Assert(m_context)->exit_status.load(); } uint64_t getLogCategories() override { return LogInstance().GetCategoryMask(); } bool baseInitialize() override { - if (!AppInitBasicSetup(gArgs, Assert(m_context)->exit_status)) return false; - if (!AppInitParameterInteraction(gArgs)) return false; + if (!AppInitBasicSetup(args(), Assert(m_context)->exit_status)) return false; + if (!AppInitParameterInteraction(args())) return false; m_context->kernel = std::make_unique(); if (!AppInitSanityChecks(*m_context->kernel)) return false; @@ -929,7 +929,7 @@ class NodeImpl : public Node { StartShutdown(); // Stop RPC for clean shutdown if any of waitfor* commands is executed. - if (gArgs.GetBoolArg("-server", false)) { + if (args().GetBoolArg("-server", false)) { InterruptRPC(); StopRPC(); } @@ -938,28 +938,28 @@ class NodeImpl : public Node bool isSettingIgnored(const std::string& name) override { bool ignored = false; - gArgs.LockSettings([&](util::Settings& settings) { + args().LockSettings([&](util::Settings& settings) { if (auto* options = util::FindKey(settings.command_line_options, name)) { ignored = !options->empty(); } }); return ignored; } - util::SettingsValue getPersistentSetting(const std::string& name) override { return gArgs.GetPersistentSetting(name); } + util::SettingsValue getPersistentSetting(const std::string& name) override { return args().GetPersistentSetting(name); } void updateRwSetting(const std::string& name, const util::SettingsValue& value) override { - gArgs.LockSettings([&](util::Settings& settings) { + args().LockSettings([&](util::Settings& settings) { if (value.isNull()) { settings.rw_settings.erase(name); } else { settings.rw_settings[name] = value; } }); - gArgs.WriteSettingsFile(); + args().WriteSettingsFile(); } void forceSetting(const std::string& name, const util::SettingsValue& value) override { - gArgs.LockSettings([&](util::Settings& settings) { + args().LockSettings([&](util::Settings& settings) { if (value.isNull()) { settings.forced_settings.erase(name); } else { @@ -969,11 +969,11 @@ class NodeImpl : public Node } void resetSettings() override { - gArgs.WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true); - gArgs.LockSettings([&](util::Settings& settings) { + args().WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true); + args().LockSettings([&](util::Settings& settings) { settings.rw_settings.clear(); }); - gArgs.WriteSettingsFile(); + args().WriteSettingsFile(); } void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); } bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); } @@ -1050,7 +1050,7 @@ class NodeImpl : public Node { #ifdef ENABLE_EXTERNAL_SIGNER std::vector signers = {}; - const std::string command = gArgs.GetArg("-signer", ""); + const std::string command = args().GetArg("-signer", ""); if (command == "") return {}; ExternalSigner::Enumerate(command, signers, Params().NetworkIDString()); std::vector> result; @@ -1073,7 +1073,7 @@ class NodeImpl : public Node int64_t getTotalBytesSent() override { return m_context->connman ? m_context->connman->GetTotalBytesSent() : 0; } size_t getMempoolSize() override { return m_context->mempool ? m_context->mempool->size() : 0; } size_t getMempoolDynamicUsage() override { return m_context->mempool ? m_context->mempool->DynamicMemoryUsage() : 0; } - size_t getMempoolMaxUsage() override { return gArgs.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000; } + size_t getMempoolMaxUsage() override { return args().GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE_MB) * 1000000; } bool getHeaderTip(int& height, int64_t& block_time) override { LOCK(::cs_main); @@ -1267,6 +1267,7 @@ class NodeImpl : public Node m_llmq.setContext(context); m_masternodeSync.setContext(context); } + ArgsManager& args() { return *Assert(Assert(m_context)->args); } ChainstateManager& chainman() { return *Assert(m_context->chainman); } NodeContext* m_context{nullptr}; }; @@ -1678,16 +1679,16 @@ class ChainImpl : public Chain } util::SettingsValue getSetting(const std::string& name) override { - return gArgs.GetSetting(name); + return args().GetSetting(name); } std::vector getSettingsList(const std::string& name) override { - return gArgs.GetSettingsList(name); + return args().GetSettingsList(name); } util::SettingsValue getRwSetting(const std::string& name) override { util::SettingsValue result; - gArgs.LockSettings([&](const util::Settings& settings) { + args().LockSettings([&](const util::Settings& settings) { if (const util::SettingsValue* value = util::FindKey(settings.rw_settings, name)) { result = *value; } @@ -1696,14 +1697,14 @@ class ChainImpl : public Chain } bool updateRwSetting(const std::string& name, const util::SettingsValue& value, bool write) override { - gArgs.LockSettings([&](util::Settings& settings) { + args().LockSettings([&](util::Settings& settings) { if (value.isNull()) { settings.rw_settings.erase(name); } else { settings.rw_settings[name] = value; } }); - return !write || gArgs.WriteSettingsFile(); + return !write || args().WriteSettingsFile(); } void requestMempoolTransactions(Notifications& notifications) override { @@ -1719,6 +1720,7 @@ class ChainImpl : public Chain } NodeContext* context() override { return &m_node; } + ArgsManager& args() { return *Assert(m_node.args); } ChainstateManager& chainman() { return *Assert(m_node.chainman); } NodeContext& m_node; }; From 7a04aaf3459f725772473b4d735a19a676a684ad Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Tue, 14 Mar 2023 09:54:18 +0100 Subject: [PATCH 05/10] Merge bitcoin/bitcoin#27238: refactor: Split logging utilities from system.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit aaced5633b81b2f08b993106a527e2a0e1d663c1 refactor: Move error() from util/system.h to logging.h (Ben Woosley) e7333b420e35054d9302f9c58fd47b6152cbd35f refactor: Extract util/exception from util/system (Ben Woosley) Pull request description: This pull request is part of the `libbitcoinkernel` project https://github.com/bitcoin/bitcoin/issues/24303 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". These commits were originally authored by empact and are taken from their parent PR #25152. #### Context There is an ongoing effort to decouple the `ArgsManager` used for command line parsing user-provided arguments from the libbitcoinkernel library (https://github.com/bitcoin/bitcoin/pull/25290, https://github.com/bitcoin/bitcoin/pull/25487, https://github.com/bitcoin/bitcoin/pull/25527, https://github.com/bitcoin/bitcoin/pull/25862, https://github.com/bitcoin/bitcoin/pull/26177, and https://github.com/bitcoin/bitcoin/pull/27125). The `ArgsManager` is defined in `system.h`. #### Changes Next to providing better code organization, this PR removes some reliance of the tree of libbitcoinkernel header includes on `system.h` (and thus the `ArgsManager` definition) by moving some logging functions out of the `system.*` files. Further commits splitting more functionality out of `system.h` are still in #25152 and will be submitted in separate PRs once this PR has been processed. ACKs for top commit: MarcoFalke: re-ACK aaced5633b81b2f08b993106a527e2a0e1d663c1 🐍 Tree-SHA512: cb39f4cb7a77e7dc1887b1cbf340d53decab8880fc00878a2f12dc627fe67245b4aafd4cc31a9eab0fad1e5bb5d0eb4cdb8d501323ca200fa6ab7b201ae34aea Co-authored-by: fanquake --- ci/dash/lint-tidy.sh | 1 + src/Makefile.am | 3 +++ src/addrdb.cpp | 1 + src/bitcoin-cli.cpp | 1 + src/bitcoin-tx.cpp | 1 + src/bitcoin-util.cpp | 1 + src/bitcoin-wallet.cpp | 1 + src/bitcoind.cpp | 1 + src/index/base.cpp | 1 + src/index/coinstatsindex.cpp | 1 + src/index/txindex.cpp | 1 + src/kernel/coinstats.cpp | 2 +- src/net.cpp | 3 ++- src/netbase.cpp | 2 +- src/node/blockstorage.cpp | 1 + src/qt/bitcoin.cpp | 1 + src/qt/guiutil.cpp | 1 + src/qt/initexecutor.cpp | 2 +- src/script/signingprovider.cpp | 2 +- src/util/exception.cpp | 21 +++++++++++++++++++++ src/util/exception.h | 14 ++++++++++++++ src/util/system.cpp | 8 -------- src/util/system.h | 3 --- src/util/thread.cpp | 2 +- 24 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 src/util/exception.cpp create mode 100644 src/util/exception.h diff --git a/ci/dash/lint-tidy.sh b/ci/dash/lint-tidy.sh index b17c0860668f..23875d727e68 100755 --- a/ci/dash/lint-tidy.sh +++ b/ci/dash/lint-tidy.sh @@ -122,6 +122,7 @@ iwyu_tool.py \ "src/util/bytevectorhash.cpp" \ "src/util/check.cpp" \ "src/util/error.cpp" \ + "src/util/exception.cpp" \ "src/util/getuniquepath.cpp" \ "src/util/hasher.cpp" \ "src/util/message.cpp" \ diff --git a/src/Makefile.am b/src/Makefile.am index 117325f569f3..2de22b1463a4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -448,6 +448,7 @@ BITCOIN_CORE_H = \ util/edge.h \ util/epochguard.h \ util/error.h \ + util/exception.h \ util/fastrange.h \ util/fees.h \ util/fs.h \ @@ -1069,6 +1070,7 @@ libbitcoin_util_a_SOURCES = \ util/check.cpp \ util/edge.cpp \ util/error.cpp \ + util/exception.cpp \ util/fees.cpp \ util/fs.cpp \ util/fs_helpers.cpp \ @@ -1381,6 +1383,7 @@ libdashkernel_la_SOURCES = \ uint256.cpp \ util/bytevectorhash.cpp \ util/check.cpp \ + util/exception.cpp \ util/fs.cpp \ util/fs_helpers.cpp \ util/getuniquepath.cpp \ diff --git a/src/addrdb.cpp b/src/addrdb.cpp index bd4d872313eb..db476cc3f0b1 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 24a7f446ece6..ed477475189e 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index e61539db3383..5cff3c7ac2d5 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -19,6 +19,7 @@ #include