Skip to content

Commit

Permalink
Move some static functions out of wallet.h/cpp
Browse files Browse the repository at this point in the history
>>> backports bitcoin/bitcoin@d97fe20

This commit just moves a few function declarations and updates callers.
Function bodies are moved in two followup MOVEONLY commits.

This change is desirable because wallet.h/cpp are monolithic and hard to
navigate, so pulling things out and grouping together pieces of related
functionality should improve the organization.

Another proximate motivation is the wallet process separation work in
parameter parsing and fee estimation are still done in the main process
rather than the wallet process, and having functions that run in
different processes scrambled up throughout wallet.cpp is unnecessarily
confusing.
  • Loading branch information
random-zebra committed Jun 2, 2021
1 parent f49acf7 commit 2188c3e
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ set(WALLET_SOURCES
./src/wallet/hdchain.cpp
./src/wallet/rpcdump.cpp
./src/zpiv/zerocoin.cpp
./src/wallet/fees.cpp
./src/wallet/init.cpp
./src/wallet/scriptpubkeyman.cpp
./src/wallet/rpcwallet.cpp
./src/kernel.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ BITCOIN_CORE_H = \
wallet/rpcwallet.h \
wallet/scriptpubkeyman.h \
destination_io.h \
wallet/fees.h \
wallet/init.h \
wallet/wallet.h \
wallet/walletdb.h \
warnings.h \
Expand Down Expand Up @@ -395,6 +397,8 @@ libbitcoin_wallet_a_SOURCES = \
legacy/stakemodifier.cpp \
kernel.cpp \
wallet/db.cpp \
wallet/fees.cpp \
wallet/init.cpp \
wallet/rpcdump.cpp \
wallet/rpcwallet.cpp \
wallet/hdchain.cpp \
Expand Down
2 changes: 2 additions & 0 deletions src/guiinterfaceutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define GUIINTERFACEUTIL_H

#include "guiinterface.h"
#include "tinyformat.h"
#include "util/system.h"

inline static bool UIError(const std::string &str)
{
Expand Down
12 changes: 6 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@
#include "validation.h"
#include "validationinterface.h"
#include "zpivchain.h"
#include "warnings.h"

#ifdef ENABLE_WALLET
#include "wallet/init.h"
#include "wallet/wallet.h"
#include "wallet/rpcwallet.h"

#endif
#include "warnings.h"

#include <atomic>
#include <fstream>
Expand Down Expand Up @@ -507,7 +507,7 @@ std::string HelpMessage(HelpMessageMode mode)
" " + _("Whitelisted peers cannot be DoS banned and their transactions are always relayed, even if they are already in the mempool, useful e.g. for a gateway"));

#if ENABLE_WALLET
strUsage += CWallet::GetWalletHelpString(showDebug);
strUsage += GetWalletHelpString(showDebug);
#endif

if (mode == HMM_BITCOIN_QT) {
Expand Down Expand Up @@ -1158,7 +1158,7 @@ bool AppInitParameterInteraction()

#ifdef ENABLE_WALLET
strWalletFile = gArgs.GetArg("-wallet", DEFAULT_WALLET_DAT);
if (!CWallet::ParameterInteraction())
if (!WalletParameterInteraction())
return false;
#endif // ENABLE_WALLET

Expand Down Expand Up @@ -1332,7 +1332,7 @@ bool AppInitMain()

// ********************************************************* Step 5: Verify wallet database integrity
#ifdef ENABLE_WALLET
if (!CWallet::Verify()) {
if (!WalletVerify()) {
return false;
}
#endif
Expand Down Expand Up @@ -1721,7 +1721,7 @@ bool AppInitMain()

// ********************************************************* Step 8: Backup and Load wallet
#ifdef ENABLE_WALLET
if (!CWallet::InitLoadWallet())
if (!InitLoadWallet())
return false;
#else
LogPrintf("No wallet compiled in!\n");
Expand Down
7 changes: 4 additions & 3 deletions src/qt/coincontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "optionsmodel.h"
#include "policy/policy.h"
#include "txmempool.h"
#include "wallet/fees.h"
#include "wallet/wallet.h"
#include "walletmodel.h"

Expand Down Expand Up @@ -632,7 +633,7 @@ void CoinControlDialog::updateLabels()

// tool tips
QString toolTip1 = tr("This label turns red, if the transaction size is greater than 1000 bytes.") + "<br /><br />";
toolTip1 += tr("This means a fee of at least %1 per kB is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, CWallet::GetRequiredFee(1000))) + "<br /><br />";
toolTip1 += tr("This means a fee of at least %1 per kB is required.").arg(BitcoinUnits::formatWithUnit(nDisplayUnit, GetRequiredFee(1000))) + "<br /><br />";
toolTip1 += tr("Can vary +/- 1 byte per input.");

QString toolTip3 = tr("This label turns red, if recipient receives an amount smaller than %1 (transparent) / %2 (shield)."
Expand All @@ -641,9 +642,9 @@ void CoinControlDialog::updateLabels()
// how many satoshis the estimated fee can vary per byte we guess wrong
double dFeeVary;
if (payTxFee.GetFeePerK() > 0)
dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), payTxFee.GetFeePerK()) / 1000;
dFeeVary = (double)std::max(GetRequiredFee(1000), payTxFee.GetFeePerK()) / 1000;
else
dFeeVary = (double)std::max(CWallet::GetRequiredFee(1000), mempool.estimateSmartFee(nTxConfirmTarget).GetFeePerK()) / 1000;
dFeeVary = (double)std::max(GetRequiredFee(1000), mempool.estimateSmartFee(nTxConfirmTarget).GetFeePerK()) / 1000;
QString toolTip4 = tr("Can vary +/- %1 u%2 per input.").arg(dFeeVary).arg(CURRENCY_UNIT.c_str());

ui->labelCoinControlFee->setToolTip(toolTip4);
Expand Down
11 changes: 6 additions & 5 deletions src/qt/pivx/sendcustomfeedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include "qt/pivx/sendcustomfeedialog.h"
#include "qt/pivx/forms/ui_sendcustomfeedialog.h"
#include "qt/pivx/qtutils.h"
#include "walletmodel.h"
#include "qt/walletmodel.h"
#include "optionsmodel.h"
#include "guiutil.h"
#include "wallet/fees.h"
#include <QListView>
#include <QComboBox>

Expand Down Expand Up @@ -123,19 +124,19 @@ void SendCustomFeeDialog::accept()
// Check insane fee
const CAmount insaneFee = ::minRelayTxFee.GetFeePerK() * 10000;
if (customFee >= insaneFee) {
ui->lineEditCustomFee->setText(BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), insaneFee - CWallet::GetRequiredFee(1000)));
ui->lineEditCustomFee->setText(BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), insaneFee - GetRequiredFee(1000)));
inform(tr("Fee too high. Must be below: %1").arg(
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), insaneFee)));
} else if (customFee < CWallet::GetRequiredFee(1000)) {
} else if (customFee < GetRequiredFee(1000)) {
CAmount nFee = 0;
if (walletModel->hasWalletCustomFee()) {
walletModel->getWalletCustomFee(nFee);
} else {
nFee = CWallet::GetRequiredFee(1000);
nFee = GetRequiredFee(1000);
}
ui->lineEditCustomFee->setText(BitcoinUnits::format(walletModel->getOptionsModel()->getDisplayUnit(), nFee));
inform(tr("Fee too low. Must be at least: %1").arg(
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), CWallet::GetRequiredFee(1000))));
BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), GetRequiredFee(1000))));
} else {
walletModel->setWalletCustomFee(fUseCustomFee, customFee);
QDialog::accept();
Expand Down
13 changes: 13 additions & 0 deletions src/wallet/fees.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Copyright (c) 2021 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "wallet/fees.h"

#include "policy/policy.h"
#include "txmempool.h"
#include "util/system.h"
#include "validation.h"
#include "wallet/wallet.h"
27 changes: 27 additions & 0 deletions src/wallet/fees.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Copyright (c) 2021 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef PIVX_WALLET_FEES_H
#define PIVX_WALLET_FEES_H

#include "amount.h"

class CTxMemPool;

/**
* Return the minimum required fee taking into account the
* floating relay fee and user set minimum transaction fee
*/
CAmount GetRequiredFee(unsigned int nTxBytes);

/**
* Estimate the minimum fee considering user set parameters
* and the required fee
*/
CAmount GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool);


