From 700c42b85d20e624bef4228eef062c93084efab5 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Thu, 27 Jul 2017 10:08:31 -0400 Subject: Add height, depth, and hash methods 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 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/interfaces/chain.cpp') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 2571a91031..138d1bfd5f 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -4,7 +4,9 @@ #include +#include #include +#include #include #include @@ -16,6 +18,34 @@ 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(); + } }; class LockingStateImpl : public LockImpl, public UniqueLock -- cgit v1.2.3