mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-12 21:52:41 +02:00
test: move gbt proposal mode tests to new file
Additionally this commit gives each test its own function. The assert_submitblock helper is absorbed into assert_template. Review hint: git show --color-moved=dimmed-zebra
This commit is contained in:
@ -5,8 +5,10 @@
|
||||
"""Test mining RPCs
|
||||
|
||||
- getmininginfo
|
||||
- getblocktemplate proposal mode
|
||||
- submitblock"""
|
||||
- getblocktemplate
|
||||
- submitblock
|
||||
|
||||
mining_template_verification.py tests getblocktemplate in proposal mode"""
|
||||
|
||||
import copy
|
||||
from decimal import Decimal
|
||||
@ -54,18 +56,6 @@ VERSIONBITS_TOP_BITS = 0x20000000
|
||||
VERSIONBITS_DEPLOYMENT_TESTDUMMY_BIT = 28
|
||||
DEFAULT_BLOCK_MIN_TX_FEE = 1000 # default `-blockmintxfee` setting [sat/kvB]
|
||||
|
||||
|
||||
def assert_template(node, block, expect, rehash=True):
|
||||
if rehash:
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
rsp = node.getblocktemplate(template_request={
|
||||
'data': block.serialize().hex(),
|
||||
'mode': 'proposal',
|
||||
'rules': ['segwit'],
|
||||
})
|
||||
assert_equal(rsp, expect)
|
||||
|
||||
|
||||
class MiningTest(BitcoinTestFramework):
|
||||
def set_test_params(self):
|
||||
self.num_nodes = 3
|
||||
@ -225,8 +215,13 @@ class MiningTest(BitcoinTestFramework):
|
||||
block.nBits = int(tmpl["bits"], 16)
|
||||
block.nNonce = 0
|
||||
block.vtx = [create_coinbase(height=int(tmpl["height"]))]
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
block.solve()
|
||||
assert_template(node, block, None)
|
||||
assert_equal(node.getblocktemplate(template_request={
|
||||
'data': block.serialize().hex(),
|
||||
'mode': 'proposal',
|
||||
'rules': ['segwit'],
|
||||
}), None)
|
||||
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.nTime = t
|
||||
@ -376,12 +371,6 @@ class MiningTest(BitcoinTestFramework):
|
||||
self.wallet = MiniWallet(node)
|
||||
self.mine_chain()
|
||||
|
||||
def assert_submitblock(block, result_str_1, result_str_2=None):
|
||||
block.solve()
|
||||
result_str_2 = result_str_2 or 'duplicate-invalid'
|
||||
assert_equal(result_str_1, node.submitblock(hexdata=block.serialize().hex()))
|
||||
assert_equal(result_str_2, node.submitblock(hexdata=block.serialize().hex()))
|
||||
|
||||
self.log.info('getmininginfo')
|
||||
mining_info = node.getmininginfo()
|
||||
assert_equal(mining_info['blocks'], 200)
|
||||
@ -433,103 +422,24 @@ class MiningTest(BitcoinTestFramework):
|
||||
block.nBits = int(tmpl["bits"], 16)
|
||||
block.nNonce = 0
|
||||
block.vtx = [coinbase_tx]
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
|
||||
self.log.info("getblocktemplate: segwit rule must be set")
|
||||
assert_raises_rpc_error(-8, "getblocktemplate must be called with the segwit rule set", node.getblocktemplate, {})
|
||||
|
||||
self.log.info("getblocktemplate: Test valid block")
|
||||
assert_template(node, block, None)
|
||||
|
||||
self.log.info("submitblock: Test block decode failure")
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.submitblock, block.serialize()[:-15].hex())
|
||||
|
||||
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.vtx[0].vin[0].prevout.hash += 1
|
||||
assert_template(node, bad_block, 'bad-cb-missing')
|
||||
|
||||
self.log.info("submitblock: Test bad input hash for coinbase transaction")
|
||||
bad_block.solve()
|
||||
assert_equal("bad-cb-missing", node.submitblock(hexdata=bad_block.serialize().hex()))
|
||||
|
||||
self.log.info("submitblock: Test block with no transactions")
|
||||
no_tx_block = copy.deepcopy(block)
|
||||
no_tx_block.vtx.clear()
|
||||
no_tx_block.hashMerkleRoot = 0
|
||||
no_tx_block.solve()
|
||||
assert_equal("bad-blk-length", node.submitblock(hexdata=no_tx_block.serialize().hex()))
|
||||
|
||||
self.log.info("submitblock: Test empty block")
|
||||
assert_equal('high-hash', node.submitblock(hexdata=CBlock().serialize().hex()))
|
||||
|
||||
self.log.info("getblocktemplate: Test truncated final transaction")
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {
|
||||
'data': block.serialize()[:-1].hex(),
|
||||
'mode': 'proposal',
|
||||
'rules': ['segwit'],
|
||||
})
|
||||
|
||||
self.log.info("getblocktemplate: Test duplicate transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.vtx.append(bad_block.vtx[0])
|
||||
assert_template(node, bad_block, 'bad-txns-duplicate')
|
||||
assert_submitblock(bad_block, 'bad-txns-duplicate', 'bad-txns-duplicate')
|
||||
|
||||
self.log.info("getblocktemplate: Test invalid transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_tx = copy.deepcopy(bad_block.vtx[0])
|
||||
bad_tx.vin[0].prevout.hash = 255
|
||||
bad_block.vtx.append(bad_tx)
|
||||
assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
|
||||
assert_submitblock(bad_block, 'bad-txns-inputs-missingorspent')
|
||||
|
||||
self.log.info("getblocktemplate: Test nonfinal transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.vtx[0].nLockTime = 2**32 - 1
|
||||
assert_template(node, bad_block, 'bad-txns-nonfinal')
|
||||
assert_submitblock(bad_block, 'bad-txns-nonfinal')
|
||||
|
||||
self.log.info("getblocktemplate: Test bad tx count")
|
||||
# The tx count is immediately after the block header
|
||||
bad_block_sn = bytearray(block.serialize())
|
||||
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1)
|
||||
bad_block_sn[BLOCK_HEADER_SIZE] += 1
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {
|
||||
'data': bad_block_sn.hex(),
|
||||
'mode': 'proposal',
|
||||
'rules': ['segwit'],
|
||||
})
|
||||
|
||||
self.log.info("getblocktemplate: Test bad bits")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.nBits = 469762303 # impossible in the real world
|
||||
assert_template(node, bad_block, 'bad-diffbits')
|
||||
|
||||
self.log.info("getblocktemplate: Test bad merkle root")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.hashMerkleRoot += 1
|
||||
assert_template(node, bad_block, 'bad-txnmrklroot', False)
|
||||
assert_submitblock(bad_block, 'bad-txnmrklroot', 'bad-txnmrklroot')
|
||||
|
||||
self.log.info("getblocktemplate: Test bad timestamps")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.nTime = 2**32 - 1
|
||||
assert_template(node, bad_block, 'time-too-new')
|
||||
assert_submitblock(bad_block, 'time-too-new', 'time-too-new')
|
||||
bad_block.nTime = 0
|
||||
assert_template(node, bad_block, 'time-too-old')
|
||||
assert_submitblock(bad_block, 'time-too-old', 'time-too-old')
|
||||
|
||||
self.log.info("getblocktemplate: Test not best block")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.hashPrevBlock = 123
|
||||
assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
|
||||
assert_submitblock(bad_block, 'prev-blk-not-found', 'prev-blk-not-found')
|
||||
|
||||
self.log.info('submitheader tests')
|
||||
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * BLOCK_HEADER_SIZE))
|
||||
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * (BLOCK_HEADER_SIZE-2)))
|
||||
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
|
||||
|
||||
missing_ancestor_block = copy.deepcopy(block)
|
||||
missing_ancestor_block.hashPrevBlock = 123
|
||||
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, missing_ancestor_block).serialize().hex()))
|
||||
|
||||
block.nTime += 1
|
||||
block.solve()
|
||||
|
169
test/functional/mining_template_verification.py
Executable file
169
test/functional/mining_template_verification.py
Executable file
@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2024-Present The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test getblocktemplate RPC in proposal mode
|
||||
|
||||
Generate several blocks and test them against the getblocktemplate RPC.
|
||||
"""
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import copy
|
||||
|
||||
from test_framework.blocktools import (
|
||||
create_block,
|
||||
create_coinbase,
|
||||
)
|
||||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
assert_equal,
|
||||
assert_raises_rpc_error,
|
||||
)
|
||||
|
||||
from test_framework.messages import (
|
||||
BLOCK_HEADER_SIZE,
|
||||
)
|
||||
|
||||
def assert_template(node, block, expect, *, rehash=True, submit=True, solve=True, expect_submit=None):
|
||||
if rehash:
|
||||
block.hashMerkleRoot = block.calc_merkle_root()
|
||||
|
||||
rsp = node.getblocktemplate(template_request={
|
||||
'data': block.serialize().hex(),
|
||||
'mode': 'proposal',
|
||||
'rules': ['segwit'],
|
||||
})
|
||||
assert_equal(rsp, expect)
|
||||
# Only attempt to submit invalid templates
|
||||
if submit and expect is not None:
|
||||
# submitblock runs checks in a different order, so may not return
|
||||
# the same error
|
||||
if expect_submit is None:
|
||||
expect_submit = expect
|
||||
if solve:
|
||||
block.solve()
|
||||
assert_equal(node.submitblock(block.serialize().hex()), expect_submit)
|
||||
|
||||
class MiningTemplateVerificationTest(BitcoinTestFramework):
|
||||
|
||||
def set_test_params(self):
|
||||
self.num_nodes = 1
|
||||
|
||||
def valid_block_test(self, node, block):
|
||||
self.log.info("Valid block")
|
||||
assert_template(node, block, None)
|
||||
|
||||
def cb_missing_test(self, node, block):
|
||||
self.log.info("Bad input hash for coinbase transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.vtx[0].vin[0].prevout.hash += 1
|
||||
assert_template(node, bad_block, 'bad-cb-missing')
|
||||
|
||||
def block_without_transactions_test(self, node, block):
|
||||
self.log.info("Block with no transactions")
|
||||
|
||||
no_tx_block = copy.deepcopy(block)
|
||||
no_tx_block.vtx.clear()
|
||||
no_tx_block.hashMerkleRoot = 0
|
||||
no_tx_block.solve()
|
||||
assert_template(node, no_tx_block, 'bad-blk-length', rehash=False)
|
||||
|
||||
def truncated_final_transaction_test(self, node, block):
|
||||
self.log.info("Truncated final transaction")
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate,
|
||||
template_request={
|
||||
"data": block.serialize()[:-1].hex(),
|
||||
"mode": "proposal",
|
||||
"rules": ["segwit"],
|
||||
}
|
||||
)
|
||||
|
||||
def duplicate_transaction_test(self, node, block):
|
||||
self.log.info("Duplicate transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.vtx.append(bad_block.vtx[0])
|
||||
assert_template(node, bad_block, 'bad-txns-duplicate')
|
||||
|
||||
def thin_air_spending_test(self, node, block):
|
||||
self.log.info("Transaction that spends from thin air")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_tx = copy.deepcopy(bad_block.vtx[0])
|
||||
bad_tx.vin[0].prevout.hash = 255
|
||||
bad_block.vtx.append(bad_tx)
|
||||
assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
|
||||
|
||||
def non_final_transaction_test(self, node, block):
|
||||
self.log.info("Non-final transaction")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.vtx[0].nLockTime = 2**32 - 1
|
||||
assert_template(node, bad_block, 'bad-txns-nonfinal')
|
||||
|
||||
def bad_tx_count_test(self, node, block):
|
||||
self.log.info("Bad tx count")
|
||||
# The tx count is immediately after the block header
|
||||
bad_block_sn = bytearray(block.serialize())
|
||||
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 1)
|
||||
bad_block_sn[BLOCK_HEADER_SIZE] += 1
|
||||
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {
|
||||
'data': bad_block_sn.hex(),
|
||||
'mode': 'proposal',
|
||||
'rules': ['segwit'],
|
||||
})
|
||||
|
||||
def nbits_test(self, node, block):
|
||||
self.log.info("Extremely high nBits")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.nBits = 469762303 # impossible in the real world
|
||||
assert_template(node, bad_block, "bad-diffbits", solve=False, expect_submit="high-hash")
|
||||
|
||||
def merkle_root_test(self, node, block):
|
||||
self.log.info("Bad merkle root")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.hashMerkleRoot += 1
|
||||
assert_template(node, bad_block, 'bad-txnmrklroot', rehash=False)
|
||||
|
||||
def bad_timestamp_test(self, node, block):
|
||||
self.log.info("Bad timestamps")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.nTime = 2**32 - 1
|
||||
assert_template(node, bad_block, 'time-too-new')
|
||||
bad_block.nTime = 0
|
||||
assert_template(node, bad_block, 'time-too-old')
|
||||
|
||||
def current_tip_test(self, node, block):
|
||||
self.log.info("Block must build on the current tip")
|
||||
bad_block = copy.deepcopy(block)
|
||||
bad_block.hashPrevBlock = 123
|
||||
bad_block.solve()
|
||||
|
||||
assert_template(node, bad_block, "inconclusive-not-best-prevblk", expect_submit="prev-blk-not-found")
|
||||
|
||||
def run_test(self):
|
||||
node = self.nodes[0]
|
||||
|
||||
block_0_height = node.getblockcount()
|
||||
self.generate(node, sync_fun=self.no_op, nblocks=1)
|
||||
block_1 = node.getblock(node.getbestblockhash())
|
||||
block_2 = create_block(
|
||||
int(block_1["hash"], 16),
|
||||
create_coinbase(block_0_height + 2),
|
||||
block_1["mediantime"] + 1,
|
||||
)
|
||||
|
||||
self.valid_block_test(node, block_2)
|
||||
self.cb_missing_test(node, block_2)
|
||||
self.block_without_transactions_test(node, block_2)
|
||||
self.truncated_final_transaction_test(node, block_2)
|
||||
self.duplicate_transaction_test(node, block_2)
|
||||
self.thin_air_spending_test(node, block_2)
|
||||
self.non_final_transaction_test(node, block_2)
|
||||
self.bad_tx_count_test(node, block_2)
|
||||
self.nbits_test(node, block_2)
|
||||
self.merkle_root_test(node, block_2)
|
||||
self.bad_timestamp_test(node, block_2)
|
||||
self.current_tip_test(node, block_2)
|
||||
|
||||
if __name__ == "__main__":
|
||||
MiningTemplateVerificationTest(__file__).main()
|
@ -209,6 +209,7 @@ BASE_SCRIPTS = [
|
||||
'rpc_decodescript.py',
|
||||
'rpc_blockchain.py --v1transport',
|
||||
'rpc_blockchain.py --v2transport',
|
||||
'mining_template_verification.py',
|
||||
'rpc_deprecated.py',
|
||||
'wallet_disable.py',
|
||||
'wallet_change_address.py',
|
||||
|
Reference in New Issue
Block a user