Merge pull request #4482 from christiancfifi/backports_pr2

Backports 15747, 15532, 14556, 14594, 15136, 20491, 14715
This commit is contained in:
UdjinM6 2021-10-15 13:26:43 +03:00 committed by GitHub
commit 38dee8b361
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 744 additions and 767 deletions

View File

@ -17,11 +17,11 @@ struct nontrivial_t {
}; };
typedef prevector<28, unsigned char> prevec; typedef prevector<28, unsigned char> prevec;
static_assert(!IS_TRIVIALLY_CONSTRUCTIBLE<nontrivial_t>::value, static_assert(!std::is_trivially_default_constructible<nontrivial_t>::value,
"expected nontrivial_t to not be trivially constructible"); "expected nontrivial_t to not be trivially constructible");
typedef unsigned char trivial_t; typedef unsigned char trivial_t;
static_assert(IS_TRIVIALLY_CONSTRUCTIBLE<trivial_t>::value, static_assert(std::is_trivially_default_constructible<trivial_t>::value,
"expected trivial_t to be trivially constructible"); "expected trivial_t to be trivially constructible");
template <typename T> template <typename T>

View File

@ -309,6 +309,8 @@ bool BlockFilter::BuildParams(GCSFilter::Params& params) const
params.m_P = BASIC_FILTER_P; params.m_P = BASIC_FILTER_P;
params.m_M = BASIC_FILTER_M; params.m_M = BASIC_FILTER_M;
return true; return true;
case BlockFilterType::INVALID:
return false;
} }
return false; return false;

View File

