2016-03-19 12:58:06 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2014-11-17 10:47:40 -08:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2017-01-17 15:34:40 -08:00
|
|
|
"""Test block proposals with getblocktemplate."""
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 13:42:12 -07:00
|
|
|
from binascii import b2a_hex
|
|
|
|
import copy
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 12:09:19 -07:00
|
|
|
from test_framework.blocktools import create_coinbase
|
2017-04-11 10:03:03 -07:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
2017-04-11 13:42:12 -07:00
|
|
|
from test_framework.mininode import CBlock
|
2017-04-11 10:03:03 -07:00
|
|
|
from test_framework.util import *
|
|
|
|
|
2014-11-17 10:47:40 -08:00
|
|
|
def b2x(b):
|
|
|
|
return b2a_hex(b).decode('ascii')
|
|
|
|
|
2017-04-11 13:42:12 -07:00
|
|
|
def assert_template(node, block, expect, rehash=True):
|
|
|
|
if rehash:
|
|
|
|
block.hashMerkleRoot = block.calc_merkle_root()
|
|
|
|
rsp = node.getblocktemplate({'data': b2x(block.serialize()), 'mode': 'proposal'})
|
2017-04-11 12:13:09 -07:00
|
|
|
assert_equal(rsp, expect)
|
2014-11-17 10:47:40 -08:00
|
|
|
|
|
|
|
class GetBlockTemplateProposalTest(BitcoinTestFramework):
|
|
|
|
|
2016-05-14 04:01:31 -07:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.num_nodes = 2
|
|
|
|
self.setup_clean_chain = False
|
|
|
|
|
2014-11-17 10:47:40 -08:00
|
|
|
def run_test(self):
|
|
|
|
node = self.nodes[0]
|
2017-04-11 10:05:41 -07:00
|
|
|
# Mine a block to leave initial block download
|
|
|
|
node.generate(1)
|
2014-11-17 10:47:40 -08:00
|
|
|
tmpl = node.getblocktemplate()
|
2017-04-11 13:42:12 -07:00
|
|
|
self.log.info("getblocktemplate: Test capability advertised")
|
|
|
|
assert 'proposal' in tmpl['capabilities']
|
2017-04-11 12:09:19 -07:00
|
|
|
assert 'coinbasetxn' not in tmpl
|
|
|
|
|
|
|
|
coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
|
|
|
|
# sequence numbers must not be max for nLockTime to have effect
|
|
|
|
coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
|
2017-04-11 13:42:12 -07:00
|
|
|
coinbase_tx.rehash()
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 13:42:12 -07:00
|
|
|
block = CBlock()
|
|
|
|
block.nVersion = tmpl["version"]
|
|
|
|
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
|
|
|
|
block.nTime = tmpl["curtime"]
|
|
|
|
block.nBits = int(tmpl["bits"], 16)
|
|
|
|
block.nNonce = 0
|
|
|
|
block.vtx = [coinbase_tx]
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 13:39:37 -07:00
|
|
|
self.log.info("getblocktemplate: Test valid block")
|
|
|
|
assert_template(node, block, None)
|
|
|
|
|
2017-04-11 13:42:35 -07:00
|
|
|
self.log.info("submitblock: Test block decode failure")
|
|
|
|
assert_raises_jsonrpc(-22, "Block decode failed", node.submitblock, b2x(block.serialize()[:-15]))
|
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.vtx[0].vin[0].prevout.hash += 1
|
|
|
|
bad_block.vtx[0].rehash()
|
|
|
|
assert_template(node, bad_block, 'bad-cb-missing')
|
|
|
|
|
2017-04-11 13:42:35 -07:00
|
|
|
self.log.info("submitblock: Test invalid coinbase transaction")
|
|
|
|
assert_raises_jsonrpc(-22, "Block does not start with a coinbase", node.submitblock, b2x(bad_block.serialize()))
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test truncated final transaction")
|
2017-04-11 13:42:12 -07:00
|
|
|
assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'})
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test duplicate transaction")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.vtx.append(bad_block.vtx[0])
|
|
|
|
assert_template(node, bad_block, 'bad-txns-duplicate')
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test invalid transaction")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_tx = copy.deepcopy(bad_block.vtx[0])
|
|
|
|
bad_tx.vin[0].prevout.hash = 255
|
|
|
|
bad_tx.rehash()
|
|
|
|
bad_block.vtx.append(bad_tx)
|
|
|
|
assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test nonfinal transaction")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.vtx[0].nLockTime = 2 ** 32 - 1
|
|
|
|
bad_block.vtx[0].rehash()
|
|
|
|
assert_template(node, bad_block, 'bad-txns-nonfinal')
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test bad tx count")
|
2017-04-11 13:42:12 -07:00
|
|
|
# The tx count is immediately after the block header
|
|
|
|
TX_COUNT_OFFSET = 80
|
|
|
|
bad_block_sn = bytearray(block.serialize())
|
|
|
|
assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
|
|
|
|
bad_block_sn[TX_COUNT_OFFSET] += 1
|
|
|
|
assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test bad bits")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.nBits = 469762303 # impossible in the real world
|
|
|
|
assert_template(node, bad_block, 'bad-diffbits')
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test bad merkle root")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.hashMerkleRoot += 1
|
|
|
|
assert_template(node, bad_block, 'bad-txnmrklroot', False)
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test bad timestamps")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.nTime = 2 ** 31 - 1
|
|
|
|
assert_template(node, bad_block, 'time-too-new')
|
|
|
|
bad_block.nTime = 0
|
|
|
|
assert_template(node, bad_block, 'time-too-old')
|
2014-11-17 10:47:40 -08:00
|
|
|
|
2017-04-11 10:10:20 -07:00
|
|
|
self.log.info("getblocktemplate: Test not best block")
|
2017-04-11 13:42:12 -07:00
|
|
|
bad_block = copy.deepcopy(block)
|
|
|
|
bad_block.hashPrevBlock = 123
|
|
|
|
assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
|
2014-11-17 10:47:40 -08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
GetBlockTemplateProposalTest().main()
|