Skip to content

Commit 45827fd

Browse files
committed
test: check for block reject reasons in p2p_segwit.py [2/2]
This commit adds specific expected reject reasons for segwit blocks sent to the node, that are only showing up if one script threads is used. For this reason, the node is started with the parameter `-par=1`.
1 parent 4eb532f commit 45827fd

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

test/functional/p2p_segwit.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def set_test_params(self):
198198
self.num_nodes = 2
199199
# This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
200200
self.extra_args = [
201-
["-acceptnonstdtxn=1", f"-testactivationheight=segwit@{SEGWIT_HEIGHT}", "[email protected]"],
201+
["-acceptnonstdtxn=1", f"-testactivationheight=segwit@{SEGWIT_HEIGHT}", "[email protected]", "-par=1"],
202202
["-acceptnonstdtxn=0", f"-testactivationheight=segwit@{SEGWIT_HEIGHT}"],
203203
]
204204
self.supports_cli = False
@@ -509,8 +509,8 @@ def test_v0_outputs_arent_spendable(self):
509509
# 'block-validation-failed' (if script check threads > 1) or
510510
# 'non-mandatory-script-verify-flag (Witness program was passed an
511511
# empty witness)' (otherwise).
512-
# TODO: support multiple acceptable reject reasons.
513-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False, with_witness=False)
512+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False, with_witness=False,
513+
reason='non-mandatory-script-verify-flag (Witness program was passed an empty witness)')
514514

515515
self.utxo.pop(0)
516516
self.utxo.append(UTXO(txid, 2, value))
@@ -993,7 +993,8 @@ def test_extra_witness_data(self):
993993
self.update_witness_block_with_transactions(block, [tx])
994994

995995
# Extra witness data should not be allowed.
996-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
996+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
997+
reason='non-mandatory-script-verify-flag (Witness provided for non-witness script)')
997998

998999
# Try extra signature data. Ok if we're not spending a witness output.
9991000
block.vtx[1].wit.vtxinwit = []
@@ -1018,7 +1019,8 @@ def test_extra_witness_data(self):
10181019
self.update_witness_block_with_transactions(block, [tx2])
10191020

10201021
# This has extra witness data, so it should fail.
1021-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1022+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1023+
reason='non-mandatory-script-verify-flag (Stack size must be exactly one after execution)')
10221024

10231025
# Now get rid of the extra witness, but add extra scriptSig data
10241026
tx2.vin[0].scriptSig = CScript([OP_TRUE])
@@ -1030,7 +1032,8 @@ def test_extra_witness_data(self):
10301032
block.solve()
10311033

10321034
# This has extra signature data for a witness input, so it should fail.
1033-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1035+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1036+
reason='non-mandatory-script-verify-flag (Witness requires empty scriptSig)')
10341037

10351038
# Now get rid of the extra scriptsig on the witness input, and verify
10361039
# success (even with extra scriptsig data in the non-witness input)
@@ -1068,7 +1071,8 @@ def test_max_witness_push_length(self):
10681071
tx2.rehash()
10691072

10701073
self.update_witness_block_with_transactions(block, [tx, tx2])
1071-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1074+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1075+
reason='non-mandatory-script-verify-flag (Push value size limit exceeded)')
10721076

10731077
# Now reduce the length of the stack element
10741078
tx2.wit.vtxinwit[0].scriptWitness.stack[0] = b'a' * (MAX_SCRIPT_ELEMENT_SIZE)
@@ -1108,7 +1112,8 @@ def test_max_witness_script_length(self):
11081112

11091113
self.update_witness_block_with_transactions(block, [tx, tx2])
11101114

1111-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1115+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1116+
reason='non-mandatory-script-verify-flag (Script is too big)')
11121117

11131118
# Try again with one less byte in the witness script
11141119
witness_script = CScript([b'a' * MAX_SCRIPT_ELEMENT_SIZE] * 19 + [OP_DROP] * 62 + [OP_TRUE])
@@ -1188,6 +1193,8 @@ def serialize_with_witness(self):
11881193

11891194
block.vtx = [block.vtx[0]]
11901195
self.update_witness_block_with_transactions(block, [tx2])
1196+
# This block doesn't result in a specific reject reason, but an iostream exception:
1197+
# "Exception 'CDataStream::read(): end of data: unspecified iostream_category error' (...) caught"
11911198
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
11921199

11931200
# Now make one of the intermediate witnesses be incorrect
@@ -1197,7 +1204,8 @@ def serialize_with_witness(self):
11971204

11981205
block.vtx = [block.vtx[0]]
11991206
self.update_witness_block_with_transactions(block, [tx2])
1200-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1207+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1208+
reason='non-mandatory-script-verify-flag (Operation not valid with the current stack size)')
12011209

12021210
# Fix the broken witness and the block should be accepted.
12031211
tx2.wit.vtxinwit[5].scriptWitness.stack = [b'a', witness_script]
@@ -1568,13 +1576,17 @@ def test_signature_version_1(self):
15681576
# Too-large input value
15691577
sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue + 1, key)
15701578
self.update_witness_block_with_transactions(block, [tx])
1571-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1579+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1580+
reason='non-mandatory-script-verify-flag (Script evaluated without error '
1581+
'but finished with a false/empty top stack element')
15721582

15731583
# Too-small input value
15741584
sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue - 1, key)
15751585
block.vtx.pop() # remove last tx
15761586
self.update_witness_block_with_transactions(block, [tx])
1577-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1587+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1588+
reason='non-mandatory-script-verify-flag (Script evaluated without error '
1589+
'but finished with a false/empty top stack element')
15781590

15791591
# Now try correct value
15801592
sign_p2pk_witness_input(witness_script, tx, 0, hashtype, prev_utxo.nValue, key)
@@ -1676,7 +1688,8 @@ def test_signature_version_1(self):
16761688
tx2.vin[0].scriptSig = CScript([signature, pubkey])
16771689
block = self.build_next_block()
16781690
self.update_witness_block_with_transactions(block, [tx, tx2])
1679-
test_witness_block(self.nodes[0], self.test_node, block, accepted=False)
1691+
test_witness_block(self.nodes[0], self.test_node, block, accepted=False,
1692+
reason='non-mandatory-script-verify-flag (Witness requires empty scriptSig)')
16801693

16811694
# Move the signature to the witness.
16821695
block.vtx.pop()

0 commit comments

Comments
 (0)