From 9e228351e761d8d24413bbc4ac1610b4f3dec2bf Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Thu, 30 May 2024 16:46:29 +0200 Subject: [PATCH] rpc: getTransactionsUpdated via miner interface --- src/interfaces/mining.h | 4 ++++ src/node/interfaces.cpp | 5 +++++ src/rpc/mining.cpp | 7 +++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/interfaces/mining.h b/src/interfaces/mining.h index 9494fd75bf..3ebc48dffa 100644 --- a/src/interfaces/mining.h +++ b/src/interfaces/mining.h @@ -41,6 +41,10 @@ public: */ virtual std::unique_ptr createNewBlock(const CScript& script_pub_key, bool use_mempool = true) = 0; + //! Return the number of transaction updates in the mempool, + //! used to decide whether to make a new block template. + virtual unsigned int getTransactionsUpdated() = 0; + /** * Check a block is completely valid from start to finish. * Only works on top of our current best block. diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 356a01b286..91ee858597 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -855,6 +855,11 @@ public: return tip->GetBlockHash(); } + unsigned int getTransactionsUpdated() override + { + return context()->mempool->GetTransactionsUpdated(); + } + bool testBlockValidity(BlockValidationState& state, const CBlock& block, bool check_merkle_root) override { LOCK(::cs_main); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index a65413de1e..3cca6a53fa 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -738,7 +738,6 @@ static RPCHelpMan getblocktemplate() } static unsigned int nTransactionsUpdatedLast; - const CTxMemPool& mempool = EnsureMemPool(node); if (!lpval.isNull()) { @@ -774,7 +773,7 @@ static RPCHelpMan getblocktemplate() { // Timeout: Check transactions for update // without holding the mempool lock to avoid deadlocks - if (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLastLP) + if (miner.getTransactionsUpdated() != nTransactionsUpdatedLastLP) break; checktxtime += std::chrono::seconds(10); } @@ -804,13 +803,13 @@ static RPCHelpMan getblocktemplate() static int64_t time_start; static std::unique_ptr pblocktemplate; if (!pindexPrev || pindexPrev->GetBlockHash() != miner.getTipHash() || - (mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5)) + (miner.getTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - time_start > 5)) { // Clear pindexPrev so future calls make a new block, despite any failures from here on pindexPrev = nullptr; // Store the pindexBest used before createNewBlock, to avoid races - nTransactionsUpdatedLast = mempool.GetTransactionsUpdated(); + nTransactionsUpdatedLast = miner.getTransactionsUpdated(); CBlockIndex* pindexPrevNew = chainman.m_blockman.LookupBlockIndex(miner.getTipHash()); time_start = GetTime();