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.
7 - getblocktemplate proposal mode
10 from binascii
import b2a_hex
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 *
19 return b2a_hex(b
).decode('ascii')
21 def assert_template(node
, block
, expect
, rehash
=True):
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
):
30 self
.setup_clean_chain
= False
34 # Mine a block to leave initial block download
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
47 block
.nVersion
= tmpl
["version"]
48 block
.hashPrevBlock
= int(tmpl
["previousblockhash"], 16)
49 block
.nTime
= tmpl
["curtime"]
50 block
.nBits
= int(tmpl
["bits"], 16)
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
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
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')
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__':