@ -85,9 +85,10 @@ public:
constexpr uint8_t BASIC_FILTER_P = 19; constexpr uint8_t BASIC_FILTER_P = 19;
constexpr uint32_t BASIC_FILTER_M = 784931; constexpr uint32_t BASIC_FILTER_M = 784931;
enum BlockFilterType : uint8_t enum class BlockFilterType : uint8_t
{ {
BASIC_FILTER = 0, BASIC_FILTER = 0,
INVALID = 255,
}; };
/** Get the human-readable name for a filter type. Returns empty string for unknown types. */ /** Get the human-readable name for a filter type. Returns empty string for unknown types. */
@ -109,7 +110,7 @@ const std::string& ListBlockFilterTypes();
class BlockFilter class BlockFilter
{ {
private: private:
BlockFilterType m_filter_type; BlockFilterType m_filter_type = BlockFilterType::INVALID;
uint256 m_block_hash; uint256 m_block_hash;
GCSFilter m_filter; GCSFilter m_filter;

View File

@ -19,7 +19,7 @@
* Maximum amount of time that a block timestamp is allowed to exceed the * Maximum amount of time that a block timestamp is allowed to exceed the
* current network-adjusted time before the block will be accepted. * current network-adjusted time before the block will be accepted.
*/ */
static const int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60; static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
/** /**
* Timestamp window used as a grace period by code that compares external * Timestamp window used as a grace period by code that compares external
@ -27,7 +27,13 @@ static const int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
* to block timestamps. This should be set at least as high as * to block timestamps. This should be set at least as high as
* MAX_FUTURE_BLOCK_TIME. * MAX_FUTURE_BLOCK_TIME.
*/ */
static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME; static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME;
/**
* Maximum gap between node time and block time used
* for the "Catching up..." mode in GUI.
*/
static constexpr int64_t MAX_BLOCK_TIME_GAP = 25 * 60;
class CBlockFileInfo class CBlockFileInfo
{ {

View File

@ -812,8 +812,10 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
return false; return false;
} }
const auto bal = mixingWallet.GetBalance();
// check if there is anything left to do // check if there is anything left to do
CAmount nBalanceAnonymized = mixingWallet.GetAnonymizedBalance(); CAmount nBalanceAnonymized = bal.m_anonymized;
nBalanceNeedsAnonymized = CCoinJoinClientOptions::GetAmount() * COIN - nBalanceAnonymized; nBalanceNeedsAnonymized = CCoinJoinClientOptions::GetAmount() * COIN - nBalanceAnonymized;
if (nBalanceNeedsAnonymized < 0) { if (nBalanceNeedsAnonymized < 0) {
@ -843,8 +845,8 @@ bool CCoinJoinClientSession::DoAutomaticDenominating(CConnman& connman, bool fDr
// excluding denoms // excluding denoms
CAmount nBalanceAnonimizableNonDenom = mixingWallet.GetAnonymizableBalance(true); CAmount nBalanceAnonimizableNonDenom = mixingWallet.GetAnonymizableBalance(true);
// denoms // denoms
CAmount nBalanceDenominatedConf = mixingWallet.GetDenominatedBalance(); CAmount nBalanceDenominatedConf = bal.m_denominated_trusted;
CAmount nBalanceDenominatedUnconf = mixingWallet.GetDenominatedBalance(true); CAmount nBalanceDenominatedUnconf = bal.m_denominated_untrusted_pending;
CAmount nBalanceDenominated = nBalanceDenominatedConf + nBalanceDenominatedUnconf; CAmount nBalanceDenominated = nBalanceDenominatedConf + nBalanceDenominatedUnconf;
CAmount nBalanceToDenominate = CCoinJoinClientOptions::GetAmount() * COIN - nBalanceDenominated; CAmount nBalanceToDenominate = CCoinJoinClientOptions::GetAmount() * COIN - nBalanceDenominated;

View File

@ -10,16 +10,6 @@
#include <config/dash-config.h> #include <config/dash-config.h>
#endif #endif
#include <type_traits>
// GCC 4.8 is missing some C++11 type_traits,
// https://www.gnu.org/software/gcc/gcc-5/changes.html
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 5
#define IS_TRIVIALLY_CONSTRUCTIBLE std::has_trivial_default_constructor
#else
#define IS_TRIVIALLY_CONSTRUCTIBLE std::is_trivially_default_constructible
#endif
#ifdef WIN32 #ifdef WIN32
#ifdef _WIN32_WINNT #ifdef _WIN32_WINNT
#undef _WIN32_WINNT #undef _WIN32_WINNT

View File

@ -343,7 +343,8 @@ public:
return result; return result;
} }
bool tryGetTxStatus(const uint256& txid, bool tryGetTxStatus(const uint256& txid,
interfaces::WalletTxStatus& tx_status) override interfaces::WalletTxStatus& tx_status,
int64_t& block_time) override
{ {
TRY_LOCK(::cs_main, locked_chain); TRY_LOCK(::cs_main, locked_chain);
if (!locked_chain) { if (!locked_chain) {
@ -358,6 +359,7 @@ public:
return false; return false;
} }
tx_status = MakeWalletTxStatus(mi->second); tx_status = MakeWalletTxStatus(mi->second);
block_time = ::chainActive.Tip()->GetBlockTime();
return true; return true;
} }
WalletTx getWalletTxDetails(const uint256& txid, WalletTx getWalletTxDetails(const uint256& txid,
@ -381,16 +383,17 @@ public:
bool isFullyMixed(const COutPoint& outpoint) override { return m_wallet->IsFullyMixed(outpoint); } bool isFullyMixed(const COutPoint& outpoint) override { return m_wallet->IsFullyMixed(outpoint); }
WalletBalances getBalances() override WalletBalances getBalances() override
{ {
const auto bal = m_wallet->GetBalance();
WalletBalances result; WalletBalances result;
result.balance = m_wallet->GetBalance(); result.balance = bal.m_mine_trusted;
result.unconfirmed_balance = m_wallet->GetUnconfirmedBalance(); result.unconfirmed_balance = bal.m_mine_untrusted_pending;
result.immature_balance = m_wallet->GetImmatureBalance(); result.immature_balance = bal.m_mine_immature;
result.anonymized_balance = m_wallet->GetAnonymizedBalance(); result.anonymized_balance = bal.m_anonymized;
result.have_watch_only = m_wallet->HaveWatchOnly(); result.have_watch_only = m_wallet->HaveWatchOnly();
if (result.have_watch_only) { if (result.have_watch_only) {
result.watch_only_balance = m_wallet->GetBalance(ISMINE_WATCH_ONLY); result.watch_only_balance = bal.m_watchonly_trusted;
result.unconfirmed_watch_only_balance = m_wallet->GetUnconfirmedWatchOnlyBalance(); result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending;
result.immature_watch_only_balance = m_wallet->GetImmatureWatchOnlyBalance(); result.immature_watch_only_balance = bal.m_watchonly_immature;
} }
return result; return result;
} }
@ -408,7 +411,7 @@ public:
} }
CAmount getBalance() override CAmount getBalance() override
{ {
return m_wallet->GetBalance(); return m_wallet->GetBalance().m_mine_trusted;
} }
CAmount getAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfirmed) override CAmount getAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfirmed) override
{ {
@ -416,11 +419,16 @@ public:
} }
CAmount getAnonymizedBalance() override CAmount getAnonymizedBalance() override
{ {
return m_wallet->GetAnonymizedBalance(); return m_wallet->GetBalance().m_anonymized;
} }
CAmount getDenominatedBalance(bool unconfirmed) override CAmount getDenominatedBalance(bool unconfirmed) override
{ {
return m_wallet->GetDenominatedBalance(unconfirmed); const auto bal = m_wallet->GetBalance();
if (unconfirmed) {
return bal.m_denominated_untrusted_pending;
} else {
return bal.m_denominated_trusted;
}
} }
CAmount getNormalizedAnonymizedBalance() override CAmount getNormalizedAnonymizedBalance() override
{ {
@ -433,7 +441,7 @@ public:
CAmount getAvailableBalance(const CCoinControl& coin_control) override CAmount getAvailableBalance(const CCoinControl& coin_control) override
{ {
if (coin_control.IsUsingCoinJoin()) { if (coin_control.IsUsingCoinJoin()) {
return m_wallet->GetAnonymizedBalance(&coin_control); return m_wallet->GetBalance(0, false, &coin_control).m_anonymized;
} else { } else {
return m_wallet->GetAvailableBalance(&coin_control); return m_wallet->GetAvailableBalance(&coin_control);
} }

View File

@ -183,7 +183,8 @@ public:
//! Try to get updated status for a particular transaction, if possible without blocking. //! Try to get updated status for a particular transaction, if possible without blocking.
virtual bool tryGetTxStatus(const uint256& txid, virtual bool tryGetTxStatus(const uint256& txid,
WalletTxStatus& tx_status) = 0; WalletTxStatus& tx_status,
int64_t& block_time) = 0;
//! Get transaction details. //! Get transaction details.
virtual WalletTx getWalletTxDetails(const uint256& txid, virtual WalletTx getWalletTxDetails(const uint256& txid,

View File

@ -15,8 +15,6 @@
#include <iterator> #include <iterator>
#include <type_traits> #include <type_traits>
#include <compat.h>
/** Implements a drop-in replacement for std::vector<T> which stores up to N /** Implements a drop-in replacement for std::vector<T> which stores up to N
* elements directly (without heap allocation). The types Size and Diff are * elements directly (without heap allocation). The types Size and Diff are
* used to store element counts, and can be any unsigned + signed type. * used to store element counts, and can be any unsigned + signed type.
@ -203,11 +201,7 @@ private:
T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); } T* item_ptr(difference_type pos) { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); } const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
void fill(T* dst, ptrdiff_t count) { void fill(T* dst, ptrdiff_t count, const T& value = T{}) {
std::fill_n(dst, count, T{});
}
void fill(T* dst, ptrdiff_t count, const T& value) {
std::fill_n(dst, count, value); std::fill_n(dst, count, value);
} }
@ -226,7 +220,7 @@ private:
} }
void fill(T* dst, const T* src, ptrdiff_t count) { void fill(T* dst, const T* src, ptrdiff_t count) {
if (IS_TRIVIALLY_CONSTRUCTIBLE<T>::value) { if (std::is_trivially_constructible<T>::value) {
::memmove(dst, src, count * sizeof(T)); ::memmove(dst, src, count * sizeof(T));
} else { } else {
for (ptrdiff_t i = 0; i < count; i++) { for (ptrdiff_t i = 0; i < count; i++) {
@ -560,7 +554,7 @@ public:
static void assign_to(const_iterator b, const_iterator e, V& v) { static void assign_to(const_iterator b, const_iterator e, V& v) {
// We know that internally the iterators are pointing to continues memory, so we can directly use the pointers here // We know that internally the iterators are pointing to continues memory, so we can directly use the pointers here
// This avoids internal use of std::copy and operator++ on the iterators and instead allows efficient memcpy/memmove // This avoids internal use of std::copy and operator++ on the iterators and instead allows efficient memcpy/memmove
if (IS_TRIVIALLY_CONSTRUCTIBLE<T>::value) { if (std::is_trivially_constructible<T>::value) {
auto s = e - b; auto s = e - b;
if (v.size() != s) { if (v.size() != s) {
v.resize(s); v.resize(s);

View File

@ -29,6 +29,7 @@
#include <qt/macdockiconhandler.h> #include <qt/macdockiconhandler.h>
#endif #endif
#include <chain.h>
#include <chainparams.h> #include <chainparams.h>
#include <interfaces/handler.h> #include <interfaces/handler.h>
#include <interfaces/node.h> #include <interfaces/node.h>
@ -37,6 +38,7 @@
#include <qt/masternodelist.h> #include <qt/masternodelist.h>
#include <iostream> #include <iostream>
#include <memory>
#include <QAction> #include <QAction>
#include <QApplication> #include <QApplication>
@ -46,6 +48,7 @@
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QDragEnterEvent> #include <QDragEnterEvent>
#include <QListWidget> #include <QListWidget>
#include <QMenu>
#include <QMenuBar> #include <QMenuBar>
#include <QMessageBox> #include <QMessageBox>
#include <QMimeData> #include <QMimeData>
@ -55,6 +58,7 @@
#include <QStackedWidget> #include <QStackedWidget>
#include <QStatusBar> #include <QStatusBar>
#include <QStyle> #include <QStyle>
#include <QSystemTrayIcon>
#include <QTimer> #include <QTimer>
#include <QToolBar> #include <QToolBar>
#include <QToolButton> #include <QToolButton>
@ -76,7 +80,8 @@ const std::string BitcoinGUI::DEFAULT_UIPLATFORM =
BitcoinGUI::BitcoinGUI(interfaces::Node& node, const NetworkStyle* networkStyle, QWidget* parent) : BitcoinGUI::BitcoinGUI(interfaces::Node& node, const NetworkStyle* networkStyle, QWidget* parent) :
QMainWindow(parent), QMainWindow(parent),
m_node(node), m_node(node),
m_network_style(networkStyle) m_network_style(networkStyle),
trayIconMenu{new QMenu()}
{ {
GUIUtil::loadTheme(true); GUIUtil::loadTheme(true);
@ -692,9 +697,8 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel)
// while the client has not yet fully loaded // while the client has not yet fully loaded
if (trayIcon) { if (trayIcon) {
// do so only if trayIcon is already set // do so only if trayIcon is already set
trayIconMenu = new QMenu(this); trayIcon->setContextMenu(trayIconMenu.get());
trayIcon->setContextMenu(trayIconMenu); createIconMenu(trayIconMenu.get());
createIconMenu(trayIconMenu);
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
// Show main window on tray icon click // Show main window on tray icon click
@ -1318,16 +1322,12 @@ void BitcoinGUI::setNumBlocks(int count, const QDateTime& blockDate, const QStri
// Set icon state: spinning if catching up, tick otherwise // Set icon state: spinning if catching up, tick otherwise
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (walletFrame) if (walletFrame) {
{ if(secs < MAX_BLOCK_TIME_GAP) {
if(secs < 25*60) // 90*60 in bitcoin
{
modalOverlay->showHide(true, true); modalOverlay->showHide(true, true);
// TODO instead of hiding it forever, we should add meaningful information about MN sync to the overlay // TODO instead of hiding it forever, we should add meaningful information about MN sync to the overlay
modalOverlay->hideForever(); modalOverlay->hideForever();
} } else {
else
{
modalOverlay->showHide(); modalOverlay->showHide();
} }
} }

View File

@ -14,7 +14,6 @@
#include <QLabel> #include <QLabel>
#include <QMainWindow> #include <QMainWindow>
#include <QMap> #include <QMap>
#include <QMenu>
#include <QPoint> #include <QPoint>
#include <QPushButton> #include <QPushButton>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
@ -47,6 +46,7 @@ QT_BEGIN_NAMESPACE
class QAction; class QAction;
class QButtonGroup; class QButtonGroup;
class QComboBox; class QComboBox;
class QMenu;
class QProgressBar; class QProgressBar;
class QProgressDialog; class QProgressDialog;
class QToolButton; class QToolButton;
@ -165,7 +165,7 @@ private:
QComboBox* m_wallet_selector = nullptr; QComboBox* m_wallet_selector = nullptr;
QSystemTrayIcon* trayIcon = nullptr; QSystemTrayIcon* trayIcon = nullptr;
QMenu* trayIconMenu = nullptr; const std::unique_ptr<QMenu> trayIconMenu;
QMenu* dockIconMenu = nullptr; QMenu* dockIconMenu = nullptr;
Notificator* notificator = nullptr; Notificator* notificator = nullptr;
RPCConsole* rpcConsole = nullptr; RPCConsole* rpcConsole = nullptr;

File diff suppressed because it is too large Load Diff

View File

@ -1133,10 +1133,9 @@ void RPCConsole::startExecutor()
void RPCConsole::on_stackedWidgetRPC_currentChanged(int index) void RPCConsole::on_stackedWidgetRPC_currentChanged(int index)
{ {
if (ui->stackedWidgetRPC->widget(index) == ui->pageConsole) if (ui->stackedWidgetRPC->widget(index) == ui->pageConsole) {
ui->lineEdit->setFocus(); ui->lineEdit->setFocus();
else if (ui->stackedWidgetRPC->widget(index) != ui->pagePeers) }
clearSelectedNode();
} }
void RPCConsole::on_openDebugLogfileButton_clicked() void RPCConsole::on_openDebugLogfileButton_clicked()

View File

@ -5,6 +5,7 @@
#include <qt/transactionrecord.h> #include <qt/transactionrecord.h>
#include <chain.h>
#include <consensus/consensus.h> #include <consensus/consensus.h>
#include <interfaces/wallet.h> #include <interfaces/wallet.h>
#include <interfaces/node.h> #include <interfaces/node.h>
@ -13,6 +14,7 @@
#include <stdint.h> #include <stdint.h>
#include <QDateTime>
/* Return positive answer if transaction should be shown in list. /* Return positive answer if transaction should be shown in list.
*/ */
@ -252,7 +254,7 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(interfaces::Wal
return parts; return parts;
} }
void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int chainLockHeight) void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int chainLockHeight, int64_t block_time)
{ {
// Determine transaction status // Determine transaction status
@ -269,10 +271,9 @@ void TransactionRecord::updateStatus(const interfaces::WalletTxStatus& wtx, int
status.lockedByChainLocks = wtx.is_chainlocked; status.lockedByChainLocks = wtx.is_chainlocked;
status.lockedByInstantSend = wtx.is_islocked; status.lockedByInstantSend = wtx.is_islocked;
if (!wtx.is_final) const bool up_to_date = ((int64_t)QDateTime::currentMSecsSinceEpoch() / 1000 - block_time < MAX_BLOCK_TIME_GAP);
{ if (up_to_date && !wtx.is_final) {
if (wtx.lock_time < LOCKTIME_THRESHOLD) if (wtx.lock_time < LOCKTIME_THRESHOLD) {
{
status.status = TransactionStatus::OpenUntilBlock; status.status = TransactionStatus::OpenUntilBlock;
status.open_for = wtx.lock_time - numBlocks; status.open_for = wtx.lock_time - numBlocks;
} }

View File

@ -161,7 +161,7 @@ public:
/** Update status from core wallet tx. /** Update status from core wallet tx.
*/ */
void updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int chainLockHeight); void updateStatus(const interfaces::WalletTxStatus& wtx, int numBlocks, int chainLockHeight, int64_t block_time);
/** Return whether a status update is needed. /** Return whether a status update is needed.
*/ */

View File

@ -199,8 +199,9 @@ public:
// try to update the status of this transaction from the wallet. // try to update the status of this transaction from the wallet.
// Otherwise, simply re-use the cached status. // Otherwise, simply re-use the cached status.
interfaces::WalletTxStatus wtx; interfaces::WalletTxStatus wtx;
if (rec->statusUpdateNeeded(numBlocks, parent->getChainLockHeight()) && wallet.tryGetTxStatus(rec->hash, wtx)) { int64_t block_time;
rec->updateStatus(wtx, numBlocks, parent->getChainLockHeight()); if (rec->statusUpdateNeeded(numBlocks, parent->getChainLockHeight()) && wallet.tryGetTxStatus(rec->hash, wtx, block_time)) {
rec->updateStatus(wtx, numBlocks, parent->getChainLockHeight(), block_time);
} }
return rec; return rec;
} }

View File

@ -117,6 +117,12 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType()); BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());
BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash()); BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());
BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter()); BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());
BlockFilter default_ctor_block_filter_1;
BlockFilter default_ctor_block_filter_2;
BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetFilterType(), default_ctor_block_filter_2.GetFilterType());
BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetBlockHash(), default_ctor_block_filter_2.GetBlockHash());
BOOST_CHECK(default_ctor_block_filter_1.GetEncodedFilter() == default_ctor_block_filter_2.GetEncodedFilter());
} }
BOOST_AUTO_TEST_CASE(blockfilters_json_test) BOOST_AUTO_TEST_CASE(blockfilters_json_test)

