[tests] don't override __init__() in individual tests
[bitcoinplatinum.git] / test / functional / mining.py
blob8edc704670cd7bb82399198aee4297a0a26aba33
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):
28 def set_test_params(self):
29 self.num_nodes = 2
30 self.setup_clean_chain = False
32 def run_test(self):
33 node = self.nodes[0]
34 # Mine a block to leave initial block download
35 node.generate(1)
36 tmpl = node.getblocktemplate()
37 self.log.info("getblocktemplate: Test capability advertised")
38 assert 'proposal' in tmpl['capabilities']
39 assert 'coinbasetxn' not in tmpl
41 coinbase_tx = create_coinbase(height=int(tmpl["height"]) + 1)
42 # sequence numbers must not be max for nLockTime to have effect
43 coinbase_tx.vin[0].nSequence = 2 ** 32 - 2
44 coinbase_tx.rehash()
46 block = CBlock()
47 block.nVersion = tmpl["version"]
48 block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
49 block.nTime = tmpl["curtime"]
50 block.nBits = int(tmpl["bits"], 16)
51 block.nNonce = 0
52 block.vtx = [coinbase_tx]
54 self.log.info("getblocktemplate: Test valid block")
55 assert_template(node, block, None)
57 self.log.info("submitblock: Test block decode failure")
58 assert_raises_jsonrpc(-22, "Block decode failed", node.submitblock, b2x(block.serialize()[:-15]))
60 self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
61 bad_block = copy.deepcopy(block)
62 bad_block.vtx[0].vin[0].prevout.hash += 1
63 bad_block.vtx[0].rehash()
64 assert_template(node, bad_block, 'bad-cb-missing')
66 self.log.info("submitblock: Test invalid coinbase transaction")
67 assert_raises_jsonrpc(-22, "Block does not start with a coinbase", node.submitblock, b2x(bad_block.serialize()))
69 self.log.info("getblocktemplate: Test truncated final transaction")
70 assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(block.serialize()[:-1]), 'mode': 'proposal'})
72 self.log.info("getblocktemplate: Test duplicate transaction")
73 bad_block = copy.deepcopy(block)
74 bad_block.vtx.append(bad_block.vtx[0])
75 assert_template(node, bad_block, 'bad-txns-duplicate')
77 self.log.info("getblocktemplate: Test invalid transaction")
78 bad_block = copy.deepcopy(block)
79 bad_tx = copy.deepcopy(bad_block.vtx[0])
80 bad_tx.vin[0].prevout.hash = 255
81 bad_tx.rehash()
82 bad_block.vtx.append(bad_tx)
83 assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
85 self.log.info("getblocktemplate: Test nonfinal transaction")
86 bad_block = copy.deepcopy(block)
87 bad_block.vtx[0].nLockTime = 2 ** 32 - 1
88 bad_block.vtx[0].rehash()
89 assert_template(node, bad_block, 'bad-txns-nonfinal')
91 self.log.info("getblocktemplate: Test bad tx count")
92 # The tx count is immediately after the block header
93 TX_COUNT_OFFSET = 80
94 bad_block_sn = bytearray(block.serialize())
95 assert_equal(bad_block_sn[TX_COUNT_OFFSET], 1)
96 bad_block_sn[TX_COUNT_OFFSET] += 1
97 assert_raises_jsonrpc(-22, "Block decode failed", node.getblocktemplate, {'data': b2x(bad_block_sn), 'mode': 'proposal'})
99 self.log.info("getblocktemplate: Test bad bits")
100 bad_block = copy.deepcopy(block)
101 bad_block.nBits = 469762303 # impossible in the real world
102 assert_template(node, bad_block, 'bad-diffbits')
104 self.log.info("getblocktemplate: Test bad merkle root")
105 bad_block = copy.deepcopy(block)
106 bad_block.hashMerkleRoot += 1
107 assert_template(node, bad_block, 'bad-txnmrklroot', False)
109 self.log.info("getblocktemplate: Test bad timestamps")
110 bad_block = copy.deepcopy(block)
111 bad_block.nTime = 2 ** 31 - 1
112 assert_template(node, bad_block, 'time-too-new')
113 bad_block.nTime = 0
114 assert_template(node, bad_block, 'time-too-old')
116 self.log.info("getblocktemplate: Test not best block")
117 bad_block = copy.deepcopy(block)
118 bad_block.hashPrevBlock = 123
119 assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
121 if __name__ == '__main__':
122 MiningTest().main()