-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmining_basic.py
More file actions
executable file
·319 lines (275 loc) · 15 KB
/
Copy pathmining_basic.py
File metadata and controls
executable file
·319 lines (275 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
# Copyright (c) 2014-2020 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test mining RPCs
- getmininginfo
- getblocktemplate proposal mode
- submitblock"""
import copy
from io import BytesIO
from test_framework.blocktools import (
create_coinbase,
NORMAL_GBT_REQUEST_PARAMS,
TIME_GENESIS_BLOCK,
)
from test_framework.messages import (
CBlock,
CBlockHeader,
CTransaction,
BLOCK_HEADER_SIZE,
)
from test_framework.script import CScript
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
assert_raises_rpc_error,
)
VERSIONBITS_TOP_BITS = 0x20000000
VERSIONBITS_DEPLOYMENT_TESTDUMMY_BIT = 28
def assert_template(node, block, expect, rehash=True):
if rehash:
block.hashMerkleRoot = block.calc_merkle_root()
rsp = node.getblocktemplate(template_request={
'data': block.serialize().hex(),
'mode': 'proposal',
'rules': ['segwit'],
})
assert_equal(rsp, expect)
class MiningTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.setup_clean_chain = True
self.supports_cli = False
def skip_test_if_missing_module(self):
self.skip_if_no_wallet()
def mine_chain(self):
self.log.info('Create some old blocks')
for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
self.nodes[0].setmocktime(t)
self.nodes[0].generate(1)
mining_info = self.nodes[0].getmininginfo()
assert_equal(mining_info['blocks'], 200)
assert_equal(mining_info['currentblocktx'], 0)
assert_equal(mining_info['currentblockweight'], 4000)
self.log.info('test blockversion')
# In PoS, -blockversion override is not honored (version is set by consensus).
# Restart node to reset staking state, advance time for coinstake search.
self.restart_node(0, extra_args=['-mocktime={}'.format(t)])
self.connect_nodes(0, 1)
self.nodes[0].setmocktime(t + 120)
# Verify getblocktemplate returns a version with VERSIONBITS_TOP_BITS set.
tmpl_version = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)['version']
assert_equal(tmpl_version & VERSIONBITS_TOP_BITS, VERSIONBITS_TOP_BITS)
# Keep mocktime for PoS staking to work in run_test.
# Save the last mocktime so run_test can use it.
self.mock_time = t + 120
self.restart_node(0, extra_args=['-mocktime={}'.format(self.mock_time)])
self.connect_nodes(0, 1)
def run_test(self):
self.mine_chain()
node = self.nodes[0]
# Sync framework mocktime tracking with the -mocktime CLI arg.
# node.setmocktime() is a raw RPC that does NOT update node.mocktime,
# so we must set both explicitly.
node.setmocktime(self.mock_time)
node.mocktime = self.mock_time
def assert_submitblock(block, result_str_1, result_str_2=None):
block.solve()
result_str_2 = result_str_2 or 'duplicate-invalid'
assert_equal(result_str_1, node.submitblock(hexdata=block.serialize().hex()))
assert_equal(result_str_2, node.submitblock(hexdata=block.serialize().hex()))
self.log.info('getmininginfo')
mining_info = node.getmininginfo()
assert_equal(mining_info['blocks'], 200)
assert_equal(mining_info['chain'], self.chain)
assert 'currentblocktx' not in mining_info
assert 'currentblockweight' not in mining_info
# PoS difficulty and networkhashps differ from PoW values;
# just verify they are present and non-negative.
assert mining_info['difficulty'] >= 0
assert mining_info['networkhashps'] >= 0
assert_equal(mining_info['pooledtx'], 0)
# Mine a block to leave initial block download
self.mock_time += 120
node.setmocktime(self.mock_time)
node.mocktime = self.mock_time
node.generatetoaddress(1, node.get_deterministic_priv_key().address)
# Advance mocktime so getblocktemplate can create a new coinstake
# (PoS requires GetAdjustedTime() > nLastCoinStakeSearchTime)
self.mock_time += 120
node.setmocktime(self.mock_time)
node.mocktime = self.mock_time
tmpl = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
self.log.info("getblocktemplate: Test capability advertised")
assert 'proposal' in tmpl['capabilities']
assert 'coinbasetxn' not in tmpl
next_height = int(tmpl["height"])
# PoS coinbase: empty output (value=0, empty script)
coinbase_tx = create_coinbase(height=next_height, outputScriptPubKey=CScript())
# sequence numbers must not be max for nLockTime to have effect
coinbase_tx.vin[0].nSequence = 2**32 - 2
coinbase_tx.rehash()
# Deserialize the coinstake transaction from the template
coinstake_tx = CTransaction()
coinstake_tx.deserialize(BytesIO(bytes.fromhex(tmpl['transactions'][0]['data'])))
coinstake_tx.rehash()
# The reported values must come from the transactions they name. The
# coinstake pays out through its first real output, vout[0] being the
# empty marker, and a proof-of-stake coinbase carries a single empty
# output. The coinbase has only ever one output, so reading a coinstake
# value from it used to run off the end of its output vector.
assert_equal(tmpl['coinstakevalue'], coinstake_tx.vout[1].nValue)
assert_equal(tmpl['coinbasevalue'], 0)
block = CBlock()
block.nVersion = tmpl["version"]
block.hashPrevBlock = int(tmpl["previousblockhash"], 16)
block.nTime = tmpl["curtime"]
block.nBits = int(tmpl["bits"], 16)
block.nNonce = 0
block.vtx = [coinbase_tx, coinstake_tx]
self.log.info("getblocktemplate: segwit rule must be set")
assert_raises_rpc_error(-8, "getblocktemplate must be called with the segwit rule set", node.getblocktemplate)
self.log.info("getblocktemplate: Test valid block")
assert_template(node, block, None)
self.log.info("submitblock: Test block decode failure")
assert_raises_rpc_error(-22, "Block decode failed", node.submitblock, block.serialize()[:-15].hex())
self.log.info("getblocktemplate: Test bad input hash for coinbase transaction")
bad_block = copy.deepcopy(block)
bad_block.vtx[0].vin[0].prevout.hash += 1
bad_block.vtx[0].rehash()
assert_template(node, bad_block, 'bad-cb-missing')
self.log.info("submitblock: Test invalid coinbase transaction")
assert_raises_rpc_error(-22, "Block does not start with a coinbase", node.submitblock, bad_block.serialize().hex())
self.log.info("getblocktemplate: Test truncated final transaction")
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {
'data': block.serialize()[:-1].hex(),
'mode': 'proposal',
'rules': ['segwit'],
})
self.log.info("getblocktemplate: Test duplicate transaction")
bad_block = copy.deepcopy(block)
bad_block.vtx.append(bad_block.vtx[1]) # Duplicate coinstake
# In PoS, the coinstake position check (bad-cs-missing) fires before
# merkle mutation detection because with 3 leaves [A,B,B], the mutation
# check only compares pairs at even indices (0,1) missing the dup at 2.
assert_template(node, bad_block, 'bad-cs-missing')
assert_submitblock(bad_block, 'bad-cs-missing', 'bad-cs-missing')
self.log.info("getblocktemplate: Test invalid transaction")
bad_block = copy.deepcopy(block)
bad_tx = copy.deepcopy(bad_block.vtx[0])
bad_tx.vin[0].prevout.hash = 255
bad_tx.rehash()
bad_block.vtx.append(bad_tx)
assert_template(node, bad_block, 'bad-txns-inputs-missingorspent')
assert_submitblock(bad_block, 'bad-txns-inputs-missingorspent')
self.log.info("getblocktemplate: Test nonfinal transaction")
bad_block = copy.deepcopy(block)
bad_block.vtx[0].nLockTime = 2**32 - 1
bad_block.vtx[0].rehash()
assert_template(node, bad_block, 'bad-txns-nonfinal')
assert_submitblock(bad_block, 'bad-txns-nonfinal')
self.log.info("getblocktemplate: Test bad tx count")
# The tx count is immediately after the block header
bad_block_sn = bytearray(block.serialize())
assert_equal(bad_block_sn[BLOCK_HEADER_SIZE], 2) # coinbase + coinstake
bad_block_sn[BLOCK_HEADER_SIZE] += 1
assert_raises_rpc_error(-22, "Block decode failed", node.getblocktemplate, {
'data': bad_block_sn.hex(),
'mode': 'proposal',
'rules': ['segwit'],
})
self.log.info("getblocktemplate: Test bad bits (skipped — PoS does not validate nBits in proposal mode)")
# In Bitcoin PoW, ContextualCheckBlockHeader rejects blocks with wrong nBits.
# ReddCoin PoS sets nBits via GetNextWorkRequired in template creation;
# the proposal validation path does not re-check nBits for PoS blocks.
self.log.info("getblocktemplate: Test bad merkle root")
bad_block = copy.deepcopy(block)
bad_block.hashMerkleRoot += 1
assert_template(node, bad_block, 'bad-txnmrklroot', False)
assert_submitblock(bad_block, 'bad-txnmrklroot', 'bad-txnmrklroot')
self.log.info("getblocktemplate: Test bad timestamps")
bad_block = copy.deepcopy(block)
bad_block.nTime = 2**31 - 1
assert_template(node, bad_block, 'time-too-new')
assert_submitblock(bad_block, 'time-too-new', 'time-too-new')
bad_block.nTime = 0
assert_template(node, bad_block, 'time-too-old')
# submitblock runs CheckBlock before ContextualCheckBlockHeader.
# CheckBlock catches coinstake timestamp mismatch (bad-cs-time)
# before ContextualCheckBlockHeader can report time-too-old.
assert_submitblock(bad_block, 'bad-cs-time', 'bad-cs-time')
self.log.info("getblocktemplate: Test not best block")
bad_block = copy.deepcopy(block)
bad_block.hashPrevBlock = 123
assert_template(node, bad_block, 'inconclusive-not-best-prevblk')
assert_submitblock(bad_block, 'prev-blk-not-found', 'prev-blk-not-found')
self.log.info('submitheader tests')
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='xx' * BLOCK_HEADER_SIZE))
assert_raises_rpc_error(-22, 'Block header decode failed', lambda: node.submitheader(hexdata='ff' * (BLOCK_HEADER_SIZE-2)))
assert_raises_rpc_error(-25, 'Must submit previous header', lambda: node.submitheader(hexdata=super(CBlock, bad_block).serialize().hex()))
# PoS: block.nTime must equal coinstake.nTime (CheckCoinStakeTimestamp).
# Use nNonce to create different hashes for submitheader tests instead
# of modifying nTime which would break the coinstake timestamp match.
# block.solve() is a no-op for PoS, so nNonce is not overwritten.
block.nNonce = 1
block.rehash()
def chain_tip(b_hash, *, status='headers-only', branchlen=1):
return {'hash': b_hash, 'height': 202, 'branchlen': branchlen, 'status': status}
assert chain_tip(block.hash) not in node.getchaintips()
node.submitheader(hexdata=block.serialize().hex())
assert chain_tip(block.hash) in node.getchaintips()
node.submitheader(hexdata=CBlockHeader(block).serialize().hex()) # Noop
assert chain_tip(block.hash) in node.getchaintips()
bad_block_root = copy.deepcopy(block)
bad_block_root.hashMerkleRoot += 2
bad_block_root.solve()
assert chain_tip(bad_block_root.hash) not in node.getchaintips()
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
assert chain_tip(bad_block_root.hash) in node.getchaintips()
# Should still reject invalid blocks, even if we have the header:
assert_equal(node.submitblock(hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot')
assert_equal(node.submitblock(hexdata=bad_block_root.serialize().hex()), 'bad-txnmrklroot')
assert chain_tip(bad_block_root.hash) in node.getchaintips()
# We know the header for this invalid block, so should just return early without error:
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
assert chain_tip(bad_block_root.hash) in node.getchaintips()
bad_block_lock = copy.deepcopy(block)
bad_block_lock.vtx[0].nLockTime = 2**32 - 1
bad_block_lock.vtx[0].rehash()
bad_block_lock.hashMerkleRoot = bad_block_lock.calc_merkle_root()
bad_block_lock.solve()
assert_equal(node.submitblock(hexdata=bad_block_lock.serialize().hex()), 'bad-txns-nonfinal')
assert_equal(node.submitblock(hexdata=bad_block_lock.serialize().hex()), 'duplicate-invalid')
# Build a "good" block on top of the submitted bad block
bad_block2 = copy.deepcopy(block)
bad_block2.hashPrevBlock = bad_block_lock.sha256
bad_block2.solve()
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=CBlockHeader(bad_block2).serialize().hex()))
# Should reject invalid header right away
bad_block_time = copy.deepcopy(block)
bad_block_time.nTime = 1
bad_block_time.solve()
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))
# PoS: Skip P2P block delivery test — constructing a valid PoS block
# in Python requires a valid coinstake kernel hash which depends on
# internal chain state. The submitheader tests above already validate
# header-first relay. Instead, verify blocks can be generated and
# accepted by the node itself.
# Advance mocktime past nLastCoinStakeSearchTime (set during getblocktemplate)
self.mock_time += 120
node.setmocktime(self.mock_time)
node.mocktime = self.mock_time
node.generate(1)
assert_equal(node.getblockcount(), 202)
# Building a few blocks should give the same results
node.generate(10)
assert_raises_rpc_error(-25, 'time-too-old', lambda: node.submitheader(hexdata=CBlockHeader(bad_block_time).serialize().hex()))
assert_raises_rpc_error(-25, 'bad-prevblk', lambda: node.submitheader(hexdata=CBlockHeader(bad_block2).serialize().hex()))
node.submitheader(hexdata=CBlockHeader(block).serialize().hex())
node.submitheader(hexdata=CBlockHeader(bad_block_root).serialize().hex())
# Submit original block — header was submitted but full block was never
# delivered (P2P block delivery skipped in PoS), so it's inconclusive.
assert_equal(node.submitblock(hexdata=block.serialize().hex()), 'inconclusive')
if __name__ == '__main__':
MiningTest().main()