forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_ipc_mining.py
More file actions
executable file
·383 lines (327 loc) · 18.2 KB
/
interface_ipc_mining.py
File metadata and controls
executable file
·383 lines (327 loc) · 18.2 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env python3
# Copyright (c) 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 the IPC (multiprocess) Mining interface."""
import asyncio
import time
from contextlib import AsyncExitStack
from io import BytesIO
import re
from test_framework.blocktools import NULL_OUTPOINT
from test_framework.messages import (
MAX_BLOCK_WEIGHT,
CBlockHeader,
CTransaction,
CTxIn,
CTxOut,
CTxInWitness,
ser_uint256,
COIN,
from_hex,
msg_headers,
)
from test_framework.script import (
CScript,
CScriptNum,
)
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
assert_greater_than_or_equal,
assert_not_equal
)
from test_framework.wallet import MiniWallet
from test_framework.p2p import P2PInterface
from test_framework.ipc_util import (
destroying,
mining_create_block_template,
load_capnp_modules,
make_capnp_init_ctx,
mining_get_block,
mining_get_coinbase_tx,
mining_wait_next_template,
wait_and_do,
)
# Test may be skipped and not have capnp installed
try:
import capnp # type: ignore[import] # noqa: F401
except ModuleNotFoundError:
pass
class IPCMiningTest(BitcoinTestFramework):
def skip_test_if_missing_module(self):
self.skip_if_no_ipc()
self.skip_if_no_py_capnp()
def set_test_params(self):
self.num_nodes = 2
def setup_nodes(self):
self.extra_init = [{"ipcbind": True}, {}]
super().setup_nodes()
# Use this function to also load the capnp modules (we cannot use set_test_params for this,
# as it is being called before knowing whether capnp is available).
self.capnp_modules = load_capnp_modules(self.config)
async def build_coinbase_test(self, template, ctx, miniwallet):
self.log.debug("Build coinbase transaction using getCoinbaseTx()")
assert template is not None
coinbase_res = await mining_get_coinbase_tx(template, ctx)
coinbase_tx = CTransaction()
coinbase_tx.version = coinbase_res.version
coinbase_tx.vin = [CTxIn()]
coinbase_tx.vin[0].prevout = NULL_OUTPOINT
coinbase_tx.vin[0].nSequence = coinbase_res.sequence
# Verify there's no dummy extraNonce in the coinbase scriptSig
current_block_height = self.nodes[0].getchaintips()[0]["height"]
expected_scriptsig = CScript([CScriptNum(current_block_height + 1)])
assert_equal(coinbase_res.scriptSigPrefix.hex(), expected_scriptsig.hex())
# Typically a mining pool appends its name and an extraNonce
coinbase_tx.vin[0].scriptSig = coinbase_res.scriptSigPrefix
# We currently always provide a coinbase witness, even for empty
# blocks, but this may change, so always check:
has_witness = coinbase_res.witness is not None
if has_witness:
coinbase_tx.wit.vtxinwit = [CTxInWitness()]
coinbase_tx.wit.vtxinwit[0].scriptWitness.stack = [coinbase_res.witness]
# First output is our payout
coinbase_tx.vout = [CTxOut()]
coinbase_tx.vout[0].scriptPubKey = miniwallet.get_output_script()
coinbase_tx.vout[0].nValue = coinbase_res.blockRewardRemaining
# Add SegWit OP_RETURN. This is currently always present even for
# empty blocks, but this may change.
for output_data in coinbase_res.requiredOutputs:
output = CTxOut()
output.deserialize(BytesIO(output_data))
coinbase_tx.vout.append(output)
coinbase_tx.nLockTime = coinbase_res.lockTime
return coinbase_tx
async def make_mining_ctx(self):
"""Create IPC context and Mining proxy object."""
ctx, init = await make_capnp_init_ctx(self)
self.log.debug("Create Mining proxy object")
mining = init.makeMining(ctx).result
return ctx, mining
def run_mining_interface_test(self):
"""Test Mining interface methods."""
self.log.info("Running Mining interface test")
block_hash_size = 32
timeout = 1000.0 # 1000 milliseconds
async def async_routine():
ctx, mining = await self.make_mining_ctx()
blockref = await mining.getTip(ctx)
current_block_height = self.nodes[0].getchaintips()[0]["height"]
assert_equal(blockref.result.height, current_block_height)
self.log.debug("Mine a block")
newblockref = (await wait_and_do(
mining.waitTipChanged(ctx, blockref.result.hash, timeout),
lambda: self.generate(self.nodes[0], 1))).result
assert_equal(len(newblockref.hash), block_hash_size)
assert_equal(newblockref.height, current_block_height + 1)
self.log.debug("Wait for timeout")
oldblockref = (await mining.waitTipChanged(ctx, newblockref.hash, timeout)).result
assert_equal(len(newblockref.hash), block_hash_size)
assert_equal(oldblockref.hash, newblockref.hash)
assert_equal(oldblockref.height, newblockref.height)
self.log.debug("interrupt() should abort waitTipChanged()")
async def wait_for_tip():
long_timeout = 60000.0 # 1 minute
result = (await mining.waitTipChanged(ctx, newblockref.hash, long_timeout)).result
# Unlike a timeout, interrupt() returns an empty BlockRef.
assert_equal(len(result.hash), 0)
await wait_and_do(wait_for_tip(), mining.interrupt())
asyncio.run(capnp.run(async_routine()))
def run_block_template_test(self):
"""Test BlockTemplate interface methods."""
self.log.info("Running BlockTemplate interface test")
block_header_size = 80
timeout = 1000.0 # 1000 milliseconds
async def async_routine():
ctx, mining = await self.make_mining_ctx()
async with AsyncExitStack() as stack:
self.log.debug("createNewBlock() should wait if tip is still updating")
self.disconnect_nodes(0, 1)
node1_block_hash = self.generate(self.nodes[1], 1, sync_fun=self.no_op)[0]
header = from_hex(CBlockHeader(), self.nodes[1].getblockheader(node1_block_hash, False))
header_only_peer = self.nodes[0].add_p2p_connection(P2PInterface())
header_only_peer.send_and_ping(msg_headers([header]))
start = time.time()
async with destroying((await mining.createNewBlock(ctx, self.default_block_create_options)).result, ctx):
pass
# Lower-bound only: a heavily loaded CI host might still exceed 0.9s
# even without the cooldown, so this can miss regressions but avoids
# spurious failures.
assert_greater_than_or_equal(time.time() - start, 0.9)
self.log.debug("interrupt() should abort createNewBlock() during cooldown")
async def create_block():
result = await mining.createNewBlock(ctx, self.default_block_create_options)
# interrupt() causes createNewBlock to return nullptr
assert_equal(result._has("result"), False)
await wait_and_do(create_block(), mining.interrupt())
header_only_peer.peer_disconnect()
self.connect_nodes(0, 1)
self.sync_all()
self.log.debug("Create a template")
template = await mining_create_block_template(mining, stack, ctx, self.default_block_create_options)
assert template is not None
self.log.debug("Test some inspectors of Template")
header = (await template.getBlockHeader(ctx)).result
assert_equal(len(header), block_header_size)
block = await mining_get_block(template, ctx)
current_tip = self.nodes[0].getbestblockhash()
assert_equal(ser_uint256(block.hashPrevBlock), ser_uint256(int(current_tip, 16)))
assert_greater_than_or_equal(len(block.vtx), 1)
txfees = await template.getTxFees(ctx)
assert_equal(len(txfees.result), 0)
txsigops = await template.getTxSigops(ctx)
assert_equal(len(txsigops.result), 0)
self.log.debug("Wait for a new template")
waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
waitoptions.timeout = timeout
waitoptions.feeThreshold = 1
template2 = await wait_and_do(
mining_wait_next_template(template, stack, ctx, waitoptions),
lambda: self.generate(self.nodes[0], 1))
assert template2 is not None
block2 = await mining_get_block(template2, ctx)
assert_equal(len(block2.vtx), 1)
self.log.debug("Wait for another, but time out")
template3 = await mining_wait_next_template(template2, stack, ctx, waitoptions)
assert template3 is None
self.log.debug("Wait for another, get one after increase in fees in the mempool")
template4 = await wait_and_do(
mining_wait_next_template(template2, stack, ctx, waitoptions),
lambda: self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0]))
assert template4 is not None
block3 = await mining_get_block(template4, ctx)
assert_equal(len(block3.vtx), 2)
self.log.debug("Wait again, this should return the same template, since the fee threshold is zero")
waitoptions.feeThreshold = 0
template5 = await mining_wait_next_template(template4, stack, ctx, waitoptions)
assert template5 is not None
block4 = await mining_get_block(template5, ctx)
assert_equal(len(block4.vtx), 2)
waitoptions.feeThreshold = 1
self.log.debug("Wait for another, get one after increase in fees in the mempool")
template6 = await wait_and_do(
mining_wait_next_template(template5, stack, ctx, waitoptions),
lambda: self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0]))
assert template6 is not None
block4 = await mining_get_block(template6, ctx)
assert_equal(len(block4.vtx), 3)
self.log.debug("Wait for another, but time out, since the fee threshold is set now")
template7 = await mining_wait_next_template(template6, stack, ctx, waitoptions)
assert template7 is None
self.log.debug("interruptWait should abort the current wait")
async def wait_for_block():
new_waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
new_waitoptions.timeout = timeout * 60 # 1 minute wait
new_waitoptions.feeThreshold = 1
template7 = await mining_wait_next_template(template6, stack, ctx, new_waitoptions)
assert template7 is None
await wait_and_do(wait_for_block(), template6.interruptWait())
asyncio.run(capnp.run(async_routine()))
def run_ipc_option_override_test(self):
self.log.info("Running IPC option override test")
# Set an absurd reserved weight. `-blockreservedweight` is RPC-only, so
# with this setting RPC templates would be empty. IPC clients set
# blockReservedWeight per template request and are unaffected; later in
# the test the IPC template includes a mempool transaction.
self.restart_node(0, extra_args=[f"-blockreservedweight={MAX_BLOCK_WEIGHT}"])
async def async_routine():
ctx, mining = await self.make_mining_ctx()
self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
async with AsyncExitStack() as stack:
opts = self.capnp_modules['mining'].BlockCreateOptions()
template = await mining_create_block_template(mining, stack, ctx, opts)
assert template is not None
block = await mining_get_block(template, ctx)
assert_equal(len(block.vtx), 2)
self.log.debug("Use absurdly large reserved weight to force an empty template")
opts.blockReservedWeight = MAX_BLOCK_WEIGHT
empty_template = await mining_create_block_template(mining, stack, ctx, opts)
assert empty_template is not None
empty_block = await mining_get_block(empty_template, ctx)
assert_equal(len(empty_block.vtx), 1)
self.log.debug("Enforce minimum reserved weight for IPC clients too")
opts.blockReservedWeight = 0
try:
await mining.createNewBlock(ctx, opts)
raise AssertionError("createNewBlock unexpectedly succeeded")
except capnp.lib.capnp.KjException as e:
if e.type == "DISCONNECTED":
# The remote exception isn't caught currently and leads to a
# std::terminate call. Just detect and restart in this case.
# This bug is fixed with
# https://github.com/bitcoin-core/libmultiprocess/pull/218
assert_equal(e.description, "Peer disconnected.")
self.nodes[0].wait_until_stopped(expected_ret_code=(-11, -6, 1, 66), expected_stderr=re.compile(""))
self.start_node(0)
else:
assert_equal(e.description, "remote exception: std::exception: block_reserved_weight (0) must be at least 2000 weight units")
assert_equal(e.type, "FAILED")
asyncio.run(capnp.run(async_routine()))
def run_coinbase_and_submission_test(self):
"""Test coinbase construction (getCoinbaseTx) and block submission (submitSolution)."""
self.log.info("Running coinbase construction and submission test")
async def async_routine():
ctx, mining = await self.make_mining_ctx()
current_block_height = self.nodes[0].getchaintips()[0]["height"]
check_opts = self.capnp_modules['mining'].BlockCheckOptions()
async with destroying((await mining.createNewBlock(ctx, self.default_block_create_options)).result, ctx) as template:
block = await mining_get_block(template, ctx)
balance = self.miniwallet.get_balance()
coinbase = await self.build_coinbase_test(template, ctx, self.miniwallet)
# Reduce payout for balance comparison simplicity
coinbase.vout[0].nValue = COIN
block.vtx[0] = coinbase
block.hashMerkleRoot = block.calc_merkle_root()
original_version = block.nVersion
self.log.debug("Submit a block with a bad version")
block.nVersion = 0
block.solve()
check = await mining.checkBlock(ctx, block.serialize(), check_opts)
assert_equal(check.result, False)
assert_equal(check.reason, "bad-version(0x00000000)")
submitted = (await template.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize())).result
assert_equal(submitted, False)
self.log.debug("Submit a valid block")
block.nVersion = original_version
block.solve()
self.log.debug("First call checkBlock()")
block_valid = (await mining.checkBlock(ctx, block.serialize(), check_opts)).result
assert_equal(block_valid, True)
# The remote template block will be mutated, capture the original:
remote_block_before = await mining_get_block(template, ctx)
self.log.debug("Submitted coinbase must include witness")
assert_not_equal(coinbase.serialize_without_witness().hex(), coinbase.serialize().hex())
submitted = (await template.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize_without_witness())).result
assert_equal(submitted, False)
self.log.debug("Even a rejected submitSolution() mutates the template's block")
# Can be used by clients to download and inspect the (rejected)
# reconstructed block.
remote_block_after = await mining_get_block(template, ctx)
assert_not_equal(remote_block_before.serialize().hex(), remote_block_after.serialize().hex())
self.log.debug("Submit again, with the witness")
submitted = (await template.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize())).result
assert_equal(submitted, True)
self.log.debug("Block should propagate")
# Check that the IPC node actually updates its own chain
assert_equal(self.nodes[0].getchaintips()[0]["height"], current_block_height + 1)
# Stalls if a regression causes submitSolution() to accept an invalid block:
self.sync_all()
# Check that the other node accepts the block
assert_equal(self.nodes[0].getchaintips()[0], self.nodes[1].getchaintips()[0])
self.miniwallet.rescan_utxos()
assert_equal(self.miniwallet.get_balance(), balance + 1)
self.log.debug("Check block should fail now, since it is a duplicate")
check = await mining.checkBlock(ctx, block.serialize(), check_opts)
assert_equal(check.result, False)
assert_equal(check.reason, "inconclusive-not-best-prevblk")
asyncio.run(capnp.run(async_routine()))
def run_test(self):
self.miniwallet = MiniWallet(self.nodes[0])
self.default_block_create_options = self.capnp_modules['mining'].BlockCreateOptions()
self.run_mining_interface_test()
self.run_block_template_test()
self.run_coinbase_and_submission_test()
self.run_ipc_option_override_test()
if __name__ == '__main__':
IPCMiningTest(__file__).main()