Skip to content

Commit d73f37d

Browse files
committed
Merge bitcoin/bitcoin#31346: Set notifications m_tip_block in LoadChainTip()
37946c0 Set notifications m_tip_block in LoadChainTip() (Sjors Provoost) Pull request description: Ensure KernelNotifications `m_tip_block` is set even if no new block arrives. Suggested in bitcoin/bitcoin#31297 (comment) ACKs for top commit: ryanofsky: Code review ACK 37946c0, fixing comment bug caught by @mzumsande in bitcoin/bitcoin#31346 (comment) in another really helpful clarification mzumsande: Code Review ACK 37946c0 TheCharlatan: ACK 37946c0 Tree-SHA512: 931bf820440a0cdda276f6dbd63f03fdbcdc90b18e7d5e160a74bdd9d0290acc706c35aab15bbdcd6e5e0b77565b3d07ff49b0dcf6551cb83961bae67be5d1bb
2 parents 78f1bff + 37946c0 commit d73f37d

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

src/init.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17701770

17711771
node.background_init_thread = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
17721772
ScheduleBatchPriority();
1773-
// Import blocks
1773+
// Import blocks and ActivateBestChain()
17741774
ImportBlocks(chainman, vImportFiles);
17751775
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
17761776
LogPrintf("Stopping after block import\n");
@@ -1793,8 +1793,18 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17931793
}
17941794
});
17951795

1796-
// Wait for genesis block to be processed
1797-
if (WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip() == nullptr)) {
1796+
/*
1797+
* Wait for genesis block to be processed. Typically kernel_notifications.m_tip_block
1798+
* has already been set by a call to LoadChainTip() in CompleteChainstateInitialization().
1799+
* But this is skipped if the chainstate doesn't exist yet or is being wiped:
1800+
*
1801+
* 1. first startup with an empty datadir
1802+
* 2. reindex
1803+
* 3. reindex-chainstate
1804+
*
1805+
* In these case it's connected by a call to ActivateBestChain() in the initload thread.
1806+
*/
1807+
{
17981808
WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
17991809
kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
18001810
return !kernel_notifications.m_tip_block.IsNull() || ShutdownRequested(node);

src/interfaces/mining.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ class Mining
7575
virtual std::optional<BlockRef> getTip() = 0;
7676

7777
/**
78-
* Waits for the connected tip to change. If the tip was not connected on
79-
* startup, this will wait.
78+
* Waits for the connected tip to change. During node initialization, this will
79+
* wait until the tip is connected.
8080
*
8181
* @param[in] current_tip block hash of the current chain tip. Function waits
8282
* for the chain tip to differ from this.

src/node/blockstorage.h

+1
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ class BlockManager
430430
void CleanupBlockRevFiles() const;
431431
};
432432

433+
// Calls ActivateBestChain() even if no blocks are imported.
433434
void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths);
434435
} // namespace node
435436

src/node/kernel_notifications.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class KernelNotifications : public kernel::Notifications
5656

5757
Mutex m_tip_block_mutex;
5858
std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex);
59-
//! The block for which the last blockTip notification was received for.
60-
//! The initial ZERO means that no block has been connected yet, which may
61-
//! be true even long after startup, until shutdown.
59+
//! The block for which the last blockTip notification was received.
60+
//! It's first set when the tip is connected during node initialization.
61+
//! Might be unset during an early shutdown.
6262
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO};
6363

6464
private:

src/validation.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -4721,6 +4721,13 @@ bool Chainstate::LoadChainTip()
47214721
m_chain.Height(),
47224722
FormatISO8601DateTime(tip->GetBlockTime()),
47234723
GuessVerificationProgress(m_chainman.GetParams().TxData(), tip));
4724+
4725+
// Ensure KernelNotifications m_tip_block is set even if no new block arrives.
4726+
if (this->GetRole() != ChainstateRole::BACKGROUND) {
4727+
// Ignoring return value for now.
4728+
(void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), *pindex);
4729+
}
4730+
47244731
return true;
47254732
}
47264733

0 commit comments

Comments
 (0)