0diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py
1index 3d28bba136..e01c753d24 100755
2--- a/test/functional/interface_ipc.py
3+++ b/test/functional/interface_ipc.py
4@@ -96,6 +96,14 @@ class IPCInterfaceTest(BitcoinTestFramework):
5 tx.deserialize(coinbase_data)
6 return tx
7
8+ async def create_block_template(self, stack, ctx, mining, opts):
9+ # Destroyed when stack exits.
10+ return await stack.enter_async_context(destroying((await mining.result.createNewBlock(opts)).result, ctx))
11+
12+ async def wait_next_template(self, stack, ctx, template, waitoptions):
13+ # Destroyed when stack exits.
14+ return await stack.enter_async_context(destroying((await template.waitNext(ctx, waitoptions)).result, ctx))
15+
16 def run_echo_test(self):
17 self.log.info("Running echo test")
18 async def async_routine():
19@@ -148,7 +156,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
20 opts.useMempool = True
21 opts.blockReservedWeight = 4000
22 opts.coinbaseOutputMaxAdditionalSigops = 0
23- template = await stack.enter_async_context(destroying((await mining.result.createNewBlock(opts)).result, ctx))
24+ template = await self.create_block_template(stack, ctx, mining, opts)
25 self.log.debug("Test some inspectors of Template")
26 header = await template.getBlockHeader(ctx)
27 assert_equal(len(header.result), block_header_size)
28@@ -168,7 +176,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
29 waitoptions.timeout = timeout
30 waitoptions.feeThreshold = 1
31 self.generate(self.nodes[0], 1)
32- template2 = await stack.enter_async_context(destroying((await template.waitNext(ctx, waitoptions)).result, ctx))
33+ template2 = await self.wait_next_template(stack, ctx, template, waitoptions)
34 block2 = await self.parse_and_deserialize_block(template2, ctx)
35 assert_equal(len(block2.vtx), 1)
36 self.log.debug("Wait for another, but time out")
37@@ -176,18 +184,18 @@ class IPCInterfaceTest(BitcoinTestFramework):
38 assert_equal(template3.to_dict(), {})
39 self.log.debug("Wait for another, get one after increase in fees in the mempool")
40 miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
41- template4 = await stack.enter_async_context(destroying((await template2.waitNext(ctx, waitoptions)).result, ctx))
42+ template4 = await self.wait_next_template(stack, ctx, template2, waitoptions)
43 block3 = await self.parse_and_deserialize_block(template4, ctx)
44 assert_equal(len(block3.vtx), 2)
45 self.log.debug("Wait again, this should return the same template, since the fee threshold is zero")
46 waitoptions.feeThreshold = 0
47- template5 = await stack.enter_async_context(destroying((await template4.waitNext(ctx, waitoptions)).result, ctx))
48+ template5 = await self.wait_next_template(stack, ctx, template4, waitoptions)
49 block4 = await self.parse_and_deserialize_block(template5, ctx)
50 assert_equal(len(block4.vtx), 2)
51 waitoptions.feeThreshold = 1
52 self.log.debug("Wait for another, get one after increase in fees in the mempool")
53 miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
54- template6 = await stack.enter_async_context(destroying((await template5.waitNext(ctx, waitoptions)).result, ctx))
55+ template6 = await self.wait_next_template(stack, ctx, template5, waitoptions)
56 block4 = await self.parse_and_deserialize_block(template6, ctx)
57 assert_equal(len(block4.vtx), 3)
58 self.log.debug("Wait for another, but time out, since the fee threshold is set now")