From 63684d6922e5c034ff620d23c35ba175edae49c4 Mon Sep 17 00:00:00 2001 From: Enoch Azariah Date: Wed, 4 Mar 2026 00:41:08 +0100 Subject: [PATCH] test: move make_mining_ctx to ipc_util.py The async routines in both interface_ipc.py and interface_ipc_mining.py contain redundant code to initialize the mining proxy object. Move the make_mining_ctx helper into test_framework/ipc_util.py and update both test files to use it. This removes the boilerplate and prevents code duplication across the IPC test suite. --- test/functional/interface_ipc.py | 15 ++++----------- test/functional/interface_ipc_mining.py | 19 ++++++------------- test/functional/test_framework/ipc_util.py | 7 +++++++ 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py index 1e279e00cb3..71a49685bf7 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 67f77fee5f7..6f2237a883d 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 11497463eb3..c80f78f79b7 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