Skip to content

Commit cb25cd6

Browse files
committed
Merge #14119: qa: Read reject reasons from debug log, not p2p messages
fac3e22 qa: Read reject reasons from debug log, not p2p messages (MarcoFalke) Pull request description: For local testing we don't need to rely on p2p messages just to assert a reject reason. Replace reading p2p messages with reading from the debug log file. Tree-SHA512: fa59598ecf5e00cfb420ef1892d90aa415501fd882e1c608894dc577b0d00e93a442326d3a9167fef77d26aafbe345b730b49109982ccad68a5942384564a90b
2 parents eea87ef + fac3e22 commit cb25cd6

File tree

8 files changed

+138
-191
lines changed

8 files changed

+138
-191
lines changed

test/functional/feature_block.py

Lines changed: 40 additions & 40 deletions
Large diffs are not rendered by default.

test/functional/feature_cltv.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010

1111
from test_framework.blocktools import create_coinbase, create_block, create_transaction
1212
from test_framework.messages import CTransaction, msg_block, ToHex
13-
from test_framework.mininode import mininode_lock, P2PInterface
13+
from test_framework.mininode import P2PInterface
1414
from test_framework.script import CScript, OP_1NEGATE, OP_CHECKLOCKTIMEVERIFY, OP_DROP, CScriptNum
1515
from test_framework.test_framework import BitcoinTestFramework
16-
from test_framework.util import assert_equal, bytes_to_hex_str, hex_str_to_bytes, wait_until
16+
from test_framework.util import (
17+
assert_equal,
18+
bytes_to_hex_str,
19+
hex_str_to_bytes,
20+
)
1721

1822
from io import BytesIO
1923

@@ -51,10 +55,11 @@ def cltv_validate(node, tx, height):
5155
list(CScript(new_tx.vin[0].scriptSig)))
5256
return new_tx
5357

58+
5459
class BIP65Test(BitcoinTestFramework):
5560
def set_test_params(self):
5661
self.num_nodes = 1
57-
self.extra_args = [['-whitelist=127.0.0.1']]
62+
self.extra_args = [['-whitelist=127.0.0.1', '-par=1']] # Use only one script thread to get the exact reject reason for testing
5863
self.setup_clean_chain = True
5964

6065
def run_test(self):
@@ -88,15 +93,11 @@ def run_test(self):
8893
block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time)
8994
block.nVersion = 3
9095
block.solve()
91-
self.nodes[0].p2p.send_and_ping(msg_block(block))
92-
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
9396

94-
wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock)
95-
with mininode_lock:
96-
assert_equal(self.nodes[0].p2p.last_message["reject"].code, REJECT_OBSOLETE)
97-
assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'bad-version(0x00000003)')
98-
assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256)
99-
del self.nodes[0].p2p.last_message["reject"]
97+
with self.nodes[0].assert_debug_log(expected_msgs=['{}, bad-version(0x00000003)'.format(block.hash)]):
98+
self.nodes[0].p2p.send_and_ping(msg_block(block))
99+
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
100+
self.nodes[0].p2p.sync_with_ping()
100101

101102
self.log.info("Test that invalid-according-to-cltv transactions cannot appear in a block")
102103
block.nVersion = 4
@@ -118,18 +119,10 @@ def run_test(self):
118119
block.hashMerkleRoot = block.calc_merkle_root()
119120
block.solve()
120121

121-
self.nodes[0].p2p.send_and_ping(msg_block(block))
122-
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
123-
124-
wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock)
125-
with mininode_lock:
126-
assert self.nodes[0].p2p.last_message["reject"].code in [REJECT_INVALID, REJECT_NONSTANDARD]
127-
assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256)
128-
if self.nodes[0].p2p.last_message["reject"].code == REJECT_INVALID:
129-
# Generic rejection when a block is invalid
130-
assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'block-validation-failed')
131-
else:
132-
assert b'Negative locktime' in self.nodes[0].p2p.last_message["reject"].reason
122+
with self.nodes[0].assert_debug_log(expected_msgs=['CheckInputs on {} failed with non-mandatory-script-verify-flag (Negative locktime)'.format(block.vtx[-1].hash)]):
123+
self.nodes[0].p2p.send_and_ping(msg_block(block))
124+
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
125+
self.nodes[0].p2p.sync_with_ping()
133126

134127
self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted")
135128
spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1)

test/functional/feature_csv_activation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ def create_test_block(self, txs, version=536870912):
165165
block.solve()
166166
return block
167167

168-
def sync_blocks(self, blocks, success=True, reject_code=None, reject_reason=None, request_block=True):
168+
def sync_blocks(self, blocks, success=True):
169169
"""Sends blocks to test node. Syncs and verifies that tip has advanced to most recent block.
170170
171171
Call with success = False if the tip shouldn't advance to the most recent block."""
172-
self.nodes[0].p2p.send_blocks_and_test(blocks, self.nodes[0], success=success, reject_code=reject_code, reject_reason=reject_reason, request_block=request_block)
172+
self.nodes[0].p2p.send_blocks_and_test(blocks, self.nodes[0], success=success)
173173

174174
def run_test(self):
175175
self.nodes[0].add_p2p_connection(P2PDataStore())

test/functional/feature_dersig.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
from test_framework.mininode import mininode_lock, P2PInterface
1313
from test_framework.script import CScript
1414
from test_framework.test_framework import BitcoinTestFramework
15-
from test_framework.util import assert_equal, bytes_to_hex_str, wait_until
15+
from test_framework.util import (
16+
assert_equal,
17+
bytes_to_hex_str,
18+
wait_until,
19+
)
1620

1721
DERSIG_HEIGHT = 1251
1822