#endif // PIVX_WALLET_FEES_H
14 changes: 14 additions & 0 deletions src/wallet/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Copyright (c) 2021 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "wallet/init.h"

#include "guiinterfaceutil.h"
#include "net.h"
#include "util/system.h"
#include "utilmoneystr.h"
#include "validation.h"
#include "wallet/wallet.h"
26 changes: 26 additions & 0 deletions src/wallet/init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Copyright (c) 2021 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef PIVX_WALLET_INIT_H
#define PIVX_WALLET_INIT_H

#include <string>

//! Return the wallets help message.
std::string GetWalletHelpString(bool showDebug);

//! Wallets parameter interaction
bool WalletParameterInteraction();

//! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
// This function will perform salvage on the wallet if requested, as long as only one wallet is
// being loaded (CWallet::ParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
bool WalletVerify();

//! Load wallet databases.
bool InitLoadWallet();

#endif // PIVX_WALLET_INIT_H
3 changes: 2 additions & 1 deletion src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "coincontrol.h"
#include "core_io.h"
#include "destination_io.h"
#include "init.h"
#include "httpserver.h"
#include "key_io.h"
#include "masternode-sync.h"
Expand All @@ -30,6 +29,8 @@
#include "wallet/walletdb.h"
#include "zpivchain.h"

