8
8
import struct
9
9
import time
10
10
11
- from test_framework .blocktools import create_block , create_coinbase , add_witness_commitment , WITNESS_COMMITMENT_HEADER
11
+ from test_framework .blocktools import (
12
+ WITNESS_COMMITMENT_HEADER ,
13
+ add_witness_commitment ,
14
+ create_block ,
15
+ create_coinbase ,
16
+ )
12
17
from test_framework .key import ECKey
13
18
from test_framework .messages import (
14
19
BIP125_SEQUENCE_NUMBER ,
@@ -194,7 +199,7 @@ def set_test_params(self):
194
199
self .num_nodes = 2
195
200
# This test tests SegWit both pre and post-activation, so use the normal BIP9 activation.
196
201
self .extra_args = [
197
- [
"-acceptnonstdtxn=1" ,
f"-testactivationheight=segwit@{ SEGWIT_HEIGHT } " ,
"[email protected] " ],
202
+ [
"-acceptnonstdtxn=1" ,
f"-testactivationheight=segwit@{ SEGWIT_HEIGHT } " ,
"[email protected] " , "-par=1" ],
198
203
["-acceptnonstdtxn=0" , f"-testactivationheight=segwit@{ SEGWIT_HEIGHT } " ],
199
204
]
200
205
self .supports_cli = False
@@ -505,8 +510,8 @@ def test_v0_outputs_arent_spendable(self):
505
510
# 'block-validation-failed' (if script check threads > 1) or
506
511
# 'non-mandatory-script-verify-flag (Witness program was passed an
507
512
# empty witness)' (otherwise).
508
- # TODO: support multiple acceptable reject reasons.
509
- test_witness_block ( self . nodes [ 0 ], self . test_node , block , accepted = False , with_witness = False )
513
+ test_witness_block ( self . nodes [ 0 ], self . test_node , block , accepted = False , with_witness = False ,
514
+ reason = 'non-mandatory-script-verify-flag (Witness program was passed an empty witness)' )
510
515
511
516
self .utxo .pop (0 )
512
517
self .utxo .append (UTXO (txid , 2 , value ))
@@ -786,7 +791,7 @@ def test_witness_commitments(self):
786
791
block_3 .rehash ()
787
792
block_3 .solve ()
788
793
789
- test_witness_block (self .nodes [0 ], self .test_node , block_3 , accepted = False )
794
+ test_witness_block (self .nodes [0 ], self .test_node , block_3 , accepted = False , reason = 'bad-witness-merkle-match' )
790
795
791
796
# Add a different commitment with different nonce, but in the
792
797
# right location, and with some funds burned(!).
@@ -852,7 +857,7 @@ def test_block_malleability(self):
852
857
# Change the nonce -- should not cause the block to be permanently
853
858
# failed
854
859
block .vtx [0 ].wit .vtxinwit [0 ].scriptWitness .stack = [ser_uint256 (1 )]
855
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
860
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False , reason = 'bad-witness-merkle-match' )
856
861
857
862
# Changing the witness reserved value doesn't change the block hash
858
863
block .vtx [0 ].wit .vtxinwit [0 ].scriptWitness .stack = [ser_uint256 (0 )]
@@ -861,7 +866,7 @@ def test_block_malleability(self):
861
866
@subtest # type: ignore
862
867
def test_witness_block_size (self ):
863
868
# TODO: Test that non-witness carrying blocks can't exceed 1MB
864
- # Skipping this test for now; this is covered in p2p-fullblocktest .py
869
+ # Skipping this test for now; this is covered in feature_block .py
865
870
866
871
# Test that witness-bearing blocks are limited at ceil(base + wit/4) <= 1MB.
867
872
block = self .build_next_block ()
@@ -917,7 +922,7 @@ def test_witness_block_size(self):
917
922
# limit
918
923
assert len (block .serialize ()) > 2 * 1024 * 1024
919
924
920
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
925
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False , reason = 'bad-blk-weight' )
921
926
922
927
# Now resize the second transaction to make the block fit.
923
928
cur_length = len (block .vtx [- 1 ].wit .vtxinwit [0 ].scriptWitness .stack [0 ])
@@ -989,7 +994,8 @@ def test_extra_witness_data(self):
989
994
self .update_witness_block_with_transactions (block , [tx ])
990
995
991
996
# Extra witness data should not be allowed.
992
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
997
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
998
+ reason = 'non-mandatory-script-verify-flag (Witness provided for non-witness script)' )
993
999
994
1000
# Try extra signature data. Ok if we're not spending a witness output.
995
1001
block .vtx [1 ].wit .vtxinwit = []
@@ -1014,7 +1020,8 @@ def test_extra_witness_data(self):
1014
1020
self .update_witness_block_with_transactions (block , [tx2 ])
1015
1021
1016
1022
# This has extra witness data, so it should fail.
1017
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1023
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1024
+ reason = 'non-mandatory-script-verify-flag (Stack size must be exactly one after execution)' )
1018
1025
1019
1026
# Now get rid of the extra witness, but add extra scriptSig data
1020
1027
tx2 .vin [0 ].scriptSig = CScript ([OP_TRUE ])
@@ -1026,7 +1033,8 @@ def test_extra_witness_data(self):
1026
1033
block .solve ()
1027
1034
1028
1035
# This has extra signature data for a witness input, so it should fail.
1029
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1036
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1037
+ reason = 'non-mandatory-script-verify-flag (Witness requires empty scriptSig)' )
1030
1038
1031
1039
# Now get rid of the extra scriptsig on the witness input, and verify
1032
1040
# success (even with extra scriptsig data in the non-witness input)
@@ -1064,7 +1072,8 @@ def test_max_witness_push_length(self):
1064
1072
tx2 .rehash ()
1065
1073
1066
1074
self .update_witness_block_with_transactions (block , [tx , tx2 ])
1067
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1075
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1076
+ reason = 'non-mandatory-script-verify-flag (Push value size limit exceeded)' )
1068
1077
1069
1078
# Now reduce the length of the stack element
1070
1079
tx2 .wit .vtxinwit [0 ].scriptWitness .stack [0 ] = b'a' * (MAX_SCRIPT_ELEMENT_SIZE )
@@ -1104,7 +1113,8 @@ def test_max_witness_script_length(self):
1104
1113
1105
1114
self .update_witness_block_with_transactions (block , [tx , tx2 ])
1106
1115
1107
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1116
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1117
+ reason = 'non-mandatory-script-verify-flag (Script is too big)' )
1108
1118
1109
1119
# Try again with one less byte in the witness script
1110
1120
witness_script = CScript ([b'a' * MAX_SCRIPT_ELEMENT_SIZE ] * 19 + [OP_DROP ] * 62 + [OP_TRUE ])
@@ -1176,14 +1186,16 @@ def serialize_with_witness(self):
1176
1186
1177
1187
block = self .build_next_block ()
1178
1188
self .update_witness_block_with_transactions (block , [tx2 ])
1179
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1189
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False , reason = 'bad-txnmrklroot' )
1180
1190
1181
1191
# Now try using a too short vtxinwit
1182
1192
tx2 .wit .vtxinwit .pop ()
1183
1193
tx2 .wit .vtxinwit .pop ()
1184
1194
1185
1195
block .vtx = [block .vtx [0 ]]
1186
1196
self .update_witness_block_with_transactions (block , [tx2 ])
1197
+ # This block doesn't result in a specific reject reason, but an iostream exception:
1198
+ # "Exception 'CDataStream::read(): end of data: unspecified iostream_category error' (...) caught"
1187
1199
test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1188
1200
1189
1201
# Now make one of the intermediate witnesses be incorrect
@@ -1193,7 +1205,8 @@ def serialize_with_witness(self):
1193
1205
1194
1206
block .vtx = [block .vtx [0 ]]
1195
1207
self .update_witness_block_with_transactions (block , [tx2 ])
1196
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1208
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1209
+ reason = 'non-mandatory-script-verify-flag (Operation not valid with the current stack size)' )
1197
1210
1198
1211
# Fix the broken witness and the block should be accepted.
1199
1212
tx2 .wit .vtxinwit [5 ].scriptWitness .stack = [b'a' , witness_script ]
@@ -1415,7 +1428,7 @@ def test_premature_coinbase_witness_spend(self):
1415
1428
self .sync_blocks ()
1416
1429
block2 = self .build_next_block ()
1417
1430
self .update_witness_block_with_transactions (block2 , [spend_tx ])
1418
- test_witness_block (self .nodes [0 ], self .test_node , block2 , accepted = False )
1431
+ test_witness_block (self .nodes [0 ], self .test_node , block2 , accepted = False , reason = 'bad-txns-premature-spend-of-coinbase' )
1419
1432
1420
1433
# Advancing one more block should allow the spend.
1421
1434
self .generate (self .nodes [0 ], 1 )
@@ -1564,13 +1577,17 @@ def test_signature_version_1(self):
1564
1577
# Too-large input value
1565
1578
sign_p2pk_witness_input (witness_script , tx , 0 , hashtype , prev_utxo .nValue + 1 , key )
1566
1579
self .update_witness_block_with_transactions (block , [tx ])
1567
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1580
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1581
+ reason = 'non-mandatory-script-verify-flag (Script evaluated without error '
1582
+ 'but finished with a false/empty top stack element' )
1568
1583
1569
1584
# Too-small input value
1570
1585
sign_p2pk_witness_input (witness_script , tx , 0 , hashtype , prev_utxo .nValue - 1 , key )
1571
1586
block .vtx .pop () # remove last tx
1572
1587
self .update_witness_block_with_transactions (block , [tx ])
1573
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1588
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1589
+ reason = 'non-mandatory-script-verify-flag (Script evaluated without error '
1590
+ 'but finished with a false/empty top stack element' )
1574
1591
1575
1592
# Now try correct value
1576
1593
sign_p2pk_witness_input (witness_script , tx , 0 , hashtype , prev_utxo .nValue , key )
@@ -1672,7 +1689,8 @@ def test_signature_version_1(self):
1672
1689
tx2 .vin [0 ].scriptSig = CScript ([signature , pubkey ])
1673
1690
block = self .build_next_block ()
1674
1691
self .update_witness_block_with_transactions (block , [tx , tx2 ])
1675
- test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False )
1692
+ test_witness_block (self .nodes [0 ], self .test_node , block , accepted = False ,
1693
+ reason = 'non-mandatory-script-verify-flag (Witness requires empty scriptSig)' )
1676
1694
1677
1695
# Move the signature to the witness.
1678
1696
block .vtx .pop ()
@@ -1918,7 +1936,7 @@ def test_witness_sigops(self):
1918
1936
1919
1937
block_2 = self .build_next_block ()
1920
1938
self .update_witness_block_with_transactions (block_2 , [tx2 ])
1921
- test_witness_block (self .nodes [0 ], self .test_node , block_2 , accepted = False )
1939
+ test_witness_block (self .nodes [0 ], self .test_node , block_2 , accepted = False , reason = 'bad-blk-sigops' )
1922
1940
1923
1941
# Try dropping the last input in tx2, and add an output that has
1924
1942
# too many sigops (contributing to legacy sigop count).
@@ -1931,7 +1949,7 @@ def test_witness_sigops(self):
1931
1949
tx2 .rehash ()
1932
1950
block_3 = self .build_next_block ()
1933
1951
self .update_witness_block_with_transactions (block_3 , [tx2 ])
1934
- test_witness_block (self .nodes [0 ], self .test_node , block_3 , accepted = False )
1952
+ test_witness_block (self .nodes [0 ], self .test_node , block_3 , accepted = False , reason = 'bad-blk-sigops' )
1935
1953
1936
1954
# If we drop the last checksig in this output, the tx should succeed.
1937
1955
block_4 = self .build_next_block ()
0 commit comments