In commit "ipc, test: Add tests for unclean disconnect and thread busy behavior" (1fea3bae):
The block of code initializing the mining object in each async routine appears redundant. It could be replaced by the make_mining_ctx method introduced in commit 4e49fa2a.
While this might be outside the immediate scope of this PR, moving make_mining_ctx to ipc_util.py would allow it to be reused across both interface_ipc_mining.py and interface_ipc.py, preventing code duplication.
Here is a diff showing how this change could be implemented:
diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py
index 6201fe122e..ce96392899 100755
--- a/test/functional/interface_ipc.py
+++ b/test/functional/interface_ipc.py
@@ -11,6 +11,7 @@ from test_framework.util import assert_equal
from test_framework.ipc_util import (
load_capnp_modules,
make_capnp_init_ctx,
+ make_mining_ctx,
)
# Test may be skipped and not have capnp installed
@@ -93,10 +94,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
disconnected_log_check = ExitStack()
async def async_routine():
- ctx, init = await make_capnp_init_ctx(self)
- self.log.debug("Create Mining proxy object")
- mining = init.makeMining(ctx).result
-
+ ctx, mining = await make_mining_ctx(self)
self.log.debug("Create a template")
opts = self.capnp_modules['mining'].BlockCreateOptions()
template = (await mining.createNewBlock(ctx, opts)).result
@@ -129,10 +127,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
timeout = self.rpc_timeout * 1000.0
async def async_routine():
- ctx, init = await make_capnp_init_ctx(self)
- self.log.debug("Create Mining proxy object")
- mining = init.makeMining(ctx).result
-
+ ctx, mining = await make_mining_ctx(self)
self.log.debug("Create a template")
opts = self.capnp_modules['mining'].BlockCreateOptions()
template = (await mining.createNewBlock(ctx, opts)).result
diff --git a/test/functional/interface_ipc_mining.py b/test/functional/interface_ipc_mining.py
index d4bf42ba9e..750a26c69f 100755
--- a/test/functional/interface_ipc_mining.py
+++ b/test/functional/interface_ipc_mining.py
@@ -31,11 +31,11 @@ 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,
+ make_mining_ctx,
)
# Test may be skipped and not have capnp installed
@@ -100,13 +100,6 @@ class IPCMiningTest(BitcoinTestFramework):
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")
@@ -114,7 +107,7 @@ class IPCMiningTest(BitcoinTestFramework):
timeout = 1000.0 # 1000 milliseconds
async def async_routine():
- ctx, mining = await self.make_mining_ctx()
+ ctx, mining = await make_mining_ctx(self)
blockref = await mining.getTip(ctx)
current_block_height = self.nodes[0].getchaintips()[0]["height"]
assert_equal(blockref.result.height, current_block_height)
@@ -140,7 +133,7 @@ class IPCMiningTest(BitcoinTestFramework):
timeout = 1000.0 # 1000 milliseconds
async def async_routine():
- ctx, mining = await self.make_mining_ctx()
+ ctx, mining = await make_mining_ctx(self)
blockref = await mining.getTip(ctx)
async with AsyncExitStack() as stack:
@@ -222,7 +215,7 @@ class IPCMiningTest(BitcoinTestFramework):
self.restart_node(0, extra_args=[f"-blockreservedweight={MAX_BLOCK_WEIGHT}"])
async def async_routine():
- ctx, mining = await self.make_mining_ctx()
+ ctx, mining = await make_mining_ctx(self)
self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
async with AsyncExitStack() as stack:
@@ -264,7 +257,7 @@ class IPCMiningTest(BitcoinTestFramework):
self.log.info("Running coinbase construction and submission test")
async def async_routine():
- ctx, mining = await self.make_mining_ctx()
+ ctx, mining = await make_mining_ctx(self)
current_block_height = self.nodes[0].getchaintips()[0]["height"]
check_opts = self.capnp_modules['mining'].BlockCheckOptions()
diff --git a/test/functional/test_framework/ipc_util.py b/test/functional/test_framework/ipc_util.py
index 11497463eb..a3f2ca44fc 100644
--- a/test/functional/test_framework/ipc_util.py
+++ b/test/functional/test_framework/ipc_util.py
@@ -148,3 +148,10 @@ async def mining_get_coinbase_tx(block_template, ctx) -> CoinbaseTxData:
requiredOutputs=[bytes(output) for output in template_capnp.requiredOutputs],
lockTime=int(template_capnp.lockTime),
)
+
+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