#include <init.h> // for StartShutdown

#include <stdint.h>
#include <univalue.h>

Expand Down
23 changes: 15 additions & 8 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

#include "budget/budgetmanager.h"
#include "coincontrol.h"
<<<<<<< Upstream, based on master
#include "evo/deterministicmns.h"
#include "init.h"
=======
>>>>>>> 834e2bb Move some static functions out of wallet.h/cpp
#include "guiinterfaceutil.h"
#include "masternode.h"
#include "masternode-payments.h"
Expand All @@ -21,8 +24,12 @@
#include "spork.h"
#include "util/system.h"
#include "utilmoneystr.h"
#include "wallet/init.h"
#include "wallet/fees.h"
#include "zpivchain.h"

#include <init.h> // for StartShutdown/ShutdownRequested

#include <future>
#include <boost/algorithm/string/replace.hpp>

Expand Down Expand Up @@ -692,7 +699,7 @@ void CWallet::SyncMetaData(std::pair<typename TxSpendMap<T>::iterator, typename

///////// Init ////////////////

bool CWallet::ParameterInteraction()
bool WalletParameterInteraction()
{
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
return true;
Expand Down Expand Up @@ -2160,7 +2167,7 @@ void CWallet::Flush(bool shutdown)
bitdb.Flush(shutdown);
}

bool CWallet::Verify()
bool WalletVerify()
{
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
return true;
Expand Down Expand Up @@ -3591,12 +3598,12 @@ CWallet::CommitResult CWallet::CommitTransaction(CTransactionRef tx, CReserveKey
return res;
}

CAmount CWallet::GetRequiredFee(unsigned int nTxBytes)
CAmount GetRequiredFee(unsigned int nTxBytes)
{
return std::max(minTxFee.GetFee(nTxBytes), ::minRelayTxFee.GetFee(nTxBytes));
return std::max(CWallet::minTxFee.GetFee(nTxBytes), ::minRelayTxFee.GetFee(nTxBytes));
}

CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool)
CAmount GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool)
{
// payTxFee is user-set "I want to pay this much"
CAmount nFeeNeeded = payTxFee.GetFee(nTxBytes);
Expand Down Expand Up @@ -4259,7 +4266,7 @@ void CWallet::LockIfMyCollateral(const CTransactionRef& ptx)
}
}

std::string CWallet::GetWalletHelpString(bool showDebug)
std::string GetWalletHelpString(bool showDebug)
{
std::string strUsage = HelpMessageGroup(_("Wallet options:"));
strUsage += HelpMessageOpt("-backuppath=<dir|file>", _("Specify custom backup path to add a copy of any wallet backup. If set as dir, every backup generates a timestamped file. If set as file, will rewrite to that file every backup."));
Expand Down Expand Up @@ -4490,7 +4497,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
return walletInstance;
}

bool CWallet::InitLoadWallet()
bool InitLoadWallet()
{
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
LogPrintf("Wallet disabled!\n");
Expand All @@ -4509,7 +4516,7 @@ bool CWallet::InitLoadWallet()
}
}

CWallet * const pwallet = CreateWalletFromFile(walletFile);
CWallet * const pwallet = CWallet::CreateWalletFromFile(walletFile);
if (!pwallet) {
return false;
}
Expand Down
22 changes: 0 additions & 22 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1078,16 +1078,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
CAmount GetUnconfirmedShieldedBalance() const;

static CFeeRate minTxFee;
/**
* Estimate the minimum fee considering user set parameters
* and the required fee
*/
static CAmount GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarget, const CTxMemPool& pool);
/**
* Return the minimum required fee taking into account the
* floating relay fee and user set minimum transaction fee
*/
static CAmount GetRequiredFee(unsigned int nTxBytes);

size_t KeypoolCountExternalKeys();
bool TopUpKeyPool(unsigned int kpSize = 0);
Expand Down Expand Up @@ -1161,23 +1151,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
//! Flush wallet (bitdb flush)
void Flush(bool shutdown=false);

//! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
// This function will perform salvage on the wallet if requested, as long as only one wallet is
// being loaded (CWallet::ParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
static bool Verify();

/* Mark a transaction (and it in-wallet descendants) as abandoned so its inputs may be respent. */
bool AbandonTransaction(const uint256& hashTx);

/* Returns the wallets help message */
static std::string GetWalletHelpString(bool showDebug);

/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
static CWallet* CreateWalletFromFile(const std::string walletFile);
static bool InitLoadWallet();

/* Wallets parameter interaction */
static bool ParameterInteraction();

/**
* Wallet post-init setup
Expand Down

0 comments on commit 2188c3e

Please sign in to comment.