Skip to content

Commit e97b113

Browse files
committed
[tests] Change invalidblockrequest to use BitcoinTestFramework
[tests] update tests from changes to mininode in #11771 - added by @conscott [tests] trivial update to hex conversion for readability - added by @conscott
1 parent 2b7064e commit e97b113

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

test/functional/p2p_invalid_block.py

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,62 @@
1111
re-requested.
1212
"""
1313
import copy
14-
import time
1514

1615
from test_framework.blocktools import create_block, create_coinbase, create_transaction
17-
from test_framework.comptool import RejectResult, TestInstance, TestManager
1816
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
2119
from test_framework.util import assert_equal
2220

23-
class InvalidBlockRequestTest(ComparisonTestFramework):
21+
class InvalidBlockRequestTest(BitcoinTestFramework):
2422
def set_test_params(self):
2523
self.num_nodes = 1
2624
self.setup_clean_chain = True
25+
self.extra_args = [["-whitelist=127.0.0.1"]]
2726

2827
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+
3332
network_thread_start()
34-
test.run()
33+
node.p2p.wait_for_verack()
3534

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
4039

4140
self.log.info("Create a new block with an anyone-can-spend coinbase")
4241

4342
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)
4644
block.solve()
4745
# 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)
5249

5350
self.log.info("Mature the block.")
51+
node.generate(100)
5452

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
6457

6558
# Use merkle-root malleability to generate an invalid block with
6659
# same blockheader.
6760
# Manufacture a block with 3 transactions (coinbase, spend of prior
6861
# coinbase, spend of that spend). Duplicate the 3rd transaction to
6962
# leave merkle root and blockheader unchanged but invalidate the block.
7063
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
7367

7468
# 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)
7670
tx2 = create_transaction(tx1, 0, b'\x51', 50 * COIN)
7771

7872
block2.vtx.extend([tx1, tx2])
@@ -88,23 +82,20 @@ def get_tests(self):
8882
assert_equal(orig_hash, block2.rehash())
8983
assert(block2_orig.vtx != block2.vtx)
9084

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')
9486

9587
self.log.info("Test very broken block.")
9688

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
9991
block3.vtx[0].vout[0].nValue = 100 * COIN # Too high!
10092
block3.vtx[0].sha256 = None
10193
block3.vtx[0].calc_sha256()
10294
block3.hashMerkleRoot = block3.calc_merkle_root()
10395
block3.rehash()
10496
block3.solve()
10597

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')
10899

109100
if __name__ == '__main__':
110101
InvalidBlockRequestTest().main()

0 commit comments

Comments
 (0)