@@ -42,7 +46,7 @@ def unDERify(tx):
4246
class BIP66Test(BitcoinTestFramework):
4347
def set_test_params(self):
4448
self.num_nodes = 1
45-
self.extra_args = [['-whitelist=127.0.0.1']]
49+
self.extra_args = [['-whitelist=127.0.0.1', '-par=1', '-enablebip61']] # Use only one script thread to get the exact reject reason for testing
4650
self.setup_clean_chain = True
4751

4852
def run_test(self):
@@ -78,15 +82,11 @@ def run_test(self):
7882
block.nVersion = 2
7983
block.rehash()
8084
block.solve()
81-
self.nodes[0].p2p.send_and_ping(msg_block(block))
82-
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
8385

84-
wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock)
85-
with mininode_lock:
86-
assert_equal(self.nodes[0].p2p.last_message["reject"].code, REJECT_OBSOLETE)
87-
assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'bad-version(0x00000002)')
88-
assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256)
89-
del self.nodes[0].p2p.last_message["reject"]
86+
with self.nodes[0].assert_debug_log(expected_msgs=['{}, bad-version(0x00000002)'.format(block.hash)]):
87+
self.nodes[0].p2p.send_and_ping(msg_block(block))
88+
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
89+
self.nodes[0].p2p.sync_with_ping()
9090

9191
self.log.info("Test that transactions with non-DER signatures cannot appear in a block")
9292
block.nVersion = 3
@@ -109,23 +109,16 @@ def run_test(self):
109109
block.rehash()
110110
block.solve()
111111

112-
self.nodes[0].p2p.send_and_ping(msg_block(block))
113-
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
112+
with self.nodes[0].assert_debug_log(expected_msgs=['CheckInputs on {} failed with non-mandatory-script-verify-flag (Non-canonical DER signature)'.format(block.vtx[-1].hash)]):
113+
self.nodes[0].p2p.send_and_ping(msg_block(block))
114+
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
115+
self.nodes[0].p2p.sync_with_ping()
114116

115117
wait_until(lambda: "reject" in self.nodes[0].p2p.last_message.keys(), lock=mininode_lock)
116118
with mininode_lock:
117-
# We can receive different reject messages depending on whether
118-
# bitcoind is running with multiple script check threads. If script
119-
# check threads are not in use, then transaction script validation
120-
# happens sequentially, and bitcoind produces more specific reject
121-
# reasons.
122119
assert self.nodes[0].p2p.last_message["reject"].code in [REJECT_INVALID, REJECT_NONSTANDARD]
123120
assert_equal(self.nodes[0].p2p.last_message["reject"].data, block.sha256)
124-
if self.nodes[0].p2p.last_message["reject"].code == REJECT_INVALID:
125-
# Generic rejection when a block is invalid
126-
assert_equal(self.nodes[0].p2p.last_message["reject"].reason, b'block-validation-failed')
127-
else:
128-
assert b'Non-canonical DER signature' in self.nodes[0].p2p.last_message["reject"].reason
121+
assert b'Non-canonical DER signature' in self.nodes[0].p2p.last_message["reject"].reason
129122

130123
self.log.info("Test that a version 3 block with a DERSIG-compliant transaction is accepted")
131124
block.vtx[1] = create_transaction(self.nodes[0], self.coinbase_txids[1], self.nodeaddress, amount=1.0)

test/functional/p2p_invalid_block.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def run_test(self):
7979
assert_equal(orig_hash, block2.rehash())
8080
assert(block2_orig.vtx != block2.vtx)
8181

82-
node.p2p.send_blocks_and_test([block2], node, success=False, request_block=False, reject_code=16, reject_reason=b'bad-txns-duplicate')
82+
node.p2p.send_blocks_and_test([block2], node, success=False, request_block=False, reject_reason='bad-txns-duplicate')
8383

8484
self.log.info("Test very broken block.")
8585

@@ -92,7 +92,7 @@ def run_test(self):
9292
block3.rehash()
9393
block3.solve()
9494

95-
node.p2p.send_blocks_and_test([block3], node, success=False, request_block=False, reject_code=16, reject_reason=b'bad-cb-amount')
95+
node.p2p.send_blocks_and_test([block3], node, success=False, request_block=False, reject_reason='bad-cb-amount')
9696

9797
if __name__ == '__main__':
9898
InvalidBlockRequestTest().main()

test/functional/p2p_invalid_tx.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def run_test(self):
116116
assert_equal(2, len(node.getpeerinfo())) # p2ps[1] is still connected
117117

118118
self.log.info('Send the withhold tx ... ')
119-
node.p2p.send_txs_and_test([tx_withhold], node, success=True)
119+
with node.assert_debug_log(expected_msgs=["bad-txns-in-belowout"]):
120+
node.p2p.send_txs_and_test([tx_withhold], node, success=True)
120121

121122
# Transactions that should end up in the mempool
122123
expected_mempool = {
@@ -134,18 +135,6 @@ def run_test(self):
134135
wait_until(lambda: 1 == len(node.getpeerinfo()), timeout=12) # p2ps[1] is no longer connected
135136
assert_equal(expected_mempool, set(node.getrawmempool()))
136137

137-
# restart node with sending BIP61 messages disabled, check that it disconnects without sending the reject message
138-
self.log.info('Test a transaction that is rejected, with BIP61 disabled')
139-
self.restart_node(0, ['-enablebip61=0', '-persistmempool=0'])
140-
self.reconnect_p2p(num_connections=1)
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)
146-
# send_txs_and_test will have waited for disconnect, so we can safely check that no reject has been received
147-
assert_equal(node.p2p.reject_code_received, None)
148-
149138

150139
if __name__ == '__main__':
151140
InvalidTxRequestTest().main()

0 commit comments

Comments
 (0)