Skip to content

Commit 3f699b9

Browse files
committed
Merge branch '2025/10/interrupt-wait'
2 parents d812921 + 8c2c9f6 commit 3f699b9

File tree

7 files changed

+35
-6
lines changed

7 files changed

+35
-6
lines changed

src/interfaces/mining.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class BlockTemplate
6868
* the tip is more than 20 minutes old.
6969
*/
7070
virtual std::unique_ptr<BlockTemplate> waitNext(const node::BlockWaitOptions options = {}) = 0;
71+
72+
/**
73+
* Interrupts the current wait for the next block template.
74+
*/
75+
virtual void interruptWait() = 0;
7176
};
7277

7378
//! Interface giving clients (RPC, Stratum v2 Template Provider in the future)

src/ipc/capnp/mining.capnp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface BlockTemplate $Proxy.wrap("interfaces::BlockTemplate") {
3333
getCoinbaseMerklePath @8 (context: Proxy.Context) -> (result: List(Data));
3434
submitSolution @9 (context: Proxy.Context, version: UInt32, timestamp: UInt32, nonce: UInt32, coinbase :Data) -> (result: Bool);
3535
waitNext @10 (context: Proxy.Context, options: BlockWaitOptions) -> (result: BlockTemplate);
36+
interruptWait @11() -> ();
3637
}
3738

3839
struct BlockCreateOptions $Proxy.wrap("node::BlockCreateOptions") {

src/sv2-tp.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ MAIN_FUNCTION
214214
UninterruptibleSleep(100ms);
215215
}
216216

217-
LogPrintLevel(BCLog::SV2, BCLog::Level::Info,
218-
"Interrupt received, waiting up to %d seconds before shutting down (-sv2interval)",
219-
options.fee_check_interval.count());
220-
221217
tp->Interrupt();
222218
tp->StopThreads();
223219
tp.reset();

src/sv2/template_provider.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <consensus/merkle.h>
55
#include <crypto/hex_base.h>
66
#include <common/args.h>
7+
#include <ipc/exception.h>
78
#include <logging.h>
89
#include <sv2/noise.h>
910
#include <consensus/validation.h> // NO_WITNESS_COMMITMENT
@@ -122,6 +123,24 @@ Sv2TemplateProvider::~Sv2TemplateProvider()
122123

123124
void Sv2TemplateProvider::Interrupt()
124125
{
126+
AssertLockNotHeld(m_tp_mutex);
127+
128+
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "Interrupt pending waitNext() calls...");
129+
{
130+
LOCK(m_tp_mutex);
131+
try {
132+
for (auto& t : GetBlockTemplates()) {
133+
t.second->interruptWait();
134+
}
135+
} catch (const ipc::Exception& e) {
136+
// Bitcoin Core v30 does not yet implement interruptWait(), fall back
137+
// to just waiting until waitNext() returns.
138+
LogPrintLevel(BCLog::SV2, BCLog::Level::Info,
139+
"Interrupt received, waiting up to %d seconds before shutting down (-sv2interval)",
140+
m_options.fee_check_interval.count());
141+
}
142+
}
143+
125144
m_flag_interrupt_sv2 = true;
126145
// Also interrupt network threads so client handlers can wind down quickly.
127146
if (m_connman) m_connman->Interrupt();

src/sv2/template_provider.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ class Sv2TemplateProvider : public Sv2EventsInterface
142142

143143
/**
144144
* Triggered on interrupt signals to stop the main event loop in ThreadSv2Handler().
145+
* Interrupts pending waitNext() calls
145146
*/
146-
void Interrupt();
147+
void Interrupt() EXCLUSIVE_LOCKS_REQUIRED(!m_tp_mutex);
147148

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

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

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

168169
private:

src/test/sv2_mock_mining.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <sync.h>
88
#include <cassert>
9+
#include <logging.h>
910

1011
namespace {
1112
static inline uint256 HashFromHeight(uint64_t h)
@@ -104,6 +105,11 @@ std::unique_ptr<interfaces::BlockTemplate> MockBlockTemplate::waitNext(const nod
104105
}
105106
}
106107

108+
void MockBlockTemplate::interruptWait()
109+
{
110+
LogPrintLevel(BCLog::SV2, BCLog::Level::Trace, "mock interruptWait()");
111+
}
112+
107113
MockMining::MockMining(std::shared_ptr<MockState> st) : state(std::move(st)) {}
108114
bool MockMining::isTestChain() { return true; }
109115
bool MockMining::isInitialBlockDownload() { return false; }

src/test/sv2_mock_mining.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class MockBlockTemplate : public interfaces::BlockTemplate {
6565
bool submitSolution(uint32_t, uint32_t, uint32_t, CTransactionRef) override;
6666

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

6970
private:
7071
std::shared_ptr<MockState> state;

0 commit comments

Comments
 (0)