[tests] rename getblocktemplate_proposals.py to mining.py
[bitcoinplatinum.git] / test / functional / mining.py
blobdbd4e29ecae80cb92f40b1964eee2a2863b69b4d
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 mining RPCs
7 - getblocktemplate proposal mode
8 - submitblock"""
10 from binascii import b2a_hex
11 import copy
13 from test_framework.blocktools import create_coinbase
14 from test_framework.test_framework import BitcoinTestFramework
15 from test_framework.mininode import CBlock
16 from test_framework.util import *
18 def b2x(b):
19 return b2a_hex(b).decode('ascii')
21 def assert_template(node, block, expect, rehash=True):
22 if rehash:
23 block.hashMerkleRoot = block.calc_merkle_root()
24 rsp = node.getblocktemplate({'data': b2x(block.serialize()), 'mode': 'proposal'})
25 assert_equal(rsp, expect)
27 class MiningTest(BitcoinTestFramework):
29 def __init__(self):
30 super().__init__()
31 self.num_nodes = 2
32 self.setup_clean_chain = False
34 def run_test(self):
35 node = self.nodes[0]
36 # Mine a block to leave initial block download
37 node.generate(1)
38 tmpl = node.getblocktemplate()
39 self.log.info("getblocktemplate: Test capability advertised")
40 assert 'proposal' in tmpl['capabilities']
41 assert 'coinbasetxn' not in tmpl
43 coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
44 # sequence numbers must not be max for nLockTime to have effect
45 coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
46 coinbase_tx.rehash()
48 block = CBlock()
49 block.nVersion = tmpl["version"]
50 block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
51 block.nTime = tmpl["curtime"]
52 block.nBits = int(tmpl["bits"], 16)
53 block.nNonce = 0
54 block.vtx = [coinbase_tx]
56 self.log.info("getblocktemplate: Test valid block")
57 assert_template(node, block, None)
59 self.log.info("submitblock: Test block decode failure")
60 assert_raises_jsonrpc(-22, "Block decode failed", node.submitblock, b2x(block.serialize()[:-15]))
62 self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
63 bad_block = copy.deepcopy(block)
64 bad_block.vtx[0].vin[0].prevout.hash += 1
65 bad_block.vtx[0].rehash()
66 assert_template(node, bad_block, 'bad-cb-missing')
68 self.log.info("submitblock: Test invalid coinbase transaction")
69 assert_raises_jsonrpc(-22, "Block does not start with a coinbase", node.submitblock, b2x(bad_block.serialize()))
71 self.log.info("getblocktemplate: Test truncated final transaction")
72 assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'})
74 self.log.info("getblocktemplate: Test duplicate transaction")
75 bad_block = copy.deepcopy(block)
76 bad_block.vtx.append(bad_block.vtx[0])
77 assert_template(node, bad_block, 'bad-txns-duplicate')
79 self.log.info("getblocktemplate: Test invalid transaction")
80 bad_block = copy.deepcopy(block)
81 bad_tx = copy.deepcopy(bad_block.vtx[0])
82 bad_tx.vin[0].prevout.hash = 255
83 bad_tx.rehash()
84 bad_block.vtx.append(bad_tx)
85 assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
87 self.log.info("getblocktemplate: Test nonfinal transaction")
88 bad_block = copy.deepcopy(block)
89 bad_block.vtx[0].nLockTime = 2 ** 32 - 1
90 bad_block.vtx[0].rehash()
91 assert_template(node, bad_block, 'bad-txns-nonfinal')
93 self.log.info("getblocktemplate: Test bad tx count")
94 # The tx count is immediately after the block header
95 TX_COUNT_OFFSET = 80
96 bad_block_sn = bytearray(block.serialize())
97 assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
98 bad_block_sn[TX_COUNT_OFFSET] += 1
99 assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
101 self.log.info("getblocktemplate: Test bad bits")
102 bad_block = copy.deepcopy(block)
103 bad_block.nBits = 469762303 # impossible in the real world
104 assert_template(node, bad_block, 'bad-diffbits')
106 self.log.info("getblocktemplate: Test bad merkle root")
107 bad_block = copy.deepcopy(block)
108 bad_block.hashMerkleRoot += 1
109 assert_template(node, bad_block, 'bad-txnmrklroot', False)
111 self.log.info("getblocktemplate: Test bad timestamps")
112 bad_block = copy.deepcopy(block)
113 bad_block.nTime = 2 ** 31 - 1
114 assert_template(node, bad_block, 'time-too-new')
115 bad_block.nTime = 0
116 assert_template(node, bad_block, 'time-too-old')
118 self.log.info("getblocktemplate: Test not best block")
119 bad_block = copy.deepcopy(block)
120 bad_block.hashPrevBlock = 123
121 assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
123 if __name__ == '__main__':
124 MiningTest().main()