Skip to content

Commit 18f9354

Browse files
committed
test: add tests for bitcoin-cli -generate
1 parent 4818124 commit 18f9354

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

test/functional/interface_bitcoin_cli.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test bitcoin-cli"""
6+
67
from decimal import Decimal
78
from test_framework.test_framework import BitcoinTestFramework
89
from test_framework.util import assert_equal, assert_raises_process_error, get_auth_cookie
@@ -13,6 +14,10 @@
1314
BLOCKS = 101
1415
BALANCE = (BLOCKS - 100) * 50
1516

17+
JSON_PARSING_ERROR = 'error: Error parsing JSON:foo'
18+
BLOCKS_VALUE_OF_ZERO = 'error: the first argument (number of blocks to generate, default: 1) must be an integer value greater than zero'
19+
TOO_MANY_ARGS = 'error: too many arguments (maximum 2 for nblocks and maxtries)'
20+
1621
class TestBitcoinCli(BitcoinTestFramework):
1722
def set_test_params(self):
1823
self.setup_clean_chain = True
@@ -75,7 +80,7 @@ def run_test(self):
7580
assert_equal(cli_get_info['relayfee'], network_info['relayfee'])
7681
assert_equal(self.nodes[0].cli.getwalletinfo(), wallet_info)
7782

78-
# Setup to test -getinfo and -rpcwallet= with multiple wallets.
83+
# Setup to test -getinfo, -generate, and -rpcwallet= with multiple wallets.
7984
wallets = ['', 'Encrypted', 'secret']
8085
amounts = [BALANCE + Decimal('9.999928'), Decimal(9), Decimal(31)]
8186
self.nodes[0].createwallet(wallet_name=wallets[1])
@@ -131,9 +136,38 @@ def run_test(self):
131136
cli_get_info = self.nodes[0].cli('-getinfo', '-rpcwallet={}'.format(wallets[2])).send_cli()
132137
assert 'balance' not in cli_get_info_keys
133138
assert 'balances' not in cli_get_info_keys
139+
140+
# Test bitcoin-cli -generate.
141+
n1 = 3
142+
n2 = 5
143+
w2.walletpassphrase(password, self.rpc_timeout)
144+
blocks = self.nodes[0].getblockcount()
145+
146+
self.log.info('Test -generate with no args')
147+
generate = self.nodes[0].cli('-generate').send_cli()
148+
assert_equal(set(generate.keys()), {'address', 'blocks'})
149+
assert_equal(len(generate["blocks"]), 1)
150+
assert_equal(self.nodes[0].getblockcount(), blocks + 1)
151+
152+
self.log.info('Test -generate with bad args')
153+
assert_raises_process_error(1, JSON_PARSING_ERROR, self.nodes[0].cli('-generate', 'foo').echo)
154+
assert_raises_process_error(1, BLOCKS_VALUE_OF_ZERO, self.nodes[0].cli('-generate', 0).echo)
155+
assert_raises_process_error(1, TOO_MANY_ARGS, self.nodes[0].cli('-generate', 1, 2, 3).echo)
156+
157+
self.log.info('Test -generate with nblocks')
158+
generate = self.nodes[0].cli('-generate', n1).send_cli()
159+
assert_equal(set(generate.keys()), {'address', 'blocks'})
160+
assert_equal(len(generate["blocks"]), n1)
161+
assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n1)
162+
163+
self.log.info('Test -generate with nblocks and maxtries')
164+
generate = self.nodes[0].cli('-generate', n2, 1000000).send_cli()
165+
assert_equal(set(generate.keys()), {'address', 'blocks'})
166+
assert_equal(len(generate["blocks"]), n2)
167+
assert_equal(self.nodes[0].getblockcount(), blocks + 1 + n1 + n2)
134168
else:
135169
self.log.info("*** Wallet not compiled; cli getwalletinfo and -getinfo wallet tests skipped")
136-
self.nodes[0].generate(1) # maintain block parity with the wallet_compiled conditional branch
170+
self.nodes[0].generate(10) # maintain block parity with the wallet_compiled conditional branch
137171

138172
self.log.info("Test -version with node stopped")
139173
self.stop_node(0)
@@ -145,7 +179,7 @@ def run_test(self):
145179
self.nodes[0].wait_for_cookie_credentials() # ensure cookie file is available to avoid race condition
146180
blocks = self.nodes[0].cli('-rpcwait').send_cli('getblockcount')
147181
self.nodes[0].wait_for_rpc_connection()
148-
assert_equal(blocks, BLOCKS + 1)
182+
assert_equal(blocks, BLOCKS + 10)
149183

150184

151185
if __name__ == '__main__':

0 commit comments

Comments
 (0)