Skip to content

Commit bf53ebe

Browse files
committed
test: add multiwallet tests for bitcoin-cli -generate
1 parent 4b859cf commit bf53ebe

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

test/functional/interface_bitcoin_cli.py

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
from decimal import Decimal
88
from test_framework.test_framework import BitcoinTestFramework
9-
from test_framework.util import assert_equal, assert_raises_process_error, get_auth_cookie
9+
from test_framework.util import (
10+
assert_equal,
11+
assert_raises_process_error,
12+
assert_raises_rpc_error,
13+
get_auth_cookie,
14+
)
1015

1116
# The block reward of coinbaseoutput.nValue (50) BTC/block matures after
1217
# COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
@@ -17,6 +22,8 @@
1722
JSON_PARSING_ERROR = 'error: Error parsing JSON:foo'
1823
BLOCKS_VALUE_OF_ZERO = 'error: the first argument (number of blocks to generate, default: 1) must be an integer value greater than zero'
1924
TOO_MANY_ARGS = 'error: too many arguments (maximum 2 for nblocks and maxtries)'
25+
WALLET_NOT_LOADED = 'Requested wallet does not exist or is not loaded'
26+
WALLET_NOT_SPECIFIED = 'Wallet file not specified'
2027

2128
class TestBitcoinCli(BitcoinTestFramework):
2229
def set_test_params(self):
@@ -88,6 +95,8 @@ def run_test(self):
8895
w1 = self.nodes[0].get_wallet_rpc(wallets[0])
8996
w2 = self.nodes[0].get_wallet_rpc(wallets[1])
9097
w3 = self.nodes[0].get_wallet_rpc(wallets[2])
98+
rpcwallet2 = '-rpcwallet={}'.format(wallets[1])
99+
rpcwallet3 = '-rpcwallet={}'.format(wallets[2])
91100
w1.walletpassphrase(password, self.rpc_timeout)
92101
w2.encryptwallet(password)
93102
w1.sendtoaddress(w2.getnewaddress(), amounts[1])
@@ -128,18 +137,18 @@ def run_test(self):
128137
assert_equal(cli_get_info['balance'], amounts[1])
129138

130139
self.log.info("Test -getinfo with -rpcwallet=remaining-non-default-wallet returns only its balance")
131-
cli_get_info = self.nodes[0].cli('-getinfo', '-rpcwallet={}'.format(wallets[1])).send_cli()
140+
cli_get_info = self.nodes[0].cli('-getinfo', rpcwallet2).send_cli()
132141
assert 'balances' not in cli_get_info.keys()
133142
assert_equal(cli_get_info['balance'], amounts[1])
134143

135144
self.log.info("Test -getinfo with -rpcwallet=unloaded wallet returns no balances")
136-
cli_get_info = self.nodes[0].cli('-getinfo', '-rpcwallet={}'.format(wallets[2])).send_cli()
145+
cli_get_info = self.nodes[0].cli('-getinfo', rpcwallet3).send_cli()
137146
assert 'balance' not in cli_get_info_keys
138147
assert 'balances' not in cli_get_info_keys
139148

140149
# Test bitcoin-cli -generate.
141150
n1 = 3
142-
n2 = 5
151+
n2 = 4
143152
w2.walletpassphrase(password, self.rpc_timeout)
144153
blocks = self.nodes[0].getblockcount()
145154

@@ -165,9 +174,56 @@ def run_test(self):
165174
assert_equal(set(generate.keys()), {'address', 'blocks'})
166175
assert_equal(len(generate["blocks"]), n2)
167176
assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n1 + n2)
177+
178+
self.log.info('Test -generate -rpcwallet in single-wallet mode')
179+
generate = self.nodes[0].cli(rpcwallet2, '-generate').send_cli()
180+
assert_equal(set(generate.keys()), {'address', 'blocks'})
181+
assert_equal(len(generate["blocks"]), 1)
182+
assert_equal(self.nodes[0].getblockcount(), blocks + 2 + n1 + n2)
183+
184+
self.log.info('Test -generate -rpcwallet=unloaded wallet raises RPC error')
185+
assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate').echo)
186+
assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate', 'foo').echo)
187+
assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate', 0).echo)
188+
assert_raises_rpc_error(-18, WALLET_NOT_LOADED, self.nodes[0].cli(rpcwallet3, '-generate', 1, 2, 3).echo)
189+
190+
# Test bitcoin-cli -generate with -rpcwallet in multiwallet mode.
191+
self.nodes[0].loadwallet(wallets[2])
192+
n3 = 4
193+
n4 = 10
194+
blocks = self.nodes[0].getblockcount()
195+
196+
self.log.info('Test -generate -rpcwallet with no args')
197+
generate = self.nodes[0].cli(rpcwallet2, '-generate').send_cli()
198+
assert_equal(set(generate.keys()), {'address', 'blocks'})
199+
assert_equal(len(generate["blocks"]), 1)
200+
assert_equal(self.nodes[0].getblockcount(), blocks + 1)
201+
202+
self.log.info('Test -generate -rpcwallet with bad args')
203+
assert_raises_process_error(1, JSON_PARSING_ERROR, self.nodes[0].cli(rpcwallet2, '-generate', 'foo').echo)
204+
assert_raises_process_error(1, BLOCKS_VALUE_OF_ZERO, self.nodes[0].cli(rpcwallet2, '-generate', 0).echo)
205+
assert_raises_process_error(1, TOO_MANY_ARGS, self.nodes[0].cli(rpcwallet2, '-generate', 1, 2, 3).echo)
206+
207+
self.log.info('Test -generate -rpcwallet with nblocks')
208+
generate = self.nodes[0].cli(rpcwallet2, '-generate', n3).send_cli()
209+
assert_equal(set(generate.keys()), {'address', 'blocks'})
210+
assert_equal(len(generate["blocks"]), n3)
211+
assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n3)
212+
213+
self.log.info('Test -generate -rpcwallet with nblocks and maxtries')
214+
generate = self.nodes[0].cli(rpcwallet2, '-generate', n4, 1000000).send_cli()
215+
assert_equal(set(generate.keys()), {'address', 'blocks'})
216+
assert_equal(len(generate["blocks"]), n4)
217+
assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n3 + n4)
218+
219+
self.log.info('Test -generate without -rpcwallet in multiwallet mode raises RPC error')
220+
assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate').echo)
221+
assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate', 'foo').echo)
222+
assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate', 0).echo)
223+
assert_raises_rpc_error(-19, WALLET_NOT_SPECIFIED, self.nodes[0].cli('-generate', 1, 2, 3).echo)
168224
else:
169225
self.log.info("*** Wallet not compiled; cli getwalletinfo and -getinfo wallet tests skipped")
170-
self.nodes[0].generate(10) # maintain block parity with the wallet_compiled conditional branch
226+
self.nodes[0].generate(25) # maintain block parity with the wallet_compiled conditional branch
171227

172228
self.log.info("Test -version with node stopped")
173229
self.stop_node(0)
@@ -179,7 +235,7 @@ def run_test(self):
179235
self.nodes[0].wait_for_cookie_credentials() # ensure cookie file is available to avoid race condition
180236
blocks = self.nodes[0].cli('-rpcwait').send_cli('getblockcount')
181237
self.nodes[0].wait_for_rpc_connection()
182-
assert_equal(blocks, BLOCKS + 10)
238+
assert_equal(blocks, BLOCKS + 25)
183239

184240

185241
if __name__ == '__main__':

0 commit comments

Comments
 (0)