Skip to content

Commit 07c92b9

Browse files
committed
Merge #10976: [MOVEONLY] Move some static functions out of wallet.h/cpp
f01103c MOVEONLY: Init functions wallet/wallet.cpp -> wallet/init.cpp (Russell Yanofsky) e7fe320 MOVEONLY: Fee functions wallet/wallet.cpp -> wallet/fees.cpp (Russell Yanofsky) d97fe20 Move some static functions out of wallet.h/cpp (Russell Yanofsky) Pull request description: This just moves some static wallet fee and init functions out of `wallet/wallet.cpp` and into new `wallet/fees.cpp` and `wallet/init.cpp` source files. There is one commit updating declarations and callers, followed by two MOVEONLY commits actually moving the function bodies. 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 motivation is the wallet process separation work in bitcoin/bitcoin#10973, where (at least initially) 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. Tree-SHA512: 6e6982ff82b2ab4e681c043907e2b1801ceb9513394730070f16c46ad338278a863f5b3759aa13db76a259b268b1c919c81f4e339f0796a3cfb990161e8c316d
2 parents 3f726c9 + f01103c commit 07c92b9

File tree

13 files changed

+418
-351
lines changed

13 files changed

+418
-351
lines changed

src/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ BITCOIN_CORE_H = \
162162
wallet/crypter.h \
163163
wallet/db.h \
164164
wallet/feebumper.h \
165+
wallet/fees.h \
166+
wallet/init.h \
165167
wallet/rpcwallet.h \
166168
wallet/wallet.h \
167169
wallet/walletdb.h \
@@ -239,6 +241,8 @@ libbitcoin_wallet_a_SOURCES = \
239241
wallet/crypter.cpp \
240242
wallet/db.cpp \
241243
wallet/feebumper.cpp \
244+
wallet/fees.cpp \
245+
wallet/init.cpp \
242246
wallet/rpcdump.cpp \
243247
wallet/rpcwallet.cpp \
244248
wallet/wallet.cpp \

src/init.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "utilmoneystr.h"
4444
#include "validationinterface.h"
4545
#ifdef ENABLE_WALLET
46+
#include "wallet/init.h"
4647
#include "wallet/wallet.h"
4748
#endif
4849
#include "warnings.h"
@@ -420,7 +421,7 @@ std::string HelpMessage(HelpMessageMode mode)
420421
strUsage += HelpMessageOpt("-maxuploadtarget=<n>", strprintf(_("Tries to keep outbound traffic under the given target (in MiB per 24h), 0 = no limit (default: %d)"), DEFAULT_MAX_UPLOAD_TARGET));
421422

422423
#ifdef ENABLE_WALLET
423-
strUsage += CWallet::GetWalletHelpString(showDebug);
424+
strUsage += GetWalletHelpString(showDebug);
424425
#endif
425426

