11
11
re-requested.
12
12
"""
13
13
import copy
14
- import time
15
14
16
15
from test_framework .blocktools import create_block , create_coinbase , create_transaction
17
- from test_framework .comptool import RejectResult , TestInstance , TestManager
18
16
from test_framework .messages import COIN
19
- from test_framework .mininode import network_thread_start
20
- from test_framework .test_framework import ComparisonTestFramework
17
+ from test_framework .mininode import network_thread_start , P2PDataStore
18
+ from test_framework .test_framework import BitcoinTestFramework
21
19
from test_framework .util import assert_equal
22
20
23
- class InvalidBlockRequestTest (ComparisonTestFramework ):
21
+ class InvalidBlockRequestTest (BitcoinTestFramework ):
24
22
def set_test_params (self ):
25
23
self .num_nodes = 1
26
24
self .setup_clean_chain = True
25
+ self .extra_args = [["-whitelist=127.0.0.1" ]]
27
26
28
27
def run_test (self ):
29
- test = TestManager ( self , self . options . tmpdir )
30
- test . add_all_connections ( self .nodes )
31
- self . tip = None
32
- self . block_time = None
28
+ # Add p2p connection to node0
29
+ node = self .nodes [ 0 ] # convenience reference to the node
30
+ node . add_p2p_connection ( P2PDataStore ())
31
+
33
32
network_thread_start ()
34
- test . run ()
33
+ node . p2p . wait_for_verack ()
35
34
36
- def get_tests ( self ):
37
- if self . tip is None :
38
- self . tip = int ( "0x" + self . nodes [ 0 ]. getbestblockhash (), 0 )
39
- self . block_time = int ( time . time ()) + 1
35
+ best_block = node . getblock ( node . getbestblockhash ())
36
+ tip = int ( node . getbestblockhash (), 16 )
37
+ height = best_block [ "height" ] + 1
38
+ block_time = best_block [ " time" ] + 1
40
39
41
40
self .log .info ("Create a new block with an anyone-can-spend coinbase" )
42
41
43
42
height = 1
44
- block = create_block (self .tip , create_coinbase (height ), self .block_time )
45
- self .block_time += 1
43
+ block = create_block (tip , create_coinbase (height ), block_time )
46
44
block .solve ()
47
45
# Save the coinbase for later
48
- self .block1 = block
49
- self .tip = block .sha256
50
- height += 1
51
- yield TestInstance ([[block , True ]])
46
+ block1 = block
47
+ tip = block .sha256
48
+ node .p2p .send_blocks_and_test ([block1 ], node , True )
52
49
53
50
self .log .info ("Mature the block." )
51
+ node .generate (100 )
54
52
55
- test = TestInstance (sync_every_block = False )
56
- for i in range (100 ):
57
- block = create_block (self .tip , create_coinbase (height ), self .block_time )
58
- block .solve ()
59
- self .tip = block .sha256
60
- self .block_time += 1
61
- test .blocks_and_transactions .append ([block , True ])
62
- height += 1
63
- yield test
53
+ best_block = node .getblock (node .getbestblockhash ())
54
+ tip = int (node .getbestblockhash (), 16 )
55
+ height = best_block ["height" ] + 1
56
+ block_time = best_block ["time" ] + 1
64
57
65
58
# Use merkle-root malleability to generate an invalid block with
66
59
# same blockheader.
67
60
# Manufacture a block with 3 transactions (coinbase, spend of prior
68
61
# coinbase, spend of that spend). Duplicate the 3rd transaction to
69
62
# leave merkle root and blockheader unchanged but invalidate the block.
70
63
self .log .info ("Test merkle root malleability." )
71
- block2 = create_block (self .tip , create_coinbase (height ), self .block_time )
72
- self .block_time += 1
64
+
65
+ block2 = create_block (tip , create_coinbase (height ), block_time )
66
+ block_time += 1
73
67
74
68
# b'0x51' is OP_TRUE
75
- tx1 = create_transaction (self . block1 .vtx [0 ], 0 , b'\x51 ' , 50 * COIN )
69
+ tx1 = create_transaction (block1 .vtx [0 ], 0 , b'\x51 ' , 50 * COIN )
76
70
tx2 = create_transaction (tx1 , 0 , b'\x51 ' , 50 * COIN )
77
71
78
72
block2 .vtx .extend ([tx1 , tx2 ])
@@ -88,23 +82,20 @@ def get_tests(self):
88
82
assert_equal (orig_hash , block2 .rehash ())
89
83
assert (block2_orig .vtx != block2 .vtx )
90
84
91
- self .tip = block2 .sha256
92
- yield TestInstance ([[block2 , RejectResult (16 , b'bad-txns-duplicate' )], [block2_orig , True ]])
93
- height += 1
85
+ node .p2p .send_blocks_and_test ([block2 ], node , False , False , 16 , b'bad-txns-duplicate' )
94
86
95
87
self .log .info ("Test very broken block." )
96
88
97
- block3 = create_block (self . tip , create_coinbase (height ), self . block_time )
98
- self . block_time += 1
89
+ block3 = create_block (tip , create_coinbase (height ), block_time )
90
+ block_time += 1
99
91
block3 .vtx [0 ].vout [0 ].nValue = 100 * COIN # Too high!
100
92
block3 .vtx [0 ].sha256 = None
101
93
block3 .vtx [0 ].calc_sha256 ()
102
94
block3 .hashMerkleRoot = block3 .calc_merkle_root ()
103
95
block3 .rehash ()
104
96
block3 .solve ()
105
97
106
- yield TestInstance ([[block3 , RejectResult (16 , b'bad-cb-amount' )]])
107
-
98
+ node .p2p .send_blocks_and_test ([block3 ], node , False , False , 16 , b'bad-cb-amount' )
108
99
109
100
if __name__ == '__main__' :
110
101
InvalidBlockRequestTest ().main ()
0 commit comments