test: move make_mining_ctx to ipc_util.py
What changed, and why it matters
This commit is a simple code cleanup in Bitcoin Core's test suite. It moves a small helper function that creates a mining test object from two individual test files into a shared utility file, so the same code isn't duplicated. There is no change to the actual Bitcoin node software, no change to how tests behave, and no security relevance.
No action required. This is a benign test refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors test-only Python code. A helper make_mining_ctx is extracted from interface_ipc_mining.py and placed in test_framework/ipc_util.py, then both interface_ipc.py and interface_ipc_mining.py are updated to import and call the shared helper. The implementation is identical: it awaits make_capnp_init_ctx(self), logs ‘Create Mining proxy object’, and returns init.makeMining(ctx).result. No functional behavior is altered.
Changed components
test/functional/interface_ipc.pytest/functional/interface_ipc_mining.pytest/functional/test_framework/ipc_util.pyInspect captured patch +17 / −24
diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py
index 1e279e00..71a49685 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
@@ -55,9 +56,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
block_hash_size = 32
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("Test simple inspectors")
assert (await mining.isTestChain(ctx)).result
assert not (await mining.isInitialBlockDownload(ctx)).result
@@ -93,10 +92,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 +125,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 67f77fee..6f2237a8 100755
--- a/test/functional/interface_ipc_mining.py
+++ b/test/functional/interface_ipc_mining.py
@@ -37,11 +37,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
@@ -106,13 +106,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")
@@ -120,7 +113,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)
@@ -159,7 +152,7 @@ class IPCMiningTest(BitcoinTestFramework):
async def async_routine():
while True:
try:
- ctx, mining = await self.make_mining_ctx()
+ ctx, mining = await make_mining_ctx(self)
break
except (ConnectionRefusedError, FileNotFoundError):
# Poll quickly to connect as soon as socket becomes
@@ -182,7 +175,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)
async with AsyncExitStack() as stack:
self.log.debug("createNewBlock() should wait if tip is still updating")
@@ -309,7 +302,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:
@@ -349,7 +342,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 11497463..c80f78f7 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
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.