0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-04 13:55:23 -05:00

Add waitTipChanged to Mining interface

Co-authored-by: TheCharlatan <seb.kung@gmail.com>
This commit is contained in:
Sjors Provoost 2024-08-26 11:30:24 +02:00
parent 7eccdaf160
commit b94b27cf05
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
2 changed files with 35 additions and 0 deletions

View file

@ -12,6 +12,7 @@
#include <primitives/transaction.h> // for CTransactionRef
#include <stdint.h> // for int64_t
#include <uint256.h> // for uint256
#include <util/time.h> // for MillisecondsDouble
#include <memory> // for unique_ptr, shared_ptr
#include <optional> // for optional
@ -60,6 +61,17 @@ public:
virtual std::optional<BlockRef> getTip() = 0;
/**
* Waits for the tip to change
*
* @param[in] current_tip block hash of the current chain tip. Function waits
* for the chain tip to change if this matches, otherwise
* it returns right away.
* @param[in] timeout how long to wait for a new tip
* @returns Hash and height of the current chain tip after this call.
*/
virtual BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout = MillisecondsDouble::max()) = 0;
/**
* Construct a new block template
*
* @param[in] script_pub_key the coinbase output

View file

@ -34,6 +34,7 @@
#include <node/interface_ui.h>
#include <node/mini_miner.h>
#include <node/miner.h>
#include <node/kernel_notifications.h>
#include <node/transaction.h>
#include <node/types.h>
#include <node/warnings.h>
@ -935,6 +936,27 @@ public:
return BlockRef{tip->GetBlockHash(), tip->nHeight};
}
BlockRef waitTipChanged(uint256 current_tip, MillisecondsDouble timeout) override
{
// Interrupt check interval
const MillisecondsDouble tick{1000};
auto now{std::chrono::steady_clock::now()};
auto deadline = now + timeout;
// std::chrono does not check against overflow
if (deadline < now) deadline = std::chrono::steady_clock::time_point::max();
{
WAIT_LOCK(notifications().m_tip_block_mutex, lock);
while ((notifications().m_tip_block == uint256() || notifications().m_tip_block == current_tip) && !chainman().m_interrupt) {
now = std::chrono::steady_clock::now();
if (now >= deadline) break;
notifications().m_tip_block_cv.wait_until(lock, std::min(deadline, now + tick));
}
}
// Must release m_tip_block_mutex before locking cs_main, to avoid deadlocks.
LOCK(::cs_main);
return BlockRef{chainman().ActiveChain().Tip()->GetBlockHash(), chainman().ActiveChain().Tip()->nHeight};
}
bool processNewBlock(const std::shared_ptr<const CBlock>& block, bool* new_block) override
{
return chainman().ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/new_block);
@ -967,6 +989,7 @@ public:
NodeContext* context() override { return &m_node; }
ChainstateManager& chainman() { return *Assert(m_node.chainman); }
KernelNotifications& notifications() { return *Assert(m_node.notifications); }
NodeContext& m_node;
};
} // namespace