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_rpc_error
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
['currentblocktx'], 0)
42 assert_equal(mining_info
['currentblockweight'], 0)
43 assert_equal(mining_info
['difficulty'], Decimal('4.656542373906925E-10'))
44 assert_equal(mining_info
['networkhashps'], Decimal('0.003333333333333334'))
45 assert_equal(mining_info
['pooledtx'], 0)
47 # Mine a block to leave initial block download
49 tmpl
= node
.getblocktemplate()
50 self
.log
.info("getblocktemplate: Test capability advertised")
51 assert 'proposal' in tmpl
['capabilities']
52 assert 'coinbasetxn' not in tmpl
54 coinbase_tx
= create_coinbase(height
=int(tmpl
["height"]) + 1)
55 # sequence numbers must not be max for nLockTime to have effect
56 coinbase_tx
.vin
[0].nSequence
= 2 ** 32 - 2
60 block
.nVersion
= tmpl
["version"]
61 block
.hashPrevBlock
= int(tmpl
["previousblockhash"], 16)
62 block
.nTime
= tmpl
["curtime"]
63 block
.nBits
= int(tmpl
["bits"], 16)
65 block
.vtx
= [coinbase_tx
]
67 self
.log
.info("getblocktemplate: Test valid block")
68 assert_template(node
, block
, None)
70 self
.log
.info("submitblock: Test block decode failure")
71 assert_raises_rpc_error(-22, "Block decode failed", node
.submitblock
, b2x(block
.serialize()[:-15]))
73 self
.log
.info("getblocktemplate: Test bad input hash for coinbase transaction")
74 bad_block
= copy
.deepcopy(block
)
75 bad_block
.vtx
[0].vin
[0].prevout
.hash += 1
76 bad_block
.vtx
[0].rehash()
77 assert_template(node
, bad_block
, 'bad-cb-missing')
79 self
.log
.info("submitblock: Test invalid coinbase transaction")
80 assert_raises_rpc_error(-22, "Block does not start with a coinbase", node
.submitblock
, b2x(bad_block
.serialize()))
82 self
.log
.info("getblocktemplate: Test truncated final transaction")
83 assert_raises_rpc_error(-22, "Block decode failed", node
.getblocktemplate
, {'data': b2x(block
.serialize()[:-1]), 'mode': 'proposal'})
85 self
.log
.info("getblocktemplate: Test duplicate transaction")
86 bad_block
= copy
.deepcopy(block
)
87 bad_block
.vtx
.append(bad_block
.vtx
[0])
88 assert_template(node
, bad_block
, 'bad-txns-duplicate')
90 self
.log
.info("getblocktemplate: Test invalid transaction")
91 bad_block
= copy
.deepcopy(block
)
92 bad_tx
= copy
.deepcopy(bad_block
.vtx
[0])
93 bad_tx
.vin
[0].prevout
.hash = 255
95 bad_block
.vtx
.append(bad_tx
)
96 assert_template(node
, bad_block
, 'bad-txns-inputs-missingorspent')
98 self
.log
.info("getblocktemplate: Test nonfinal transaction")
99 bad_block
= copy
.deepcopy(block
)
100 bad_block
.vtx
[0].nLockTime
= 2 ** 32 - 1
101 bad_block
.vtx
[0].rehash()
102 assert_template(node
, bad_block
, 'bad-txns-nonfinal')
104 self
.log
.info("getblocktemplate: Test bad tx count")
105 # The tx count is immediately after the block header
107 bad_block_sn
= bytearray(block
.serialize())
108 assert_equal(bad_block_sn
[TX_COUNT_OFFSET
], 1)
109 bad_block_sn
[TX_COUNT_OFFSET
] += 1
110 assert_raises_rpc_error(-22, "Block decode failed", node
.getblocktemplate
, {'data': b2x(bad_block_sn
), 'mode': 'proposal'})
112 self
.log
.info("getblocktemplate: Test bad bits")
113 bad_block
= copy
.deepcopy(block
)
114 bad_block
.nBits
= 469762303 # impossible in the real world
115 assert_template(node
, bad_block
, 'bad-diffbits')
117 self
.log
.info("getblocktemplate: Test bad merkle root")
118 bad_block
= copy
.deepcopy(block
)
119 bad_block
.hashMerkleRoot
+= 1
120 assert_template(node
, bad_block
, 'bad-txnmrklroot', False)
122 self
.log
.info("getblocktemplate: Test bad timestamps")
123 bad_block
= copy
.deepcopy(block
)
124 bad_block
.nTime
= 2 ** 31 - 1
125 assert_template(node
, bad_block
, 'time-too-new')
127 assert_template(node
, bad_block
, 'time-too-old')
129 self
.log
.info("getblocktemplate: Test not best block")
130 bad_block
= copy
.deepcopy(block
)
131 bad_block
.hashPrevBlock
= 123
132 assert_template(node
, bad_block
, 'inconclusive-not-best-prevblk')
134 if __name__
== '__main__':