41
41
from test_framework .mininode import (
42
42
P2PInterface ,
43
43
mininode_lock ,
44
+ wait_until ,
44
45
)
45
46
from test_framework .script import (
46
47
CScript ,
@@ -221,7 +222,7 @@ def update_witness_block_with_transactions(self, block, tx_list, nonce=0):
221
222
block .solve ()
222
223
223
224
def run_test (self ):
224
- # Setup the p2p connections and start up the network thread.
225
+ # Setup the p2p connections
225
226
# self.test_node sets NODE_WITNESS|NODE_NETWORK
226
227
self .test_node = self .nodes [0 ].add_p2p_connection (TestP2PConn (), services = NODE_NETWORK | NODE_WITNESS )
227
228
# self.old_node sets only NODE_NETWORK
@@ -351,10 +352,7 @@ def test_unnecessary_witness_before_segwit_activation(self):
351
352
# Sending witness data before activation is not allowed (anti-spam
352
353
# rule).
353
354
test_witness_block (self .nodes [0 ].rpc , self .test_node , block , accepted = False )
354
- # TODO: fix synchronization so we can test reject reason
355
- # Right now, bitcoind delays sending reject messages for blocks
356
- # until the future, making synchronization here difficult.
357
- # assert_equal(self.test_node.last_message["reject"].reason, "unexpected-witness")
355
+ wait_until (lambda : 'reject' in self .test_node .last_message and self .test_node .last_message ["reject" ].reason == b"unexpected-witness" )
358
356
359
357
# But it should not be permanently marked bad...
360
358
# Resend without witness information.
@@ -605,9 +603,6 @@ def test_getblocktemplate_before_lockin(self):
605
603
@subtest
606
604
def advance_to_segwit_lockin (self ):
607
605
"""Mine enough blocks to lock in segwit, but don't activate."""
608
- # TODO: we could verify that lockin only happens at the right threshold of
609
- # signalling blocks, rather than just at the right period boundary.
610
-
611
606
height = self .nodes [0 ].getblockcount ()
612
607
# Advance to end of period, and verify lock-in happens at the end
613
608
self .nodes [0 ].generate (VB_PERIOD - 1 )
@@ -741,9 +736,6 @@ def test_standardness_v0(self):
741
736
@subtest
742
737
def advance_to_segwit_active (self ):
743
738
"""Mine enough blocks to activate segwit."""
744
- # TODO: we could verify that activation only happens at the right threshold
745
- # of signalling blocks, rather than just at the right period boundary.
746
-
747
739
height = self .nodes [0 ].getblockcount ()
748
740
self .nodes [0 ].generate (VB_PERIOD - (height % VB_PERIOD ) - 2 )
749
741
assert_equal (get_bip9_status (self .nodes [0 ], 'segwit' )['status' ], 'locked_in' )
@@ -1402,30 +1394,28 @@ def test_segwit_versions(self):
1402
1394
Future segwit version transactions are non-standard, but valid in blocks.
1403
1395
Can run this before and after segwit activation."""
1404
1396
1405
- num_tests = 17 # will test OP_0, OP1, ..., OP_16
1406
- if ( len (self .utxo ) < num_tests ) :
1397
+ NUM_SEGWIT_VERSIONS = 17 # will test OP_0, OP1, ..., OP_16
1398
+ if len (self .utxo ) < NUM_SEGWIT_VERSIONS :
1407
1399
tx = CTransaction ()
1408
1400
tx .vin .append (CTxIn (COutPoint (self .utxo [0 ].sha256 , self .utxo [0 ].n ), b"" ))
1409
- split_value = (self .utxo [0 ].nValue - 4000 ) // num_tests
1410
- for i in range (num_tests ):
1401
+ split_value = (self .utxo [0 ].nValue - 4000 ) // NUM_SEGWIT_VERSIONS
1402
+ for i in range (NUM_SEGWIT_VERSIONS ):
1411
1403
tx .vout .append (CTxOut (split_value , CScript ([OP_TRUE ])))
1412
1404
tx .rehash ()
1413
1405
block = self .build_next_block ()
1414
1406
self .update_witness_block_with_transactions (block , [tx ])
1415
1407
test_witness_block (self .nodes [0 ].rpc , self .test_node , block , accepted = True )
1416
1408
self .utxo .pop (0 )
1417
- for i in range (num_tests ):
1409
+ for i in range (NUM_SEGWIT_VERSIONS ):
1418
1410
self .utxo .append (UTXO (tx .sha256 , i , split_value ))
1419
1411
1420
1412
sync_blocks (self .nodes )
1421
1413
temp_utxo = []
1422
1414
tx = CTransaction ()
1423
- count = 0
1424
1415
witness_program = CScript ([OP_TRUE ])
1425
1416
witness_hash = sha256 (witness_program )
1426
1417
assert_equal (len (self .nodes [1 ].getrawmempool ()), 0 )
1427
1418
for version in list (range (OP_1 , OP_16 + 1 )) + [OP_0 ]:
1428
- count += 1
1429
1419
# First try to spend to a future version segwit script_pubkey.
1430
1420
script_pubkey = CScript ([CScriptOp (version ), witness_hash ])
1431
1421
tx .vin = [CTxIn (COutPoint (self .utxo [0 ].sha256 , self .utxo [0 ].n ), b"" )]
@@ -1680,19 +1670,19 @@ def test_signature_version_1(self):
1680
1670
# Test combinations of signature hashes.
1681
1671
# Split the utxo into a lot of outputs.
1682
1672
# Randomly choose up to 10 to spend, sign with different hashtypes, and
1683
- # output to a random number of outputs. Repeat num_tests times.
1673
+ # output to a random number of outputs. Repeat NUM_SIGHASH_TESTS times.
1684
1674
# Ensure that we've tested a situation where we use SIGHASH_SINGLE with
1685
1675
# an input index > number of outputs.
1686
- num_tests = 500
1676
+ NUM_SIGHASH_TESTS = 500
1687
1677
temp_utxos = []
1688
1678
tx = CTransaction ()
1689
1679
tx .vin .append (CTxIn (COutPoint (prev_utxo .sha256 , prev_utxo .n ), b"" ))
1690
- split_value = prev_utxo .nValue // num_tests
1691
- for i in range (num_tests ):
1680
+ split_value = prev_utxo .nValue // NUM_SIGHASH_TESTS
1681
+ for i in range (NUM_SIGHASH_TESTS ):
1692
1682
tx .vout .append (CTxOut (split_value , script_pubkey ))
1693
1683
tx .wit .vtxinwit .append (CTxInWitness ())
1694
1684
sign_p2pk_witness_input (witness_program , tx , 0 , SIGHASH_ALL , prev_utxo .nValue , key )
1695
- for i in range (num_tests ):
1685
+ for i in range (NUM_SIGHASH_TESTS ):
1696
1686
temp_utxos .append (UTXO (tx .sha256 , i , split_value ))
1697
1687
1698
1688
block = self .build_next_block ()
@@ -1701,7 +1691,7 @@ def test_signature_version_1(self):
1701
1691
1702
1692
block = self .build_next_block ()
1703
1693
used_sighash_single_out_of_bounds = False
1704
- for i in range (num_tests ):
1694
+ for i in range (NUM_SIGHASH_TESTS ):
1705
1695
# Ping regularly to keep the connection alive
1706
1696
if (not i % 100 ):
1707
1697
self .test_node .sync_with_ping ()
0 commit comments