Skip to content

Commit 7a24cca

Browse files
author
MarcoFalke
committed
Merge #19100: refactor: Combine GetWalletForJSONRPCRequest and EnsureWalletIsAvailable functions
f42f5e5 refactor: Combine GetWalletForJSONRPCRequest and EnsureWalletIsAvailable functions (Russell Yanofsky) Pull request description: This simplifies control flow and also helps get rid of the ::vpwallets variable in #19101 since EnsureWalletIsAvailable doesn't have access to the request context. ACKs for top commit: MarcoFalke: ACK f42f5e5 (reviewed code to check that this is a refactor) 💢 promag: Tested ACK f42f5e5. Tree-SHA512: eb10685de3db3c1d10c3a797d8da5c8c731e4a8c9024bbb7245929ba767a77a52783a739b8cb1fa7af6fcd233dcf9c8ebbe414eb8b902e2542601aac18625997
2 parents 45a6811 + f42f5e5 commit 7a24cca

File tree

3 files changed

+58
-212
lines changed

3 files changed

+58
-212
lines changed

src/wallet/rpcdump.cpp

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,8 @@ static void RescanWallet(CWallet& wallet, const WalletRescanReserver& reserver,
9393
UniValue importprivkey(const JSONRPCRequest& request)
9494
{
9595
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
96+
if (!wallet) return NullUniValue;
9697
CWallet* const pwallet = wallet.get();
97-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
98-
return NullUniValue;
99-
}
10098

10199
RPCHelpMan{"importprivkey",
102100
"\nAdds a private key (as returned by dumpprivkey) to your wallet. Requires a new wallet backup.\n"
@@ -196,10 +194,8 @@ UniValue importprivkey(const JSONRPCRequest& request)
196194
UniValue abortrescan(const JSONRPCRequest& request)
197195
{
198196
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
197+
if (!wallet) return NullUniValue;
199198
CWallet* const pwallet = wallet.get();
200-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
201-
return NullUniValue;
202-
}
203199

204200
RPCHelpMan{"abortrescan",
205201
"\nStops current wallet rescan triggered by an RPC call, e.g. by an importprivkey call.\n"
@@ -224,10 +220,8 @@ UniValue abortrescan(const JSONRPCRequest& request)
224220
UniValue importaddress(const JSONRPCRequest& request)
225221
{
226222
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
223+
if (!wallet) return NullUniValue;
227224
CWallet* const pwallet = wallet.get();
228-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
229-
return NullUniValue;
230-
}
231225

232226
RPCHelpMan{"importaddress",
233227
"\nAdds an address or script (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
@@ -326,10 +320,8 @@ UniValue importaddress(const JSONRPCRequest& request)
326320
UniValue importprunedfunds(const JSONRPCRequest& request)
327321
{
328322
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
323+
if (!wallet) return NullUniValue;
329324
CWallet* const pwallet = wallet.get();
330-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
331-
return NullUniValue;
332-
}
333325

334326
RPCHelpMan{"importprunedfunds",
335327
"\nImports funds without rescan. Corresponding address or script must previously be included in wallet. Aimed towards pruned wallets. The end-user is responsible to import additional transactions that subsequently spend the imported outputs or rescan after the point in the blockchain the transaction is included.\n",
@@ -384,10 +376,8 @@ UniValue importprunedfunds(const JSONRPCRequest& request)
384376
UniValue removeprunedfunds(const JSONRPCRequest& request)
385377
{
386378
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
379+
if (!wallet) return NullUniValue;
387380
CWallet* const pwallet = wallet.get();
388-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
389-
return NullUniValue;
390-
}
391381

392382
RPCHelpMan{"removeprunedfunds",
393383
"\nDeletes the specified transaction from the wallet. Meant for use with pruned wallets and as a companion to importprunedfunds. This will affect wallet balances.\n",
@@ -423,10 +413,8 @@ UniValue removeprunedfunds(const JSONRPCRequest& request)
423413
UniValue importpubkey(const JSONRPCRequest& request)
424414
{
425415
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
416+
if (!wallet) return NullUniValue;
426417
CWallet* const pwallet = wallet.get();
427-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
428-
return NullUniValue;
429-
}
430418

431419
RPCHelpMan{"importpubkey",
432420
"\nAdds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.\n"
@@ -510,10 +498,8 @@ UniValue importpubkey(const JSONRPCRequest& request)
510498
UniValue importwallet(const JSONRPCRequest& request)
511499
{
512500
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
501+
if (!wallet) return NullUniValue;
513502
CWallet* const pwallet = wallet.get();
514-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
515-
return NullUniValue;
516-
}
517503

518504
RPCHelpMan{"importwallet",
519505
"\nImports keys from a wallet dump file (see dumpwallet). Requires a new wallet backup to include imported keys.\n"
@@ -668,10 +654,8 @@ UniValue importwallet(const JSONRPCRequest& request)
668654
UniValue dumpprivkey(const JSONRPCRequest& request)
669655
{
670656
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
657+
if (!wallet) return NullUniValue;
671658
const CWallet* const pwallet = wallet.get();
672-
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
673-
return NullUniValue;
674-
}
675659

676660
RPCHelpMan{"dumpprivkey",
677661
"\nReveals the private key corresponding to 'address'.\n"
@@ -715,9 +699,7 @@ UniValue dumpprivkey(const JSONRPCRequest& request)
715699
UniValue dumpwallet(const JSONRPCRequest& request)
716700
{
717701
std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
718-
if (!EnsureWalletIsAvailable(pwallet.get(), request.fHelp)) {
719-
return NullUniValue;
720-
}
702+
if (!pwallet) return NullUniValue;
721703

722704
RPCHelpMan{"dumpwallet",
723705
"\nDumps all wallet keys in a human-readable format to a server-side file. This does not allow overwriting existing files.\n"
@@ -1260,10 +1242,8 @@ static int64_t GetImportTimestamp(const UniValue& data, int64_t now)
12601242
UniValue importmulti(const JSONRPCRequest& mainRequest)
12611243
{
12621244
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(mainRequest);
1245+
if (!wallet) return NullUniValue;
12631246
CWallet* const pwallet = wallet.get();
1264-
if (!EnsureWalletIsAvailable(pwallet, mainRequest.fHelp)) {
1265-
return NullUniValue;
1266-
}
12671247

12681248
RPCHelpMan{"importmulti",
12691249
"\nImport addresses/scripts (with private or public keys, redeem script (P2SH)), optionally rescanning the blockchain from the earliest creation time of the imported scripts. Requires a new wallet backup.\n"
@@ -1588,10 +1568,8 @@ static UniValue ProcessDescriptorImport(CWallet * const pwallet, const UniValue&
15881568
UniValue importdescriptors(const JSONRPCRequest& main_request) {
15891569
// Acquire the wallet
15901570
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(main_request);
1571+
if (!wallet) return NullUniValue;
15911572
CWallet* const pwallet = wallet.get();
1592-
if (!EnsureWalletIsAvailable(pwallet, main_request.fHelp)) {
1593-
return NullUniValue;
1594-
}
15951573

15961574
RPCHelpMan{"importdescriptors",
15971575
"\nImport descriptors. This will trigger a rescan of the blockchain based on the earliest timestamp of all descriptors being imported. Requires a new wallet backup.\n"

0 commit comments

Comments
 (0)