MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / run_test

Method run_test

test/functional/p2p_invalid_block.py:41–138  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

39 self.noban_tx_relay = True
40
41 def run_test(self):
42 # Add p2p connection to node0
43 node = self.nodes[0] # convenience reference to the node
44 peer = node.add_p2p_connection(P2PDataStore())
45
46 best_block = node.getblock(node.getbestblockhash())
47 tip = int(node.getbestblockhash(), 16)
48 height = best_block["height"] + 1
49 block_time = best_block["time"] + 1
50
51 self.log.info("Create a new block with an anyone-can-spend coinbase")
52
53 block = create_block(tip, height=height, ntime=block_time)
54 block.solve()
55 # Save the coinbase for later
56 block1 = block
57 peer.send_blocks_and_test([block1], node, success=True)
58
59 self.log.info("Mature the block.")
60 self.generatetoaddress(node, 100, node.get_deterministic_priv_key().address)
61
62 best_block = node.getblock(node.getbestblockhash())
63 tip = int(node.getbestblockhash(), 16)
64 height = best_block["height"] + 1
65 block_time = best_block["time"] + 1
66
67 # Use merkle-root malleability to generate an invalid block with
68 # same blockheader (CVE-2012-2459).
69 # Manufacture a block with 3 transactions (coinbase, spend of prior
70 # coinbase, spend of that spend). Duplicate the 3rd transaction to
71 # leave merkle root and blockheader unchanged but invalidate the block.
72 # For more information on merkle-root malleability see src/consensus/merkle.cpp.
73 self.log.info("Test merkle root malleability.")
74
75 tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=bytes([OP_TRUE]), amount=50 * COIN)
76 tx2 = create_tx_with_script(tx1, 0, script_sig=bytes([OP_TRUE]), amount=50 * COIN)
77 block2 = create_block(tip, height=height, ntime=block_time, txlist=[tx1, tx2])
78 block_time += 1
79 block2.solve()
80 orig_hash = block2.hash_int
81 block2_orig = copy.deepcopy(block2)
82
83 # Mutate block 2
84 block2.vtx.append(tx2)
85 assert_equal(block2.hashMerkleRoot, block2.calc_merkle_root())
86 assert_equal(orig_hash, block2.hash_int)
87 assert_not_equal(block2_orig.vtx, block2.vtx)
88
89 peer.send_blocks_and_test([block2], node, success=False, reject_reason='bad-txns-duplicate')
90
91 # Check transactions for duplicate inputs (CVE-2018-17144)
92 self.log.info("Test duplicate input block.")
93
94 block2_dup = copy.deepcopy(block2_orig)
95 block2_dup.vtx[2].vin.append(block2_dup.vtx[2].vin[0])
96 block2_dup.hashMerkleRoot = block2_dup.calc_merkle_root()
97 block2_dup.solve()
98 peer.send_blocks_and_test([block2_dup], node, success=False, reject_reason='bad-txns-inputs-duplicate')

Callers

nothing calls this directly

Calls 14

P2PDataStoreClass · 0.90
create_blockFunction · 0.90
create_tx_with_scriptFunction · 0.90
assert_equalFunction · 0.90
assert_not_equalFunction · 0.90
create_coinbaseFunction · 0.90
add_p2p_connectionMethod · 0.80
infoMethod · 0.80
solveMethod · 0.80
send_blocks_and_testMethod · 0.80
calc_merkle_rootMethod · 0.80

Tested by

no test coverage detected