mining: add interrupt()

Both waitTipChanged() and createNewBlock() can take a long time to
return. Add a way for clients to interrupt them.

The new m_interrupt_mining is safely accessed with a lock on
m_tip_block_mutex, but it has no guard annotation. A more thorough
solution is discussed here:
https://github.com/bitcoin/bitcoin/pull/34184#discussion_r2743566474
This commit is contained in:
Sjors Provoost committed 2026-02-20 16:49:52 +01:00
1 parent a11297a904
commit 1e82fa498c
6 files changed
+58 -17

No files matched your search

+16
View File
@@ -137,6 +137,14 @@ class IPCMiningTest(BitcoinTestFramework):
assert_equal(oldblockref.hash, newblockref.hash)
assert_equal(oldblockref.height, newblockref.height)
self.log.debug("interrupt() should abort waitTipChanged()")
async def wait_for_tip():
long_timeout = 60000.0 # 1 minute
result = (await mining.waitTipChanged(ctx, newblockref.hash, long_timeout)).result
# Unlike a timeout, interrupt() returns an empty BlockRef.
assert_equal(len(result.hash), 0)
await wait_and_do(wait_for_tip(), mining.interrupt())
asyncio.run(capnp.run(async_routine()))
def run_block_template_test(self):
@@ -163,6 +171,14 @@ class IPCMiningTest(BitcoinTestFramework):
# spurious failures.
assert_greater_than_or_equal(time.time() - start, 0.9)
self.log.debug("interrupt() should abort createNewBlock() during cooldown")
async def create_block():
result = await mining.createNewBlock(ctx, self.default_block_create_options)
# interrupt() causes createNewBlock to return nullptr
assert_equal(result._has("result"), False)
await wait_and_do(create_block(), mining.interrupt())
header_only_peer.peer_disconnect()
self.connect_nodes(0, 1)
self.sync_all()