mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-18 08:32:30 +01:00
test: fix interface_ipc.py template destruction
Use context managers to destroy block templates. Previously, block templates were not being destroyed before disconnecting because the destroy coroutines were called but never awaited. It's not necessary to explicitly destroy the templates since they will get garbage collected asynchronously, but it's good to destroy them to make the test more predictable, and to make the destroy calls that are present actually do something. This change also removes `await waitnext` expressions without changing behavior, because the previous code was misleading about what order waitNext calls were executed. This change is easiest to review ignoring whitespace. Co-authored-by: Sjors Provoost <sjors@sprovoost.nl>
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
"""Test the IPC (multiprocess) interface."""
|
"""Test the IPC (multiprocess) interface."""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from contextlib import asynccontextmanager, AsyncExitStack
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
@@ -21,6 +22,21 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def destroying(obj, ctx):
|
||||||
|
"""Call obj.destroy(ctx) at end of with: block. Similar to contextlib.closing."""
|
||||||
|
try:
|
||||||
|
yield obj
|
||||||
|
finally:
|
||||||
|
await obj.destroy(ctx)
|
||||||
|
|
||||||
|
async def create_block_template(mining, stack, ctx, opts):
|
||||||
|
"""Call mining.createNewBlock() and return template, then call template.destroy() when stack exits."""
|
||||||
|
return await stack.enter_async_context(destroying((await mining.createNewBlock(opts)).result, ctx))
|
||||||
|
|
||||||
|
async def wait_next_template(template, stack, ctx, opts):
|
||||||
|
"""Call template.waitNext() and return template, then call template.destroy() when stack exits."""
|
||||||
|
return await stack.enter_async_context(destroying((await template.waitNext(ctx, opts)).result, ctx))
|
||||||
|
|
||||||
class IPCInterfaceTest(BitcoinTestFramework):
|
class IPCInterfaceTest(BitcoinTestFramework):
|
||||||
|
|
||||||
@@ -77,13 +93,13 @@ class IPCInterfaceTest(BitcoinTestFramework):
|
|||||||
return ctx, init
|
return ctx, init
|
||||||
|
|
||||||
async def parse_and_deserialize_block(self, block_template, ctx):
|
async def parse_and_deserialize_block(self, block_template, ctx):
|
||||||
block_data = BytesIO((await block_template.result.getBlock(ctx)).result)
|
block_data = BytesIO((await block_template.getBlock(ctx)).result)
|
||||||
block = CBlock()
|
block = CBlock()
|
||||||
block.deserialize(block_data)
|
block.deserialize(block_data)
|
||||||
return block
|
return block
|
||||||
|
|
||||||
async def parse_and_deserialize_coinbase_tx(self, block_template, ctx):
|
async def parse_and_deserialize_coinbase_tx(self, block_template, ctx):
|
||||||
coinbase_data = BytesIO((await block_template.result.getCoinbaseTx(ctx)).result)
|
coinbase_data = BytesIO((await block_template.getCoinbaseTx(ctx)).result)
|
||||||
tx = CTransaction()
|
tx = CTransaction()
|
||||||
tx.deserialize(coinbase_data)
|
tx.deserialize(coinbase_data)
|
||||||
return tx
|
return tx
|
||||||
@@ -134,126 +150,124 @@ class IPCInterfaceTest(BitcoinTestFramework):
|
|||||||
assert_equal(oldblockref.result.hash, newblockref.result.hash)
|
assert_equal(oldblockref.result.hash, newblockref.result.hash)
|
||||||
assert_equal(oldblockref.result.height, newblockref.result.height)
|
assert_equal(oldblockref.result.height, newblockref.result.height)
|
||||||
|
|
||||||
self.log.debug("Create a template")
|
async with AsyncExitStack() as stack:
|
||||||
opts = self.capnp_modules['mining'].BlockCreateOptions()
|
self.log.debug("Create a template")
|
||||||
opts.useMempool = True
|
opts = self.capnp_modules['mining'].BlockCreateOptions()
|
||||||
opts.blockReservedWeight = 4000
|
opts.useMempool = True
|
||||||
opts.coinbaseOutputMaxAdditionalSigops = 0
|
opts.blockReservedWeight = 4000
|
||||||
template = mining.result.createNewBlock(opts)
|
opts.coinbaseOutputMaxAdditionalSigops = 0
|
||||||
self.log.debug("Test some inspectors of Template")
|
template = await create_block_template(mining.result, stack, ctx, opts)
|
||||||
header = await template.result.getBlockHeader(ctx)
|
self.log.debug("Test some inspectors of Template")
|
||||||
assert_equal(len(header.result), block_header_size)
|
header = await template.getBlockHeader(ctx)
|
||||||
block = await self.parse_and_deserialize_block(template, ctx)
|
assert_equal(len(header.result), block_header_size)
|
||||||
assert_equal(ser_uint256(block.hashPrevBlock), newblockref.result.hash)
|
block = await self.parse_and_deserialize_block(template, ctx)
|
||||||
assert len(block.vtx) >= 1
|
assert_equal(ser_uint256(block.hashPrevBlock), newblockref.result.hash)
|
||||||
txfees = await template.result.getTxFees(ctx)
|
assert len(block.vtx) >= 1
|
||||||
assert_equal(len(txfees.result), 0)
|
txfees = await template.getTxFees(ctx)
|
||||||
txsigops = await template.result.getTxSigops(ctx)
|
assert_equal(len(txfees.result), 0)
|
||||||
assert_equal(len(txsigops.result), 0)
|
txsigops = await template.getTxSigops(ctx)
|
||||||
coinbase_data = BytesIO((await template.result.getCoinbaseTx(ctx)).result)
|
assert_equal(len(txsigops.result), 0)
|
||||||
coinbase = CTransaction()
|
coinbase_data = BytesIO((await template.getCoinbaseTx(ctx)).result)
|
||||||
coinbase.deserialize(coinbase_data)
|
coinbase = CTransaction()
|
||||||
assert_equal(coinbase.vin[0].prevout.hash, 0)
|
coinbase.deserialize(coinbase_data)
|
||||||
self.log.debug("Wait for a new template")
|
assert_equal(coinbase.vin[0].prevout.hash, 0)
|
||||||
waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
|
self.log.debug("Wait for a new template")
|
||||||
waitoptions.timeout = timeout
|
waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
|
||||||
waitoptions.feeThreshold = 1
|
waitoptions.timeout = timeout
|
||||||
waitnext = template.result.waitNext(ctx, waitoptions)
|
waitoptions.feeThreshold = 1
|
||||||
self.generate(self.nodes[0], 1)
|
self.generate(self.nodes[0], 1)
|
||||||
template2 = await waitnext
|
template2 = await wait_next_template(template, stack, ctx, waitoptions)
|
||||||
block2 = await self.parse_and_deserialize_block(template2, ctx)
|
block2 = await self.parse_and_deserialize_block(template2, ctx)
|
||||||
assert_equal(len(block2.vtx), 1)
|
assert_equal(len(block2.vtx), 1)
|
||||||
self.log.debug("Wait for another, but time out")
|
self.log.debug("Wait for another, but time out")
|
||||||
template3 = await template2.result.waitNext(ctx, waitoptions)
|
template3 = await template2.waitNext(ctx, waitoptions)
|
||||||
assert_equal(template3.to_dict(), {})
|
assert_equal(template3.to_dict(), {})
|
||||||
self.log.debug("Wait for another, get one after increase in fees in the mempool")
|
self.log.debug("Wait for another, get one after increase in fees in the mempool")
|
||||||
waitnext = template2.result.waitNext(ctx, waitoptions)
|
|
||||||
miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
|
|
||||||
template4 = await waitnext
|
|
||||||
block3 = await self.parse_and_deserialize_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 template4.result.waitNext(ctx, waitoptions)
|
|
||||||
block4 = await self.parse_and_deserialize_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")
|
|
||||||
waitnext = template5.result.waitNext(ctx, waitoptions)
|
|
||||||
miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
|
|
||||||
template6 = await waitnext
|
|
||||||
block4 = await self.parse_and_deserialize_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 template6.result.waitNext(ctx, waitoptions)
|
|
||||||
assert_equal(template7.to_dict(), {})
|
|
||||||
|
|
||||||
self.log.debug("interruptWait should abort the current wait")
|
|
||||||
wait_started = asyncio.Event()
|
|
||||||
async def wait_for_block():
|
|
||||||
new_waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
|
|
||||||
new_waitoptions.timeout = waitoptions.timeout * 60 # 1 minute wait
|
|
||||||
new_waitoptions.feeThreshold = 1
|
|
||||||
wait_started.set()
|
|
||||||
return await template6.result.waitNext(ctx, new_waitoptions)
|
|
||||||
|
|
||||||
async def interrupt_wait():
|
|
||||||
await wait_started.wait() # Wait for confirmation wait started
|
|
||||||
await asyncio.sleep(0.1) # Minimal buffer
|
|
||||||
template6.result.interruptWait()
|
|
||||||
miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
|
miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
|
||||||
|
template4 = await wait_next_template(template2, stack, ctx, waitoptions)
|
||||||
|
block3 = await self.parse_and_deserialize_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 wait_next_template(template4, stack, ctx, waitoptions)
|
||||||
|
block4 = await self.parse_and_deserialize_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")
|
||||||
|
miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
|
||||||
|
template6 = await wait_next_template(template5, stack, ctx, waitoptions)
|
||||||
|
block4 = await self.parse_and_deserialize_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 template6.waitNext(ctx, waitoptions)
|
||||||
|
assert_equal(template7.to_dict(), {})
|
||||||
|
|
||||||
wait_task = asyncio.create_task(wait_for_block())
|
self.log.debug("interruptWait should abort the current wait")
|
||||||
interrupt_task = asyncio.create_task(interrupt_wait())
|
wait_started = asyncio.Event()
|
||||||
|
async def wait_for_block():
|
||||||
|
new_waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
|
||||||
|
new_waitoptions.timeout = waitoptions.timeout * 60 # 1 minute wait
|
||||||
|
new_waitoptions.feeThreshold = 1
|
||||||
|
wait_started.set()
|
||||||
|
return await template6.waitNext(ctx, new_waitoptions)
|
||||||
|
|
||||||
result = await wait_task
|
async def interrupt_wait():
|
||||||
await interrupt_task
|
await wait_started.wait() # Wait for confirmation wait started
|
||||||
assert_equal(result.to_dict(), {})
|
await asyncio.sleep(0.1) # Minimal buffer
|
||||||
|
template6.interruptWait()
|
||||||
|
miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
|
||||||
|
|
||||||
|
wait_task = asyncio.create_task(wait_for_block())
|
||||||
|
interrupt_task = asyncio.create_task(interrupt_wait())
|
||||||
|
|
||||||
|
result = await wait_task
|
||||||
|
await interrupt_task
|
||||||
|
assert_equal(result.to_dict(), {})
|
||||||
|
|
||||||
current_block_height = self.nodes[0].getchaintips()[0]["height"]
|
current_block_height = self.nodes[0].getchaintips()[0]["height"]
|
||||||
check_opts = self.capnp_modules['mining'].BlockCheckOptions()
|
check_opts = self.capnp_modules['mining'].BlockCheckOptions()
|
||||||
template = await mining.result.createNewBlock(opts)
|
async with destroying((await mining.result.createNewBlock(opts)).result, ctx) as template:
|
||||||
block = await self.parse_and_deserialize_block(template, ctx)
|
block = await self.parse_and_deserialize_block(template, ctx)
|
||||||
coinbase = await self.parse_and_deserialize_coinbase_tx(template, ctx)
|
coinbase = await self.parse_and_deserialize_coinbase_tx(template, ctx)
|
||||||
balance = miniwallet.get_balance()
|
balance = miniwallet.get_balance()
|
||||||
coinbase.vout[0].scriptPubKey = miniwallet.get_output_script()
|
coinbase.vout[0].scriptPubKey = miniwallet.get_output_script()
|
||||||
coinbase.vout[0].nValue = COIN
|
coinbase.vout[0].nValue = COIN
|
||||||
block.vtx[0] = coinbase
|
block.vtx[0] = coinbase
|
||||||
block.hashMerkleRoot = block.calc_merkle_root()
|
block.hashMerkleRoot = block.calc_merkle_root()
|
||||||
original_version = block.nVersion
|
original_version = block.nVersion
|
||||||
self.log.debug("Submit a block with a bad version")
|
self.log.debug("Submit a block with a bad version")
|
||||||
block.nVersion = 0
|
block.nVersion = 0
|
||||||
block.solve()
|
block.solve()
|
||||||
res = await mining.result.checkBlock(block.serialize(), check_opts)
|
res = await mining.result.checkBlock(block.serialize(), check_opts)
|
||||||
assert_equal(res.result, False)
|
assert_equal(res.result, False)
|
||||||
assert_equal(res.reason, "bad-version(0x00000000)")
|
assert_equal(res.reason, "bad-version(0x00000000)")
|
||||||
res = await template.result.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize())
|
res = await template.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize())
|
||||||
assert_equal(res.result, False)
|
assert_equal(res.result, False)
|
||||||
self.log.debug("Submit a valid block")
|
self.log.debug("Submit a valid block")
|
||||||
block.nVersion = original_version
|
block.nVersion = original_version
|
||||||
block.solve()
|
block.solve()
|
||||||
|
|
||||||
self.log.debug("First call checkBlock()")
|
self.log.debug("First call checkBlock()")
|
||||||
res = await mining.result.checkBlock(block.serialize(), check_opts)
|
res = await mining.result.checkBlock(block.serialize(), check_opts)
|
||||||
assert_equal(res.result, True)
|
assert_equal(res.result, True)
|
||||||
|
|
||||||
# The remote template block will be mutated, capture the original:
|
# The remote template block will be mutated, capture the original:
|
||||||
remote_block_before = await self.parse_and_deserialize_block(template, ctx)
|
remote_block_before = await self.parse_and_deserialize_block(template, ctx)
|
||||||
|
|
||||||
self.log.debug("Submitted coinbase must include witness")
|
self.log.debug("Submitted coinbase must include witness")
|
||||||
assert_not_equal(coinbase.serialize_without_witness().hex(), coinbase.serialize().hex())
|
assert_not_equal(coinbase.serialize_without_witness().hex(), coinbase.serialize().hex())
|
||||||
res = await template.result.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize_without_witness())
|
res = await template.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize_without_witness())
|
||||||
assert_equal(res.result, False)
|
assert_equal(res.result, False)
|
||||||
|
|
||||||
self.log.debug("Even a rejected submitBlock() mutates the template's block")
|
self.log.debug("Even a rejected submitBlock() mutates the template's block")
|
||||||
# Can be used by clients to download and inspect the (rejected)
|
# Can be used by clients to download and inspect the (rejected)
|
||||||
# reconstructed block.
|
# reconstructed block.
|
||||||
remote_block_after = await self.parse_and_deserialize_block(template, ctx)
|
remote_block_after = await self.parse_and_deserialize_block(template, ctx)
|
||||||
assert_not_equal(remote_block_before.serialize().hex(), remote_block_after.serialize().hex())
|
assert_not_equal(remote_block_before.serialize().hex(), remote_block_after.serialize().hex())
|
||||||
|
|
||||||
self.log.debug("Submit again, with the witness")
|
self.log.debug("Submit again, with the witness")
|
||||||
res = await template.result.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize())
|
res = await template.submitSolution(ctx, block.nVersion, block.nTime, block.nNonce, coinbase.serialize())
|
||||||
assert_equal(res.result, True)
|
assert_equal(res.result, True)
|
||||||
|
|
||||||
self.log.debug("Block should propagate")
|
self.log.debug("Block should propagate")
|
||||||
# Check that the IPC node actually updates its own chain
|
# Check that the IPC node actually updates its own chain
|
||||||
@@ -270,14 +284,6 @@ class IPCInterfaceTest(BitcoinTestFramework):
|
|||||||
assert_equal(res.result, False)
|
assert_equal(res.result, False)
|
||||||
assert_equal(res.reason, "inconclusive-not-best-prevblk")
|
assert_equal(res.reason, "inconclusive-not-best-prevblk")
|
||||||
|
|
||||||
self.log.debug("Destroy template objects")
|
|
||||||
template.result.destroy(ctx)
|
|
||||||
template2.result.destroy(ctx)
|
|
||||||
template3.result.destroy(ctx)
|
|
||||||
template4.result.destroy(ctx)
|
|
||||||
template5.result.destroy(ctx)
|
|
||||||
template6.result.destroy(ctx)
|
|
||||||
template7.result.destroy(ctx)
|
|
||||||
asyncio.run(capnp.run(async_routine()))
|
asyncio.run(capnp.run(async_routine()))
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user