Skip to content

Commit

Permalink
gui: Avoid Wallet::GetBalance in WalletModel::pollBalanceChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
promag committed Feb 16, 2020
1 parent 2a2631f commit 0933a37
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,16 +380,17 @@ class WalletImpl : public Wallet
}
return result;
}
bool tryGetBalances(WalletBalances& balances, int& num_blocks) override
bool tryGetBalances(WalletBalances& balances, int& num_blocks, bool force, int cached_num_blocks) override
{
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;
}
balances = getBalances();
num_blocks = locked_chain->getHeight().get_value_or(-1);
return true;
}
CAmount getBalance() override { return m_wallet->GetBalance().m_mine_trusted; }
Expand Down
7 changes: 5 additions & 2 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ class Wallet
//! Get balances.
virtual WalletBalances getBalances() = 0;

//! Get balances if possible without blocking.
virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
//! Get balances if possible without waiting for chain and wallet locks.
virtual bool tryGetBalances(WalletBalances& balances,
int& num_blocks,
bool force,
int cached_num_blocks) = 0;

//! Get balance.
virtual CAmount getBalance() = 0;
Expand Down
17 changes: 7 additions & 10 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,18 @@ void WalletModel::pollBalanceChanged()
// rescan.
interfaces::WalletBalances new_balances;
int numBlocks = -1;
if (!m_wallet->tryGetBalances(new_balances, numBlocks)) {
if (!m_wallet->tryGetBalances(new_balances, numBlocks, fForceCheckBalanceChanged, cachedNumBlocks)) {
return;
}

if(fForceCheckBalanceChanged || numBlocks != cachedNumBlocks)
{
fForceCheckBalanceChanged = false;
fForceCheckBalanceChanged = false;

// Balance and number of transactions might have changed
cachedNumBlocks = numBlocks;
// Balance and number of transactions might have changed
cachedNumBlocks = numBlocks;

checkBalanceChanged(new_balances);
if(transactionTableModel)
transactionTableModel->updateConfirmations();
}
checkBalanceChanged(new_balances);
if(transactionTableModel)
transactionTableModel->updateConfirmations();
}

void WalletModel::checkBalanceChanged(const interfaces::WalletBalances& new_balances)
Expand Down

0 comments on commit 0933a37

Please sign in to comment.