Skip to content

Commit d358466

Browse files
committed
Remove remaining wallet accesses to node globals
1 parent b1b2b23 commit d358466

File tree

12 files changed

+63
-49
lines changed

12 files changed

+63
-49
lines changed

src/interfaces/chain.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <protocol.h>
1919
#include <rpc/protocol.h>
2020
#include <rpc/server.h>
21+
#include <shutdown.h>
2122
#include <sync.h>
2223
#include <threadsafety.h>
2324
#include <timedata.h>
@@ -340,15 +341,23 @@ class ChainImpl : public Chain
340341
{
341342
return ::mempool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
342343
}
344+
CFeeRate relayMinFee() override { return ::minRelayTxFee; }
345+
CFeeRate relayIncrementalFee() override { return ::incrementalRelayFee; }
346+
CFeeRate relayDustFee() override { return ::dustRelayFee; }
343347
CAmount maxTxFee() override { return ::maxTxFee; }
344348
bool getPruneMode() override { return ::fPruneMode; }
345349
bool p2pEnabled() override { return g_connman != nullptr; }
346350
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
351+
bool shutdownRequested() override { return ShutdownRequested(); }
347352
int64_t getAdjustedTime() override { return GetAdjustedTime(); }
348353
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }
349354
void initWarning(const std::string& message) override { InitWarning(message); }
350355
void initError(const std::string& message) override { InitError(message); }
351356
void loadWallet(std::unique_ptr<Wallet> wallet) override { ::uiInterface.LoadWallet(wallet); }
357+
void showProgress(const std::string& title, int progress, bool resume_possible) override
358+
{
359+
::uiInterface.ShowProgress(title, progress, resume_possible);
360+
}
352361
std::unique_ptr<Handler> handleNotifications(Notifications& notifications) override
353362
{
354363
return MakeUnique<NotificationsHandlerImpl>(*this, notifications);

src/interfaces/chain.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,15 @@ class Chain
202202
//! Mempool minimum fee.
203203
virtual CFeeRate mempoolMinFee() = 0;
204204

205+
//! Relay current minimum fee (from -minrelaytxfee and -incrementalrelayfee settings).
206+
virtual CFeeRate relayMinFee() = 0;
207+
208+
//! Relay incremental fee setting (-incrementalrelayfee), reflecting cost of relay.
209+
virtual CFeeRate relayIncrementalFee() = 0;
210+
211+
//! Relay dust fee setting (-dustrelayfee), reflecting lowest rate it's economical to spend.
212+
virtual CFeeRate relayDustFee() = 0;
213+
205214
//! Node max tx fee setting (-maxtxfee).
206215
//! This could be replaced by a per-wallet max fee, as proposed at
207216
//! https://github.com/bitcoin/bitcoin/issues/15355
@@ -214,9 +223,12 @@ class Chain
214223
//! Check if p2p enabled.
215224
virtual bool p2pEnabled() = 0;
216225

217-
// Check if in IBD.
226+
//! Check if in IBD.
218227
virtual bool isInitialBlockDownload() = 0;
219228

229+
//! Check if shutdown requested.
230+
virtual bool shutdownRequested() = 0;
231+
220232
//! Get adjusted time.
221233
virtual int64_t getAdjustedTime() = 0;
222234

@@ -232,6 +244,9 @@ class Chain
232244
//! Send wallet load notification to the GUI.
233245
virtual void loadWallet(std::unique_ptr<Wallet> wallet) = 0;
234246

247+
//! Send progress indicator.
248+
virtual void showProgress(const std::string& title, int progress, bool resume_possible) = 0;
249+
235250
//! Chain notifications.
236251
class Notifications
237252
{

src/interfaces/wallet.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ class PendingWalletTxImpl : public PendingWalletTx
4747

4848
const CTransaction& get() override { return *m_tx; }
4949

50-
int64_t getVirtualSize() override { return GetVirtualTransactionSize(*m_tx); }
51-
5250
bool commit(WalletValueMap value_map,
5351
WalletOrderForm order_form,
5452
std::string& reject_reason) override
@@ -99,12 +97,8 @@ WalletTx MakeWalletTx(interfaces::Chain::Lock& locked_chain, CWallet& wallet, co
9997
//! Construct wallet tx status struct.
10098
WalletTxStatus MakeWalletTxStatus(interfaces::Chain::Lock& locked_chain, const CWalletTx& wtx)
10199
{
102-
LockAnnotation lock(::cs_main); // Temporary, for mapBlockIndex below. Removed in upcoming commit.
103-
104100
WalletTxStatus result;
105-
auto mi = ::mapBlockIndex.find(wtx.hashBlock);
106-
CBlockIndex* block = mi != ::mapBlockIndex.end() ? mi->second : nullptr;
107-
result.block_height = (block ? block->nHeight : std::numeric_limits<int>::max());
101+
result.block_height = locked_chain.getBlockHeight(wtx.hashBlock).get_value_or(std::numeric_limits<int>::max());
108102
result.blocks_to_maturity = wtx.GetBlocksToMaturity(locked_chain);
109103
result.depth_in_main_chain = wtx.GetDepthInMainChain(locked_chain);
110104
result.time_received = wtx.nTimeReceived;

src/interfaces/wallet.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,6 @@ class PendingWalletTx
292292
//! Get transaction data.
293293
virtual const CTransaction& get() = 0;
294294

295-
//! Get virtual transaction size.
296-
virtual int64_t getVirtualSize() = 0;
297-
298295
//! Send pending transaction and commit to wallet.
299296
virtual bool commit(WalletValueMap value_map,
300297
WalletOrderForm order_form,

src/qt/walletmodeltransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ std::unique_ptr<interfaces::PendingWalletTx>& WalletModelTransaction::getWtx()
2929

3030
unsigned int WalletModelTransaction::getTransactionSize()
3131
{
32-
return wtx ? wtx->getVirtualSize() : 0;
32+
return wtx ? GetVirtualTransactionSize(wtx->get()) : 0;
3333
}
3434

3535
CAmount WalletModelTransaction::getTransactionFee() const

src/wallet/feebumper.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,17 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
123123
// The wallet uses a conservative WALLET_INCREMENTAL_RELAY_FEE value to
124124
// future proof against changes to network wide policy for incremental relay
125125
// fee that our node may not be aware of.
126+
CFeeRate nodeIncrementalRelayFee = wallet->chain().relayIncrementalFee();
126127
CFeeRate walletIncrementalRelayFee = CFeeRate(WALLET_INCREMENTAL_RELAY_FEE);
127-
if (::incrementalRelayFee > walletIncrementalRelayFee) {
128-
walletIncrementalRelayFee = ::incrementalRelayFee;
128+
if (nodeIncrementalRelayFee > walletIncrementalRelayFee) {
129+
walletIncrementalRelayFee = nodeIncrementalRelayFee;
129130
}
130131

131132
if (total_fee > 0) {
132-
CAmount minTotalFee = nOldFeeRate.GetFee(maxNewTxSize) + ::incrementalRelayFee.GetFee(maxNewTxSize);
133+
CAmount minTotalFee = nOldFeeRate.GetFee(maxNewTxSize) + nodeIncrementalRelayFee.GetFee(maxNewTxSize);
133134
if (total_fee < minTotalFee) {
134135
errors.push_back(strprintf("Insufficient totalFee, must be at least %s (oldFee %s + incrementalFee %s)",
135-
FormatMoney(minTotalFee), FormatMoney(nOldFeeRate.GetFee(maxNewTxSize)), FormatMoney(::incrementalRelayFee.GetFee(maxNewTxSize))));
136+
FormatMoney(minTotalFee), FormatMoney(nOldFeeRate.GetFee(maxNewTxSize)), FormatMoney(nodeIncrementalRelayFee.GetFee(maxNewTxSize))));
136137
return Result::INVALID_PARAMETER;
137138
}
138139
CAmount requiredFee = GetRequiredFee(*wallet, maxNewTxSize);
@@ -159,9 +160,10 @@ Result CreateTransaction(const CWallet* wallet, const uint256& txid, const CCoin
159160
}
160161

161162
// Check that in all cases the new fee doesn't violate maxTxFee
162-
if (new_fee > maxTxFee) {
163+
const CAmount max_tx_fee = wallet->chain().maxTxFee();
164+
if (new_fee > max_tx_fee) {
163165
errors.push_back(strprintf("Specified or calculated fee %s is too high (cannot be higher than maxTxFee %s)",
164-
FormatMoney(new_fee), FormatMoney(maxTxFee)));
166+
FormatMoney(new_fee), FormatMoney(max_tx_fee)));
165167
return Result::WALLET_ERROR;
166168
}
167169

src/wallet/fees.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ CAmount GetMinimumFee(const CWallet& wallet, unsigned int nTxBytes, const CCoinC
2222
{
2323
CAmount fee_needed = GetMinimumFeeRate(wallet, coin_control, feeCalc).GetFee(nTxBytes);
2424
// Always obey the maximum
25-
if (fee_needed > maxTxFee) {
26-
fee_needed = maxTxFee;
25+
const CAmount max_tx_fee = wallet.chain().maxTxFee();
26+
if (fee_needed > max_tx_fee) {
27+
fee_needed = max_tx_fee;
2728
if (feeCalc) feeCalc->reason = FeeReason::MAXTXFEE;
2829
}
2930
return fee_needed;
3031
}
3132

3233
CFeeRate GetRequiredFeeRate(const CWallet& wallet)
3334
{
34-
return std::max(wallet.m_min_fee, ::minRelayTxFee);
35+
return std::max(wallet.m_min_fee, wallet.chain().relayMinFee());
3536
}
3637

3738
CFeeRate GetMinimumFeeRate(const CWallet& wallet, const CCoinControl& coin_control, FeeCalculation* feeCalc)
@@ -96,6 +97,6 @@ CFeeRate GetDiscardRate(const CWallet& wallet)
9697
// Don't let discard_rate be greater than longest possible fee estimate if we get a valid fee estimate
9798
discard_rate = (discard_rate == CFeeRate(0)) ? wallet.m_discard_rate : std::min(discard_rate, wallet.m_discard_rate);
9899
// Discard rate must be at least dustRelayFee
99-
discard_rate = std::max(discard_rate, ::dustRelayFee);
100+
discard_rate = std::max(discard_rate, wallet.chain().relayDustFee());
100101
return discard_rate;
101102
}

src/wallet/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
153153

154154
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
155155

156-
uiInterface.InitMessage(_("Verifying wallet(s)..."));
156+
chain.initMessage(_("Verifying wallet(s)..."));
157157

158158
// Parameter interaction code should have thrown an error if -salvagewallet
159159
// was enabled with more than wallet file, so the wallet_files size check

src/wallet/rpcdump.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,11 @@ UniValue importwallet(const JSONRPCRequest& request)
595595

596596
// Use uiInterface.ShowProgress instead of pwallet.ShowProgress because pwallet.ShowProgress has a cancel button tied to AbortRescan which
597597
// we don't want for this progress bar showing the import progress. uiInterface.ShowProgress does not have a cancel button.
598-
uiInterface.ShowProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
598+
pwallet->chain().showProgress(strprintf("%s " + _("Importing..."), pwallet->GetDisplayName()), 0, false); // show progress dialog in GUI
599599
std::vector<std::tuple<CKey, int64_t, bool, std::string>> keys;
600600
std::vector<std::pair<CScript, int64_t>> scripts;
601601
while (file.good()) {
602-
uiInterface.ShowProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
602+
pwallet->chain().showProgress("", std::max(1, std::min(50, (int)(((double)file.tellg() / (double)nFilesize) * 100))), false);
603603
std::string line;
604604
std::getline(file, line);
605605
if (line.empty() || line[0] == '#')
@@ -637,13 +637,13 @@ UniValue importwallet(const JSONRPCRequest& request)
637637
file.close();
638638
// We now know whether we are importing private keys, so we can error if private keys are disabled
639639
if (keys.size() > 0 && pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
640-
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
640+
pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
641641
throw JSONRPCError(RPC_WALLET_ERROR, "Importing wallets is disabled when private keys are disabled");
642642
}
643643
double total = (double)(keys.size() + scripts.size());
644644
double progress = 0;
645645
for (const auto& key_tuple : keys) {
646-
uiInterface.ShowProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
646+
pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
647647
const CKey& key = std::get<0>(key_tuple);
648648
int64_t time = std::get<1>(key_tuple);
649649
bool has_label = std::get<2>(key_tuple);
@@ -668,7 +668,7 @@ UniValue importwallet(const JSONRPCRequest& request)
668668
progress++;
669669
}
670670
for (const auto& script_pair : scripts) {
671-
uiInterface.ShowProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
671+
pwallet->chain().showProgress("", std::max(50, std::min(75, (int)((progress / total) * 100) + 50)), false);
672672
const CScript& script = script_pair.first;
673673
int64_t time = script_pair.second;
674674
CScriptID id(script);
@@ -687,10 +687,10 @@ UniValue importwallet(const JSONRPCRequest& request)
687687
}
688688
progress++;
689689
}
690-
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
690+
pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
691691
pwallet->UpdateTimeFirstKey(nTimeBegin);
692692
}
693-
uiInterface.ShowProgress("", 100, false); // hide progress dialog in GUI
693+
pwallet->chain().showProgress("", 100, false); // hide progress dialog in GUI
694694
RescanWallet(*pwallet, reserver, nTimeBegin, false /* update */);
695695
pwallet->MarkDirty();
696696

src/wallet/rpcwallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,8 +2367,8 @@ static UniValue settxfee(const JSONRPCRequest& request)
23672367
CFeeRate tx_fee_rate(nAmount, 1000);
23682368
if (tx_fee_rate == 0) {
23692369
// automatic selection
2370-
} else if (tx_fee_rate < ::minRelayTxFee) {
2371-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than min relay tx fee (%s)", ::minRelayTxFee.ToString()));
2370+
} else if (tx_fee_rate < pwallet->chain().relayMinFee()) {
2371+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than min relay tx fee (%s)", pwallet->chain().relayMinFee().ToString()));
23722372
} else if (tx_fee_rate < pwallet->m_min_fee) {
23732373
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("txfee cannot be less than wallet min fee (%s)", pwallet->m_min_fee.ToString()));
23742374
}

0 commit comments

Comments
 (0)