Merge bitcoin/bitcoin#36908: mining: avoid checkBlock crash when chain has no tip
What changed, and why it matters
This update fixes a crash in Bitcoin Core that could happen right after starting the node with an empty data directory or during a blockchain reindex. If an external program called the mining interface's checkBlock function in a brief window before the very first genesis block was connected, the node would hit an internal assumption and shut down. The fix makes the mining interface wait until the chain has an actual tip before becoming available.
Apply the patch. It is a targeted, low-risk fix that closes a startup-time crash window. Operators running external mining clients via IPC should upgrade, though the crash requires local access and a well-timed call during startup or reindex.
Security signals we found
Denial-of-service vector: external IPC client can crash the node during startup
Assertion failure in TestBlockValidity due to null chain tip
Race condition between chainstate load and genesis block connection
Fix extends prior incomplete mitigation from PR #34661
Evidence from the diff
The commit merges PR #36908. Previously, MakeMining only waited for chainstate_loaded before exposing the Mining interface. However, on first startup or with -reindex/-reindex-chainstate, the chainstate is loaded before the genesis block is connected, so the active chain has no tip. A Mining.checkBlock call in this window reached TestBlockValidity(), which asserts that the tip exists, causing the node to abort. The fix changes MakeMining to wait until chainstate_loaded is true and kernel_notifications.TipBlock() is non-null. The test interface_ipc_mining.py is updated to exercise checkBlock during this early startup window with -reindex.
Changed components
src/node/interfaces.cpp MakeMining()src/interfaces/mining.h MakeMining() documentationMining.checkBlock / TestBlockValidityKernel notification chainstate_loaded / TipBlock stateInspect captured patch +13 / −5
### src/interfaces/mining.h
@@ -211,7 +211,8 @@ class Mining
//! Return implementation of Mining interface.
//!
-//! @param[in] wait_loaded waits for chainstate data to be loaded before
+//! @param[in] wait_loaded waits for chainstate data to be loaded and a
+//! chain tip to be connected before
//! returning. Used to prevent external clients from
//! being able to crash the node during startup.
std::unique_ptr<Mining> MakeMining(const node::NodeContext& node, bool wait_loaded=true);
### src/node/interfaces.cpp
@@ -1078,7 +1078,7 @@ std::unique_ptr<Mining> MakeMining(const node::NodeContext& context, bool wait_l
util::SignalInterrupt& interrupt(*Assert(context.shutdown_signal));
WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
- return kernel_notifications.m_state.chainstate_loaded || interrupt;
+ return (kernel_notifications.m_state.chainstate_loaded && kernel_notifications.TipBlock()) || interrupt;
});
if (interrupt) return nullptr;
}
### test/functional/interface_ipc_mining.py
@@ -17,6 +17,7 @@
WITNESS_COMMITMENT_HEADER,
)
from test_framework.messages import (
+ CBlock,
CBlockHeader,
COIN,
CTransaction,
@@ -169,13 +170,17 @@ async def wait_for_tip():
asyncio.run(capnp.run(async_routine()))
def run_early_startup_test(self):
- """Make sure mining.createNewBlock safely returns on early startup as
- soon as mining interface is available """
+ """Make sure mining.createNewBlock and mining.checkBlock safely return on
+ early startup as soon as mining interface is available """
self.log.info("Running Mining interface early startup test")
node = self.nodes[0]
self.stop_node(node.index)
- node.start()
+ # Start with -reindex to recreate the window where the chainstate is
+ # loaded but no tip is connected yet. The mining interface
+ # used to be handed out as soon as the chainstate was loaded, so a
+ # checkBlock call in this window could crash the node.
+ node.start(node.extra_args + ["-reindex"])
async def async_routine():
while True:
@@ -187,6 +192,8 @@ async def async_routine():
# available but without using a lot of CPU
await asyncio.sleep(0.005)
+ opts = self.capnp_modules['mining'].BlockCheckOptions()
+ await mining.checkBlock(ctx, CBlock().serialize(), opts)
opts = self.capnp_modules['mining'].BlockCreateOptions()
await mining.createNewBlock(ctx, opts)
Why this scored 62/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.