View File

@ -288,7 +288,7 @@ static UniValue setlabel(const JSONRPCRequest& request)
static CTransactionRef SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue) static CTransactionRef SendMoney(CWallet * const pwallet, const CTxDestination &address, CAmount nValue, bool fSubtractFeeFromAmount, const CCoinControl& coin_control, mapValue_t mapValue)
{ {
CAmount curBalance = pwallet->GetBalance(); CAmount curBalance = pwallet->GetBalance().m_mine_trusted;
// Check amount // Check amount
if (nValue <= 0) if (nValue <= 0)
@ -827,12 +827,14 @@ static UniValue getbalance(const JSONRPCRequest& request)
fAddLocked = addlocked.get_bool(); fAddLocked = addlocked.get_bool();
} }
isminefilter filter = ISMINE_SPENDABLE; bool include_watchonly = false;
if (!request.params[3].isNull() && request.params[3].get_bool()) { if (!request.params[3].isNull() && request.params[3].get_bool()) {
filter = filter | ISMINE_WATCH_ONLY; include_watchonly = true;
} }
return ValueFromAmount(pwallet->GetBalance(filter, min_depth, fAddLocked)); const auto bal = pwallet->GetBalance(min_depth, fAddLocked);
return ValueFromAmount(bal.m_mine_trusted + (include_watchonly ? bal.m_watchonly_trusted : 0));
} }
static UniValue getunconfirmedbalance(const JSONRPCRequest &request) static UniValue getunconfirmedbalance(const JSONRPCRequest &request)
@ -856,7 +858,7 @@ static UniValue getunconfirmedbalance(const JSONRPCRequest &request)
LOCK2(cs_main, pwallet->cs_wallet); LOCK2(cs_main, pwallet->cs_wallet);
return ValueFromAmount(pwallet->GetUnconfirmedBalance()); return ValueFromAmount(pwallet->GetBalance().m_mine_untrusted_pending);
} }
@ -2642,12 +2644,13 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
bool fHDEnabled = pwallet->GetHDChain(hdChainCurrent); bool fHDEnabled = pwallet->GetHDChain(hdChainCurrent);
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
const auto bal = pwallet->GetBalance();
obj.pushKV("walletname", pwallet->GetName()); obj.pushKV("walletname", pwallet->GetName());
obj.pushKV("walletversion", pwallet->GetVersion()); obj.pushKV("walletversion", pwallet->GetVersion());
obj.pushKV("balance", ValueFromAmount(pwallet->GetBalance())); obj.pushKV("balance", ValueFromAmount(bal.m_mine_trusted));
obj.pushKV("coinjoin_balance", ValueFromAmount(pwallet->GetAnonymizedBalance())); obj.pushKV("coinjoin_balance", ValueFromAmount(bal.m_anonymized));
obj.pushKV("unconfirmed_balance", ValueFromAmount(pwallet->GetUnconfirmedBalance())); obj.pushKV("unconfirmed_balance", ValueFromAmount(bal.m_mine_untrusted_pending));
obj.pushKV("immature_balance", ValueFromAmount(pwallet->GetImmatureBalance())); obj.pushKV("immature_balance", ValueFromAmount(bal.m_mine_immature));
obj.pushKV("txcount", (int)pwallet->mapWallet.size()); obj.pushKV("txcount", (int)pwallet->mapWallet.size());
obj.pushKV("timefirstkey", pwallet->GetTimeFirstKey()); obj.pushKV("timefirstkey", pwallet->GetTimeFirstKey());
obj.pushKV("keypoololdest", pwallet->GetOldestKeyPoolTime()); obj.pushKV("keypoololdest", pwallet->GetOldestKeyPoolTime());

