Skip to content

Commit cea38b4

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22183: Remove gArgs from wallet.h and wallet.cpp
c3c2132 Use `context.args` in `src/wallet/load.cpp`. (Kiminuo) 25de4e7 Use `context.args` in `CWallet::Create` instead of `gArgs`. (Kiminuo) aa5e7c9 Fix typo in bitcoin-cli.cpp (Kiminuo) Pull request description: The PR attempts to move us an inch towards the [goal](bitcoin/bitcoin#21244 (comment)) by using `WalletContext` in `wallet.{h|cpp}` code instead of relying on the global state (i.e. `gArgs`). Edit: The PR builds on #19101. ACKs for top commit: ryanofsky: Code review ACK c3c2132. Changes since last review: just rebasing and adding wallet load commit Tree-SHA512: 2b436f5a219e32c2d529f55a89edca086d949396cebf9e089a21aa7b1c180e3c0fb17468f415dfc834f8e1614f8b3914c7e9a0bd33b95e7e0199c0dfe5ca9490
2 parents 84be9a8 + c3c2132 commit cea38b4

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ static void GetWalletBalances(UniValue& result)
885885
}
886886

887887
/**
888-
* GetProgressBar contructs a progress bar with 5% intervals.
888+
* GetProgressBar constructs a progress bar with 5% intervals.
889889
*
890890
* @param[in] progress The proportion of the progress bar to be filled between 0 and 1.
891891
* @param[out] progress_bar String representation of the progress bar.

src/wallet/load.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <fs.h>
99
#include <interfaces/chain.h>
1010
#include <scheduler.h>
11+
#include <util/check.h>
1112
#include <util/string.h>
1213
#include <util/system.h>
1314
#include <util/translation.h>
@@ -20,8 +21,10 @@
2021
bool VerifyWallets(WalletContext& context)
2122
{
2223
interfaces::Chain& chain = *context.chain;
23-
if (gArgs.IsArgSet("-walletdir")) {
24-
fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
24+
ArgsManager& args = *Assert(context.args);
25+
26+
if (args.IsArgSet("-walletdir")) {
27+
fs::path wallet_dir = args.GetArg("-walletdir", "");
2528
boost::system::error_code error;
2629
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
2730
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
@@ -36,7 +39,7 @@ bool VerifyWallets(WalletContext& context)
3639
chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string()));
3740
return false;
3841
}
39-
gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
42+
args.ForceSetArg("-walletdir", canonical_wallet_dir.string());
4043
}
4144

4245
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
@@ -45,7 +48,7 @@ bool VerifyWallets(WalletContext& context)
4548

4649
// For backwards compatibility if an unnamed top level wallet exists in the
4750
// wallets directory, include it in the default list of wallets to load.
48-
if (!gArgs.IsArgSet("wallet")) {
51+
if (!args.IsArgSet("wallet")) {
4952
DatabaseOptions options;
5053
DatabaseStatus status;
5154
bilingual_str error_string;

src/wallet/test/wallet_tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
200200
wallet->SetupLegacyScriptPubKeyMan();
201201
WITH_LOCK(wallet->cs_wallet, wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash()));
202202
WalletContext context;
203+
context.args = &gArgs;
203204
AddWallet(context, wallet);
204205
UniValue keys;
205206
keys.setArray();
@@ -260,6 +261,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
260261
// Import key into wallet and call dumpwallet to create backup file.
261262
{
262263
WalletContext context;
264+
context.args = &gArgs;
263265
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(m_node.chain.get(), "", CreateDummyWalletDatabase());
264266
{
265267
auto spk_man = wallet->GetOrCreateLegacyScriptPubKeyMan();
@@ -287,6 +289,7 @@ BOOST_FIXTURE_TEST_CASE(importwallet_rescan, TestChain100Setup)
287289
wallet->SetupLegacyScriptPubKeyMan();
288290

289291
WalletContext context;
292+
context.args = &gArgs;
290293
JSONRPCRequest request;
291294
request.context = &context;
292295
request.params.setArray();
@@ -685,6 +688,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
685688
gArgs.ForceSetArg("-unsafesqlitesync", "1");
686689
// Create new wallet with known key and unload it.
687690
WalletContext context;
691+
context.args = &gArgs;
688692
context.chain = m_node.chain.get();
689693
auto wallet = TestLoadWallet(context);
690694
CKey key;
@@ -781,6 +785,7 @@ BOOST_FIXTURE_TEST_CASE(CreateWallet, TestChain100Setup)
781785
BOOST_FIXTURE_TEST_CASE(CreateWalletWithoutChain, BasicTestingSetup)
782786
{
783787
WalletContext context;
788+
context.args = &gArgs;
784789
auto wallet = TestLoadWallet(context);
785790
BOOST_CHECK(wallet);
786791
UnloadWallet(std::move(wallet));
@@ -790,6 +795,7 @@ BOOST_FIXTURE_TEST_CASE(ZapSelectTx, TestChain100Setup)
790795
{
791796
gArgs.ForceSetArg("-unsafesqlitesync", "1");
792797
WalletContext context;
798+
context.args = &gArgs;
793799
context.chain = m_node.chain.get();
794800
auto wallet = TestLoadWallet(context);
795801
CKey key;

src/wallet/wallet.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,7 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
25102510
std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings)
25112511
{
25122512
interfaces::Chain* chain = context.chain;
2513+
ArgsManager& args = *Assert(context.args);
25132514
const std::string& walletFile = database->Filename();
25142515

25152516
int64_t nStart = GetTimeMillis();
@@ -2591,28 +2592,28 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
25912592
}
25922593
}
25932594

2594-
if (!gArgs.GetArg("-addresstype", "").empty()) {
2595-
std::optional<OutputType> parsed = ParseOutputType(gArgs.GetArg("-addresstype", ""));
2595+
if (!args.GetArg("-addresstype", "").empty()) {
2596+
std::optional<OutputType> parsed = ParseOutputType(args.GetArg("-addresstype", ""));
25962597
if (!parsed) {
2597-
error = strprintf(_("Unknown address type '%s'"), gArgs.GetArg("-addresstype", ""));
2598+
error = strprintf(_("Unknown address type '%s'"), args.GetArg("-addresstype", ""));
25982599
return nullptr;
25992600
}
26002601
walletInstance->m_default_address_type = parsed.value();
26012602
}
26022603

2603-
if (!gArgs.GetArg("-changetype", "").empty()) {
2604-
std::optional<OutputType> parsed = ParseOutputType(gArgs.GetArg("-changetype", ""));
2604+
if (!args.GetArg("-changetype", "").empty()) {
2605+
std::optional<OutputType> parsed = ParseOutputType(args.GetArg("-changetype", ""));
26052606
if (!parsed) {
2606-
error = strprintf(_("Unknown change type '%s'"), gArgs.GetArg("-changetype", ""));
2607+
error = strprintf(_("Unknown change type '%s'"), args.GetArg("-changetype", ""));
26072608
return nullptr;
26082609
}
26092610
walletInstance->m_default_change_type = parsed.value();
26102611
}
26112612

2612-
if (gArgs.IsArgSet("-mintxfee")) {
2613-
std::optional<CAmount> min_tx_fee = ParseMoney(gArgs.GetArg("-mintxfee", ""));
2613+
if (args.IsArgSet("-mintxfee")) {
2614+
std::optional<CAmount> min_tx_fee = ParseMoney(args.GetArg("-mintxfee", ""));
26142615
if (!min_tx_fee || min_tx_fee.value() == 0) {
2615-
error = AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", ""));
2616+
error = AmountErrMsg("mintxfee", args.GetArg("-mintxfee", ""));
26162617
return nullptr;
26172618
} else if (min_tx_fee.value() > HIGH_TX_FEE_PER_KB) {
26182619
warnings.push_back(AmountHighWarn("-mintxfee") + Untranslated(" ") +
@@ -2622,8 +2623,8 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
26222623
walletInstance->m_min_fee = CFeeRate{min_tx_fee.value()};
26232624
}
26242625

2625-
if (gArgs.IsArgSet("-maxapsfee")) {
2626-
const std::string max_aps_fee{gArgs.GetArg("-maxapsfee", "")};
2626+
if (args.IsArgSet("-maxapsfee")) {
2627+
const std::string max_aps_fee{args.GetArg("-maxapsfee", "")};
26272628
if (max_aps_fee == "-1") {
26282629
walletInstance->m_max_aps_fee = -1;
26292630
} else if (std::optional<CAmount> max_fee = ParseMoney(max_aps_fee)) {
@@ -2638,10 +2639,10 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
26382639
}
26392640
}
26402641

2641-
if (gArgs.IsArgSet("-fallbackfee")) {
2642-
std::optional<CAmount> fallback_fee = ParseMoney(gArgs.GetArg("-fallbackfee", ""));
2642+
if (args.IsArgSet("-fallbackfee")) {
2643+
std::optional<CAmount> fallback_fee = ParseMoney(args.GetArg("-fallbackfee", ""));
26432644
if (!fallback_fee) {
2644-
error = strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), gArgs.GetArg("-fallbackfee", ""));
2645+
error = strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), args.GetArg("-fallbackfee", ""));
26452646
return nullptr;
26462647
} else if (fallback_fee.value() > HIGH_TX_FEE_PER_KB) {
26472648
warnings.push_back(AmountHighWarn("-fallbackfee") + Untranslated(" ") +
@@ -2653,10 +2654,10 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
26532654
// Disable fallback fee in case value was set to 0, enable if non-null value
26542655
walletInstance->m_allow_fallback_fee = walletInstance->m_fallback_fee.GetFeePerK() != 0;
26552656

2656-
if (gArgs.IsArgSet("-discardfee")) {
2657-
std::optional<CAmount> discard_fee = ParseMoney(gArgs.GetArg("-discardfee", ""));
2657+
if (args.IsArgSet("-discardfee")) {
2658+
std::optional<CAmount> discard_fee = ParseMoney(args.GetArg("-discardfee", ""));
26582659
if (!discard_fee) {
2659-
error = strprintf(_("Invalid amount for -discardfee=<amount>: '%s'"), gArgs.GetArg("-discardfee", ""));
2660+
error = strprintf(_("Invalid amount for -discardfee=<amount>: '%s'"), args.GetArg("-discardfee", ""));
26602661
return nullptr;
26612662
} else if (discard_fee.value() > HIGH_TX_FEE_PER_KB) {
26622663
warnings.push_back(AmountHighWarn("-discardfee") + Untranslated(" ") +
@@ -2665,10 +2666,10 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
26652666
walletInstance->m_discard_rate = CFeeRate{discard_fee.value()};
26662667
}
26672668

2668-
if (gArgs.IsArgSet("-paytxfee")) {
2669-
std::optional<CAmount> pay_tx_fee = ParseMoney(gArgs.GetArg("-paytxfee", ""));
2669+
if (args.IsArgSet("-paytxfee")) {
2670+
std::optional<CAmount> pay_tx_fee = ParseMoney(args.GetArg("-paytxfee", ""));
26702671
if (!pay_tx_fee) {
2671-
error = AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", ""));
2672+
error = AmountErrMsg("paytxfee", args.GetArg("-paytxfee", ""));
26722673
return nullptr;
26732674
} else if (pay_tx_fee.value() > HIGH_TX_FEE_PER_KB) {
26742675
warnings.push_back(AmountHighWarn("-paytxfee") + Untranslated(" ") +
@@ -2679,23 +2680,23 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
26792680

26802681
if (chain && walletInstance->m_pay_tx_fee < chain->relayMinFee()) {
26812682
error = strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
2682-
gArgs.GetArg("-paytxfee", ""), chain->relayMinFee().ToString());
2683+
args.GetArg("-paytxfee", ""), chain->relayMinFee().ToString());
26832684
return nullptr;
26842685
}
26852686
}
26862687

2687-
if (gArgs.IsArgSet("-maxtxfee")) {
2688-
std::optional<CAmount> max_fee = ParseMoney(gArgs.GetArg("-maxtxfee", ""));
2688+
if (args.IsArgSet("-maxtxfee")) {
2689+
std::optional<CAmount> max_fee = ParseMoney(args.GetArg("-maxtxfee", ""));
26892690
if (!max_fee) {
2690-
error = AmountErrMsg("maxtxfee", gArgs.GetArg("-maxtxfee", ""));
2691+
error = AmountErrMsg("maxtxfee", args.GetArg("-maxtxfee", ""));
26912692
return nullptr;
26922693
} else if (max_fee.value() > HIGH_MAX_TX_FEE) {
26932694
warnings.push_back(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
26942695
}
26952696

26962697
if (chain && CFeeRate{max_fee.value(), 1000} < chain->relayMinFee()) {
26972698
error = strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
2698-
gArgs.GetArg("-maxtxfee", ""), chain->relayMinFee().ToString());
2699+
args.GetArg("-maxtxfee", ""), chain->relayMinFee().ToString());
26992700
return nullptr;
27002701
}
27012702

@@ -2707,9 +2708,9 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
27072708
_("The wallet will avoid paying less than the minimum relay fee."));
27082709
}
27092710

2710-
walletInstance->m_confirm_target = gArgs.GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
2711-
walletInstance->m_spend_zero_conf_change = gArgs.GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
2712-
walletInstance->m_signal_rbf = gArgs.GetBoolArg("-walletrbf", DEFAULT_WALLET_RBF);
2711+
walletInstance->m_confirm_target = args.GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
2712+
walletInstance->m_spend_zero_conf_change = args.GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
2713+
walletInstance->m_signal_rbf = args.GetBoolArg("-walletrbf", DEFAULT_WALLET_RBF);
27132714

27142715
walletInstance->WalletLogPrintf("Wallet completed loading in %15dms\n", GetTimeMillis() - nStart);
27152716

@@ -2729,7 +2730,7 @@ std::shared_ptr<CWallet> CWallet::Create(WalletContext& context, const std::stri
27292730
}
27302731
}
27312732

2732-
walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
2733+
walletInstance->SetBroadcastTransactions(args.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
27332734

27342735
{
27352736
walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", walletInstance->GetKeyPoolSize());

0 commit comments

Comments
 (0)