From de13363a472ea30dff2f8f55c6ae572281115380 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Mon, 15 Jul 2019 18:20:12 -0400 Subject: [PATCH] [wallet] Move getBlockHeight from Chain::Lock interface to simple Chain Add HaveChain to assert chain access for wallet-tool in LoadToWallet. --- src/interfaces/chain.cpp | 18 +++++++++--------- src/interfaces/chain.h | 10 +++++----- src/wallet/wallet.cpp | 7 +++---- src/wallet/wallet.h | 3 +++ 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index b891cf2ee5..ccdbf1f1d1 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -55,15 +55,6 @@ bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock { - Optional getBlockHeight(const uint256& hash) override - { - LockAssertion lock(::cs_main); - CBlockIndex* block = LookupBlockIndex(hash); - if (block && ::ChainActive().Contains(block)) { - return block->nHeight; - } - return nullopt; - } uint256 getBlockHash(int height) override { LockAssertion lock(::cs_main); @@ -234,6 +225,15 @@ public: } return nullopt; } + Optional getBlockHeight(const uint256& hash) override + { + LOCK(::cs_main); + CBlockIndex* block = LookupBlockIndex(hash); + if (block && ::ChainActive().Contains(block)) { + return block->nHeight; + } + return nullopt; + } bool findBlock(const uint256& hash, const FoundBlock& block) override { WAIT_LOCK(cs_main, lock); diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 03635fd74f..b85484c11b 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -87,11 +87,6 @@ public: public: virtual ~Lock() {} - //! 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 hash. Height must be valid or this function will abort. virtual uint256 getBlockHash(int height) = 0; @@ -135,6 +130,11 @@ public: //! 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; + //! Return whether node has the block and optionally return block metadata //! or contents. virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3be0453978..b7117e2e59 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -885,10 +885,9 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose) void CWallet::LoadToWallet(CWalletTx& wtxIn) { - // If wallet doesn't have a chain (e.g bitcoin-wallet), lock can't be taken. - auto locked_chain = LockChain(); - if (locked_chain) { - Optional block_height = locked_chain->getBlockHeight(wtxIn.m_confirm.hashBlock); + // If wallet doesn't have a chain (e.g wallet-tool), don't bother to update txn. + if (HaveChain()) { + Optional block_height = chain().getBlockHeight(wtxIn.m_confirm.hashBlock); if (block_height) { // Update cached block height variable since it not stored in the // serialized transaction. diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 662f5d4bb3..fa2821dfea 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -778,6 +778,9 @@ public: /** Interface to assert chain access and if successful lock it */ std::unique_ptr LockChain() { return m_chain ? m_chain->lock() : nullptr; } + /** Interface to assert chain access */ + bool HaveChain() const { return m_chain ? true : false; } + std::map mapWallet GUARDED_BY(cs_wallet); typedef std::multimap TxItems;