// 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 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(); } }; 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(); } }; } // namespace std::unique_ptr MakeChain() { return MakeUnique(); } } // namespace interfaces