From 2ffb07929ef480bd114defdc10b6a84463f222be Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Mon, 7 Jan 2019 22:56:46 -0800 Subject: Add findFork and findBlock to the Chain interface And use them to remove uses of chainActive and mapBlockIndex in wallet code This commit does not change behavior. Co-authored-by: Ben Woosley --- src/interfaces/chain.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/interfaces/chain.cpp') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index ca2533bb1..6e2224bc3 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include #include @@ -58,6 +60,22 @@ class LockImpl : public Chain::Lock assert(block != nullptr); return block->GetMedianTimePast(); } + 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; + } }; class LockingStateImpl : public LockImpl, public UniqueLock @@ -77,6 +95,27 @@ public: 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; + } }; } // namespace -- cgit v1.2.3