From 61cb9c8e7863e21944fe9e1262c490e974bceef3 Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Wed, 27 Jun 2018 09:02:43 +0200 Subject: [PATCH] wallet: add min/max conf filter to GetWatchOnlyBalance() --- src/wallet/wallet.cpp | 21 ++++++++------------- src/wallet/wallet.h | 2 +- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 78cacc0206641..1150bbc8ee0a6 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -2186,7 +2186,7 @@ CAmount CWallet::GetImmatureBalance() const return nTotal; } -CAmount CWallet::GetWatchOnlyBalance() const +CAmount CWallet::GetWatchOnlyBalance(int min_depth, int max_depth) const { CAmount nTotal = 0; { @@ -2194,8 +2194,13 @@ CAmount CWallet::GetWatchOnlyBalance() const for (const auto& entry : mapWallet) { const CWalletTx* pcoin = &entry.second; - if (pcoin->IsTrusted()) + int depth = pcoin->GetDepthInMainChain(); + if ( (min_depth == 0 && !pcoin->IsTrusted() && depth == 0 && pcoin->InMempool()) /* either 0 conf... */ + || + (pcoin->IsTrusted() && (min_depth == 1 || depth >= min_depth) && (max_depth < 0 || depth <= max_depth)) /*... or within depth limits (if set) */ + ) { nTotal += pcoin->GetAvailableWatchOnlyCredit(); + } } } @@ -2204,17 +2209,7 @@ CAmount CWallet::GetWatchOnlyBalance() const CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const { - CAmount nTotal = 0; - { - LOCK2(cs_main, cs_wallet); - for (const auto& entry : mapWallet) - { - const CWalletTx* pcoin = &entry.second; - if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool()) - nTotal += pcoin->GetAvailableWatchOnlyCredit(); - } - } - return nTotal; + return GetWatchOnlyBalance(0, 0); } CAmount CWallet::GetImmatureWatchOnlyBalance() const diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1ec2a9e7716e7..efed731d3f581 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -946,7 +946,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface CAmount GetBalance() const; CAmount GetUnconfirmedBalance() const; CAmount GetImmatureBalance() const; - CAmount GetWatchOnlyBalance() const; + CAmount GetWatchOnlyBalance(int min_depth = 1, int max_depth = -1 /* unlimited */) const; CAmount GetUnconfirmedWatchOnlyBalance() const; CAmount GetImmatureWatchOnlyBalance() const; CAmount GetLegacyBalance(const isminefilter& filter, int minDepth, const std::string* account) const;