Skip to content

Commit 1386ab7

Browse files
committed
Use CWallet::m_last_block_processed_height in GetDepthInMainChain
Avoid to lock chain to query state thanks to tracking last block height in CWallet. Adaptation of btc@0ff03871add000f8b4d8f82aeb168eed2fc9dc5f
1 parent 9adeb61 commit 1386ab7

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/wallet/wallet.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4321,24 +4321,11 @@ CWalletKey::CWalletKey(int64_t nExpires)
43214321

43224322
int CWalletTx::GetDepthInMainChain() const
43234323
{
4324+
assert(pwallet != nullptr);
4325+
AssertLockHeld(pwallet->cs_wallet);
43244326
if (isUnconfirmed() || isAbandoned()) return 0;
4325-
AssertLockHeld(cs_main);
4326-
int nResult;
4327-
4328-
// Find the block it claims to be in
4329-
BlockMap::iterator mi = mapBlockIndex.find(m_confirm.hashBlock);
4330-
if (mi == mapBlockIndex.end()) {
4331-
nResult = 0;
4332-
} else {
4333-
CBlockIndex* pindex = (*mi).second;
4334-
if (!pindex || !chainActive.Contains(pindex)) {
4335-
nResult = 0;
4336-
} else {
4337-
nResult = (isConflicted() ? (-1) : 1) * (chainActive.Height() - pindex->nHeight + 1);
4338-
}
4339-
}
43404327

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

43444331
int CWalletTx::GetBlocksToMaturity() const

src/wallet/wallet.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,13 @@ class CWalletTx
520520
* 0 : in memory pool, waiting to be included in a block
521521
* >=1 : this many blocks deep in the main chain
522522
*/
523-
int GetDepthInMainChain() const;
523+
// TODO: Remove "NO_THREAD_SAFETY_ANALYSIS" and replace it with the correct
524+
// annotation "EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)". The annotation
525+
// "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid having to
526+
// resolve the issue of member access into incomplete type CWallet. Note
527+
// that we still have the runtime check "AssertLockHeld(pwallet->cs_wallet)"
528+
// in place.
529+
int GetDepthInMainChain() const NO_THREAD_SAFETY_ANALYSIS;
524530
bool IsInMainChainImmature() const;
525531
int GetBlocksToMaturity() const;
526532

0 commit comments

Comments
 (0)