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

Code before:
            capnp_dir = Path(capnp.__path__[0]).parent
        src_dir = Path(self.config['environment']['SRCDIR']) / "src"
        mp_dir = src_dir / "ipc" / "libmultiprocess" / "include"
        # List of import directories. Note: it is important for mp_dir to be
        # listed first, in case there are other libmultiprocess installations on
        # the system, to ensure that `import "/mp/proxy.capnp"` lines load the
        # same file as capnp.load() loads directly below, and there are not
        # "failed: Duplicate ID @0xcc316e3f71a040fb" errors.
        imports = [str(mp_dir), str(capnp_dir), str(src_dir)]
        return {
            "proxy": capnp.load(str(mp_dir / "mp" / "proxy.capnp"), imports=imports),
            "init": capnp.load(str(src_dir / "ipc" / "capnp" / "init.capnp"), imports=imports),
            "echo": capnp.load(str(src_dir / "ipc" / "capnp" / "echo.capnp"), imports=imports),
            "mining": capnp.load(str(src_dir / "ipc" / "capnp" / "mining.capnp"), imports=imports),
        }

    def set_test_params(self):
        self.num_nodes = 2

    def setup_nodes(self):
        self.extra_init = [{"ipcbind": True}, {}]
        # Set an absurd reserved weight. `-blockreservedweight` is RPC-only, so
        # with this setting RPC templates would be empty. IPC clients set
        # blockReservedWeight per template request and are unaffected; later in
        # the test the IPC template includes a mempool transaction.
        self.extra_args =[{f"-blockreservedweight={MAX_BLOCK_WEIGHT}"}, {}]
        super().setup_nodes()
        # Use this function to also load the capnp modules (we cannot use set_test_params for this,
        # as it is being called before knowing whether capnp is available).
        self.capnp_modules = self.load_capnp_modules()

    async def make_capnp_init_ctx(self):
        node = self.nodes[0]
        # Establish a connection, and create Init proxy object.
        connection = await capnp.AsyncIoStream.create_unix_connection(node.ipc_socket_path)
        client = capnp.TwoPartyClient(connection)
        init = client.bootstrap().cast_as(self.capnp_modules['init'].Init)
        # Create a remote thread on the server for the IPC calls to be executed in.
        threadmap = init.construct().threadMap
        thread = threadmap.makeThread("pythread").result
        ctx = self.capnp_modules['proxy'].Context()
        ctx.thread = thread
        # Return both.
        return ctx, init

    async def parse_and_deserialize_block(self, block_template, ctx):
        block_data = BytesIO((await block_template.getBlock(ctx)).result)
        block = CBlock()
        block.deserialize(block_data)
        return block


Review comment (bug):
In "mining: fix -blockreservedweight shadows IPC option"  f69320d3279e2abe1e5d47f496ad54deb4c20fcf

This seems like a weird way to test this.
It would be better if we didn't generalise here and had a separate subtest for this.

