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.
8 - getblocktemplate proposal mode
12 from binascii
import b2a_hex
13 from decimal
import Decimal
15 from test_framework
.blocktools
import create_coinbase
16 from test_framework
.mininode
import CBlock
17 from test_framework
.test_framework
import BitcoinTestFramework
18 from test_framework
.util
import assert_equal
, assert_raises_jsonrpc
21 return b2a_hex(b
).decode('ascii')
23 def assert_template(node
, block
, expect
, rehash
=True):
25 block
.hashMerkleRoot
= block
.calc_merkle_root()
26 rsp
= node
.getblocktemplate({'data': b2x(block
.serialize()), 'mode': 'proposal'})
27 assert_equal(rsp
, expect
)
29 class MiningTest(BitcoinTestFramework
):
30 def set_test_params(self
):
32 self
.setup_clean_chain
= False
37 self
.log
.info('getmininginfo')
38 mining_info
= node
.getmininginfo()
39 assert_equal(mining_info
['blocks'], 200)
40 assert_equal(mining_info
['chain'], 'regtest')
41 assert_equal(mining_info
['currentblocksize'], 0)
42 assert_equal(mining_info
['currentblocktx'], 0)
43 assert_equal(mining_info
['currentblockweight'], 0)
44 assert_equal(mining_info
['difficulty'], Decimal('4.656542373906925E-10'))
45 assert_equal(mining_info
['networkhashps'], Decimal('0.003333333333333334'))
46 assert_equal(mining_info
['pooledtx'], 0)
48 # Mine a block to leave initial block download
50 tmpl
= node
.getblocktemplate()
51 self
.log
.info("getblocktemplate: Test capability advertised")
52 assert 'proposal' in tmpl
['capabilities']
53 assert 'coinbasetxn' not in tmpl
55 coinbase_tx
= create_coinbase(height
=int(tmpl
["height"]) + 1)
56 # sequence numbers must not be max for nLockTime to have effect
57 coinbase_tx
.vin
[0].nSequence
= 2 ** 32 - 2
61 block
.nVersion
= tmpl
["version"]
62 block
.hashPrevBlock
= int(tmpl
["previousblockhash"], 16)
63 block
.nTime
= tmpl
["curtime"]
64 block
.nBits
= int(tmpl
["bits"], 16)
66 block
.vtx
= [coinbase_tx
]
68 self
.log
.info("getblocktemplate: Test valid block")
69 assert_template(node
, block
, None)
71 self
.log
.info("submitblock: Test block decode failure")
72 assert_raises_jsonrpc(-22, "Block decode failed", node
.submitblock
, b2x(block
.serialize()[:-15]))
74 self
.log
.info("getblocktemplate: Test bad input hash for coinbase transaction")
75 bad_block
= copy
.deepcopy(block
)
76 bad_block
.vtx
[0].vin
[0].prevout
.hash += 1
77 bad_block
.vtx
[0].rehash()
78 assert_template(node
, bad_block
, 'bad-cb-missing')
80 self
.log
.info("submitblock: Test invalid coinbase transaction")
81 assert_raises_jsonrpc(-22, "Block does not start with a coinbase", node
.submitblock
, b2x(bad_block
.serialize()))
83 self
.log
.info("getblocktemplate: Test truncated final transaction")
84 assert_raises_jsonrpc(-22, "Block decode failed", node
.getblocktemplate
, {'data': b2x(block
.serialize()[:-1]), 'mode': 'proposal'})
86 self
.log
.info("getblocktemplate: Test duplicate transaction")
87 bad_block
= copy
.deepcopy(block
)
88 bad_block
.vtx
.append(bad_block
.vtx
[0])
89 assert_template(node
, bad_block
, 'bad-txns-duplicate')
91 self
.log
.info("getblocktemplate: Test invalid transaction")
92 bad_block
= copy
.deepcopy(block
)
93 bad_tx
= copy
.deepcopy(bad_block
.vtx
[0])
94 bad_tx
.vin
[0].prevout
.hash = 255
96 bad_block
.vtx
.append(bad_tx
)
97 assert_template(node
, bad_block
, 'bad-txns-inputs-missingorspent')
99 self
.log
.info("getblocktemplate: Test nonfinal transaction")
100 bad_block
= copy
.deepcopy(block
)
101 bad_block
.vtx
[0].nLockTime
= 2 ** 32 - 1
102 bad_block
.vtx
[0].rehash()
103 assert_template(node
, bad_block
, 'bad-txns-nonfinal')
105 self
.log
.info("getblocktemplate: Test bad tx count")
106 # The tx count is immediately after the block header
108 bad_block_sn
= bytearray(block
.serialize())
109 assert_equal(bad_block_sn
[TX_COUNT_OFFSET
], 1)
110 bad_block_sn
[TX_COUNT_OFFSET
] += 1
111 assert_raises_jsonrpc(-22, "Block decode failed", node
.getblocktemplate
, {'data': b2x(bad_block_sn
), 'mode': 'proposal'})
113 self
.log
.info("getblocktemplate: Test bad bits")
114 bad_block
= copy
.deepcopy(block
)
115 bad_block
.nBits
= 469762303 # impossible in the real world
116 assert_template(node
, bad_block
, 'bad-diffbits')
118 self
.log
.info("getblocktemplate: Test bad merkle root")
119 bad_block
= copy
.deepcopy(block
)
120 bad_block
.hashMerkleRoot
+= 1
121 assert_template(node
, bad_block
, 'bad-txnmrklroot', False)
123 self
.log
.info("getblocktemplate: Test bad timestamps")
124 bad_block
= copy
.deepcopy(block
)
125 bad_block
.nTime
= 2 ** 31 - 1
126 assert_template(node
, bad_block
, 'time-too-new')
128 assert_template(node
, bad_block
, 'time-too-old')
130 self
.log
.info("getblocktemplate: Test not best block")
131 bad_block
= copy
.deepcopy(block
)
132 bad_block
.hashPrevBlock
= 123
133 assert_template(node
, bad_block
, 'inconclusive-not-best-prevblk')
135 if __name__
== '__main__':