Repository: bitcoin/bitcoin
File: test/functional/interface_ipc.py (Python)

Code before:
        self.log.info("Running echo test")
        async def async_routine():
            ctx, init = await self.make_capnp_init_ctx()
            self.log.debug("Create Echo proxy object")
            echo = init.makeEcho(ctx).result
            self.log.debug("Test a few invocations of echo")
            for s in ["hallo", "", "haha"]:
                result_eval = (await echo.echo(ctx, s)).result
                assert_equal(s, result_eval)
            self.log.debug("Destroy the Echo object")
            echo.destroy(ctx)
        asyncio.run(capnp.run(async_routine()))

    def run_mining_test(self):
        self.log.info("Running mining test")
        async def async_routine():
            ctx, init = await self.make_capnp_init_ctx()
            self.log.debug("Create Mining proxy object")
            mining = init.makeMining(ctx)
            self.log.debug("Test simple inspectors")
            assert (await mining.result.isTestChain(ctx))
            assert (await mining.result.isInitialBlockDownload(ctx))
            blockref = await mining.result.getTip(ctx)
            assert blockref.hasResult
            assert_equal(len(blockref.result.hash), 32)
            assert blockref.result.height > 100
            self.log.debug("Mine a block")
            wait = mining.result.waitTipChanged(ctx, blockref.result.hash, 1000.0)
            self.generate(self.nodes[0], 1)
            newblockref = await wait
            assert_equal(len(newblockref.result.hash), 32)
            assert_equal(newblockref.result.height, blockref.result.height + 1)
            self.log.debug("Wait for timeout (1000 milliseconds)")
            wait = mining.result.waitTipChanged(ctx, newblockref.result.hash, 1000.0)
            oldblockref = await wait
            assert_equal(len(newblockref.result.hash), 32)
            assert_equal(oldblockref.result.hash, newblockref.result.hash)
            assert_equal(oldblockref.result.height, newblockref.result.height)

            self.log.debug("Create a template")
            opts = self.capnp_modules['mining'].BlockCreateOptions()
            opts.useMempool = True
            opts.blockReservedWeight = 4000
            opts.coinbaseOutputMaxAdditionalSigops = 0
            template = mining.result.createNewBlock(opts)
            self.log.debug("Test some inspectors of Template")
            header = await template.result.getBlockHeader(ctx)
            assert_equal(len(header.result), 80)
            block = await self.parse_and_deserialize_block(template, ctx)
            assert_equal(ser_uint256(block.hashPrevBlock), newblockref.result.hash)
            assert len(block.vtx) >= 1

Review comment (bug):
In "tests: add functional tests for IPC interface" 592e253f76764f108e9c34fa0da32451091b946a

