Skip to content

Commit 6ab96ec

Browse files
author
MarcoFalke
committed
Merge #18574: cli: call getbalances.ismine.trusted instead of getwalletinfo.balance
5df0877 test: update and harden interface_bitcoin_cli tests (Jon Atack) 7501977 cli -getinfo: use getbalances instead of deprecated getwalletinfo balance (Jon Atack) Pull request description: Extracted from #18453 to preserve that PR as a discussion on multiwallet RPC/CLI. This PR updates `bitcoin-cli -getinfo` to fetch the wallet balance from `getbalances` in order to no longer depend on `getwalletinfo.balance` which was deprecated a year ago in facfb41. I found this when removing the getwalletinfo() `balance`, `unconfirmed_balance`, and `immature_balance` fields to see what broke from depending on them. I didn't see any perceivable change in `-getinfo` run time from the change. Test coverage for this change is provided by `test/functional/interface_bitcoin_cli.py`, which the second commit updates to (a) no longer depend on getwalletinfo.balances and (b) test the -getinfo blockcount and balance fields against non-default, non-zero values. ACKs for top commit: robot-visions: ACK 5df0877 MarcoFalke: ACK 5df0877 vasild: re-ACK 5df0877 promag: Code review ACK 5df0877. theStack: ACK bitcoin/bitcoin@5df0877 Tree-SHA512: 0dd8c62f915b1c0112e42b132dcf74a141bdd1f51e7c17d4a698b374ec296f4f9836f7058dbe237cf24f9bfb32ea5000e14f7089e2e86472d9c6a175be26e910
2 parents d84c9aa + 5df0877 commit 6ab96ec

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/bitcoin-cli.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class GetinfoRequestHandler: public BaseRequestHandler
228228
const int ID_NETWORKINFO = 0;
229229
const int ID_BLOCKCHAININFO = 1;
230230
const int ID_WALLETINFO = 2;
231+
const int ID_BALANCES = 3;
231232

232233
/** Create a simulated `getinfo` request. */
233234
UniValue PrepareRequest(const std::string& method, const std::vector<std::string>& args) override
@@ -239,16 +240,17 @@ class GetinfoRequestHandler: public BaseRequestHandler
239240
result.push_back(JSONRPCRequestObj("getnetworkinfo", NullUniValue, ID_NETWORKINFO));
240241
result.push_back(JSONRPCRequestObj("getblockchaininfo", NullUniValue, ID_BLOCKCHAININFO));
241242
result.push_back(JSONRPCRequestObj("getwalletinfo", NullUniValue, ID_WALLETINFO));
243+
result.push_back(JSONRPCRequestObj("getbalances", NullUniValue, ID_BALANCES));
242244
return result;
243245
}
244246

