Skip to content

Commit

Permalink
wallet: Avoid use of Chain::Lock in tryGetTxStatus and tryGetBalances
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ryanofsky committed Mar 31, 2020
1 parent bf30cd4 commit f6da44c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <sync.h>
#include <ui_interface.h>
#include <uint256.h>
#include <util/check.h>
#include <util/system.h>
#include <wallet/feebumper.h>
#include <wallet/fees.h>
Expand Down Expand Up @@ -62,7 +63,7 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
{
WalletTxStatus result;
result.block_height = locked_chain.getBlockHeight(wtx.m_confirm.hashBlock).get_value_or(std::numeric_limits<int>::max());
result.block_height = wtx.m_confirm.block_height > 0 ? wtx.m_confirm.block_height : std::numeric_limits<int>::max();
result.blocks_to_maturity = wtx.GetBlocksToMaturity();
result.depth_in_main_chain = wtx.GetDepthInMainChain();
result.time_received = wtx.nTimeReceived;
Expand Down Expand Up @@ -317,13 +318,9 @@ class WalletImpl : public Wallet
if (mi == m_wallet->mapWallet.end()) {
return false;
}
if (Optional<int> height = locked_chain->getHeight()) {
num_blocks = *height;
block_time = locked_chain->getBlockTime(*height);
} else {
num_blocks = -1;
block_time = -1;
}
num_blocks = m_wallet->GetLastBlockHeight();
block_time = -1;
CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time)));
tx_status = MakeWalletTxStatus(*locked_chain, mi->second);
return true;
}
Expand Down Expand Up @@ -372,12 +369,12 @@ class WalletImpl : public Wallet
{
auto locked_chain = m_wallet->chain().lock(true /* try_lock */);
if (!locked_chain) return false;
num_blocks = locked_chain->getHeight().get_value_or(-1);
if (!force && num_blocks == cached_num_blocks) return false;
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
if (!locked_wallet) {
return false;
}
num_blocks = m_wallet->GetLastBlockHeight();
if (!force && num_blocks == cached_num_blocks) return false;
balances = getBalances();
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,12 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
assert(m_last_block_processed_height >= 0);
return m_last_block_processed_height;
};
uint256 GetLastBlockHash() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
{
AssertLockHeld(cs_wallet);
assert(m_last_block_processed_height >= 0);
return m_last_block_processed;
}
/** Set last block processed height, currently only use in unit test */
void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
{
Expand Down

0 comments on commit f6da44c

Please sign in to comment.