Skip to content

Commit 6acec6b

Browse files
committed
multiprocess: Add type conversion code for UniValue types
Extend IPC unit test to cover this and verify the serialization happens correctly.
1 parent 0cc74fc commit 6acec6b

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/ipc/capnp/common-types.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <clientversion.h>
99
#include <streams.h>
10+
#include <univalue.h>
1011

1112
#include <cstddef>
1213
#include <mp/proxy-types.h>
@@ -84,6 +85,24 @@ CustomReadField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context,
8485
value.Unserialize(stream);
8586
});
8687
}
88+
89+
template <typename Value, typename Output>
90+
void CustomBuildField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output)
91+
{
92+
std::string str = value.write();
93+
auto result = output.init(str.size());
94+
memcpy(result.begin(), str.data(), str.size());
95+
}
96+
97+
template <typename Input, typename ReadDest>
98+
decltype(auto) CustomReadField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Input&& input,
99+
ReadDest&& read_dest)
100+
{
101+
return read_dest.update([&](auto& value) {
102+
auto data = input.get();
103+
value.read(std::string_view{data.begin(), data.size()});
104+
});
105+
}
87106
} // namespace mp
88107

89108
#endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H

src/test/ipc_test.capnp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ $Proxy.includeTypes("ipc/capnp/common-types.h");
1414
interface FooInterface $Proxy.wrap("FooImplementation") {
1515
add @0 (a :Int32, b :Int32) -> (result :Int32);
1616
passOutPoint @1 (arg :Data) -> (result :Data);
17+
passUniValue @2 (arg :Text) -> (result :Text);
1718
}

src/test/ipc_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ void IpcTest()
5555
COutPoint txout2{foo->passOutPoint(txout1)};
5656
BOOST_CHECK(txout1 == txout2);
5757

58+
UniValue uni1{UniValue::VOBJ};
59+
uni1.pushKV("i", 1);
60+
uni1.pushKV("s", "two");
61+
UniValue uni2{foo->passUniValue(uni1)};
62+
BOOST_CHECK_EQUAL(uni1.write(), uni2.write());
63+
5864
// Test cleanup: disconnect pipe and join thread
5965
disconnect_client();
6066
thread.join();

src/test/ipc_test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
#define BITCOIN_TEST_IPC_TEST_H
77

88
#include <primitives/transaction.h>
9+
#include <univalue.h>
910

1011
class FooImplementation
1112
{
1213
public:
1314
int add(int a, int b) { return a + b; }
1415
COutPoint passOutPoint(COutPoint o) { return o; }
16+
UniValue passUniValue(UniValue v) { return v; }
1517
};
1618

1719
void IpcTest();

0 commit comments

Comments
 (0)