Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/interfaces/mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class BlockTemplate
* the tip is more than 20 minutes old.
*/
virtual std::unique_ptr<BlockTemplate> waitNext(const node::BlockWaitOptions options = {}) = 0;

/**
* Interrupts the current wait for the next block template.
*/
virtual void interruptWait() = 0;
};

//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)
Expand Down
1 change: 1 addition & 0 deletions src/ipc/capnp/mining.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
getCoinbaseMerklePath @8 (context: Proxy.Context) -> (result: List(Data));
submitSolution @9 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (result: Bool);
waitNext @10 (context: Proxy.Context, options: BlockWaitOptions) -> (result: BlockTemplate);
interruptWait @11() -> ();
}

struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {
Expand Down
4 changes: 0 additions & 4 deletions src/sv2-tp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,6 @@ MAIN_FUNCTION
UninterruptibleSleep(100ms);
}

LogPrintLevel(BCLog::SV2, BCLog::Level::Info,
"Interrupt received, waiting up to %d seconds before shutting down (-sv2interval)",
options.fee_check_interval.count());

tp->Interrupt();
tp->StopThreads();
tp.reset();
Expand Down
19 changes: 19 additions & 0 deletions src/sv2/template_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <consensus/merkle.h>
#include <crypto/hex_base.h>
#include <common/args.h>
#include <ipc/exception.h>
#include <logging.h>
#include <sv2/noise.h>
#include <consensus/validation.h> // NO_WITNESS_COMMITMENT
Expand Down Expand Up @@ -122,6 +123,24 @@ Sv2TemplateProvider::~Sv2TemplateProvider()

void Sv2TemplateProvider::Interrupt()
{
AssertLockNotHeld(m_tp_mutex);

LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Interrupt pending waitNext() calls...");
{
LOCK(m_tp_mutex);
try {
for (auto& t : GetBlockTemplates()) {
t.second->interruptWait();
}
} catch (const ipc::Exception& e) {
// Bitcoin Core v30 does not yet implement interruptWait(), fall back
// to just waiting until waitNext() returns.
LogPrintLevel(BCLog::SV2, BCLog::Level::Info,
"Interrupt received, waiting up to %d seconds before shutting down (-sv2interval)",
m_options.fee_check_interval.count());
}
}

m_flag_interrupt_sv2 = true;
// Also interrupt network threads so client handlers can wind down quickly.
if (m_connman) m_connman->Interrupt();
Expand Down
5 changes: 3 additions & 2 deletions src/sv2/template_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ class Sv2TemplateProvider : public Sv2EventsInterface

/**
* Triggered on interrupt signals to stop the main event loop in ThreadSv2Handler().
* Interrupts pending waitNext() calls
*/
void Interrupt();
void Interrupt() EXCLUSIVE_LOCKS_REQUIRED(!m_tp_mutex);

/**
* Tear down of the template provider thread and any other necessary tear down.
Expand All @@ -162,7 +163,7 @@ class Sv2TemplateProvider : public Sv2EventsInterface

void SubmitSolution(node::Sv2SubmitSolutionMsg solution) EXCLUSIVE_LOCKS_REQUIRED(!m_tp_mutex) override;

/* Block templates that connected clients may be working on, only used for tests */
/* Block templates that connected clients may be working on */
BlockTemplateCache& GetBlockTemplates() EXCLUSIVE_LOCKS_REQUIRED(m_tp_mutex) { return m_block_template_cache; }

private:
Expand Down
6 changes: 6 additions & 0 deletions src/test/sv2_mock_mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <sync.h>
#include <cassert>
#include <logging.h>

namespace {
static inline uint256 HashFromHeight(uint64_t h)
Expand Down Expand Up @@ -94,6 +95,11 @@ std::unique_ptr<interfaces::BlockTemplate> MockBlockTemplate::waitNext(const nod
}
}

void MockBlockTemplate::interruptWait()
{
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "mock interruptWait()");
}

MockMining::MockMining(std::shared_ptr<MockState> st) : state(std::move(st)) {}
bool MockMining::isTestChain() { return true; }
bool MockMining::isInitialBlockDownload() { return false; }
Expand Down
1 change: 1 addition & 0 deletions src/test/sv2_mock_mining.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class MockBlockTemplate : public interfaces::BlockTemplate {
bool submitSolution(uint32_t, uint32_t, uint32_t, CTransactionRef) override;

std::unique_ptr<interfaces::BlockTemplate> waitNext(const node::BlockWaitOptions options = {}) override;
void interruptWait() override;

private:
std::shared_ptr<MockState> state;
Expand Down
Loading