|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2024-Present The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test getblocktemplate RPC in proposal mode |
| 6 | +
|
| 7 | +Generate several blocks and test them against the getblocktemplate RPC. |
| 8 | +""" |
| 9 | + |
| 10 | +from concurrent.futures import ThreadPoolExecutor |
| 11 | + |
| 12 | +import copy |
| 13 | + |
| 14 | +from test_framework.blocktools import ( |
| 15 | + create_block, |
| 16 | + create_coinbase, |
| 17 | +) |
| 18 | + |
| 19 | +from test_framework.test_framework import BitcoinTestFramework |
| 20 | +from test_framework.util import ( |
| 21 | + assert_equal, |
| 22 | + assert_raises_rpc_error, |
| 23 | +) |
| 24 | + |
| 25 | +from test_framework.messages import ( |
| 26 | + BLOCK_HEADER_SIZE, |
| 27 | +) |
| 28 | + |
| 29 | +def assert_template(node, block, expect, *, rehash=True, submit=True, solve=True, expect_submit=None): |
| 30 | + if rehash: |
| 31 | + block.hashMerkleRoot = block.calc_merkle_root() |
| 32 | + |
| 33 | + rsp = node.getblocktemplate(template_request={ |
| 34 | + 'data': block.serialize().hex(), |
| 35 | + 'mode': 'proposal', |
| 36 | + 'rules': ['segwit'], |
| 37 | + }) |
| 38 | + assert_equal(rsp, expect) |
| 39 | + # Only attempt to submit invalid templates |
| 40 | + if submit and expect is not None: |
| 41 | + # submitblock runs checks in a different order, so may not return |
| 42 | + # the same error |
| 43 | + if expect_submit is None: |
| 44 | + expect_submit = expect |
| 45 | + if solve: |
| 46 | + block.solve() |
| 47 | + assert_equal(node.submitblock(block.serialize().hex()), expect_submit) |
| 48 | + |
| 49 | +class MiningTemplateVerificationTest(BitcoinTestFramework): |
| 50 | + |
| 51 | + def set_test_params(self): |
| 52 | + self.num_nodes = 1 |
| 53 | + |
| 54 | + def valid_block_test(self, node, block): |
| 55 | + self.log.info("Valid block") |
| 56 | + assert_template(node, block, None) |
| 57 | + |
| 58 | + def cb_missing_test(self, node, block): |
| 59 | + self.log.info("Bad input hash for coinbase transaction") |
| 60 | + bad_block = copy.deepcopy(block) |
| 61 | + bad_block.vtx[0].vin[0].prevout.hash += 1 |
| 62 | + assert_template(node, bad_block, 'bad-cb-missing') |
| 63 | + |
| 64 | + def block_without_transactions_test(self, node, block): |
| 65 | + self.log.info("Block with no transactions") |
| 66 | + |
| 67 | + no_tx_block = copy.deepcopy(block) |
| 68 | + no_tx_block.vtx.clear() |
| 69 | + no_tx_block.hashMerkleRoot = 0 |
| 70 | + no_tx_block.solve() |
| 71 | + assert_template(node, no_tx_block, 'bad-blk-length', rehash=False) |
| 72 | + |
| 73 | + def truncated_final_transaction_test(self, node, block): |
| 74 | + self.log.info("Truncated final transaction") |
| 75 | + assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, |
| 76 | + template_request={ |
| 77 | + "data": block.serialize()[:-1].hex(), |
| 78 | + "mode": "proposal", |
| 79 | + "rules": ["segwit"], |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + def duplicate_transaction_test(self, node, block): |
| 84 | + self.log.info("Duplicate transaction") |
| 85 | + bad_block = copy.deepcopy(block) |
| 86 | + bad_block.vtx.append(bad_block.vtx[0]) |
| 87 | + assert_template(node, bad_block, 'bad-txns-duplicate') |
| 88 | + |
| 89 | + def thin_air_spending_test(self, node, block): |
| 90 | + self.log.info("Transaction that spends from thin air") |
| 91 | + bad_block = copy.deepcopy(block) |
| 92 | + bad_tx = copy.deepcopy(bad_block.vtx[0]) |
| 93 | + bad_tx.vin[0].prevout.hash = 255 |
| 94 | + bad_block.vtx.append(bad_tx) |
| 95 | + assert_template(node, bad_block, 'bad-txns-inputs-missingorspent') |
| 96 | + |
| 97 | + def non_final_transaction_test(self, node, block): |
| 98 | + self.log.info("Non-final transaction") |
| 99 | + bad_block = copy.deepcopy(block) |
| 100 | + bad_block.vtx[0].nLockTime = 2**32 - 1 |
| 101 | + assert_template(node, bad_block, 'bad-txns-nonfinal') |
| 102 | + |
| 103 | + def bad_tx_count_test(self, node, block): |
| 104 | + self.log.info("Bad tx count") |
| 105 | + # The tx count is immediately after the block header |
| 106 | + bad_block_sn = bytearray(block.serialize()) |
| 107 | + assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1) |
| 108 | + bad_block_sn[BLOCK_HEADER_SIZE] += 1 |
| 109 | + assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, { |
| 110 | + 'data': bad_block_sn.hex(), |
| 111 | + 'mode': 'proposal', |
| 112 | + 'rules': ['segwit'], |
| 113 | + }) |
| 114 | + |
| 115 | + def nbits_test(self, node, block): |
| 116 | + self.log.info("Extremely high nBits") |
| 117 | + bad_block = copy.deepcopy(block) |
| 118 | + bad_block.nBits = 469762303 # impossible in the real world |
| 119 | + assert_template(node, bad_block, "bad-diffbits", solve=False, expect_submit="high-hash") |
| 120 | + |
| 121 | + def merkle_root_test(self, node, block): |
| 122 | + self.log.info("Bad merkle root") |
| 123 | + bad_block = copy.deepcopy(block) |
| 124 | + bad_block.hashMerkleRoot += 1 |
| 125 | + assert_template(node, bad_block, 'bad-txnmrklroot', rehash=False) |
| 126 | + |
| 127 | + def bad_timestamp_test(self, node, block): |
| 128 | + self.log.info("Bad timestamps") |
| 129 | + bad_block = copy.deepcopy(block) |
| 130 | + bad_block.nTime = 2**32 - 1 |
| 131 | + assert_template(node, bad_block, 'time-too-new') |
| 132 | + bad_block.nTime = 0 |
| 133 | + assert_template(node, bad_block, 'time-too-old') |
| 134 | + |
| 135 | + def current_tip_test(self, node, block): |
| 136 | + self.log.info("Block must build on the current tip") |
| 137 | + bad_block = copy.deepcopy(block) |
| 138 | + bad_block.hashPrevBlock = 123 |
| 139 | + bad_block.solve() |
| 140 | + |
| 141 | + assert_template(node, bad_block, "inconclusive-not-best-prevblk", expect_submit="prev-blk-not-found") |
| 142 | + |
| 143 | + def run_test(self): |
| 144 | + node = self.nodes[0] |
| 145 | + |
| 146 | + block_0_height = node.getblockcount() |
| 147 | + self.generate(node, sync_fun=self.no_op, nblocks=1) |
| 148 | + block_1 = node.getblock(node.getbestblockhash()) |
| 149 | + block_2 = create_block( |
| 150 | + int(block_1["hash"], 16), |
| 151 | + create_coinbase(block_0_height + 2), |
| 152 | + block_1["mediantime"] + 1, |
| 153 | + ) |
| 154 | + |
| 155 | + self.valid_block_test(node, block_2) |
| 156 | + self.cb_missing_test(node, block_2) |
| 157 | + self.block_without_transactions_test(node, block_2) |
| 158 | + self.truncated_final_transaction_test(node, block_2) |
| 159 | + self.duplicate_transaction_test(node, block_2) |
| 160 | + self.thin_air_spending_test(node, block_2) |
| 161 | + self.non_final_transaction_test(node, block_2) |
| 162 | + self.bad_tx_count_test(node, block_2) |
| 163 | + self.nbits_test(node, block_2) |
| 164 | + self.merkle_root_test(node, block_2) |
| 165 | + self.bad_timestamp_test(node, block_2) |
| 166 | + self.current_tip_test(node, block_2) |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + MiningTemplateVerificationTest(__file__).main() |
0 commit comments