Skip to content

Commit

Permalink
Use CWallet::m_last_block_processed_height in GetDepthInMainChain
Browse files Browse the repository at this point in the history
Avoid to lock chain to query state thanks to tracking last block
height in CWallet.

Adaptation of btc@0ff03871add000f8b4d8f82aeb168eed2fc9dc5f
  • Loading branch information
furszy committed Mar 10, 2021
1 parent 9adeb61 commit 1386ab7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
19 changes: 3 additions & 16 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4321,24 +4321,11 @@ CWalletKey::CWalletKey(int64_t nExpires)

int CWalletTx::GetDepthInMainChain() const
{
assert(pwallet != nullptr);
AssertLockHeld(pwallet->cs_wallet);
if (isUnconfirmed() || isAbandoned()) return 0;
AssertLockHeld(cs_main);
int nResult;

// Find the block it claims to be in
BlockMap::iterator mi = mapBlockIndex.find(m_confirm.hashBlock);
if (mi == mapBlockIndex.end()) {
nResult = 0;
} else {
CBlockIndex* pindex = (*mi).second;
if (!pindex || !chainActive.Contains(pindex)) {
nResult = 0;
} else {
nResult = (isConflicted() ? (-1) : 1) * (chainActive.Height() - pindex->nHeight + 1);
}
}

return nResult;
return (pwallet->GetLastBlockHeight() - m_confirm.block_height + 1) * (isConflicted() ? -1 : 1);
}

int CWalletTx::GetBlocksToMaturity() const
Expand Down
8 changes: 7 additions & 1 deletion src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,13 @@ class CWalletTx
* 0 : in memory pool, waiting to be included in a block
* >=1 : this many blocks deep in the main chain
*/
int GetDepthInMainChain() const;
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
// annotation "EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)". The annotation
// "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid having to
// resolve the issue of member access into incomplete type CWallet. Note
// that we still have the runtime check "AssertLockHeld(pwallet->cs_wallet)"
// in place.
int GetDepthInMainChain() const NO_THREAD_SAFETY_ANALYSIS;
bool IsInMainChainImmature() const;
int GetBlocksToMaturity() const;

Expand Down

0 comments on commit 1386ab7

Please sign in to comment.