View File

@ -57,7 +57,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS); BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::SUCCESS);
BOOST_CHECK_EQUAL(failed_block, null_block); BOOST_CHECK_EQUAL(failed_block, null_block);
BOOST_CHECK_EQUAL(stop_block, newTip); BOOST_CHECK_EQUAL(stop_block, newTip);
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 1000 * COIN); BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 1000 * COIN);
} }
// Prune the older block file. // Prune the older block file.
@ -75,7 +75,7 @@ BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup)
BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE); BOOST_CHECK_EQUAL(wallet.ScanForWalletTransactions(oldTip, nullptr, reserver, failed_block, stop_block), CWallet::ScanResult::FAILURE);
BOOST_CHECK_EQUAL(failed_block, oldTip); BOOST_CHECK_EQUAL(failed_block, oldTip);
BOOST_CHECK_EQUAL(stop_block, newTip); BOOST_CHECK_EQUAL(stop_block, newTip);
BOOST_CHECK_EQUAL(wallet.GetImmatureBalance(), 500 * COIN); BOOST_CHECK_EQUAL(wallet.GetBalance().m_mine_immature, 500 * COIN);
} }
// Verify importmulti RPC returns failure for a key whose creation time is // Verify importmulti RPC returns failure for a key whose creation time is

View File

