mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-24 18:23:26 -03:00
Set notifications m_tip_block in LoadChainTip()
Ensure KernelNotifications m_tip_block is set even if no new block arrives. Additionally, have node init always wait for this to happen.
This commit is contained in:
parent
2eccb8bc5e
commit
37946c0aaf
5 changed files with 26 additions and 8 deletions
16
src/init.cpp
16
src/init.cpp
|
@ -1770,7 +1770,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
|
|
||||||
node.background_init_thread = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
|
node.background_init_thread = std::thread(&util::TraceThread, "initload", [=, &chainman, &args, &node] {
|
||||||
ScheduleBatchPriority();
|
ScheduleBatchPriority();
|
||||||
// Import blocks
|
// Import blocks and ActivateBestChain()
|
||||||
ImportBlocks(chainman, vImportFiles);
|
ImportBlocks(chainman, vImportFiles);
|
||||||
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
|
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
|
||||||
LogPrintf("Stopping after block import\n");
|
LogPrintf("Stopping after block import\n");
|
||||||
|
@ -1793,8 +1793,18 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Wait for genesis block to be processed
|
/*
|
||||||
if (WITH_LOCK(chainman.GetMutex(), return chainman.ActiveTip() == nullptr)) {
|
* Wait for genesis block to be processed. Typically kernel_notifications.m_tip_block
|
||||||
|
* has already been set by a call to LoadChainTip() in CompleteChainstateInitialization().
|
||||||
|
* But this is skipped if the chainstate doesn't exist yet or is being wiped:
|
||||||
|
*
|
||||||
|
* 1. first startup with an empty datadir
|
||||||
|
* 2. reindex
|
||||||
|
* 3. reindex-chainstate
|
||||||
|
*
|
||||||
|
* In these case it's connected by a call to ActivateBestChain() in the initload thread.
|
||||||
|
*/
|
||||||
|
{
|
||||||
WAIT_LOCK(kernel_notifications.m_tip_block_mutex, lock);
|
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) {
|
kernel_notifications.m_tip_block_cv.wait(lock, [&]() EXCLUSIVE_LOCKS_REQUIRED(kernel_notifications.m_tip_block_mutex) {
|
||||||
return !kernel_notifications.m_tip_block.IsNull() || ShutdownRequested(node);
|
return !kernel_notifications.m_tip_block.IsNull() || ShutdownRequested(node);
|
||||||
|
|
|
@ -75,8 +75,8 @@ public:
|
||||||
virtual std::optional<BlockRef> getTip() = 0;
|
virtual std::optional<BlockRef> getTip() = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Waits for the connected tip to change. If the tip was not connected on
|
* Waits for the connected tip to change. During node initialization, this will
|
||||||
* startup, this will wait.
|
* wait until the tip is connected.
|
||||||
*
|
*
|
||||||
* @param[in] current_tip block hash of the current chain tip. Function waits
|
* @param[in] current_tip block hash of the current chain tip. Function waits
|
||||||
* for the chain tip to differ from this.
|
* for the chain tip to differ from this.
|
||||||
|
|
|
@ -430,6 +430,7 @@ public:
|
||||||
void CleanupBlockRevFiles() const;
|
void CleanupBlockRevFiles() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Calls ActivateBestChain() even if no blocks are imported.
|
||||||
void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths);
|
void ImportBlocks(ChainstateManager& chainman, std::span<const fs::path> import_paths);
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|
||||||
|
|
|
@ -56,9 +56,9 @@ public:
|
||||||
|
|
||||||
Mutex m_tip_block_mutex;
|
Mutex m_tip_block_mutex;
|
||||||
std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex);
|
std::condition_variable m_tip_block_cv GUARDED_BY(m_tip_block_mutex);
|
||||||
//! The block for which the last blockTip notification was received for.
|
//! The block for which the last blockTip notification was received.
|
||||||
//! The initial ZERO means that no block has been connected yet, which may
|
//! It's first set when the tip is connected during node initialization.
|
||||||
//! be true even long after startup, until shutdown.
|
//! Might be unset during an early shutdown.
|
||||||
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO};
|
uint256 m_tip_block GUARDED_BY(m_tip_block_mutex){uint256::ZERO};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -4721,6 +4721,13 @@ bool Chainstate::LoadChainTip()
|
||||||
m_chain.Height(),
|
m_chain.Height(),
|
||||||
FormatISO8601DateTime(tip->GetBlockTime()),
|
FormatISO8601DateTime(tip->GetBlockTime()),
|
||||||
GuessVerificationProgress(m_chainman.GetParams().TxData(), tip));
|
GuessVerificationProgress(m_chainman.GetParams().TxData(), tip));
|
||||||
|
|
||||||
|
// Ensure KernelNotifications m_tip_block is set even if no new block arrives.
|
||||||
|
if (this->GetRole() != ChainstateRole::BACKGROUND) {
|
||||||
|
// Ignoring return value for now.
|
||||||
|
(void)m_chainman.GetNotifications().blockTip(GetSynchronizationState(/*init=*/true, m_chainman.m_blockman.m_blockfiles_indexed), *pindex);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue