[tests] don't build blocks manually in getblocktemplate test
[bitcoinplatinum.git] / test / functional / getblocktemplate_proposals.py
bloba65d14ce064b0bff45dcf6cfdaa516b2ec5021ae
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2016 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 block proposals with getblocktemplate."""
7 from binascii import b2a_hex
8 import copy
10 from test_framework.blocktools import create_coinbase
11 from test_framework.test_framework import BitcoinTestFramework
12 from test_framework.mininode import CBlock
13 from test_framework.util import *
15 def b2x(b):
16 return b2a_hex(b).decode('ascii')
18 def assert_template(node, block, expect, rehash=True):
19 if rehash:
20 block.hashMerkleRoot = block.calc_merkle_root()
21 rsp = node.getblocktemplate({'data': b2x(block.serialize()), 'mode': 'proposal'})
22 assert_equal(rsp, expect)
24 class GetBlockTemplateProposalTest(BitcoinTestFramework):
26 def __init__(self):
27 super().__init__()
28 self.num_nodes = 2
29 self.setup_clean_chain = False
31 def run_test(self):
32 node = self.nodes[0]
33 # Mine a block to leave initial block download
34 node.generate(1)
35 tmpl = node.getblocktemplate()
36 self.log.info("getblocktemplate: Test capability advertised")
37 assert 'proposal' in tmpl['capabilities']
38 assert 'coinbasetxn' not in tmpl
40 coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
41 # sequence numbers must not be max for nLockTime to have effect
42 coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
43 coinbase_tx.rehash()
45 block = CBlock()
46 block.nVersion = tmpl["version"]
47 block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
48 block.nTime = tmpl["curtime"]
49 block.nBits = int(tmpl["bits"], 16)
50 block.nNonce = 0
51 block.vtx = [coinbase_tx]
53 self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
54 bad_block = copy.deepcopy(block)
55 bad_block.vtx[0].vin[0].prevout.hash += 1
56 bad_block.vtx[0].rehash()
57 assert_template(node, bad_block, 'bad-cb-missing')
60 self.log.info("getblocktemplate: Test truncated final transaction")
61 assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'})
63 self.log.info("getblocktemplate: Test duplicate transaction")
64 bad_block = copy.deepcopy(block)
65 bad_block.vtx.append(bad_block.vtx[0])
66 assert_template(node, bad_block, 'bad-txns-duplicate')
68 self.log.info("getblocktemplate: Test invalid transaction")
69 bad_block = copy.deepcopy(block)
70 bad_tx = copy.deepcopy(bad_block.vtx[0])
71 bad_tx.vin[0].prevout.hash = 255
72 bad_tx.rehash()
73 bad_block.vtx.append(bad_tx)
74 assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
76 self.log.info("getblocktemplate: Test nonfinal transaction")
77 bad_block = copy.deepcopy(block)
78 bad_block.vtx[0].nLockTime = 2 ** 32 - 1
79 bad_block.vtx[0].rehash()
80 assert_template(node, bad_block, 'bad-txns-nonfinal')
82 self.log.info("getblocktemplate: Test bad tx count")
83 # The tx count is immediately after the block header
84 TX_COUNT_OFFSET = 80
85 bad_block_sn = bytearray(block.serialize())
86 assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
87 bad_block_sn[TX_COUNT_OFFSET] += 1
88 assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
90 self.log.info("getblocktemplate: Test bad bits")
91 bad_block = copy.deepcopy(block)
92 bad_block.nBits = 469762303 # impossible in the real world
93 assert_template(node, bad_block, 'bad-diffbits')
95 self.log.info("getblocktemplate: Test bad merkle root")
96 bad_block = copy.deepcopy(block)
97 bad_block.hashMerkleRoot += 1
98 assert_template(node, bad_block, 'bad-txnmrklroot', False)
100 self.log.info("getblocktemplate: Test bad timestamps")
101 bad_block = copy.deepcopy(block)
102 bad_block.nTime = 2 ** 31 - 1
103 assert_template(node, bad_block, 'time-too-new')
104 bad_block.nTime = 0
105 assert_template(node, bad_block, 'time-too-old')
107 self.log.info("getblocktemplate: Test valid block")
108 assert_template(node, block, None)
110 self.log.info("getblocktemplate: Test not best block")
111 bad_block = copy.deepcopy(block)
112 bad_block.hashPrevBlock = 123
113 assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
115 if __name__ == '__main__':
116 GetBlockTemplateProposalTest().main()