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
):
32 self
.setup_clean_chain
= False
36 # Mine a block to leave initial block download
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
49 block
.nVersion
= tmpl
["version"]
50 block
.hashPrevBlock
= int(tmpl
["previousblockhash"], 16)
51 block
.nTime
= tmpl
["curtime"]
52 block
.nBits
= int(tmpl
["bits"], 16)
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
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
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')
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__':