@ -2649,19 +2649,34 @@ std::unordered_set<const CWalletTx*, WalletTxHasher> CWallet::GetSpendableTXs()
return ret; return ret;
} }
CAmount CWallet::GetBalance(const isminefilter& filter, const int min_depth, const bool fAddLocked) const CWallet::Balance CWallet::GetBalance(const int min_depth, const bool fAddLocked, const CCoinControl* coinControl) const
{ {
CAmount nTotal = 0; Balance ret;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) { for (auto pcoin : GetSpendableTXs()) {
if (pcoin->IsTrusted() && ((pcoin->GetDepthInMainChain() >= min_depth) || (fAddLocked && pcoin->IsLockedByInstantSend()))) { const bool is_trusted{pcoin->IsTrusted()};
nTotal += pcoin->GetAvailableCredit(true, filter); const int tx_depth{pcoin->GetDepthInMainChain()};
const CAmount tx_credit_mine{pcoin->GetAvailableCredit(/* fUseCache */ true, ISMINE_SPENDABLE)};
const CAmount tx_credit_watchonly{pcoin->GetAvailableCredit(/* fUseCache */ true, ISMINE_WATCH_ONLY)};
if (is_trusted && ((tx_depth >= min_depth) || (fAddLocked && pcoin->IsLockedByInstantSend()))) {
ret.m_mine_trusted += tx_credit_mine;
ret.m_watchonly_trusted += tx_credit_watchonly;
}
if (!is_trusted && tx_depth == 0 && pcoin->InMempool()) {
ret.m_mine_untrusted_pending += tx_credit_mine;
ret.m_watchonly_untrusted_pending += tx_credit_watchonly;
}
ret.m_mine_immature += pcoin->GetImmatureCredit();
ret.m_watchonly_immature += pcoin->GetImmatureWatchOnlyCredit();
if (CCoinJoinClientOptions::IsEnabled()) {
ret.m_anonymized += pcoin->GetAnonymizedCredit(coinControl);
ret.m_denominated_trusted += pcoin->GetDenominatedCredit(false);
ret.m_denominated_untrusted_pending += pcoin->GetDenominatedCredit(true);
} }
} }
} }
return ret;
return nTotal;
} }
CAmount CWallet::GetAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfirmed) const CAmount CWallet::GetAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfirmed) const
@ -2686,21 +2701,6 @@ CAmount CWallet::GetAnonymizableBalance(bool fSkipDenominated, bool fSkipUnconfi
return nTotal; return nTotal;
} }
CAmount CWallet::GetAnonymizedBalance(const CCoinControl* coinControl) const
{
if (!CCoinJoinClientOptions::IsEnabled()) return 0;
CAmount nTotal = 0;
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
nTotal += pcoin->GetAnonymizedCredit(coinControl);
}
return nTotal;
}
// Note: calculated including unconfirmed, // Note: calculated including unconfirmed,
// that's ok as long as we use it for informational purposes only // that's ok as long as we use it for informational purposes only
float CWallet::GetAverageAnonymizedRounds() const float CWallet::GetAverageAnonymizedRounds() const
@ -2747,71 +2747,6 @@ CAmount CWallet::GetNormalizedAnonymizedBalance() const
return nTotal; return nTotal;
} }
CAmount CWallet::GetDenominatedBalance(bool unconfirmed) const
{
if (!CCoinJoinClientOptions::IsEnabled()) return 0;
CAmount nTotal = 0;
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
nTotal += pcoin->GetDenominatedCredit(unconfirmed);
}
return nTotal;
}
CAmount CWallet::GetUnconfirmedBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && !pcoin->IsLockedByInstantSend() && pcoin->InMempool())
nTotal += pcoin->GetAvailableCredit();
}
}
return nTotal;
}
CAmount CWallet::GetImmatureBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
nTotal += pcoin->GetImmatureCredit();
}
}
return nTotal;
}
CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && !pcoin->IsLockedByInstantSend() && pcoin->InMempool())
nTotal += pcoin->GetAvailableCredit(true, ISMINE_WATCH_ONLY);
}
}
return nTotal;
}
CAmount CWallet::GetImmatureWatchOnlyBalance() const
{
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (auto pcoin : GetSpendableTXs()) {
nTotal += pcoin->GetImmatureWatchOnlyCredit();
}
}
return nTotal;
}
// Calculate total balance in a different way from GetBalance. The biggest // Calculate total balance in a different way from GetBalance. The biggest
// difference is that GetBalance sums up all unspent TxOuts paying to the // difference is that GetBalance sums up all unspent TxOuts paying to the
// wallet, while this sums up both spent and unspent TxOuts paying to the // wallet, while this sums up both spent and unspent TxOuts paying to the