426427
#if ENABLE_ZMQ
@@ -1035,7 +1036,7 @@ bool AppInitParameterInteraction()
10351036
if (!ParseMoney(gArgs.GetArg("-minrelaytxfee", ""), n)) {
10361037
return InitError(AmountErrMsg("minrelaytxfee", gArgs.GetArg("-minrelaytxfee", "")));
10371038
}
1038-
// High fee check is done afterward in CWallet::ParameterInteraction()
1039+
// High fee check is done afterward in WalletParameterInteraction()
10391040
::minRelayTxFee = CFeeRate(n);
10401041
} else if (incrementalRelayFee > ::minRelayTxFee) {
10411042
// Allow only setting incrementalRelayFee to control both
@@ -1068,7 +1069,7 @@ bool AppInitParameterInteraction()
10681069
nBytesPerSigOp = gArgs.GetArg("-bytespersigop", nBytesPerSigOp);
10691070

10701071
#ifdef ENABLE_WALLET
1071-
if (!CWallet::ParameterInteraction())
1072+
if (!WalletParameterInteraction())
10721073
return false;
10731074
#endif
10741075

@@ -1245,7 +1246,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12451246

12461247
// ********************************************************* Step 5: verify wallet database integrity
12471248
#ifdef ENABLE_WALLET
1248-
if (!CWallet::Verify())
1249+
if (!WalletVerify())
12491250
return false;
12501251
#endif
12511252
// ********************************************************* Step 6: network initialization
@@ -1566,7 +1567,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
15661567

15671568
// ********************************************************* Step 8: load wallet
15681569
#ifdef ENABLE_WALLET
1569-
if (!CWallet::InitLoadWallet())
1570+
if (!InitLoadWallet())
15701571
return false;
15711572
#else
15721573
LogPrintf("No wallet support compiled in!\n");

src/qt/coincontroldialog.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "policy/fees.h"
1919
#include "policy/policy.h"
2020
#include "validation.h" // For mempool
21+
#include "wallet/fees.h"
2122
#include "wallet/wallet.h"
2223

2324
#include <QApplication>
@@ -510,7 +511,7 @@ void CoinControlDialog::updateLabels(WalletModel *model, QDialog* dialog)
510511
nBytes -= 34;
511512

512513
// Fee
513-
nPayFee = CWallet::GetMinimumFee(nBytes, *coinControl, ::mempool, ::feeEstimator, nullptr /* FeeCalculation */);
514+
nPayFee = GetMinimumFee(nBytes, *coinControl, ::mempool, ::feeEstimator, nullptr /* FeeCalculation */);
514515

515516
if (nPayAmount > 0)
516517
{

src/qt/optionsdialog.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#include "netbase.h"
1818
#include "txdb.h" // for -dbcache defaults
1919

20-
#ifdef ENABLE_WALLET
21-
#include "wallet/wallet.h" // for CWallet::GetRequiredFee()
22-
#endif
23-
2420
#include <QDataWidgetMapper>
2521
#include <QDir>
2622
#include <QIntValidator>

src/qt/sendcoinsdialog.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "ui_interface.h"
2323
#include "txmempool.h"
2424
#include "policy/fees.h"
25-
#include "wallet/wallet.h"
25+
#include "wallet/fees.h"
2626

2727
#include <QFontMetrics>
2828
#include <QMessageBox>
@@ -185,7 +185,7 @@ void SendCoinsDialog::setModel(WalletModel *_model)
185185
connect(ui->checkBoxMinimumFee, SIGNAL(stateChanged(int)), this, SLOT(coinControlUpdateLabels()));
186186
connect(ui->optInRBF, SIGNAL(stateChanged(int)), this, SLOT(updateSmartFeeLabel()));
187187
connect(ui->optInRBF, SIGNAL(stateChanged(int)), this, SLOT(coinControlUpdateLabels()));
188-
ui->customFee->setSingleStep(CWallet::GetRequiredFee(1000));
188+
ui->customFee->setSingleStep(GetRequiredFee(1000));
189189
updateFeeSectionControls();
190190
updateMinFeeLabel();
191191
updateSmartFeeLabel();
@@ -610,7 +610,7 @@ void SendCoinsDialog::on_buttonMinimizeFee_clicked()
610610
void SendCoinsDialog::setMinimumFee()
611611
{
612612
ui->radioCustomPerKilobyte->setChecked(true);
613-
ui->customFee->setValue(CWallet::GetRequiredFee(1000));
613+
ui->customFee->setValue(GetRequiredFee(1000));
614614
}
615615

616616
void SendCoinsDialog::updateFeeSectionControls()
@@ -643,7 +643,7 @@ void SendCoinsDialog::updateMinFeeLabel()
643643
{
644644
if (model && model->getOptionsModel())
645645
ui->checkBoxMinimumFee->setText(tr("Pay only the required fee of %1").arg(
646-
BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), CWallet::GetRequiredFee(1000)) + "/kB")
646+
BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), GetRequiredFee(1000)) + "/kB")
647647
);
648648
}
649649

