diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -108,11 +108,6 @@ 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 BlockHash &hash) = 0; - //! Get block hash. Height must be valid or this function will abort. virtual BlockHash getBlockHash(int height) = 0; @@ -161,6 +156,11 @@ //! 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 BlockHash &hash) = 0; + //! Return whether node has the block and optionally return block metadata //! or contents. virtual bool findBlock(const BlockHash &hash, diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -66,14 +66,6 @@ } class LockImpl : public Chain::Lock, public UniqueLock { - Optional getBlockHeight(const BlockHash &hash) override { - LockAssertion lock(::cs_main); - CBlockIndex *block = LookupBlockIndex(hash); - if (block && ::ChainActive().Contains(block)) { - return block->nHeight; - } - return nullopt; - } BlockHash getBlockHash(int height) override { LockAssertion lock(::cs_main); CBlockIndex *block = ::ChainActive()[height]; @@ -257,6 +249,14 @@ } return nullopt; } + Optional getBlockHeight(const BlockHash &hash) override { + LOCK(::cs_main); + CBlockIndex *block = LookupBlockIndex(hash); + if (block && ::ChainActive().Contains(block)) { + return block->nHeight; + } + return nullopt; + } bool findBlock(const BlockHash &hash, const FoundBlock &block) override { WAIT_LOCK(cs_main, lock); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -866,6 +866,9 @@ 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; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -917,11 +917,11 @@ } 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) { + // If wallet doesn't have a chain (e.g wallet-tool), don't bother to update + // txn. + if (HaveChain()) { Optional block_height = - locked_chain->getBlockHeight(wtxIn.m_confirm.hashBlock); + chain().getBlockHeight(wtxIn.m_confirm.hashBlock); if (block_height) { // Update cached block height variable since it not stored in the // serialized transaction.