// Copyright (c) 2017-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using node::PruneLockInfo; constexpr uint8_t DB_BEST_BLOCK{'B'}; constexpr uint8_t DB_UNCOMMITTED_TIP{'U'}; constexpr auto SYNC_LOG_INTERVAL{30s}; constexpr auto SYNC_LOCATOR_WRITE_INTERVAL{30s}; template void BaseIndex::FatalErrorf(const char* fmt, const Args&... args) { auto message = tfm::format(fmt, args...); node::AbortNode(m_chain->context()->exit_status, message); } CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash) { CBlockLocator locator; bool found = chain.findBlock(block_hash, interfaces::FoundBlock().locator(locator)); assert(found); assert(!locator.IsNull()); return locator; } BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) : CDBWrapper{DBParams{ .path = path, .cache_bytes = n_cache_size, .memory_only = f_memory, .wipe_data = f_wipe, .obfuscate = f_obfuscate, .options = [] { DBOptions options; node::ReadDatabaseArgs(gArgs, options); return options; }()}} {} CBlockLocator BaseIndex::DB::ReadBestBlock() const { CBlockLocator locator; bool success = Read(DB_BEST_BLOCK, locator); if (!success) { locator.SetNull(); } return locator; } void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator) { batch.Write(DB_BEST_BLOCK, locator); } std::optional BaseIndex::DB::ReadUncommittedTip() const { uint256 block_hash; if (!Read(DB_UNCOMMITTED_TIP, block_hash)) return std::nullopt; return block_hash; } void BaseIndex::DB::WriteUncommittedTip(CDBBatch& batch, const uint256& block_hash) { batch.Write(DB_UNCOMMITTED_TIP, block_hash); } void BaseIndex::DB::EraseUncommittedTip(CDBBatch& batch) { batch.Erase(DB_UNCOMMITTED_TIP); } BaseIndex::BaseIndex(std::unique_ptr chain, std::string name) : m_chain{std::move(chain)}, m_name{std::move(name)} {} BaseIndex::~BaseIndex() { Interrupt(); Stop(); } bool BaseIndex::Init() { // m_chainstate member gives indexing code access to node internals. It is // removed in followup https://github.com/bitcoin/bitcoin/pull/24230 m_chainstate = &m_chain->context()->chainman->ActiveChainstate(); // Register to validation interface before setting the 'm_synced' flag, so that // callbacks are not missed once m_synced is true. RegisterValidationInterface(this); const auto locator{GetDB().ReadBestBlock()}; LOCK(cs_main); CChain& active_chain = m_chainstate->m_chain; const CBlockIndex* locator_index{nullptr}; if (!locator.IsNull()) { // Setting the best block to the locator's top block. If it is not part of the // best chain, we will rewind to the fork point during index sync locator_index = m_chainstate->m_blockman.LookupBlockIndex(locator.vHave.at(0)); if (!locator_index) { return InitError(strprintf(Untranslated("%s: best block of the index not found. Please rebuild the index."), GetName())); } } const CBlockIndex* resume_block{ResumeBlock(m_chainstate->m_blockman, locator_index)}; const auto uncommitted_tip{WritesEagerly() ? GetDB().ReadUncommittedTip() : std::nullopt}; if (uncommitted_tip && resume_block == locator_index) { // The entries of the blocks up to it are on disk, but without the blocks the index can't tell whether they // are still on the active chain, nor rewind them. if (!m_chainstate->m_blockman.LookupBlockIndex(*uncommitted_tip)) { return InitError(strprintf(Untranslated("%s: the block the index was last synced to is not found. Please " "rebuild the index."), GetName())); } CDBBatch batch(GetDB()); GetDB().EraseUncommittedTip(batch); GetDB().WriteBatch(batch); } SetBestBlockIndex(resume_block); // Child init const CBlockIndex* start_block = m_best_block_index.load(); if (!CustomInit(start_block ? std::make_optional(interfaces::BlockKey{start_block->GetBlockHash(), start_block->nHeight}) : std::nullopt)) { return false; } // Note: this will latch to true immediately if the user starts up with an empty // datadir and an index enabled. If this is the case, indexation will happen solely // via `BlockConnected` signals until, possibly, the next restart. m_synced = start_block == active_chain.Tip(); m_init = true; return true; } static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { AssertLockHeld(cs_main); if (!pindex_prev) { return chain.Genesis(); } if (const auto* pindex{chain.Next(pindex_prev)}) { return pindex; } // If there is no next block, we might be synced if (pindex_prev == chain.Tip()) { return nullptr; } // Since block is not in the chain, return the next block in the chain AFTER the last common ancestor. // Caller will be responsible for rewinding back to the common ancestor. return chain.Next(chain.FindFork(pindex_prev)); } void BaseIndex::ThreadSync() { const CBlockIndex* pindex = m_best_block_index.load(); if (!m_synced) { auto last_log_time{NodeClock::now()}; auto last_locator_write_time{last_log_time}; while (true) { if (m_interrupt) { SetBestBlockIndex(pindex); // No need to handle errors in Commit. If it fails, the error will be already be // logged. The best way to recover is to continue, as index cannot be corrupted by // a missed commit to disk for an advanced index state. Commit(); return; } { LOCK(cs_main); const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain); if (!pindex_next) { SetBestBlockIndex(pindex); // No need to handle errors in Commit. See rationale above. Commit(); m_synced = true; break; } if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) { FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName()); return; } pindex = pindex_next; } CBlock block; interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex); if (!m_chainstate->m_blockman.ReadBlockFromDisk(block, *pindex)) { FatalErrorf("%s: Failed to read block %s from disk", __func__, pindex->GetBlockHash().ToString()); return; } else { block_info.data = █ } if (!CustomAppend(block_info)) { FatalErrorf("%s: Failed to write block %s to index database", __func__, pindex->GetBlockHash().ToString()); return; } auto current_time{NodeClock::now()}; if (current_time - last_log_time >= SYNC_LOG_INTERVAL) { LogPrintf("Syncing %s with block chain from height %d\n", GetName(), pindex->nHeight); last_log_time = current_time; } if (current_time - last_locator_write_time >= SYNC_LOCATOR_WRITE_INTERVAL) { SetBestBlockIndex(pindex); last_locator_write_time = current_time; // No need to handle errors in Commit. See rationale above. Commit(); } } } if (pindex) { LogPrintf("%s is enabled at height %d\n", GetName(), pindex->nHeight); } else { LogPrintf("%s is enabled\n", GetName()); } } void BaseIndex::Commit() { // Don't commit anything if we haven't indexed any block yet // (this could happen if init is interrupted). bool ok = m_best_block_index != nullptr; if (ok) { // Don't commit if the index best block is not an ancestor of the chainstate's last flushed // block. Otherwise, after an unclean shutdown, the index could be // persisted ahead of a chainstate it can no longer roll back to, which // would corrupt indexes with state (e.g. coinstatsindex). const CBlockIndex* index_tip = m_best_block_index.load(); const CBlockIndex* last_flushed = WITH_LOCK(::cs_main, return m_chainstate->GetLastFlushedBlock()); if (!last_flushed || last_flushed->GetAncestor(index_tip->nHeight) != index_tip) { LogDebug(BCLog::COINDB, "Skipping commit, index is ahead of flushed chainstate (index height %d, last flush at height %d)\n", index_tip->nHeight, last_flushed ? last_flushed->nHeight : -1); if (WritesEagerly()) { CDBBatch batch(GetDB()); GetDB().WriteUncommittedTip(batch, index_tip->GetBlockHash()); GetDB().WriteBatch(batch); } return; } CDBBatch batch(GetDB()); ok = CustomCommit(batch); if (ok) { GetDB().WriteBestBlock(batch, GetLocator(*m_chain, m_best_block_index.load()->GetBlockHash())); if (WritesEagerly()) GetDB().EraseUncommittedTip(batch); ok = GetDB().WriteBatch(batch); } } if (!ok) { LogError("%s: Failed to commit latest %s state\n", __func__, GetName()); } } void BaseIndex::WriteRewindLocator(CDBBatch& batch, const CBlockIndex* new_tip) const { const auto above_new_tip{[&](const uint256& block_hash) { const CBlockIndex* block{WITH_LOCK(::cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block_hash))}; return block && block->nHeight > new_tip->nHeight && block->GetAncestor(new_tip->nHeight) == new_tip; }}; // The committed tip was an ancestor of a flushed block when it was written, so it and new_tip below it are in // the block index after a restart. A committed tip at or below new_tip needs no change. if (const auto locator{GetDB().ReadBestBlock()}; !locator.IsNull() && above_new_tip(locator.vHave.front())) { GetDB().WriteBestBlock(batch, GetLocator(*m_chain, new_tip->GetBlockHash())); } // Init() resumes from the uncommitted tip only while the block index has it, and then new_tip below it too. if (const auto uncommitted_tip{GetDB().ReadUncommittedTip()}; uncommitted_tip && above_new_tip(*uncommitted_tip)) { GetDB().WriteUncommittedTip(batch, new_tip->GetBlockHash()); } } const CBlockIndex* BaseIndex::ResumeBlock(const node::BlockManager& blockman, const CBlockIndex* locator_block) const { AssertLockHeld(::cs_main); // The entries up to the uncommitted tip are on disk, so resume from it while the block index has it above the // locator. Commit() and rewinds keep the locator below it; one that isn't was left behind by an older binary. const auto uncommitted_tip_hash{WritesEagerly() ? GetDB().ReadUncommittedTip() : std::nullopt}; const CBlockIndex* uncommitted_tip{uncommitted_tip_hash ? blockman.LookupBlockIndex(*uncommitted_tip_hash) : nullptr}; if (uncommitted_tip && (!locator_block || uncommitted_tip->GetAncestor(locator_block->nHeight) == locator_block)) { return uncommitted_tip; } return locator_block; } bool BaseIndex::RewindBlockByBlock(const CBlockIndex* current_tip, const CBlockIndex* new_tip, const std::function& rewind_block) { for (const CBlockIndex* pindex = current_tip; pindex != new_tip; pindex = pindex->pprev) { CDBBatch batch(GetDB()); if (!rewind_block(pindex, batch)) return false; WriteRewindLocator(batch, pindex->pprev); if (!GetDB().WriteBatch(batch)) return false; } return true; } bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) { assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip); if (!CustomRewind({current_tip->GetBlockHash(), current_tip->nHeight}, {new_tip->GetBlockHash(), new_tip->nHeight})) { return false; } // Don't commit here - the committed index state must never be ahead of the // flushed chainstate, otherwise unclean restarts would lead to index corruption. // Pruning has a minimum of 288 blocks-to-keep and getting the index // out of sync may be possible but a users fault. // In case we reorg beyond the pruned depth, ReadBlockFromDisk would // throw and lead to a graceful shutdown SetBestBlockIndex(new_tip); return true; } void BaseIndex::BlockConnected(const std::shared_ptr& block, const CBlockIndex* pindex) { if (!m_synced) { return; } const CBlockIndex* best_block_index = m_best_block_index.load(); if (!best_block_index) { if (pindex->nHeight != 0) { FatalErrorf("%s: First block connected is not the genesis block (height=%d)", __func__, pindex->nHeight); return; } } else { // Ensure block connects to an ancestor of the current best block. This should be the case // most of the time, but may not be immediately after the sync thread catches up and sets // m_synced. Consider the case where there is a reorg and the blocks on the stale branch are // in the ValidationInterface queue backlog even after the sync thread has caught up to the // new chain tip. In this unlikely event, log a warning and let the queue clear. if (best_block_index->GetAncestor(pindex->nHeight - 1) != pindex->pprev) { LogPrintf("%s: WARNING: Block %s does not connect to an ancestor of " "known best chain (tip=%s); not updating index\n", __func__, pindex->GetBlockHash().ToString(), best_block_index->GetBlockHash().ToString()); return; } if (best_block_index != pindex->pprev && !Rewind(best_block_index, pindex->pprev)) { FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName()); return; } } interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get()); if (CustomAppend(block_info)) { // Setting the best block index is intentionally the last step of this // function, so BlockUntilSyncedToCurrentChain callers waiting for the // best block index to be updated can rely on the block being fully // processed, and the index object being safe to delete. SetBestBlockIndex(pindex); } else { FatalErrorf("%s: Failed to write block %s to index", __func__, pindex->GetBlockHash().ToString()); return; } } void BaseIndex::BlockDisconnected(const std::shared_ptr& block, const CBlockIndex* pindex) { if (!m_synced) { return; } const CBlockIndex* best_block_index = m_best_block_index.load(); // Ignore stale-branch disconnect notifications that do not connect to the indexed chain. // We must check that pindex itself is on the indexed chain, not just that it shares // a parent — otherwise same-height siblings would incorrectly trigger a rewind. if (best_block_index && best_block_index->nHeight >= pindex->nHeight && pindex->pprev) { if (best_block_index->GetAncestor(pindex->nHeight) != pindex) { LogPrintf("%s: WARNING: Block %s is not on the indexed chain " "(tip=%s); not updating index\n", __func__, pindex->GetBlockHash().ToString(), best_block_index->GetBlockHash().ToString()); return; } if (!Rewind(best_block_index, pindex->pprev)) { FatalErrorf("%s: Failed to rewind %s to previous block after disconnect", __func__, GetName()); } } } void BaseIndex::ChainStateFlushed(const CBlockLocator& locator) { if (!m_synced) { return; } const uint256& locator_tip_hash = locator.vHave.front(); const CBlockIndex* locator_tip_index; { LOCK(cs_main); locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash); } if (!locator_tip_index) { FatalErrorf("%s: First block (hash=%s) in locator was not found", __func__, locator_tip_hash.ToString()); return; } // This checks that ChainStateFlushed callbacks are received after BlockConnected. The check may fail // immediately after the sync thread catches up and sets m_synced. Consider the case where // there is a reorg and the blocks on the stale branch are in the ValidationInterface queue // backlog even after the sync thread has caught up to the new chain tip. In this unlikely // event, log a warning and let the queue clear. const CBlockIndex* best_block_index = m_best_block_index.load(); if (best_block_index->GetAncestor(locator_tip_index->nHeight) != locator_tip_index) { LogPrintf("%s: WARNING: Locator contains block (hash=%s) not on known best " "chain (tip=%s); not writing index locator\n", __func__, locator_tip_hash.ToString(), best_block_index->GetBlockHash().ToString()); return; } // No need to handle errors in Commit. If it fails, the error will be already be logged. The // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk // for an advanced index state. Commit(); } bool BaseIndex::BlockUntilSyncedToCurrentChain() const { AssertLockNotHeld(cs_main); if (!m_synced) { return false; } { // Skip the queue-draining stuff if we know we're caught up with // m_chain.Tip(). LOCK(cs_main); const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip(); const CBlockIndex* best_block_index = m_best_block_index.load(); if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) { return true; } } LogPrintf("%s: %s is catching up on block notifications\n", __func__, GetName()); SyncWithValidationInterfaceQueue(); return true; } void BaseIndex::Interrupt() { m_interrupt(); } bool BaseIndex::StartBackgroundSync() { if (!m_init) throw std::logic_error("Error: Cannot start a non-initialized index"); m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); }); return true; } void BaseIndex::Stop() { UnregisterValidationInterface(this); if (m_thread_sync.joinable()) { m_thread_sync.join(); } } IndexSummary BaseIndex::GetSummary() const { IndexSummary summary{}; summary.name = GetName(); summary.synced = m_synced; if (const auto& pindex = m_best_block_index.load()) { summary.best_block_height = pindex->nHeight; summary.best_block_hash = pindex->GetBlockHash(); } else { summary.best_block_height = 0; summary.best_block_hash = m_chain->getBlockHash(0); } return summary; } void BaseIndex::SetBestBlockIndex(const CBlockIndex* block) { assert(!m_chainstate->m_blockman.IsPruneMode() || AllowPrune()); if (AllowPrune()) { PruneLockInfo prune_lock; prune_lock.height_first = block ? block->nHeight : 0; WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock)); } // Intentionally set m_best_block_index as the last step in this function, // after updating prune locks above, and after making any other references // to *this, so the BlockUntilSyncedToCurrentChain function (which checks // m_best_block_index as an optimization) can be used to wait for the last // BlockConnected notification and safely assume that prune locks are // updated and that the index object is safe to delete. m_best_block_index = block; }