Skip to content

Commit 420238d

Browse files
committed
Merge #10604: [wallet] [tests] Add listwallets RPC, include wallet name in getwalletinfo and add multiwallet test
3707fcd [wallet] [tests] Add listwallets to multiwallet test (John Newbery) 9508761 [wallet] [rpc] Add listwallets RPC (John Newbery) 4a05715 [wallet] [rpc] print wallet name in getwalletinfo (John Newbery) 09eacee [wallet] fix comment for CWallet::Verify() (John Newbery) Pull request description: - fix comment for CWallet::Verify (cleanup after #8694) - expose the wallet name in `getwalletinfo` rpc - add `listwallets` rpc - returns array of wallet names - add functional test for multiwallet using new rpc functionality Tree-SHA512: 52f864726bf8a28421d4f3604a6cb95fffb3f4e19edbce18efaef06142c48dd4adb9e7a65a10de2955c80f13c00803ce27c78ccbc8434d92ef12cd36c4ccb4aa
2 parents 6adc3a3 + 3707fcd commit 420238d

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,6 +2447,7 @@ UniValue getwalletinfo(const JSONRPCRequest& request)
24472447
"Returns an object containing various wallet state info.\n"
24482448
"\nResult:\n"
24492449
"{\n"
2450+
" \"walletname\": xxxxx, (string) the wallet name\n"
24502451
" \"walletversion\": xxxxx, (numeric) the wallet version\n"
24512452
" \"balance\": xxxxxxx, (numeric) the total confirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
24522453
" \"unconfirmed_balance\": xxx, (numeric) the total unconfirmed balance of the wallet in " + CURRENCY_UNIT + "\n"
@@ -2469,6 +2470,7 @@ UniValue getwalletinfo(const JSONRPCRequest& request)
24692470
UniValue obj(UniValue::VOBJ);
24702471

24712472
size_t kpExternalSize = pwallet->KeypoolCountExternalKeys();
2473+
obj.push_back(Pair("walletname", pwallet->GetName()));
24722474
obj.push_back(Pair("walletversion", pwallet->GetVersion()));
24732475
obj.push_back(Pair("balance", ValueFromAmount(pwallet->GetBalance())));
24742476
obj.push_back(Pair("unconfirmed_balance", ValueFromAmount(pwallet->GetUnconfirmedBalance())));
@@ -2489,6 +2491,39 @@ UniValue getwalletinfo(const JSONRPCRequest& request)
24892491
return obj;
24902492
}
24912493

2494+
UniValue listwallets(const JSONRPCRequest& request)
2495+
{
2496+
if (request.fHelp || request.params.size() != 0)
2497+
throw std::runtime_error(
2498+
"listwallets\n"
2499+
"Returns a list of currently loaded wallets.\n"
2500+
"For full information on the wallet, use \"getwalletinfo\"\n"
2501+
"\nResult:\n"
2502+
"[ (json array of strings)\n"
2503+
" \"walletname\" (string) the wallet name\n"
2504+
" ...\n"
2505+
"]\n"
2506+
"\nExamples:\n"
2507+
+ HelpExampleCli("listwallets", "")
2508+
+ HelpExampleRpc("listwallets", "")
2509+
);
2510+
2511+
UniValue obj(UniValue::VARR);
2512+
2513+
for (CWalletRef pwallet : vpwallets) {
2514+
2515+
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2516+
return NullUniValue;
2517+
}
2518+
2519+
LOCK(pwallet->cs_wallet);
2520+
2521+
obj.push_back(pwallet->GetName());
2522+
}
2523+
2524+
return obj;
2525+
}
2526+
24922527
UniValue resendwallettransactions(const JSONRPCRequest& request)
24932528
{
24942529
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
@@ -3085,6 +3120,7 @@ static const CRPCCommand commands[] =
30853120
{ "wallet", "listsinceblock", &listsinceblock, false, {"blockhash","target_confirmations","include_watchonly"} },
30863121
{ "wallet", "listtransactions", &listtransactions, false, {"account","count","skip","include_watchonly"} },
30873122
{ "wallet", "listunspent", &listunspent, false, {"minconf","maxconf","addresses","include_unsafe","query_options"} },
3123+
{ "wallet", "listwallets", &listwallets, true, {} },
30883124
{ "wallet", "lockunspent", &lockunspent, true, {"unlock","transactions"} },
30893125
{ "wallet", "move", &movecmd, false, {"fromaccount","toaccount","amount","minconf","comment"} },
30903126
{ "wallet", "sendfrom", &sendfrom, false, {"fromaccount","toaddress","amount","minconf","comment","comment_to"} },

src/wallet/wallet.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,9 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10661066
//! Flush wallet (bitdb flush)
10671067
void Flush(bool shutdown=false);
10681068

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

10721074
/**

test/functional/multiwallet.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
# Copyright (c) 2017 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5-
"""Test multiwallet."""
5+
"""Test multiwallet.
6+
7+
Verify that a bitcoind node can load multiple wallet files
8+
"""
69
from test_framework.test_framework import BitcoinTestFramework
7-
from test_framework.util import *
10+
from test_framework.util import assert_equal, assert_raises_jsonrpc
811

912
class MultiWalletTest(BitcoinTestFramework):
1013

@@ -18,20 +21,28 @@ def run_test(self):
1821
w1 = self.nodes[0] / "wallet/w1"
1922
w1.generate(1)
2023

21-
#accessing wallet RPC without using wallet endpoint fails
24+
# accessing wallet RPC without using wallet endpoint fails
2225
assert_raises_jsonrpc(-32601, "Method not found", self.nodes[0].getwalletinfo)
2326

24-
#check w1 wallet balance
25-
walletinfo = w1.getwalletinfo()
26-
assert_equal(walletinfo['immature_balance'], 50)
27+
# check w1 wallet balance
28+
w1_info = w1.getwalletinfo()
29+
assert_equal(w1_info['immature_balance'], 50)
30+
w1_name = w1_info['walletname']
31+
assert_equal(w1_name, "w1")
2732

28-
#check w1 wallet balance
33+
# check w1 wallet balance
2934
w2 = self.nodes[0] / "wallet/w2"
30-
walletinfo = w2.getwalletinfo()
31-
assert_equal(walletinfo['immature_balance'], 0)
35+
w2_info = w2.getwalletinfo()
36+
assert_equal(w2_info['immature_balance'], 0)
37+
w2_name = w2_info['walletname']
38+
assert_equal(w2_name, "w2")
3239

3340
w3 = self.nodes[0] / "wallet/w3"
34-
41+
w3_name = w3.getwalletinfo()['walletname']
42+
assert_equal(w3_name, "w3")
43+
44+
assert_equal({"w1", "w2", "w3"}, {w1_name, w2_name, w3_name})
45+
3546
w1.generate(101)
3647
assert_equal(w1.getbalance(), 100)
3748
assert_equal(w2.getbalance(), 0)

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'mempool_spendcoinbase.py',
9090
'mempool_reorg.py',
9191
'mempool_persist.py',
92+
'multiwallet.py',
9293
'httpbasics.py',
9394
'multi_rpc.py',
9495
'proxy_test.py',

0 commit comments

Comments
 (0)