@@ -668,7 +668,7 @@ void SendCoinsDialog::updateSmartFeeLabel()
668668
updateCoinControlState(coin_control);
669669
coin_control.m_feerate.reset(); // Explicitly use only fee estimation rate for smart fee labels
670670
FeeCalculation feeCalc;
671-
CFeeRate feeRate = CFeeRate(CWallet::GetMinimumFee(1000, coin_control, ::mempool, ::feeEstimator, &feeCalc));
671+
CFeeRate feeRate = CFeeRate(GetMinimumFee(1000, coin_control, ::mempool, ::feeEstimator, &feeCalc));
672672

673673
ui->labelSmartFee->setText(BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), feeRate.GetFeePerK()) + "/kB");
674674

src/wallet/feebumper.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "consensus/validation.h"
66
#include "wallet/coincontrol.h"
77
#include "wallet/feebumper.h"
8+
#include "wallet/fees.h"
89
#include "wallet/wallet.h"
910
#include "policy/fees.h"
1011
#include "policy/policy.h"
@@ -156,7 +157,7 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
156157
currentResult = BumpFeeResult::INVALID_PARAMETER;
157158
return;
158159
}
159-
CAmount requiredFee = CWallet::GetRequiredFee(maxNewTxSize);
160+
CAmount requiredFee = GetRequiredFee(maxNewTxSize);
160161
if (totalFee < requiredFee) {
161162
vErrors.push_back(strprintf("Insufficient totalFee (cannot be less than required fee %s)",
162163
FormatMoney(requiredFee)));
@@ -166,7 +167,7 @@ CFeeBumper::CFeeBumper(const CWallet *pWallet, const uint256 txidIn, const CCoin
166167
nNewFee = totalFee;
167168
nNewFeeRate = CFeeRate(totalFee, maxNewTxSize);
168169
} else {
169-
nNewFee = CWallet::GetMinimumFee(maxNewTxSize, coin_control, mempool, ::feeEstimator, nullptr /* FeeCalculation */);
170+
nNewFee = GetMinimumFee(maxNewTxSize, coin_control, mempool, ::feeEstimator, nullptr /* FeeCalculation */);
170171
nNewFeeRate = CFeeRate(nNewFee, maxNewTxSize);
171172

172173
// New fee rate must be at least old rate + minimum incremental relay rate

src/wallet/fees.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2017 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include "wallet/fees.h"
7+
8+
#include "policy/policy.h"
9+
#include "txmempool.h"
10+
#include "util.h"
11+
#include "validation.h"
12+
#include "wallet/coincontrol.h"
13+
#include "wallet/wallet.h"
14+
15+
16+
CAmount GetRequiredFee(unsigned int nTxBytes)
17+
{
18+
return std::max(CWallet::minTxFee.GetFee(nTxBytes), ::minRelayTxFee.GetFee(nTxBytes));
19+
}
20+
21+
22+
CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc)
23+
{
24+
/* User control of how to calculate fee uses the following parameter precedence:
25+
1. coin_control.m_feerate
26+
2. coin_control.m_confirm_target
27+
3. payTxFee (user-set global variable)
28+
4. nTxConfirmTarget (user-set global variable)
29+
The first parameter that is set is used.
30+
*/
31+
CAmount fee_needed;
32+
if (coin_control.m_feerate) { // 1.
33+
fee_needed = coin_control.m_feerate->GetFee(nTxBytes);
34+
if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
35+
// Allow to override automatic min/max check over coin control instance
36+
if (coin_control.fOverrideFeeRate) return fee_needed;
37+
}
38+
else if (!coin_control.m_confirm_target && ::payTxFee != CFeeRate(0)) { // 3. TODO: remove magic value of 0 for global payTxFee
39+
fee_needed = ::payTxFee.GetFee(nTxBytes);
40+
if (feeCalc) feeCalc->reason = FeeReason::PAYTXFEE;
41+
}
42+
else { // 2. or 4.
43+
// We will use smart fee estimation
44+
unsigned int target = coin_control.m_confirm_target ? *coin_control.m_confirm_target : ::nTxConfirmTarget;
45+
// By default estimates are economical iff we are signaling opt-in-RBF
46+
bool conservative_estimate = !coin_control.signalRbf;
47+
// Allow to override the default fee estimate mode over the CoinControl instance
48+
if (coin_control.m_fee_mode == FeeEstimateMode::CONSERVATIVE) conservative_estimate = true;
49+
else if (coin_control.m_fee_mode == FeeEstimateMode::ECONOMICAL) conservative_estimate = false;
50+
51+
fee_needed = estimator.estimateSmartFee(target, feeCalc, conservative_estimate).GetFee(nTxBytes);
52+
if (fee_needed == 0) {
53+
// if we don't have enough data for estimateSmartFee, then use fallbackFee
54+
fee_needed = CWallet::fallbackFee.GetFee(nTxBytes);
55+
if (feeCalc) feeCalc->reason = FeeReason::FALLBACK;
56+
}
57+
// Obey mempool min fee when using smart fee estimation
58+
CAmount min_mempool_fee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nTxBytes);
59+
if (fee_needed < min_mempool_fee) {
60+
fee_needed = min_mempool_fee;
61+
if (feeCalc) feeCalc->reason = FeeReason::MEMPOOL_MIN;
62+
}
63+
}
64+
65+
// prevent user from paying a fee below minRelayTxFee or minTxFee
66+
CAmount required_fee = GetRequiredFee(nTxBytes);
67+
if (required_fee > fee_needed) {
68+
fee_needed = required_fee;
69+
if (feeCalc) feeCalc->reason = FeeReason::REQUIRED;
70+
}
71+
// But always obey the maximum
72+
if (fee_needed > maxTxFee) {
73+
fee_needed = maxTxFee;
74+
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
75+
}
76+
return fee_needed;
77+
}
78+
79+
80+
CFeeRate GetDiscardRate(const CBlockPolicyEstimator& estimator)
81+
{
82+
unsigned int highest_target = estimator.HighestTargetTracked(FeeEstimateHorizon::LONG_HALFLIFE);
83+
CFeeRate discard_rate = estimator.estimateSmartFee(highest_target, nullptr /* FeeCalculation */, false /* conservative */);
84+
// Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
85+
discard_rate = (discard_rate == CFeeRate(0)) ? CWallet::m_discard_rate : std::min(discard_rate, CWallet::m_discard_rate);
86+
// Discard rate must be at least dustRelayFee
87+
discard_rate = std::max(discard_rate, ::dustRelayFee);
88+
return discard_rate;
89+
}

src/wallet/fees.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2017 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_WALLET_FEES_H
7+
#define BITCOIN_WALLET_FEES_H
8+
9+
#include "amount.h"
10+
11+
class CBlockPolicyEstimator;
12+
class CCoinControl;
13+
class CFeeRate;
14+
class CTxMemPool;
15+
struct FeeCalculation;
16+
17+
/**
18+
* Return the minimum required fee taking into account the
19+
* floating relay fee and user set minimum transaction fee
20+
*/
21+
CAmount GetRequiredFee(unsigned int nTxBytes);
22+
23+
/**
24+
* Estimate the minimum fee considering user set parameters
25+
* and the required fee
26+
*/
27+
CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_control, const CTxMemPool& pool, const CBlockPolicyEstimator& estimator, FeeCalculation *feeCalc);
28+
29+
/**
30+
* Return the maximum feerate for discarding change.
31+
*/
32+
CFeeRate GetDiscardRate(const CBlockPolicyEstimator& estimator);
33+
34+
#endif // BITCOIN_WALLET_FEES_H

0 commit comments

Comments
 (0)