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:
0diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py
1index 6201fe122e..ce96392899 100755
2--- a/test/functional/interface_ipc.py
3+++ b/test/functional/interface_ipc.py
4@@ -11,6 +11,7 @@ from test_framework.util import assert_equal
5 from test_framework.ipc_util import (
6 load_capnp_modules,
7 make_capnp_init_ctx,
8+ make_mining_ctx,
9 )
10
11 # Test may be skipped and not have capnp installed
12@@ -93,10 +94,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
13 disconnected_log_check = ExitStack()
14
15 async def async_routine():
16- ctx, init = await make_capnp_init_ctx(self)
17- self.log.debug("Create Mining proxy object")
18- mining = init.makeMining(ctx).result
19-
20+ ctx, mining = await make_mining_ctx(self)
21 self.log.debug("Create a template")
22 opts = self.capnp_modules['mining'].BlockCreateOptions()
23 template = (await mining.createNewBlock(ctx, opts)).result
24@@ -129,10 +127,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
25 timeout = self.rpc_timeout * 1000.0
26
27 async def async_routine():
28- ctx, init = await make_capnp_init_ctx(self)
29- self.log.debug("Create Mining proxy object")
30- mining = init.makeMining(ctx).result
31-
32+ ctx, mining = await make_mining_ctx(self)
33 self.log.debug("Create a template")
34 opts = self.capnp_modules['mining'].BlockCreateOptions()
35 template = (await mining.createNewBlock(ctx, opts)).result
36diff --git a/test/functional/interface_ipc_mining.py b/test/functional/interface_ipc_mining.py
37index d4bf42ba9e..750a26c69f 100755
38--- a/test/functional/interface_ipc_mining.py
39+++ b/test/functional/interface_ipc_mining.py
40@@ -31,11 +31,11 @@ from test_framework.ipc_util import (
41 destroying,
42 mining_create_block_template,
43 load_capnp_modules,
44- make_capnp_init_ctx,
45 mining_get_block,
46 mining_get_coinbase_tx,
47 mining_wait_next_template,
48 wait_and_do,
49+ make_mining_ctx,
50 )
51
52 # Test may be skipped and not have capnp installed
53@@ -100,13 +100,6 @@ class IPCMiningTest(BitcoinTestFramework):
54 coinbase_tx.nLockTime = coinbase_res.lockTime
55 return coinbase_tx
56
57- async def make_mining_ctx(self):
58- """Create IPC context and Mining proxy object."""
59- ctx, init = await make_capnp_init_ctx(self)
60- self.log.debug("Create Mining proxy object")
61- mining = init.makeMining(ctx).result
62- return ctx, mining
63-
64 def run_mining_interface_test(self):
65 """Test Mining interface methods."""
66 self.log.info("Running Mining interface test")
67@@ -114,7 +107,7 @@ class IPCMiningTest(BitcoinTestFramework):
68 timeout = 1000.0 # 1000 milliseconds
69
70 async def async_routine():
71- ctx, mining = await self.make_mining_ctx()
72+ ctx, mining = await make_mining_ctx(self)
73 blockref = await mining.getTip(ctx)
74 current_block_height = self.nodes[0].getchaintips()[0]["height"]
75 assert_equal(blockref.result.height, current_block_height)
76@@ -140,7 +133,7 @@ class IPCMiningTest(BitcoinTestFramework):
77 timeout = 1000.0 # 1000 milliseconds
78
79 async def async_routine():
80- ctx, mining = await self.make_mining_ctx()
81+ ctx, mining = await make_mining_ctx(self)
82 blockref = await mining.getTip(ctx)
83
84 async with AsyncExitStack() as stack:
85@@ -222,7 +215,7 @@ class IPCMiningTest(BitcoinTestFramework):
86 self.restart_node(0, extra_args=[f"-blockreservedweight={MAX_BLOCK_WEIGHT}"])
87
88 async def async_routine():
89- ctx, mining = await self.make_mining_ctx()
90+ ctx, mining = await make_mining_ctx(self)
91 self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
92
93 async with AsyncExitStack() as stack:
94@@ -264,7 +257,7 @@ class IPCMiningTest(BitcoinTestFramework):
95 self.log.info("Running coinbase construction and submission test")
96
97 async def async_routine():
98- ctx, mining = await self.make_mining_ctx()
99+ ctx, mining = await make_mining_ctx(self)
100
101 current_block_height = self.nodes[0].getchaintips()[0]["height"]
102 check_opts = self.capnp_modules['mining'].BlockCheckOptions()
103diff --git a/test/functional/test_framework/ipc_util.py b/test/functional/test_framework/ipc_util.py
104index 11497463eb..a3f2ca44fc 100644
105--- a/test/functional/test_framework/ipc_util.py
106+++ b/test/functional/test_framework/ipc_util.py
107@@ -148,3 +148,10 @@ async def mining_get_coinbase_tx(block_template, ctx) -> CoinbaseTxData:
108 requiredOutputs=[bytes(output) for output in template_capnp.requiredOutputs],
109 lockTime=int(template_capnp.lockTime),
110 )
111+
112+async def make_mining_ctx(self):
113+ """Create IPC context and Mining proxy object."""
114+ ctx, init = await make_capnp_init_ctx(self)
115+ self.log.debug("Create Mining proxy object")
116+ mining = init.makeMining(ctx).result
117+ return ctx, mining