90
90
MAX_SIGOP_COST = 80000
91
91
92
92
93
- # Calculate the virtual size of a witness block:
94
- # (base + witness/4)
95
93
def get_virtual_size (witness_block ):
94
+ """Calculate the virtual size of a witness block.
95
+
96
+ Virtual size is base + witness/4."""
96
97
base_size = len (witness_block .serialize (with_witness = False ))
97
98
total_size = len (witness_block .serialize (with_witness = True ))
98
99
# the "+3" is so we round up
@@ -171,25 +172,24 @@ def request_block(self, blockhash, inv_type, timeout=60):
171
172
self .wait_for_block (blockhash , timeout )
172
173
return self .last_message ["block" ].block
173
174
174
- # Used to keep track of anyone-can-spend outputs that we can use in the tests
175
175
class UTXO ():
176
+ """Used to keep track of anyone-can-spend outputs that we can use in the tests."""
176
177
def __init__ (self , sha256 , n , value ):
177
178
self .sha256 = sha256
178
179
self .n = n
179
180
self .nValue = value
180
181
181
- # Helper for getting the script associated with a P2PKH
182
182
def get_p2pkh_script (pubkeyhash ):
183
+ """Get the script associated with a P2PKH."""
183
184
return CScript ([CScriptOp (OP_DUP ), CScriptOp (OP_HASH160 ), pubkeyhash , CScriptOp (OP_EQUALVERIFY ), CScriptOp (OP_CHECKSIG )])
184
185
185
- # Add signature for a P2PK witness program.
186
186
def sign_p2pk_witness_input (script , tx_to , in_idx , hashtype , value , key ):
187
+ """Add signature for a P2PK witness program."""
187
188
tx_hash = SegwitVersion1SignatureHash (script , tx_to , in_idx , hashtype , value )
188
189
signature = key .sign (tx_hash ) + chr (hashtype ).encode ('latin-1' )
189
190
tx_to .wit .vtxinwit [in_idx ].scriptWitness .stack = [signature , script ]
190
191
tx_to .rehash ()
191
192
192
-
193
193
class SegWitTest (BitcoinTestFramework ):
194
194
def set_test_params (self ):
195
195
self .setup_clean_chain = True
@@ -203,9 +203,10 @@ def setup_network(self):
203
203
connect_nodes (self .nodes [0 ], 2 )
204
204
self .sync_all ()
205
205
206
- ''' Helpers '''
207
- # Build a block on top of node0's tip.
206
+ # Helper functions
207
+
208
208
def build_next_block (self , version = 4 ):
209
+ """Build a block on top of node0's tip."""
209
210
tip = self .nodes [0 ].getbestblockhash ()
210
211
height = self .nodes [0 ].getblockcount () + 1
211
212
block_time = self .nodes [0 ].getblockheader (tip )["mediantime" ] + 1
@@ -214,21 +215,21 @@ def build_next_block(self, version=4):
214
215
block .rehash ()
215
216
return block
216
217
217
- # Adds list of transactions to block, adds witness commitment, then solves.
218
218
def update_witness_block_with_transactions (self , block , tx_list , nonce = 0 ):
219
+ """Add list of transactions to block, adds witness commitment, then solves."""
219
220
block .vtx .extend (tx_list )
220
221
add_witness_commitment (block , nonce )
221
222
block .solve ()
222
223
return
223
224
224
- ''' Individual tests '''
225
+ # Individual tests
226
+
225
227
def test_witness_services (self ):
226
228
self .log .info ("Verifying NODE_WITNESS service bit" )
227
229
assert ((self .test_node .nServices & NODE_WITNESS ) != 0 )
228
230
229
- # See if sending a regular transaction works, and create a utxo
230
- # to use in later tests.
231
231
def test_non_witness_transaction (self ):
232
+ """See if sending a regular transaction works, and create a utxo to use in later tests."""
232
233
# Mine a block with an anyone-can-spend coinbase,
233
234
# let it mature, then try to spend it.
234
235
self .log .info ("Testing non-witness transaction" )
@@ -257,8 +258,8 @@ def test_non_witness_transaction(self):
257
258
self .utxo .append (UTXO (tx .sha256 , 0 , 49 * 100000000 ))
258
259
self .nodes [0 ].generate (1 )
259
260
260
- # Verify that blocks with witnesses are rejected before activation.
261
261
def test_unnecessary_witness_before_segwit_activation (self ):
262
+ """Verify that blocks with witnesses are rejected before activation."""
262
263
self .log .info ("Testing behavior of unnecessary witnesses" )
263
264
# For now, rely on earlier tests to have created at least one utxo for
264
265
# us to use
@@ -299,14 +300,17 @@ def test_unnecessary_witness_before_segwit_activation(self):
299
300
self .utxo .pop (0 )
300
301
self .utxo .append (UTXO (tx .sha256 , 0 , tx .vout [0 ].nValue ))
301
302
302
- # ~6 months after segwit activation, the SCRIPT_VERIFY_WITNESS flag was
303
- # backdated so that it applies to all blocks, going back to the genesis
304
- # block.
305
- #
306
- # Consequently, version 0 witness outputs are never spendable without
307
- # witness, and so can't be spent before segwit activation (the point at which
308
- # blocks are permitted to contain witnesses).
309
303
def test_v0_outputs_arent_spendable (self ):
304
+ """Test that v0 outputs aren't spendable before segwit activation.
305
+
306
+ ~6 months after segwit activation, the SCRIPT_VERIFY_WITNESS flag was
307
+ backdated so that it applies to all blocks, going back to the genesis
308
+ block.
309
+
310
+ Consequently, version 0 witness outputs are never spendable without
311
+ witness, and so can't be spent before segwit activation (the point at which
312
+ blocks are permitted to contain witnesses)."""
313
+
310
314
self .log .info ("Testing that v0 witness program outputs aren't spendable before activation" )
311
315
312
316
assert len (self .utxo ), "self.utxo is empty"
@@ -374,8 +378,8 @@ def test_v0_outputs_arent_spendable(self):
374
378
self .utxo .pop (0 )
375
379
self .utxo .append (UTXO (txid , 2 , value ))
376
380
377
- # Mine enough blocks for segwit's vb state to be 'started'.
378
381
def advance_to_segwit_started (self ):
382
+ """Mine enough blocks for segwit's vb state to be 'started'."""
379
383
height = self .nodes [0 ].getblockcount ()
380
384
# Will need to rewrite the tests here if we are past the first period
381
385
assert (height < VB_PERIOD - 1 )
@@ -385,10 +389,11 @@ def advance_to_segwit_started(self):
385
389
self .nodes [0 ].generate (VB_PERIOD - height - 1 )
386
390
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'started' )
387
391
388
- # Mine enough blocks to lock in segwit, but don't activate.
389
- # TODO: we could verify that lockin only happens at the right threshold of
390
- # signalling blocks, rather than just at the right period boundary.
391
392
def advance_to_segwit_lockin (self ):
393
+ """Mine enough blocks to lock in segwit, but don't activate."""
394
+ # TODO: we could verify that lockin only happens at the right threshold of
395
+ # signalling blocks, rather than just at the right period boundary.
396
+
392
397
height = self .nodes [0 ].getblockcount ()
393
398
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'started' )
394
399
# Advance to end of period, and verify lock-in happens at the end
@@ -399,19 +404,23 @@ def advance_to_segwit_lockin(self):
399
404
self .nodes [0 ].generate (1 )
400
405
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'locked_in' )
401
406
402
- # Mine enough blocks to activate segwit.
403
- # TODO: we could verify that activation only happens at the right threshold
404
- # of signalling blocks, rather than just at the right period boundary.
405
407
def advance_to_segwit_active (self ):
408
+ """Mine enough blocks to activate segwit."""
409
+ # TODO: we could verify that activation only happens at the right threshold
410
+ # of signalling blocks, rather than just at the right period boundary.
411
+
406
412
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'locked_in' )
407
413
height = self .nodes [0 ].getblockcount ()
408
414
self .nodes [0 ].generate (VB_PERIOD - (height % VB_PERIOD ) - 2 )
409
415
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'locked_in' )
410
416
self .nodes [0 ].generate (1 )
411
417
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'active' )
412
418
413
- # This test can only be run after segwit has activated
414
419
def test_witness_commitments (self ):
420
+ """Test witness commitments.
421
+
422
+ This test can only be run after segwit has activated."""
423
+
415
424
self .log .info ("Testing witness commitments" )
416
425
417
426
# First try a correct witness commitment.
@@ -617,9 +626,8 @@ def test_witness_block_size(self):
617
626
self .utxo .pop (0 )
618
627
self .utxo .append (UTXO (block .vtx [- 1 ].sha256 , 0 , block .vtx [- 1 ].vout [0 ].nValue ))
619
628
620
- # submitblock will try to add the nonce automatically, so that mining
621
- # software doesn't need to worry about doing so itself.
622
629
def test_submit_block (self ):
630
+ """Test that submitblock adds the nonce automatically when possible."""
623
631
block = self .build_next_block ()
624
632
625
633
# Try using a custom nonce and then don't supply it.
@@ -653,8 +661,8 @@ def test_submit_block(self):
653
661
# Tip should not advance!
654
662
assert (self .nodes [0 ].getbestblockhash () != block_2 .hash )
655
663
656
- # Consensus tests of extra witness data in a transaction.
657
664
def test_extra_witness_data (self ):
665
+ """Test extra witness data in a transaction."""
658
666
self .log .info ("Testing extra witness data in tx" )
659
667
660
668
assert (len (self .utxo ) > 0 )
@@ -729,7 +737,7 @@ def test_extra_witness_data(self):
729
737
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
730
738
731
739
def test_max_witness_push_length (self ):
732
- ''' Should only allow up to 520 byte pushes in witness stack '''
740
+ """Test that witness stack can only allow up to 520 byte pushes."""
733
741
self .log .info ("Testing maximum witness push size" )
734
742
MAX_SCRIPT_ELEMENT_SIZE = 520
735
743
assert (len (self .utxo ))
@@ -768,8 +776,7 @@ def test_max_witness_push_length(self):
768
776
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
769
777
770
778
def test_max_witness_program_length (self ):
771
- # Can create witness outputs that are long, but can't be greater than
772
- # 10k bytes to successfully spend
779
+ """Test that witness outputs greater than 10kB can't be spent."""
773
780
self .log .info ("Testing maximum witness program length" )
774
781
assert (len (self .utxo ))
775
782
MAX_PROGRAM_LENGTH = 10000
@@ -817,7 +824,7 @@ def test_max_witness_program_length(self):
817
824
self .utxo .append (UTXO (tx2 .sha256 , 0 , tx2 .vout [0 ].nValue ))
818
825
819
826
def test_witness_input_length (self ):
820
- ''' Ensure that vin length must match vtxinwit length '''
827
+ """Test that vin length must match vtxinwit length."""
821
828
self .log .info ("Testing witness input length" )
822
829
assert (len (self .utxo ))
823
830
@@ -941,11 +948,14 @@ def test_witness_tx_relay_before_segwit_activation(self):
941
948
self .utxo .pop (0 )
942
949
self .utxo .append (UTXO (tx_hash , 0 , tx_value ))
943
950
944
- # After segwit activates, verify that mempool:
945
- # - rejects transactions with unnecessary/extra witnesses
946
- # - accepts transactions with valid witnesses
947
- # and that witness transactions are relayed to non-upgraded peers.
948
951
def test_tx_relay_after_segwit_activation (self ):
952
+ """Test transaction relay after segwit activation.
953
+
954
+ After segwit activates, verify that mempool:
955
+ - rejects transactions with unnecessary/extra witnesses
956
+ - accepts transactions with valid witnesses
957
+ and that witness transactions are relayed to non-upgraded peers."""
958
+
949
959
self .log .info ("Testing relay of witness transactions" )
950
960
# Generate a transaction that doesn't require a witness, but send it
951
961
# with a witness. Should be rejected because we can't use a witness
@@ -1032,10 +1042,11 @@ def test_tx_relay_after_segwit_activation(self):
1032
1042
self .utxo .pop (0 )
1033
1043
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
1034
1044
1035
- # Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG
1036
- # This is true regardless of segwit activation.
1037
- # Also test that we don't ask for blocks from unupgraded peers
1038
1045
def test_block_relay (self , segwit_activated ):
1046
+ """Test that block requests to NODE_WITNESS peer are with MSG_WITNESS_FLAG.
1047
+
1048
+ This is true regardless of segwit activation.
1049
+ Also test that we don't ask for blocks from unupgraded peers."""
1039
1050
self .log .info ("Testing block relay" )
1040
1051
1041
1052
blocktype = 2 | MSG_WITNESS_FLAG
@@ -1127,8 +1138,12 @@ def test_block_relay(self, segwit_activated):
1127
1138
self .old_node .announce_tx_and_wait_for_getdata (block4 .vtx [0 ])
1128
1139
assert (block4 .sha256 not in self .old_node .getdataset )
1129
1140
1130
- # V0 segwit outputs and inputs are always standard. V0 segwit inputs may only be mined after activation, but not before.
1131
1141
def test_standardness_v0 (self , segwit_activated ):
1142
+ """Test V0 txout standardness.
1143
+
1144
+ V0 segwit outputs and inputs are always standard.
1145
+ V0 segwit inputs may only be mined after activation, but not before."""
1146
+
1132
1147
self .log .info ("Testing standardness of v0 outputs (%s activation)" % ("after" if segwit_activated else "before" ))
1133
1148
assert (len (self .utxo ))
1134
1149
@@ -1203,9 +1218,12 @@ def test_standardness_v0(self, segwit_activated):
1203
1218
self .utxo .append (UTXO (tx3 .sha256 , 0 , tx3 .vout [0 ].nValue ))
1204
1219
assert_equal (len (self .nodes [1 ].getrawmempool ()), 0 )
1205
1220
1206
- # Verify that future segwit upgraded transactions are non-standard,
1207
- # but valid in blocks. Can run this before and after segwit activation.
1208
1221
def test_segwit_versions (self ):
1222
+ """Test validity of future segwit version transactions.
1223
+
1224
+ Future segwit version transactions are non-standard, but valid in blocks.
1225
+ Can run this before and after segwit activation."""
1226
+
1209
1227
self .log .info ("Testing standardness/consensus for segwit versions (0-16)" )
1210
1228
assert (len (self .utxo ))
1211
1229
num_tests = 17 # will test OP_0, OP1, ..., OP_16
@@ -1504,8 +1522,8 @@ def test_signature_version_1(self):
1504
1522
for i in range (len (tx .vout )):
1505
1523
self .utxo .append (UTXO (tx .sha256 , i , tx .vout [i ].nValue ))
1506
1524
1507
- # Test P2SH wrapped witness programs.
1508
1525
def test_p2sh_witness (self , segwit_activated ):
1526
+ """Test P2SH wrapped witness programs."""
1509
1527
self .log .info ("Testing P2SH witness transactions" )
1510
1528
1511
1529
assert (len (self .utxo ))
@@ -1574,12 +1592,8 @@ def test_p2sh_witness(self, segwit_activated):
1574
1592
self .utxo .pop (0 )
1575
1593
self .utxo .append (UTXO (spend_tx .sha256 , 0 , spend_tx .vout [0 ].nValue ))
1576
1594
1577
- # Test the behavior of starting up a segwit-aware node after the softfork
1578
- # has activated. As segwit requires different block data than pre-segwit
1579
- # nodes would have stored, this requires special handling.
1580
- # To enable this test, pass --oldbinary=<path-to-pre-segwit-bitcoind> to
1581
- # the test.
1582
1595
def test_upgrade_after_activation (self , node_id ):
1596
+ """Test the behavior of starting up a segwit-aware node after the softfork has activated."""
1583
1597
self .log .info ("Testing software upgrade after softfork activation" )
1584
1598
1585
1599
assert (node_id != 0 ) # node0 is assumed to be a segwit-active bitcoind
@@ -1606,7 +1620,7 @@ def test_upgrade_after_activation(self, node_id):
1606
1620
height -= 1
1607
1621
1608
1622
def test_witness_sigops (self ):
1609
- '''Ensure sigop counting is correct inside witnesses.'''
1623
+ """Test sigop counting is correct inside witnesses."""
1610
1624
self .log .info ("Testing sigops limit" )
1611
1625
1612
1626
assert (len (self .utxo ))
@@ -1755,9 +1769,12 @@ def test_getblocktemplate_before_lockin(self):
1755
1769
self .nodes [0 ].setmocktime (0 )
1756
1770
self .nodes [2 ].setmocktime (0 )
1757
1771
1758
- # Uncompressed pubkeys are no longer supported in default relay policy,
1759
- # but (for now) are still valid in blocks.
1760
1772
def test_uncompressed_pubkey (self ):
1773
+ """Test uncompressed pubkey validity in segwit transactions.
1774
+
1775
+ Uncompressed pubkeys are no longer supported in default relay policy,
1776
+ but (for now) are still valid in blocks."""
1777
+
1761
1778
self .log .info ("Testing uncompressed pubkeys" )
1762
1779
# Segwit transactions using uncompressed pubkeys are not accepted
1763
1780
# under default policy, but should still pass consensus.
0 commit comments