diff --git a/src/ipc/capnp/common-types.h b/src/ipc/capnp/common-types.h index d1343c40dd1..39e368491b4 100644 --- a/src/ipc/capnp/common-types.h +++ b/src/ipc/capnp/common-types.h @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -84,6 +85,24 @@ CustomReadField(TypeList, Priority<1>, InvokeContext& invoke_context, value.Unserialize(stream); }); } + +template +void CustomBuildField(TypeList, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output) +{ + std::string str = value.write(); + auto result = output.init(str.size()); + memcpy(result.begin(), str.data(), str.size()); +} + +template +decltype(auto) CustomReadField(TypeList, Priority<1>, InvokeContext& invoke_context, Input&& input, + ReadDest&& read_dest) +{ + return read_dest.update([&](auto& value) { + auto data = input.get(); + value.read(std::string_view{data.begin(), data.size()}); + }); +} } // namespace mp #endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H diff --git a/src/test/ipc_test.capnp b/src/test/ipc_test.capnp index 7b970e2affe..55a3dc26839 100644 --- a/src/test/ipc_test.capnp +++ b/src/test/ipc_test.capnp @@ -14,4 +14,5 @@ $Proxy.includeTypes("ipc/capnp/common-types.h"); interface FooInterface $Proxy.wrap("FooImplementation") { add @0 (a :Int32, b :Int32) -> (result :Int32); passOutPoint @1 (arg :Data) -> (result :Data); + passUniValue @2 (arg :Text) -> (result :Text); } diff --git a/src/test/ipc_test.cpp b/src/test/ipc_test.cpp index f835859705d..ce4edaceb08 100644 --- a/src/test/ipc_test.cpp +++ b/src/test/ipc_test.cpp @@ -55,6 +55,12 @@ void IpcTest() COutPoint txout2{foo->passOutPoint(txout1)}; BOOST_CHECK(txout1 == txout2); + UniValue uni1{UniValue::VOBJ}; + uni1.pushKV("i", 1); + uni1.pushKV("s", "two"); + UniValue uni2{foo->passUniValue(uni1)}; + BOOST_CHECK_EQUAL(uni1.write(), uni2.write()); + // Test cleanup: disconnect pipe and join thread disconnect_client(); thread.join(); diff --git a/src/test/ipc_test.h b/src/test/ipc_test.h index f100ae8c5d2..bcfcc2125c6 100644 --- a/src/test/ipc_test.h +++ b/src/test/ipc_test.h @@ -6,12 +6,14 @@ #define BITCOIN_TEST_IPC_TEST_H #include +#include class FooImplementation { public: int add(int a, int b) { return a + b; } COutPoint passOutPoint(COutPoint o) { return o; } + UniValue passUniValue(UniValue v) { return v; } }; void IpcTest();