Skip to content

Commit f6da44c

Browse files
committed
wallet: Avoid use of Chain::Lock in tryGetTxStatus and tryGetBalances
This is a step toward removing the Chain::Lock class and reducing cs_main locking. It also helps ensure the GUI display stays up to date in the case where the node chain height runs ahead of wallet last block processed height.
1 parent bf30cd4 commit f6da44c

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/interfaces/wallet.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sync.h>
1515
#include <ui_interface.h>
1616
#include <uint256.h>
17+
#include <util/check.h>
1718
#include <util/system.h>
1819
#include <wallet/feebumper.h>
1920
#include <wallet/fees.h>
@@ -62,7 +63,7 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
6263
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
6364
{
6465
WalletTxStatus result;
65-
result.block_height = locked_chain.getBlockHeight(wtx.m_confirm.hashBlock).get_value_or(std::numeric_limits<int>::max());
66+
result.block_height = wtx.m_confirm.block_height > 0 ? wtx.m_confirm.block_height : std::numeric_limits<int>::max();
6667
result.blocks_to_maturity = wtx.GetBlocksToMaturity();
6768
result.depth_in_main_chain = wtx.GetDepthInMainChain();
6869
result.time_received = wtx.nTimeReceived;
@@ -317,13 +318,9 @@ class WalletImpl : public Wallet
317318
if (mi == m_wallet->mapWallet.end()) {
318319
return false;
319320
}
320-
if (Optional<int> height = locked_chain->getHeight()) {
321-
num_blocks = *height;
322-
block_time = locked_chain->getBlockTime(*height);
323-
} else {
324-
num_blocks = -1;
325-
block_time = -1;
326-
}
321+
num_blocks = m_wallet->GetLastBlockHeight();
322+
block_time = -1;
323+
CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time)));
327324
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
328325
return true;
329326
}
@@ -372,12 +369,12 @@ class WalletImpl : public Wallet
372369
{
373370
auto locked_chain = m_wallet->chain().lock(true /* try_lock */);
374371
if (!locked_chain) return false;
375-
num_blocks = locked_chain->getHeight().get_value_or(-1);
376-
if (!force && num_blocks == cached_num_blocks) return false;
377372
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
378373
if (!locked_wallet) {
379374
return false;
380375
}
376+
num_blocks = m_wallet->GetLastBlockHeight();
377+
if (!force && num_blocks == cached_num_blocks) return false;
381378
balances = getBalances();
382379
return true;
383380
}

src/wallet/wallet.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,12 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
12001200
assert(m_last_block_processed_height >= 0);
12011201
return m_last_block_processed_height;
12021202
};
1203+
uint256 GetLastBlockHash() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
1204+
{
1205+
AssertLockHeld(cs_wallet);
1206+
assert(m_last_block_processed_height >= 0);
1207+
return m_last_block_processed;
1208+
}
12031209
/** Set last block processed height, currently only use in unit test */
12041210
void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
12051211
{

0 commit comments

Comments
 (0)