It will be nice if we use defined constants or define new ones for the magic numbers, or use response from rpc.
It will help in improving the readability of the test.
```diff
diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py
index 6ff11cbe0f8..912fa3a459b 100755
--- a/test/functional/interface_ipc.py
+++ b/test/functional/interface_ipc.py
@@ -97,18 +97,21 @@ class IPCInterfaceTest(BitcoinTestFramework):
             assert (await mining.result.isInitialBlockDownload(ctx))
             blockref = await mining.result.getTip(ctx)
             assert blockref.hasResult
-            assert_equal(len(blockref.result.hash), 32)
-            assert blockref.result.height > 100
+            block_hash_size = 32
+            assert_equal(len(blockref.result.hash), block_hash_size)
+            current_block_height = self.nodes[0].getchaintips()[0]["height"]
+            assert blockref.result.height == current_block_height
             self.log.debug("Mine a block")
-            wait = mining.result.waitTipChanged(ctx, blockref.result.hash, 1000.0)
+            timeout = 1000.0 # 1000 milliseconds
+            wait = mining.result.waitTipChanged(ctx, blockref.result.hash, )
             self.generate(self.nodes[0], 1)
             newblockref = await wait
-            assert_equal(len(newblockref.result.hash), 32)
-            assert_equal(newblockref.result.height, blockref.result.height + 1)
-            self.log.debug("Wait for timeout (1000 milliseconds)")
-            wait = mining.result.waitTipChanged(ctx, newblockref.result.hash, 1000.0)
+            assert_equal(len(newblockref.result.hash), block_hash_size)
+            assert_equal(newblockref.result.height, current_block_height + 1)
+            self.log.debug("Wait for timeout")
+            wait = mining.result.waitTipChanged(ctx, newblockref.result.hash, timeout)
             oldblockref = await wait
-            assert_equal(len(newblockref.result.hash), 32)
+            assert_equal(len(newblockref.result.hash), block_hash_size)
             assert_equal(oldblockref.result.hash, newblockref.result.hash)
             assert_equal(oldblockref.result.height, newblockref.result.height)
 
@@ -120,7 +123,8 @@ class IPCInterfaceTest(BitcoinTestFramework):
             template = mining.result.createNewBlock(opts)
             self.log.debug("Test some inspectors of Template")
             header = await template.result.getBlockHeader(ctx)
-            assert_equal(len(header.result), 80)
+            block_header_size = 80
+            assert_equal(len(header.result), block_header_size)
             block = await self.parse_and_deserialize_block(template, ctx)
             assert_equal(ser_uint256(block.hashPrevBlock), newblockref.result.hash)
             assert len(block.vtx) >= 1
@@ -134,7 +138,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
             assert_equal(coinbase.vin[0].prevout.hash, 0)
             self.log.debug("Wait for a new template")
             waitoptions = self.capnp_modules['mining'].BlockWaitOptions()
-            waitoptions.timeout = 1000.0
+            waitoptions.timeout = timeout
             waitnext = template.result.waitNext(ctx, waitoptions)
             self.generate(self.nodes[0], 1)
             template2 = await waitnext
```

Code after:
    def run_echo_test(self):
        self.log.info("Running echo test")
        async def async_routine():
            ctx, init = await self.make_capnp_init_ctx()
            self.log.debug("Create Echo proxy object")
            echo = init.makeEcho(ctx).result
            self.log.debug("Test a few invocations of echo")
            for s in ["hallo", "", "haha"]:
                result_eval = (await echo.echo(ctx, s)).result
                assert_equal(s, result_eval)
            self.log.debug("Destroy the Echo object")
            echo.destroy(ctx)
        asyncio.run(capnp.run(async_routine()))

    def run_mining_test(self):
        self.log.info("Running mining test")
        block_hash_size = 32
        block_header_size = 80
        timeout = 1000.0 # 1000 milliseconds
        miniwallet = MiniWallet(self.nodes[0])

        async def async_routine():
            ctx, init = await self.make_capnp_init_ctx()
            self.log.debug("Create Mining proxy object")
            mining = init.makeMining(ctx)
            self.log.debug("Test simple inspectors")
            assert (await mining.result.isTestChain(ctx))
            assert (await mining.result.isInitialBlockDownload(ctx))
            blockref = await mining.result.getTip(ctx)
            assert blockref.hasResult
            assert_equal(len(blockref.result.hash), block_hash_size)
            current_block_height = self.nodes[0].getchaintips()[0]["height"]
            assert blockref.result.height == current_block_height
            self.log.debug("Mine a block")
            wait = mining.result.waitTipChanged(ctx, blockref.result.hash, )
            self.generate(self.nodes[0], 1)
            newblockref = await wait
            assert_equal(len(newblockref.result.hash), block_hash_size)
            assert_equal(newblockref.result.height, current_block_height + 1)
            self.log.debug("Wait for timeout")
            wait = mining.result.waitTipChanged(ctx, newblockref.result.hash, timeout)
            oldblockref = await wait
            assert_equal(len(newblockref.result.hash), block_hash_size)
            assert_equal(oldblockref.result.hash, newblockref.result.hash)
            assert_equal(oldblockref.result.height, newblockref.result.height)

            self.log.debug("Create a template")
            opts = self.capnp_modules['mining'].BlockCreateOptions()
            opts.useMempool = True
            opts.blockReservedWeight = 4000
            opts.coinbaseOutputMaxAdditionalSigops = 0