[wallet] Move getBlockHeight from Chain::Lock interface to simple Chain

Add HaveChain to assert chain access for wallet-tool in LoadToWallet.
This commit is contained in:
Antoine Riard 2019-07-15 18:20:12 -04:00
parent b855592d83
commit de13363a47
4 changed files with 20 additions and 18 deletions

View file

@ -55,15 +55,6 @@ bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<Rec
class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex> class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex>
{ {
Optional<int> 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 uint256 getBlockHash(int height) override
{ {
LockAssertion lock(::cs_main); LockAssertion lock(::cs_main);
@ -234,6 +225,15 @@ public:
} }
return nullopt; return nullopt;
} }
Optional<int> 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 bool findBlock(const uint256& hash, const FoundBlock& block) override
{ {
WAIT_LOCK(cs_main, lock); WAIT_LOCK(cs_main, lock);

View file

@ -87,11 +87,6 @@ public:
public: public:
virtual ~Lock() {} 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<int> getBlockHeight(const uint256& hash) = 0;
//! Get block hash. Height must be valid or this function will abort. //! Get block hash. Height must be valid or this function will abort.
virtual uint256 getBlockHash(int height) = 0; virtual uint256 getBlockHash(int height) = 0;
@ -135,6 +130,11 @@ public:
//! any blocks) //! any blocks)
virtual Optional<int> getHeight() = 0; virtual Optional<int> 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<int> getBlockHeight(const uint256& hash) = 0;
//! Return whether node has the block and optionally return block metadata //! Return whether node has the block and optionally return block metadata
//! or contents. //! or contents.
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0; virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;

View file

@ -885,10 +885,9 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose)
void CWallet::LoadToWallet(CWalletTx& wtxIn) void CWallet::LoadToWallet(CWalletTx& wtxIn)
{ {
// If wallet doesn't have a chain (e.g bitcoin-wallet), lock can't be taken. // If wallet doesn't have a chain (e.g wallet-tool), don't bother to update txn.
auto locked_chain = LockChain(); if (HaveChain()) {
if (locked_chain) { Optional<int> block_height = chain().getBlockHeight(wtxIn.m_confirm.hashBlock);
Optional<int> block_height = locked_chain->getBlockHeight(wtxIn.m_confirm.hashBlock);
if (block_height) { if (block_height) {
// Update cached block height variable since it not stored in the // Update cached block height variable since it not stored in the
// serialized transaction. // serialized transaction.

View file

@ -778,6 +778,9 @@ public:
/** Interface to assert chain access and if successful lock it */ /** Interface to assert chain access and if successful lock it */
std::unique_ptr<interfaces::Chain::Lock> LockChain() { return m_chain ? m_chain->lock() : nullptr; } std::unique_ptr<interfaces::Chain::Lock> LockChain() { return m_chain ? m_chain->lock() : nullptr; }
/** Interface to assert chain access */
bool HaveChain() const { return m_chain ? true : false; }
std::map<uint256, CWalletTx> mapWallet GUARDED_BY(cs_wallet); std::map<uint256, CWalletTx> mapWallet GUARDED_BY(cs_wallet);
typedef std::multimap<int64_t, CWalletTx*> TxItems; typedef std::multimap<int64_t, CWalletTx*> TxItems;