@@ -292,15 +292,24 @@ def run_test(self):
292
292
293
293
# Individual tests
294
294
295
+ def subtest (func ): # noqa: N805
296
+ """Wraps the subtests for logging and state assertions."""
297
+ def func_wrapper (self , * args , ** kwargs ):
298
+ self .log .info ("Subtest: {}" .format (func .__name__ ))
299
+ func (self , * args , ** kwargs )
300
+
301
+ return func_wrapper
302
+
295
303
def test_witness_services (self ):
296
304
self .log .info ("Verifying NODE_WITNESS service bit" )
297
305
assert ((self .test_node .nServices & NODE_WITNESS ) != 0 )
298
306
307
+ @subtest
299
308
def test_non_witness_transaction (self ):
300
309
"""See if sending a regular transaction works, and create a utxo to use in later tests."""
301
310
# Mine a block with an anyone-can-spend coinbase,
302
311
# let it mature, then try to spend it.
303
- self . log . info ( "Testing non-witness transaction" )
312
+
304
313
block = self .build_next_block (version = 1 )
305
314
block .solve ()
306
315
self .test_node .send_message (msg_block (block ))
@@ -326,9 +335,10 @@ def test_non_witness_transaction(self):
326
335
self .utxo .append (UTXO (tx .sha256 , 0 , 49 * 100000000 ))
327
336
self .nodes [0 ].generate (1 )
328
337
338
+ @subtest
329
339
def test_unnecessary_witness_before_segwit_activation (self ):
330
340
"""Verify that blocks with witnesses are rejected before activation."""
331
- self . log . info ( "Testing behavior of unnecessary witnesses" )
341
+
332
342
# For now, rely on earlier tests to have created at least one utxo for
333
343
# us to use
334
344
assert (len (self .utxo ) > 0 )
@@ -368,12 +378,12 @@ def test_unnecessary_witness_before_segwit_activation(self):
368
378
self .utxo .pop (0 )
369
379
self .utxo .append (UTXO (tx .sha256 , 0 , tx .vout [0 ].nValue ))
370
380
381
+ @subtest
371
382
def test_block_relay (self , segwit_activated ):
372
383
"""Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG.
373
384
374
385
This is true regardless of segwit activation.
375
386
Also test that we don't ask for blocks from unupgraded peers."""
376
- self .log .info ("Testing block relay" )
377
387
378
388
blocktype = 2 | MSG_WITNESS_FLAG
379
389
@@ -464,6 +474,7 @@ def test_block_relay(self, segwit_activated):
464
474
self .old_node .announce_tx_and_wait_for_getdata (block4 .vtx [0 ])
465
475
assert (block4 .sha256 not in self .old_node .getdataset )
466
476
477
+ @subtest
467
478
def test_v0_outputs_arent_spendable (self ):
468
479
"""Test that v0 outputs aren't spendable before segwit activation.
469
480
@@ -475,8 +486,6 @@ def test_v0_outputs_arent_spendable(self):
475
486
witness, and so can't be spent before segwit activation (the point at which
476
487
blocks are permitted to contain witnesses)."""
477
488
478
- self .log .info ("Testing that v0 witness program outputs aren't spendable before activation" )
479
-
480
489
assert len (self .utxo ), "self.utxo is empty"
481
490
482
491
# Create two outputs, a p2wsh and p2sh-p2wsh
@@ -542,6 +551,7 @@ def test_v0_outputs_arent_spendable(self):
542
551
self .utxo .pop (0 )
543
552
self .utxo .append (UTXO (txid , 2 , value ))
544
553
554
+ @subtest
545
555
def advance_to_segwit_started (self ):
546
556
"""Mine enough blocks for segwit's vb state to be 'started'."""
547
557
height = self .nodes [0 ].getblockcount ()
@@ -553,8 +563,8 @@ def advance_to_segwit_started(self):
553
563
self .nodes [0 ].generate (VB_PERIOD - height - 1 )
554
564
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'started' )
555
565
566
+ @subtest
556
567
def test_getblocktemplate_before_lockin (self ):
557
- self .log .info ("Testing getblocktemplate setting of segwit versionbit (before lockin)" )
558
568
# Node0 is segwit aware, node2 is not.
559
569
for node in [self .nodes [0 ], self .nodes [2 ]]:
560
570
gbt_results = node .getblocktemplate ()
@@ -600,6 +610,7 @@ def test_getblocktemplate_before_lockin(self):
600
610
self .nodes [0 ].setmocktime (0 )
601
611
self .nodes [2 ].setmocktime (0 )
602
612
613
+ @subtest
603
614
def advance_to_segwit_lockin (self ):
604
615
"""Mine enough blocks to lock in segwit, but don't activate."""
605
616
# TODO: we could verify that lockin only happens at the right threshold of
@@ -615,8 +626,9 @@ def advance_to_segwit_lockin(self):
615
626
self .nodes [0 ].generate (1 )
616
627
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'locked_in' )
617
628
629
+ @subtest
618
630
def test_witness_tx_relay_before_segwit_activation (self ):
619
- self . log . info ( "Testing relay of witness transactions" )
631
+
620
632
# Generate a transaction that doesn't require a witness, but send it
621
633
# with a witness. Should be rejected for premature-witness, but should
622
634
# not be added to recently rejected list.
@@ -657,13 +669,13 @@ def test_witness_tx_relay_before_segwit_activation(self):
657
669
self .utxo .pop (0 )
658
670
self .utxo .append (UTXO (tx_hash , 0 , tx_value ))
659
671
672
+ @subtest
660
673
def test_standardness_v0 (self , segwit_activated ):
661
674
"""Test V0 txout standardness.
662
675
663
676
V0 segwit outputs and inputs are always standard.
664
677
V0 segwit inputs may only be mined after activation, but not before."""
665
678
666
- self .log .info ("Testing standardness of v0 outputs (%s activation)" % ("after" if segwit_activated else "before" ))
667
679
assert (len (self .utxo ))
668
680
669
681
witness_program = CScript ([OP_TRUE ])
@@ -737,6 +749,7 @@ def test_standardness_v0(self, segwit_activated):
737
749
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
738
750
assert_equal (len (self .nodes [1 ].getrawmempool ()), 0 )
739
751
752
+ @subtest
740
753
def advance_to_segwit_active (self ):
741
754
"""Mine enough blocks to activate segwit."""
742
755
# TODO: we could verify that activation only happens at the right threshold
@@ -749,9 +762,9 @@ def advance_to_segwit_active(self):
749
762
self .nodes [0 ].generate (1 )
750
763
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'active' )
751
764
765
+ @subtest
752
766
def test_p2sh_witness (self , segwit_activated ):
753
767
"""Test P2SH wrapped witness programs."""
754
- self .log .info ("Testing P2SH witness transactions" )
755
768
756
769
assert (len (self .utxo ))
757
770
@@ -819,13 +832,12 @@ def test_p2sh_witness(self, segwit_activated):
819
832
self .utxo .pop (0 )
820
833
self .utxo .append (UTXO (spend_tx .sha256 , 0 , spend_tx .vout [0 ].nValue ))
821
834
835
+ @subtest
822
836
def test_witness_commitments (self ):
823
837
"""Test witness commitments.
824
838
825
839
This test can only be run after segwit has activated."""
826
840
827
- self .log .info ("Testing witness commitments" )
828
-
829
841
# First try a correct witness commitment.
830
842
block = self .build_next_block ()
831
843
add_witness_commitment (block )
@@ -911,8 +923,8 @@ def test_witness_commitments(self):
911
923
self .utxo .pop (0 )
912
924
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
913
925
926
+ @subtest
914
927
def test_block_malleability (self ):
915
- self .log .info ("Testing witness block malleability" )
916
928
917
929
# Make sure that a block that has too big a virtual size
918
930
# because of a too-large coinbase witness is not permanently
@@ -951,8 +963,8 @@ def test_block_malleability(self):
951
963
block .vtx [0 ].wit .vtxinwit [0 ].scriptWitness .stack = [ser_uint256 (0 )]
952
964
test_witness_block (self .nodes [0 ].rpc , self .test_node , block , accepted = True )
953
965
966
+ @subtest
954
967
def test_witness_block_size (self ):
955
- self .log .info ("Testing witness block size limit" )
956
968
# TODO: Test that non-witness carrying blocks can't exceed 1MB
957
969
# Skipping this test for now; this is covered in p2p-fullblocktest.py
958
970
@@ -1029,6 +1041,7 @@ def test_witness_block_size(self):
1029
1041
self .utxo .pop (0 )
1030
1042
self .utxo .append (UTXO (block .vtx [- 1 ].sha256 , 0 , block .vtx [- 1 ].vout [0 ].nValue ))
1031
1043
1044
+ @subtest
1032
1045
def test_submit_block (self ):
1033
1046
"""Test that submitblock adds the nonce automatically when possible."""
1034
1047
block = self .build_next_block ()
@@ -1064,9 +1077,9 @@ def test_submit_block(self):
1064
1077
# Tip should not advance!
1065
1078
assert (self .nodes [0 ].getbestblockhash () != block_2 .hash )
1066
1079
1080
+ @subtest
1067
1081
def test_extra_witness_data (self ):
1068
1082
"""Test extra witness data in a transaction."""
1069
- self .log .info ("Testing extra witness data in tx" )
1070
1083
1071
1084
assert (len (self .utxo ) > 0 )
1072
1085
@@ -1139,9 +1152,10 @@ def test_extra_witness_data(self):
1139
1152
self .utxo .pop (0 )
1140
1153
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
1141
1154
1155
+ @subtest
1142
1156
def test_max_witness_push_length (self ):
1143
1157
"""Test that witness stack can only allow up to 520 byte pushes."""
1144
- self . log . info ( "Testing maximum witness push size" )
1158
+
1145
1159
MAX_SCRIPT_ELEMENT_SIZE = 520
1146
1160
assert (len (self .utxo ))
1147
1161
@@ -1178,9 +1192,10 @@ def test_max_witness_push_length(self):
1178
1192
self .utxo .pop ()
1179
1193
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
1180
1194
1195
+ @subtest
1181
1196
def test_max_witness_program_length (self ):
1182
1197
"""Test that witness outputs greater than 10kB can't be spent."""
1183
- self . log . info ( "Testing maximum witness program length" )
1198
+
1184
1199
assert (len (self .utxo ))
1185
1200
MAX_PROGRAM_LENGTH = 10000
1186
1201
@@ -1226,9 +1241,10 @@ def test_max_witness_program_length(self):
1226
1241
self .utxo .pop ()
1227
1242
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
1228
1243
1244
+ @subtest
1229
1245
def test_witness_input_length (self ):
1230
1246
"""Test that vin length must match vtxinwit length."""
1231
- self . log . info ( "Testing witness input length" )
1247
+
1232
1248
assert (len (self .utxo ))
1233
1249
1234
1250
witness_program = CScript ([OP_DROP , OP_TRUE ])
@@ -1309,6 +1325,7 @@ def serialize_with_witness(self):
1309
1325
self .utxo .pop ()
1310
1326
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
1311
1327
1328
+ @subtest
1312
1329
def test_tx_relay_after_segwit_activation (self ):
1313
1330
"""Test transaction relay after segwit activation.
1314
1331
@@ -1317,7 +1334,6 @@ def test_tx_relay_after_segwit_activation(self):
1317
1334
- accepts transactions with valid witnesses
1318
1335
and that witness transactions are relayed to non-upgraded peers."""
1319
1336
1320
- self .log .info ("Testing relay of witness transactions" )
1321
1337
# Generate a transaction that doesn't require a witness, but send it
1322
1338
# with a witness. Should be rejected because we can't use a witness
1323
1339
# when spending a non-witness output.
@@ -1403,13 +1419,13 @@ def test_tx_relay_after_segwit_activation(self):
1403
1419
self .utxo .pop (0 )
1404
1420
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
1405
1421
1422
+ @subtest
1406
1423
def test_segwit_versions (self ):
1407
1424
"""Test validity of future segwit version transactions.
1408
1425
1409
1426
Future segwit version transactions are non-standard, but valid in blocks.
1410
1427
Can run this before and after segwit activation."""
1411
1428
1412
- self .log .info ("Testing standardness/consensus for segwit versions (0-16)" )
1413
1429
assert (len (self .utxo ))
1414
1430
num_tests = 17 # will test OP_0, OP1, ..., OP_16
1415
1431
if (len (self .utxo ) < num_tests ):
@@ -1491,8 +1507,9 @@ def test_segwit_versions(self):
1491
1507
# Add utxo to our list
1492
1508
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
1493
1509
1510
+ @subtest
1494
1511
def test_premature_coinbase_witness_spend (self ):
1495
- self . log . info ( "Testing premature coinbase witness spend" )
1512
+
1496
1513
block = self .build_next_block ()
1497
1514
# Change the output of the block to be a witness output.
1498
1515
witness_program = CScript ([OP_TRUE ])
@@ -1525,13 +1542,13 @@ def test_premature_coinbase_witness_spend(self):
1525
1542
test_witness_block (self .nodes [0 ].rpc , self .test_node , block2 , accepted = True )
1526
1543
sync_blocks (self .nodes )
1527
1544
1545
+ @subtest
1528
1546
def test_uncompressed_pubkey (self ):
1529
1547
"""Test uncompressed pubkey validity in segwit transactions.
1530
1548
1531
1549
Uncompressed pubkeys are no longer supported in default relay policy,
1532
1550
but (for now) are still valid in blocks."""
1533
1551
1534
- self .log .info ("Testing uncompressed pubkeys" )
1535
1552
# Segwit transactions using uncompressed pubkeys are not accepted
1536
1553
# under default policy, but should still pass consensus.
1537
1554
key = CECKey ()
@@ -1632,8 +1649,9 @@ def test_uncompressed_pubkey(self):
1632
1649
test_witness_block (self .nodes [0 ].rpc , self .test_node , block , accepted = True )
1633
1650
self .utxo .append (UTXO (tx5 .sha256 , 0 , tx5 .vout [0 ].nValue ))
1634
1651
1652
+ @subtest
1635
1653
def test_signature_version_1 (self ):
1636
- self . log . info ( "Testing segwit signature hash version 1" )
1654
+
1637
1655
key = CECKey ()
1638
1656
key .set_secretbytes (b"9" )
1639
1657
pubkey = CPubKey (key .get_pubkey ())
@@ -1814,8 +1832,9 @@ def test_signature_version_1(self):
1814
1832
for i in range (len (tx .vout )):
1815
1833
self .utxo .append (UTXO (tx .sha256 , i , tx .vout [i ].nValue ))
1816
1834
1835
+ @subtest
1817
1836
def test_non_standard_witness_blinding (self ):
1818
- self . log . info ( "Testing behavior of unnecessary witnesses in transactions does not blind the node for the transaction")
1837
+ """Test behavior of unnecessary witnesses in transactions does not blind the node for the transaction"""
1819
1838
assert (len (self .utxo ) > 0 )
1820
1839
1821
1840
# Create a p2sh output -- this is so we can pass the standardness
@@ -1868,8 +1887,9 @@ def test_non_standard_witness_blinding(self):
1868
1887
self .utxo .pop (0 )
1869
1888
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
1870
1889
1890
+ @subtest
1871
1891
def test_non_standard_witness (self ):
1872
- self . log . info ( "Testing detection of non-standard P2WSH witness")
1892
+ """Test detection of non-standard P2WSH witness"""
1873
1893
pad = chr (1 ).encode ('latin-1' )
1874
1894
1875
1895
# Create scripts for tests
@@ -1968,9 +1988,9 @@ def test_non_standard_witness(self):
1968
1988
1969
1989
self .utxo .pop (0 )
1970
1990
1991
+ @subtest
1971
1992
def test_upgrade_after_activation (self , node_id ):
1972
1993
"""Test the behavior of starting up a segwit-aware node after the softfork has activated."""
1973
- self .log .info ("Testing software upgrade after softfork activation" )
1974
1994
1975
1995
assert (node_id != 0 ) # node0 is assumed to be a segwit-active bitcoind
1976
1996
@@ -1995,9 +2015,9 @@ def test_upgrade_after_activation(self, node_id):
1995
2015
assert_equal (self .nodes [0 ].getblock (block_hash ), self .nodes [node_id ].getblock (block_hash ))
1996
2016
height -= 1
1997
2017
2018
+ @subtest
1998
2019
def test_witness_sigops (self ):
1999
2020
"""Test sigop counting is correct inside witnesses."""
2000
- self .log .info ("Testing sigops limit" )
2001
2021
2002
2022
assert (len (self .utxo ))
2003
2023
0 commit comments