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 ++++++++++++++++++++++++++++++ src/interfaces/chain.h | 20 ++++++++++++++++++++ src/interfaces/wallet.cpp | 6 +++--- 3 files changed, 53 insertions(+), 3 deletions(-) (limited to 'src/interfaces') 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 diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index fe5658de4b..928b8af1f4 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -5,11 +5,14 @@ #ifndef BITCOIN_INTERFACES_CHAIN_H #define BITCOIN_INTERFACES_CHAIN_H +#include + #include #include #include class CScheduler; +class uint256; namespace interfaces { @@ -28,6 +31,23 @@ public: { public: virtual ~Lock() {} + + //! Get current chain height, not including genesis block (returns 0 if + //! chain only contains genesis block, nullopt if chain does not contain + //! any blocks). + virtual Optional getHeight() = 0; + + //! Get block height above genesis block. Returns 0 for genesis block, + //! 1 for following block, and so on. Returns nullopt for a block not + //! included in the current chain. + virtual Optional getBlockHeight(const uint256& hash) = 0; + + //! Get block depth. Returns 1 for chain tip, 2 for preceding block, and + //! so on. Returns 0 for a block not included in the current chain. + virtual int getBlockDepth(const uint256& hash) = 0; + + //! Get block hash. Height must be valid or this function will abort. + virtual uint256 getBlockHash(int height) = 0; }; //! Return Lock interface. Chain is locked when this is called, and diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 8db34ed759..489cf0981c 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -333,7 +333,7 @@ public: if (mi == m_wallet.mapWallet.end()) { return false; } - num_blocks = ::chainActive.Height(); + num_blocks = locked_chain->getHeight().value_or(-1); block_time = ::chainActive.Tip()->GetBlockTime(); tx_status = MakeWalletTxStatus(*locked_chain, mi->second); return true; @@ -348,7 +348,7 @@ public: LOCK(m_wallet.cs_wallet); auto mi = m_wallet.mapWallet.find(txid); if (mi != m_wallet.mapWallet.end()) { - num_blocks = ::chainActive.Height(); + num_blocks = locked_chain->getHeight().value_or(-1); in_mempool = mi->second.InMempool(); order_form = mi->second.vOrderForm; tx_status = MakeWalletTxStatus(*locked_chain, mi->second); @@ -379,7 +379,7 @@ public: return false; } balances = getBalances(); - num_blocks = ::chainActive.Height(); + num_blocks = locked_chain->getHeight().value_or(-1); return true; } CAmount getBalance() override { return m_wallet.GetBalance(); } -- cgit v1.2.3