6
6
7
7
from decimal import Decimal
8
8
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
+ )
10
15
11
16
# The block reward of coinbaseoutput.nValue (50) BTC/block matures after
12
17
# COINBASE_MATURITY (100) blocks. Therefore, after mining 101 blocks we expect
17
22
JSON_PARSING_ERROR = 'error: Error parsing JSON:foo'
18
23
BLOCKS_VALUE_OF_ZERO = 'error: the first argument (number of blocks to generate, default: 1) must be an integer value greater than zero'
19
24
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'
20
27
21
28
class TestBitcoinCli (BitcoinTestFramework ):
22
29
def set_test_params (self ):
@@ -88,6 +95,8 @@ def run_test(self):
88
95
w1 = self .nodes [0 ].get_wallet_rpc (wallets [0 ])
89
96
w2 = self .nodes [0 ].get_wallet_rpc (wallets [1 ])
90
97
w3 = self .nodes [0 ].get_wallet_rpc (wallets [2 ])
98
+ rpcwallet2 = '-rpcwallet={}' .format (wallets [1 ])
99
+ rpcwallet3 = '-rpcwallet={}' .format (wallets [2 ])
91
100
w1 .walletpassphrase (password , self .rpc_timeout )
92
101
w2 .encryptwallet (password )
93
102
w1 .sendtoaddress (w2 .getnewaddress (), amounts [1 ])
@@ -128,18 +137,18 @@ def run_test(self):
128
137
assert_equal (cli_get_info ['balance' ], amounts [1 ])
129
138
130
139
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 ()
132
141
assert 'balances' not in cli_get_info .keys ()
133
142
assert_equal (cli_get_info ['balance' ], amounts [1 ])
134
143
135
144
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 ()
137
146
assert 'balance' not in cli_get_info_keys
138
147
assert 'balances' not in cli_get_info_keys
139
148
140
149
# Test bitcoin-cli -generate.
141
150
n1 = 3
142
- n2 = 5
151
+ n2 = 4
143
152
w2 .walletpassphrase (password , self .rpc_timeout )
144
153
blocks = self .nodes [0 ].getblockcount ()
145
154
@@ -165,9 +174,56 @@ def run_test(self):
165
174
assert_equal (set (generate .keys ()), {'address' , 'blocks' })
166
175
assert_equal (len (generate ["blocks" ]), n2 )
167
176
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 )
168
224
else :
169
225
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
171
227
172
228
self .log .info ("Test -version with node stopped" )
173
229
self .stop_node (0 )
@@ -179,7 +235,7 @@ def run_test(self):
179
235
self .nodes [0 ].wait_for_cookie_credentials () # ensure cookie file is available to avoid race condition
180
236
blocks = self .nodes [0 ].cli ('-rpcwait' ).send_cli ('getblockcount' )
181
237
self .nodes [0 ].wait_for_rpc_connection ()
182
- assert_equal (blocks , BLOCKS + 10 )
238
+ assert_equal (blocks , BLOCKS + 25 )
183
239
184
240
185
241
if __name__ == '__main__' :
0 commit comments