Skip to content

Commit

Permalink
[wallet] Move getBlockHeight from Chain::Lock interface to simple Chain
Browse files Browse the repository at this point in the history
Add HaveChain to assert chain access for wallet-tool in LoadToWallet.
  • Loading branch information
Antoine Riard committed Apr 30, 2020
1 parent b855592 commit de13363
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
18 changes: 9 additions & 9 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,6 @@ bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<Rec

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
{
LockAssertion lock(::cs_main);
Expand Down Expand Up @@ -234,6 +225,15 @@ class ChainImpl : public Chain
}
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
{
WAIT_LOCK(cs_main, lock);
Expand Down
10 changes: 5 additions & 5 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ class Chain
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<int> getBlockHeight(const uint256& hash) = 0;

//! Get block hash. Height must be valid or this function will abort.
virtual uint256 getBlockHash(int height) = 0;

Expand Down Expand Up @@ -135,6 +130,11 @@ class Chain
//! any blocks)
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
//! or contents.
virtual bool findBlock(const uint256& hash, const FoundBlock& block={}) = 0;
Expand Down
7 changes: 3 additions & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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<int> block_height = chain().getBlockHeight(wtxIn.m_confirm.hashBlock);
if (block_height) {
// Update cached block height variable since it not stored in the
// serialized transaction.
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
/** Interface to assert chain access and if successful lock it */
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);

typedef std::multimap<int64_t, CWalletTx*> TxItems;
Expand Down

0 comments on commit de13363

Please sign in to comment.