mining: avoid checkBlock crash when chain has no tip

bitcoin/bitcoin: PR #36908

2 suggestions

Optional improvements

Link

[suggestion] · The -reindex IPC test can miss the tipless window

If genesis connects before the IPC request executes, this functional test can also pass with the old chainstate_loaded-only predicate, leaving coverage of the added TipBlock() requirement dependent on startup timing.

Location: test/functional/interface_ipc_mining.py:183 (head).

Details and suggested fix

Socket polling does not synchronize the client with initload, which can activate genesis while importing the first block file. Once that happens, checkBlock no longer reaches the missing-tip assertion. The miner unit-test helper also bypasses the changed predicate with wait_loaded=false.

Keep the IPC smoke test and add a focused TestingSetup test: call setChainstateLoaded(false) and then setChainstateLoaded(true) to leave the cached tip unset, start interfaces::MakeMining(m_node) on a worker, check that it stays pending, then deliver blockTip(...) with the fixture's active tip and verify a non-null result. Use worker-start synchronization and a bounded pending check, taking care that a delayed worker does not masquerade as a blocked factory.


Link

[suggestion] · Early-startup coverage ignores the checkBlock reply

The early-startup functional test accepts any normal checkBlock reply, including a no-tip rejection that would violate the documented factory-readiness contract.

Location: test/functional/interface_ipc_mining.py:196 (head).

Details and suggested fix

With the current factory wait, CBlock()'s zero hashPrevBlock must produce False with reason inconclusive-not-best-prevblk, regardless of how far reindexing has progressed. The subsequent createNewBlock call waits for a tip independently, so its completion cannot validate the earlier response. Capture and assert the reply:

check = await mining.checkBlock(ctx, CBlock().serialize(), opts)
assert_equal(check.result, False)
assert_equal(check.reason, "inconclusive-not-best-prevblk")

These assertions distinguish the expected response from a normal no-tip rejection when the request reaches that window, but they do not guarantee that the window is exercised.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Documented decisions

The original factory-level placement was described as probably the simplest fix, rather than an immutable API requirement. Its author explicitly said that moving the wait into Mining.createNewBlock could be implemented later without client changes.

History supports centralized readiness as a simplicity choice, but does not prove that every unavailable startup condition must be handled there. It also does not directly adjudicate a checkBlock-specific early return, which was not the alternative discussed in the inspected historical proposal.

Source 1

The concrete early-startup usage protected by the original change was an IPC client connecting as soon as the socket appeared and immediately calling createNewBlock. The change added exactly that regression test. Internal initialization was deliberately exempted with wait_loaded=false so construction of the node's internal mining interface would not depend on chainstate loading.

The readiness guarantee was intended for external clients that race node startup, not for every construction of a Mining object. The historical test demonstrates a caller expecting to use createNewBlock immediately after obtaining the interface; the internal exemption explains why the guarantee is conditional.

Source 1