```diff
diff --git a/test/functional/interface_ipc.py b/test/functional/interface_ipc.py
index 02f3e0dd35d..677244e1a44 100755
--- a/test/functional/interface_ipc.py
+++ b/test/functional/interface_ipc.py
@@ -126,11 +126,6 @@ class IPCInterfaceTest(BitcoinTestFramework):

     def setup_nodes(self):
         self.extra_init = [{"ipcbind": True}, {}]
-        # Set an absurd reserved weight. `-blockreservedweight` is RPC-only, so
-        # with this setting RPC templates would be empty. IPC clients set
-        # blockReservedWeight per template request and are unaffected; later in
-        # the test the IPC template includes a mempool transaction.
-        self.extra_args =[{f"-blockreservedweight={MAX_BLOCK_WEIGHT}"}, {}]
         super().setup_nodes()
         # Use this function to also load the capnp modules (we cannot use set_test_params for this,
         # as it is being called before knowing whether capnp is available).
@@ -250,7 +245,6 @@ class IPCInterfaceTest(BitcoinTestFramework):
         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()
@@ -318,7 +312,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
                 self.log.debug("Wait for another, get one after increase in fees in the mempool")
                 template4 = await wait_and_do(
                     wait_next_template(template2, stack, ctx, waitoptions),
-                    lambda: miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0]))
+                    lambda: self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0]))
                 assert template4 is not None
                 block3 = await self.parse_and_deserialize_block(template4, ctx)
                 assert_equal(len(block3.vtx), 2)
@@ -334,7 +328,7 @@ class IPCInterfaceTest(BitcoinTestFramework):
                 self.log.debug("Wait for another, get one after increase in fees in the mempool")
                 template6 = await wait_and_do(
                     wait_next_template(template5, stack, ctx, waitoptions),
-                    lambda: miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0]))
+                    lambda: self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0]))
                 assert template6 is not None
                 block4 = await self.parse_and_deserialize_block(template6, ctx)
                 assert_equal(len(block4.vtx), 3)
@@ -352,21 +346,12 @@ class IPCInterfaceTest(BitcoinTestFramework):
                     assert template7 is None
                 await wait_and_do(wait_for_block(), template6.interruptWait())

-                self.log.debug("Use absurdly large reserved weight to force an empty template")
-                opts.blockReservedWeight = MAX_BLOCK_WEIGHT
-                empty_template = await create_block_template(mining, stack, ctx, opts)
-                assert empty_template is not None
-                block = await self.parse_and_deserialize_block(empty_template, ctx)
-                assert_equal(len(block.vtx), 1)
-                # Restore opts
-                opts.blockReservedWeight = 4000
-
             current_block_height = self.nodes[0].getchaintips()[0]["height"]
             check_opts = self.capnp_modules['mining'].BlockCheckOptions()
             async with destroying((await mining.createNewBlock(opts)).result, ctx) as template:
                 block = await self.parse_and_deserialize_block(template, ctx)
-                balance = miniwallet.get_balance()
-                coinbase = await self.build_coinbase_test(template, ctx, miniwallet)
+                balance = self.miniwallet.get_balance()
+                coinbase = await self.build_coinbase_test(template, ctx, self.miniwallet)
                 # Reduce payout for balance comparison simplicity
                 coinbase.vout[0].nValue = COIN
                 block.vtx[0] = coinbase
@@ -414,8 +399,8 @@ class IPCInterfaceTest(BitcoinTestFramework):
             # Check that the other node accepts the block
             assert_equal(self.nodes[0].getchaintips()[0], self.nodes[1].getchaintips()[0])

-            miniwallet.rescan_utxos()
-            assert_equal(miniwallet.get_balance(), balance + 1)
+            self.miniwallet.rescan_utxos()
+            assert_equal(self.miniwallet.get_balance(), balance + 1)
             self.log.debug("Check block should fail now, since it is a duplicate")
             check = await mining.checkBlock(block.serialize(), check_opts)
             assert_equal(check.result, False)
@@ -423,9 +408,40 @@ class IPCInterfaceTest(BitcoinTestFramework):

         asyncio.run(capnp.run(async_routine()))

+    def run_ipc_option_test(self):
+        self.log.debug("Test that IPC blockReservedReight runtime value will override startup -blockreservedweight")
+        # The absurd blockreservedweight startup option should not force us to have empty blocks when
+        # a lower value is added is passed via the IPC.
+        self.restart_node(0, extra_args=[f"-blockreservedweight={MAX_BLOCK_WEIGHT}"])
+        async def async_routine():
+            ctx, init = await self.make_capnp_init_ctx()
+            mining = init.makeMining(ctx).result
+            async with AsyncExitStack() as stack:
+                opts = self.capnp_modules['mining'].BlockCreateOptions()
+                opts.useMempool = True
+                opts.blockReservedWeight = 4000
+                opts.coinbaseOutputMaxAdditionalSigops = 0
+                self.miniwallet.send_self_transfer(fee_rate=10, from_node=self.nodes[0])
+                template = await create_block_template(mining, stack, ctx, opts)
+                assert template is not None
+                block = await self.parse_and_deserialize_block(template, ctx)
+                assert_equal(len(block.vtx), 2)
+                self.log.debug("Use absurdly large reserved weight to force an empty template")
+                opts.blockReservedWeight = MAX_BLOCK_WEIGHT
+                empty_template = await create_block_template(mining, stack, ctx, opts)
+                assert empty_template is not None
+                block = await self.parse_and_deserialize_block(empty_template, ctx)
+                assert_equal(len(block.vtx), 1)
+
+        asyncio.run(capnp.run(async_routine()))
+
+
     def run_test(self):
+        self.miniwallet = MiniWallet(self.nodes[0])
         self.run_echo_test()
         self.run_mining_test()
+        self.run_ipc_option_test()
+

 if __name__ == '__main__':
     IPCInterfaceTest(__file__).main()
```

Code after:
#!/usr/bin/env python3
# Copyright (c) The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test the IPC (multiprocess) interface."""
import asyncio

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal
from test_framework.ipc_util import (
    load_capnp_modules,
    make_capnp_init_ctx,
)

# Test may be skipped and not have capnp installed
try:
    import capnp  # type: ignore[import] # noqa: F401
except ModuleNotFoundError:
    pass


class IPCInterfaceTest(BitcoinTestFramework):

    def skip_test_if_missing_module(self):
        self.skip_if_no_ipc()
        self.skip_if_no_py_capnp()

    def set_test_params(self):
        self.num_nodes = 1

    def setup_nodes(self):
        self.extra_init = [{"ipcbind": True}]
        super().setup_nodes()
        # Use this function to also load the capnp modules (we cannot use set_test_params for this,
        # as it is being called before knowing whether capnp is available).
        self.capnp_modules = load_capnp_modules(self.config)

    def run_echo_test(self):
        self.log.info("Running echo test")
        async def async_routine():
            ctx, init = await make_capnp_init_ctx(self)
            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

        async def async_routine():
            ctx, init = await make_capnp_init_ctx(self)
            self.log.debug("Create Mining proxy object")
            mining = init.makeMining(ctx).result
            self.log.debug("Test simple inspectors")
            assert (await mining.isTestChain(ctx)).result
            assert not (await mining.isInitialBlockDownload(ctx)).result
            blockref = await mining.getTip(ctx)
            assert blockref.hasResult
            assert_equal(len(blockref.result.hash), block_hash_size)
            current_block_height = self.nodes[0].getchaintips()[0]["height"]
            assert_equal(blockref.result.height, current_block_height)

        asyncio.run(capnp.run(async_routine()))

    def run_test(self):
        self.run_echo_test()
        self.run_mining_test()

if __name__ == '__main__':
    IPCInterfaceTest(__file__).main()