From 5fa12a3cc6f189e4a14fd6f8f09ad45ae31895fc Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 30 Sep 2024 16:51:28 +0200 Subject: [PATCH] Have waitFeesChanged() frequently make a template --- src/interfaces/mining.h | 4 ++-- src/node/interfaces.cpp | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/interfaces/mining.h b/src/interfaces/mining.h index 1fb791aedbe91a..20fee7703028da 100644 --- a/src/interfaces/mining.h +++ b/src/interfaces/mining.h @@ -75,9 +75,9 @@ class Mining * Waits for fees in the next block to rise, a new tip or the timeout. * * @param[in] current_tip block hash that the most recent template builds on - * @param[in] fee_threshold how far total fees for the next block should rise (currently ignored) + * @param[in] fee_threshold how far total fees for the next block should rise * @param[in] options currently ignored: options for creating the block, should match those - * passed to createNewBlock (currently ignored) + * passed to createNewBlock * * @returns true if fees increased, false if a new tip arrives or the timeout occurs */ diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 182fb35b015ecd..66c01f4a9d972d 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -968,6 +968,9 @@ class MinerImpl : public Mining BlockAssembler::Options assemble_options{options}; ApplyArgsManOptions(*Assert(m_node.args), assemble_options); + // It's not necessary to verify the template, since we don't return it. + // This is also faster. + assemble_options.test_block_validity = false; while (!chainman().m_interrupt) { now = std::chrono::steady_clock::now(); @@ -977,9 +980,18 @@ class MinerImpl : public Mining return false; } - // TODO: when cluster mempool is available, actually calculate - // fees for the next block. This is currently too expensive. - if (context()->mempool->GetTransactionsUpdated() > last_mempool_update) return true; + // Did anything change at all? + if (context()->mempool->GetTransactionsUpdated() != last_mempool_update) { + auto block_template{BlockAssembler{chainman().ActiveChainstate(), context()->mempool.get(), assemble_options}.CreateNewBlock(CScript())}; + + CAmount fees = 0; + for (CAmount fee : block_template->vTxFees) { + // Skip coinbase + if (fee < 0) continue; + fees += fee; + if (fees >= fee_threshold) return true; + } + } std::this_thread::sleep_until(std::min(deadline, now + tick)); }