View File

@ -987,18 +987,23 @@ public:
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override EXCLUSIVE_LOCKS_REQUIRED(cs_main); void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override EXCLUSIVE_LOCKS_REQUIRED(cs_main);
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions! // ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main); std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
CAmount GetBalance(const isminefilter& filter=ISMINE_SPENDABLE, const int min_depth=0, bool fAddLocked = false) const; struct Balance {
CAmount GetUnconfirmedBalance() const; CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
CAmount GetImmatureBalance() const; CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)
CAmount GetUnconfirmedWatchOnlyBalance() const; CAmount m_mine_immature{0}; //!< Immature coinbases in the main chain
CAmount GetImmatureWatchOnlyBalance() const; CAmount m_watchonly_trusted{0};
CAmount m_watchonly_untrusted_pending{0};
CAmount m_watchonly_immature{0};
CAmount m_anonymized{0};
CAmount m_denominated_trusted{0};
CAmount m_denominated_untrusted_pending{0};
};
CAmount GetLegacyBalance(const isminefilter& filter, int minDepth, const bool fAddLocked) const; CAmount GetLegacyBalance(const isminefilter& filter, int minDepth, const bool fAddLocked) const;
Balance GetBalance(int min_depth = 0, const bool fAddLocked = false, const CCoinControl* coinControl = nullptr) const;
CAmount GetAnonymizableBalance(bool fSkipDenominated = false, bool fSkipUnconfirmed = true) const; CAmount GetAnonymizableBalance(bool fSkipDenominated = false, bool fSkipUnconfirmed = true) const;
CAmount GetAnonymizedBalance(const CCoinControl* coinControl = nullptr) const;
float GetAverageAnonymizedRounds() const; float GetAverageAnonymizedRounds() const;
CAmount GetNormalizedAnonymizedBalance() const; CAmount GetNormalizedAnonymizedBalance() const;
CAmount GetDenominatedBalance(bool unconfirmed=false) const;
bool GetBudgetSystemCollateralTX(CTransactionRef& tx, uint256 hash, CAmount amount, const COutPoint& outpoint=COutPoint()/*defaults null*/); bool GetBudgetSystemCollateralTX(CTransactionRef& tx, uint256 hash, CAmount amount, const COutPoint& outpoint=COutPoint()/*defaults null*/);
CAmount GetAvailableBalance(const CCoinControl* coinControl = nullptr) const; CAmount GetAvailableBalance(const CCoinControl* coinControl = nullptr) const;