Skip to content

Commit fa3e9f7

Browse files
author
MarcoFalke
committed
qa: Add TestNode::assert_debug_log
1 parent 17d6449 commit fa3e9f7

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

test/functional/p2p_invalid_tx.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,16 @@ def run_test(self):
136136

137137
# restart node with sending BIP61 messages disabled, check that it disconnects without sending the reject message
138138
self.log.info('Test a transaction that is rejected, with BIP61 disabled')
139-
self.restart_node(0, ['-enablebip61=0','-persistmempool=0'])
139+
self.restart_node(0, ['-enablebip61=0', '-persistmempool=0'])
140140
self.reconnect_p2p(num_connections=1)
141-
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)
141+
with node.assert_debug_log(expected_msgs=[
142+
"{} from peer=0 was not accepted: mandatory-script-verify-flag-failed (Invalid OP_IF construction) (code 16)".format(tx1.hash),
143+
"disconnecting peer=0",
144+
]):
145+
node.p2p.send_txs_and_test([tx1], node, success=False, expect_disconnect=True)
142146
# send_txs_and_test will have waited for disconnect, so we can safely check that no reject has been received
143147
assert_equal(node.p2p.reject_code_received, None)
144148

149+
145150
if __name__ == '__main__':
146151
InvalidTxRequestTest().main()

test/functional/test_framework/test_node.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Class for bitcoind node under test"""
66

7+
import contextlib
78
import decimal
89
import errno
910
from enum import Enum
@@ -229,6 +230,23 @@ def is_node_stopped(self):
229230
def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
230231
wait_until(self.is_node_stopped, timeout=timeout)
231232

233+
@contextlib.contextmanager
234+
def assert_debug_log(self, expected_msgs):
235+
debug_log = os.path.join(self.datadir, 'regtest', 'debug.log')
236+
with open(debug_log, encoding='utf-8') as dl:
237+
dl.seek(0, 2)
238+
prev_size = dl.tell()
239+
try:
240+
yield
241+
finally:
242+
with open(debug_log, encoding='utf-8') as dl:
243+
dl.seek(prev_size)
244+
log = dl.read()
245+
print_log = " - " + "\n - ".join(log.splitlines())
246+
for expected_msg in expected_msgs:
247+
if re.search(re.escape(expected_msg), log, flags=re.MULTILINE) is None:
248+
self._raise_assertion_error('Expected message "{}" does not partially match log:\n\n{}\n\n'.format(expected_msg, print_log))
249+
232250
def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, match=ErrorMatch.FULL_TEXT, *args, **kwargs):
233251
"""Attempt to start the node and expect it to raise an error.
234252

0 commit comments

Comments
 (0)