3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test bitcoin-cli"""
6
+
6
7
from decimal import Decimal
7
8
from test_framework .test_framework import BitcoinTestFramework
8
9
from test_framework .util import assert_equal , assert_raises_process_error , get_auth_cookie
13
14
BLOCKS = 101
14
15
BALANCE = (BLOCKS - 100 ) * 50
15
16
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
+
16
21
class TestBitcoinCli (BitcoinTestFramework ):
17
22
def set_test_params (self ):
18
23
self .setup_clean_chain = True
@@ -75,7 +80,7 @@ def run_test(self):
75
80
assert_equal (cli_get_info ['relayfee' ], network_info ['relayfee' ])
76
81
assert_equal (self .nodes [0 ].cli .getwalletinfo (), wallet_info )
77
82
78
- # Setup to test -getinfo and -rpcwallet= with multiple wallets.
83
+ # Setup to test -getinfo, -generate, and -rpcwallet= with multiple wallets.
79
84
wallets = ['' , 'Encrypted' , 'secret' ]
80
85
amounts = [BALANCE + Decimal ('9.999928' ), Decimal (9 ), Decimal (31 )]
81
86
self .nodes [0 ].createwallet (wallet_name = wallets [1 ])
@@ -131,9 +136,38 @@ def run_test(self):
131
136
cli_get_info = self .nodes [0 ].cli ('-getinfo' , '-rpcwallet={}' .format (wallets [2 ])).send_cli ()
132
137
assert 'balance' not in cli_get_info_keys
133
138
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 )
134
168
else :
135
169
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
137
171
138
172
self .log .info ("Test -version with node stopped" )
139
173
self .stop_node (0 )
@@ -145,7 +179,7 @@ def run_test(self):
145
179
self .nodes [0 ].wait_for_cookie_credentials () # ensure cookie file is available to avoid race condition
146
180
blocks = self .nodes [0 ].cli ('-rpcwait' ).send_cli ('getblockcount' )
147
181
self .nodes [0 ].wait_for_rpc_connection ()
148
- assert_equal (blocks , BLOCKS + 1 )
182
+ assert_equal (blocks , BLOCKS + 10 )
149
183
150
184
151
185
if __name__ == '__main__' :
0 commit comments