// Copyright (c) 2018 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 namespace interfaces { namespace { class LockImpl : public Chain::Lock { Optional getHeight() override { int height = ::chainActive.Height(); if (height >= 0) { return height; } return nullopt; } Optional getBlockHeight(const uint256& hash) override { CBlockIndex* block = LookupBlockIndex(hash); if (block && ::chainActive.Contains(block)) { return block->nHeight; } return nullopt; } int getBlockDepth(const uint256& hash) override { const Optional tip_height = getHeight(); const Optional height = getBlockHeight(hash); return tip_height && height ? *tip_height - *height + 1 : 0; } uint256 getBlockHash(int height) override { CBlockIndex* block = ::chainActive[height]; assert(block != nullptr); return block->GetBlockHash(); } int64_t getBlockTime(int height) override { CBlockIndex* block = ::chainActive[height]; assert(block != nullptr); return block->GetBlockTime(); } int64_t getBlockMedianTimePast(int height) override { CBlockIndex* block = ::chainActive[height]; assert(block != nullptr); return block->GetMedianTimePast(); } bool haveBlockOnDisk(int height) override { CBlockIndex* block = ::chainActive[height]; return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; } Optional findFirstBlockWithTime(int64_t time, uint256* hash) override { CBlockIndex* block = ::chainActive.FindEarliestAtLeast(time); if (block) { if (hash) *hash = block->GetBlockHash(); return block->nHeight; } return nullopt; } Optional findFirstBlockWithTimeAndHeight(int64_t time, int height) override { // TODO: Could update CChain::FindEarliestAtLeast() to take a height // parameter and use it with std::lower_bound() to make this // implementation more efficient and allow combining // findFirstBlockWithTime and findFirstBlockWithTimeAndHeight into one // method. for (CBlockIndex* block = ::chainActive[height]; block; block = ::chainActive.Next(block)) { if (block->GetBlockTime() >= time) { return block->nHeight; } } return nullopt; } Optional findPruned(int start_height, Optional stop_height) override { if (::fPruneMode) { CBlockIndex* block = stop_height ? ::chainActive[*stop_height] : ::chainActive.Tip(); while (block && block->nHeight >= start_height) { if ((block->nStatus & BLOCK_HAVE_DATA) == 0) { return block->nHeight; } block = block->pprev; } } return nullopt; } Optional findFork(const uint256& hash, Optional* height) override { const CBlockIndex* block = LookupBlockIndex(hash); const CBlockIndex* fork = block ? ::chainActive.FindFork(block) : nullptr; if (height) { if (block) { *height = block->nHeight; } else { height->reset(); } } if (fork) { return fork->nHeight; } return nullopt; } bool isPotentialTip(const uint256& hash) override { if (::chainActive.Tip()->GetBlockHash() == hash) return true; CBlockIndex* block = LookupBlockIndex(hash); return block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip(); } CBlockLocator getLocator() override { return ::chainActive.GetLocator(); } Optional findLocatorFork(const CBlockLocator& locator) override { LockAnnotation lock(::cs_main); if (CBlockIndex* fork = FindForkInGlobalIndex(::chainActive, locator)) { return fork->nHeight; } return nullopt; } }; class LockingStateImpl : public LockImpl, public UniqueLock { using UniqueLock::UniqueLock; }; class ChainImpl : public Chain { public: std::unique_ptr lock(bool try_lock) override { auto result = MakeUnique(::cs_main, "cs_main", __FILE__, __LINE__, try_lock); if (try_lock && result && !*result) return {}; // std::move necessary on some compilers due to conversion from // LockingStateImpl to Lock pointer return std::move(result); } std::unique_ptr assumeLocked() override { return MakeUnique(); } bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override { CBlockIndex* index; { LOCK(cs_main); index = LookupBlockIndex(hash); if (!index) { return false; } if (time) { *time = index->GetBlockTime(); } if (time_max) { *time_max = index->GetBlockTimeMax(); } } if (block && !ReadBlockFromDisk(*block, index, Params().GetConsensus())) { block->SetNull(); } return true; } double guessVerificationProgress(const uint256& block_hash) override { LOCK(cs_main); return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash)); } }; } // namespace std::unique_ptr MakeChain() { return MakeUnique(); } } // namespace interfaces