245247
/** Collect values from the batch and form a simulated `getinfo` reply. */
246248
UniValue ProcessReply(const UniValue &batch_in) override
247249
{
248250
UniValue result(UniValue::VOBJ);
249-
std::vector<UniValue> batch = JSONRPCProcessBatchReply(batch_in, 3);
250-
// Errors in getnetworkinfo() and getblockchaininfo() are fatal, pass them on
251-
// getwalletinfo() is allowed to fail in case there is no wallet.
251+
std::vector<UniValue> batch = JSONRPCProcessBatchReply(batch_in, batch_in.size());
252+
// Errors in getnetworkinfo() and getblockchaininfo() are fatal, pass them on;
253+
// getwalletinfo() and getbalances() are allowed to fail if there is no wallet.
252254
if (!batch[ID_NETWORKINFO]["error"].isNull()) {
253255
return batch[ID_NETWORKINFO];
254256
}
@@ -265,13 +267,15 @@ class GetinfoRequestHandler: public BaseRequestHandler
265267
result.pushKV("difficulty", batch[ID_BLOCKCHAININFO]["result"]["difficulty"]);
266268
result.pushKV("chain", UniValue(batch[ID_BLOCKCHAININFO]["result"]["chain"]));
267269
if (!batch[ID_WALLETINFO]["result"].isNull()) {
268-
result.pushKV("balance", batch[ID_WALLETINFO]["result"]["balance"]);
269270
result.pushKV("keypoolsize", batch[ID_WALLETINFO]["result"]["keypoolsize"]);
270271
if (!batch[ID_WALLETINFO]["result"]["unlocked_until"].isNull()) {
271272
result.pushKV("unlocked_until", batch[ID_WALLETINFO]["result"]["unlocked_until"]);
272273
}
273274
result.pushKV("paytxfee", batch[ID_WALLETINFO]["result"]["paytxfee"]);
274275
}
276+
if (!batch[ID_BALANCES]["result"].isNull()) {
277+
result.pushKV("balance", batch[ID_BALANCES]["result"]["mine"]["trusted"]);
278+
}
275279
result.pushKV("relayfee", batch[ID_NETWORKINFO]["result"]["relayfee"]);
276280
result.pushKV("warnings", batch[ID_NETWORKINFO]["result"]["warnings"]);
277281
return JSONRPCReplyObj(result, NullUniValue, 1);

test/functional/interface_bitcoin_cli.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
from test_framework.test_framework import BitcoinTestFramework
77
from test_framework.util import assert_equal, assert_raises_process_error, get_auth_cookie
88

9+
# The block reward of coinbaseoutput.nValue (50) BTC/block matures after
10+
# COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
11+
# node 0 to have a balance of (BLOCKS - COINBASE_MATURITY) * 50 BTC/block.
12+
BLOCKS = 101
13+
BALANCE = (BLOCKS - 100) * 50
14+
915
class TestBitcoinCli(BitcoinTestFramework):
1016

1117
def set_test_params(self):
@@ -17,6 +23,7 @@ def skip_test_if_missing_module(self):
1723

1824
def run_test(self):
1925
"""Main test logic"""
26+
self.nodes[0].generate(BLOCKS)
2027

2128
cli_response = self.nodes[0].cli("-version").send_cli()
2229
assert "{} RPC client version".format(self.config['environment']['PACKAGE_NAME']) in cli_response
@@ -35,7 +42,7 @@ def run_test(self):
3542
user, password = get_auth_cookie(self.nodes[0].datadir, self.chain)
3643

3744
self.log.info("Test -stdinrpcpass option")
38-
assert_equal(0, self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input=password).getblockcount())
45+
assert_equal(BLOCKS, self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input=password).getblockcount())
3946
assert_raises_process_error(1, "Incorrect rpcuser or rpcpassword", self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input="foo").echo)
4047

4148
self.log.info("Test -stdin and -stdinrpcpass")
@@ -51,10 +58,8 @@ def run_test(self):
5158
self.log.info("Make sure that -getinfo with arguments fails")
5259
assert_raises_process_error(1, "-getinfo takes no arguments", self.nodes[0].cli('-getinfo').help)
5360

54-
self.log.info("Compare responses from `bitcoin-cli -getinfo` and the RPCs data is retrieved from.")
61+
self.log.info("Test that -getinfo returns the expected network and blockchain info")
5562
cli_get_info = self.nodes[0].cli('-getinfo').send_cli()
56-
if self.is_wallet_compiled():
57-
wallet_info = self.nodes[0].getwalletinfo()
5863
network_info = self.nodes[0].getnetworkinfo()
5964
blockchain_info = self.nodes[0].getblockchaininfo()
6065

@@ -66,11 +71,15 @@ def run_test(self):
6671
assert_equal(cli_get_info['difficulty'], blockchain_info['difficulty'])
6772
assert_equal(cli_get_info['chain'], blockchain_info['chain'])
6873
if self.is_wallet_compiled():
69-
assert_equal(cli_get_info['balance'], wallet_info['balance'])
74+
self.log.info("Test that -getinfo returns the expected wallet info")
75+
assert_equal(cli_get_info['balance'], BALANCE)
76+
wallet_info = self.nodes[0].getwalletinfo()
7077
assert_equal(cli_get_info['keypoolsize'], wallet_info['keypoolsize'])
7178
assert_equal(cli_get_info['paytxfee'], wallet_info['paytxfee'])
7279
assert_equal(cli_get_info['relayfee'], network_info['relayfee'])
7380
# unlocked_until is not tested because the wallet is not encrypted
81+
else:
82+
self.log.info("*** Wallet not compiled; -getinfo wallet tests skipped")
7483

7584

7685
if __name__ == '__main__':

0 commit comments

Comments
 (0)