19
19
from .authproxy import JSONRPCException
20
20
from .util import (
21
21
append_config ,
22
- assert_equal ,
23
22
delete_cookie_file ,
24
23
get_rpc_proxy ,
25
24
rpc_url ,
@@ -103,22 +102,30 @@ def __init__(self, i, datadir, rpchost, timewait, binary, stderr, mocktime, cove
103
102
104
103
self .p2ps = []
105
104
105
+ def _node_msg (self , msg : str ) -> str :
106
+ """Return a modified msg that identifies this node by its index as a debugging aid."""
107
+ return "[node %d] %s" % (self .index , msg )
108
+
109
+ def _raise_assertion_error (self , msg : str ):
110
+ """Raise an AssertionError with msg modified to identify this node."""
111
+ raise AssertionError (self ._node_msg (msg ))
112
+
106
113
def __del__ (self ):
107
114
# Ensure that we don't leave any bitcoind processes lying around after
108
115
# the test ends
109
116
if self .process and self .cleanup_on_exit :
110
117
# Should only happen on test failure
111
118
# Avoid using logger, as that may have already been shutdown when
112
119
# this destructor is called.
113
- print ("Cleaning up leftover process" )
120
+ print (self . _node_msg ( "Cleaning up leftover process" ) )
114
121
self .process .kill ()
115
122
116
123
def __getattr__ (self , name ):
117
124
"""Dispatches any unrecognised messages to the RPC connection or a CLI instance."""
118
125
if self .use_cli :
119
126
return getattr (self .cli , name )
120
127
else :
121
- assert self .rpc_connected and self .rpc is not None , "Error: no RPC connection"
128
+ assert self .rpc_connected and self .rpc is not None , self . _node_msg ( "Error: no RPC connection" )
122
129
return getattr (self .rpc , name )
123
130
124
131
def start (self , extra_args = None , stderr = None , * args , ** kwargs ):
@@ -141,7 +148,8 @@ def wait_for_rpc_connection(self):
141
148
poll_per_s = 4
142
149
for _ in range (poll_per_s * self .rpc_timeout ):
143
150
if self .process .poll () is not None :
144
- raise FailedToStartError ('bitcoind exited with status {} during initialization' .format (self .process .returncode ))
151
+ raise FailedToStartError (self ._node_msg (
152
+ 'bitcoind exited with status {} during initialization' .format (self .process .returncode )))
145
153
try :
146
154
self .rpc = get_rpc_proxy (rpc_url (self .datadir , self .index , self .rpchost ), self .index , timeout = self .rpc_timeout , coveragedir = self .coverage_dir )
147
155
self .rpc .getblockcount ()
@@ -160,14 +168,13 @@ def wait_for_rpc_connection(self):
160
168
if "No RPC credentials" not in str (e ):
161
169
raise
162
170
time .sleep (1.0 / poll_per_s )
163
- raise AssertionError ("Unable to connect to bitcoind" )
171
+ self . _raise_assertion_error ("Unable to connect to bitcoind" )
164
172
165
173
def get_wallet_rpc (self , wallet_name ):
166
174
if self .use_cli :
167
175
return self .cli ("-rpcwallet={}" .format (wallet_name ))
168
176
else :
169
- assert self .rpc_connected
170
- assert self .rpc
177
+ assert self .rpc_connected and self .rpc , self ._node_msg ("RPC not connected" )
171
178
wallet_path = "wallet/%s" % wallet_name
172
179
return self .rpc / wallet_path
173
180
@@ -194,7 +201,8 @@ def is_node_stopped(self):
194
201
return False
195
202
196
203
# process has stopped. Assert that it didn't return an error code.
197
- assert_equal (return_code , 0 )
204
+ assert return_code == 0 , self ._node_msg (
205
+ "Node returned non-zero exit code (%d) when stopping" % return_code )
198
206
self .running = False
199
207
self .process = None
200
208
self .rpc_connected = False
@@ -229,19 +237,22 @@ def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, mat
229
237
stderr = log_stderr .read ().decode ('utf-8' ).strip ()
230
238
if match == ErrorMatch .PARTIAL_REGEX :
231
239
if re .search (expected_msg , stderr , flags = re .MULTILINE ) is None :
232
- raise AssertionError ('Expected message "{}" does not partially match stderr:\n "{}"' .format (expected_msg , stderr ))
240
+ self ._raise_assertion_error (
241
+ 'Expected message "{}" does not partially match stderr:\n "{}"' .format (expected_msg , stderr ))
233
242
elif match == ErrorMatch .FULL_REGEX :
234
243
if re .fullmatch (expected_msg , stderr ) is None :
235
- raise AssertionError ('Expected message "{}" does not fully match stderr:\n "{}"' .format (expected_msg , stderr ))
244
+ self ._raise_assertion_error (
245
+ 'Expected message "{}" does not fully match stderr:\n "{}"' .format (expected_msg , stderr ))
236
246
elif match == ErrorMatch .FULL_TEXT :
237
247
if expected_msg != stderr :
238
- raise AssertionError ('Expected message "{}" does not fully match stderr:\n "{}"' .format (expected_msg , stderr ))
248
+ self ._raise_assertion_error (
249
+ 'Expected message "{}" does not fully match stderr:\n "{}"' .format (expected_msg , stderr ))
239
250
else :
240
251
if expected_msg is None :
241
252
assert_msg = "bitcoind should have exited with an error"
242
253
else :
243
254
assert_msg = "bitcoind should have exited with expected error " + expected_msg
244
- raise AssertionError (assert_msg )
255
+ self . _raise_assertion_error (assert_msg )
245
256
246
257
def node_encrypt_wallet (self , passphrase ):
247
258
""""Encrypts the wallet.
@@ -272,7 +283,7 @@ def p2p(self):
272
283
273
284
Convenience property - most tests only use a single p2p connection to each
274
285
node, so this saves having to write node.p2ps[0] many times."""
275
- assert self .p2ps , "No p2p connection"
286
+ assert self .p2ps , self . _node_msg ( "No p2p connection" )
276
287
return self .p2ps [0 ]
277
288
278
289
def disconnect_p2ps (self ):
0 commit comments