From d93c4c1d6e0aab5f32306ecd7c1237257b26940d Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Mon, 7 Jan 2019 22:35:47 -0800 Subject: Add time 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 | 12 ++++++++++++ src/interfaces/chain.h | 8 ++++++++ src/interfaces/wallet.cpp | 9 +++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src/interfaces') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 138d1bfd5f..ca2533bb1d 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -46,6 +46,18 @@ class LockImpl : public Chain::Lock 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 diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 928b8af1f4..1db9d3861d 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -48,6 +49,13 @@ public: //! Get block hash. Height must be valid or this function will abort. virtual uint256 getBlockHash(int height) = 0; + + //! Get block time. Height must be valid or this function will abort. + virtual int64_t getBlockTime(int height) = 0; + + //! Get block median time past. Height must be valid or this function + //! will abort. + virtual int64_t getBlockMedianTimePast(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 489cf0981c..88c5db73a1 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -333,8 +333,13 @@ public: if (mi == m_wallet.mapWallet.end()) { return false; } - num_blocks = locked_chain->getHeight().value_or(-1); - block_time = ::chainActive.Tip()->GetBlockTime(); + if (Optional height = locked_chain->getHeight()) { + num_blocks = *height; + block_time = locked_chain->getBlockTime(*height); + } else { + num_blocks = -1; + block_time = -1; + } tx_status = MakeWalletTxStatus(*locked_chain, mi->second); return true; } -- cgit v1.2.3