From e53c7a9778ec9b1703c593ab18f60d6588f8c474 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Fri, 26 Mar 2021 13:30:48 -0400 Subject: [PATCH] fix buildprimitive error https://github.com/chaincodelabs/libmultiprocess/issues/51 --- src/Makefile.am | 1 + src/altnet/bitcoin.cpp | 1 + src/init/bitcoin-altnet.cpp | 2 +- src/ipc/capnp/common-types.h | 112 +++++++++++++++++++++++++++++++++ src/ipc/capnp/validation.capnp | 2 +- src/node/interfaces.cpp | 2 +- 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/ipc/capnp/common-types.h diff --git a/src/Makefile.am b/src/Makefile.am index a89b729d2ae8d..4940a2aa55534 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -828,6 +828,7 @@ EXTRA_DIST += $(libbitcoin_ipc_mpgen_input) if BUILD_MULTIPROCESS LIBBITCOIN_IPC=libbitcoin_ipc.a libbitcoin_ipc_a_SOURCES = \ + ipc/capnp/common-types.h \ ipc/capnp/init-types.h \ ipc/capnp/init.cpp \ ipc/capnp/protocol.cpp \ diff --git a/src/altnet/bitcoin.cpp b/src/altnet/bitcoin.cpp index 33e3e39b38b72..d61dca7804506 100644 --- a/src/altnet/bitcoin.cpp +++ b/src/altnet/bitcoin.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/src/init/bitcoin-altnet.cpp b/src/init/bitcoin-altnet.cpp index f09d7dbc7b291..8bf3951ea7683 100644 --- a/src/init/bitcoin-altnet.cpp +++ b/src/init/bitcoin-altnet.cpp @@ -20,7 +20,7 @@ class BitcoinAltnetInit : public interfaces::Init BitcoinAltnetInit(AltnetContext& altnet, const char* arg0) : m_altnet(altnet), m_ipc(interfaces::MakeIpc(EXE_NAME, arg0, *this)) {} - std::unique_ptr makeAltnet(interfaces::Validation& validation) override { return MakeUnique(m_altnet, validation); } + std::unique_ptr makeAltnet(interfaces::Validation& validation) override { return MakeAltnet(m_altnet, validation); } interfaces::Ipc* ipc() override { return m_ipc.get(); } AltnetContext& m_altnet; std::unique_ptr m_ipc; diff --git a/src/ipc/capnp/common-types.h b/src/ipc/capnp/common-types.h new file mode 100644 index 0000000000000..e822efb57073b --- /dev/null +++ b/src/ipc/capnp/common-types.h @@ -0,0 +1,112 @@ +// Copyright (c) 2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_IPC_CAPNP_COMMON_TYPES_H +#define BITCOIN_IPC_CAPNP_COMMON_TYPES_H + +#include +#include +#include +#include +#include + +namespace ipc { +namespace capnp { +//! Deserialize bitcoin value. +template +T Unserialize(T& value, const kj::ArrayPtr& data) +{ + // Could optimize, it unnecessarily copies the data into a temporary vector. + CDataStream stream(CSerializeData{data.begin(), data.end()}, SER_NETWORK, CLIENT_VERSION); + value.Unserialize(stream); + return value; +} + +//! Deserialize bitcoin value. +template +T Unserialize(const kj::ArrayPtr& data) +{ + T value; + Unserialize(value, data); + return value; +} + +template +using Deserializable = std::is_constructible; + +template +struct Unserializable +{ +private: + template + static std::true_type test(decltype(std::declval().Unserialize(std::declval()))*); + template + static std::false_type test(...); + +public: + static constexpr bool value = decltype(test(nullptr))::value; +}; + +template +struct Serializable +{ +private: + template + static std::true_type test(decltype(std::declval().Serialize(std::declval()))*); + template + static std::false_type test(...); + +public: + static constexpr bool value = decltype(test(nullptr))::value; +}; +} // namespace capnp +} // namespace ipc + +namespace mp { +template +decltype(auto) CustomReadField( + TypeList, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest, + typename std::enable_if::value>::type* enable = nullptr) +{ + assert(input.has()); + auto data = input.get(); + // Note: stream copy here is unnecessary, and can be avoided in the future + // when `VectorReader` from #12254 is added. + CDataStream stream({data.begin(), data.end()}, SER_NETWORK, CLIENT_VERSION); + return read_dest.construct(deserialize, stream); +} + +template +decltype(auto) CustomReadField( + TypeList, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest, + // FIXME instead of always preferring Deserialize implementation over Unserialize should prefer Deserializing when + // emplacing, unserialize when updating + typename std::enable_if::value && + !ipc::capnp::Deserializable::value>::type* enable = nullptr) +{ + return read_dest.update([&](auto& value) { + if (!input.has()) return; + auto data = input.get(); + // Note: stream copy here is unnecessary, and can be avoided in the future + // when `VectorReader` from #12254 is added. + CDataStream stream(CSerializeData{data.begin(), data.end()}, SER_NETWORK, CLIENT_VERSION); + value.Unserialize(stream); + }); +} + +template +void CustomBuildField( + TypeList, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output, + typename std::enable_if::type>::type>::value>::type* enable = nullptr) +{ + CDataStream stream(SER_NETWORK, CLIENT_VERSION); + value.Serialize(stream); + auto result = output.init(stream.size()); + memcpy(result.begin(), stream.data(), stream.size()); +} + +} // namespace mp + +#endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H diff --git a/src/ipc/capnp/validation.capnp b/src/ipc/capnp/validation.capnp index 40a9f4f6c1324..495d5311eee0c 100644 --- a/src/ipc/capnp/validation.capnp +++ b/src/ipc/capnp/validation.capnp @@ -9,7 +9,7 @@ $Cxx.namespace("ipc::capnp::messages"); using Proxy = import "/mp/proxy.capnp"; $Proxy.include("interfaces/validation.h"); -$Proxy.include("ipc/capnp/validation.capnp.h"); +$Proxy.includeTypes("ipc/capnp/common-types.h"); interface Validation $Proxy.wrap("interfaces::Validation") { destroy @0 (context :Proxy.Context) -> (); diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 2ebba72d26e60..a119b3937cbbf 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -667,7 +667,7 @@ class ValidationImpl : public Validation { public: explicit ValidationImpl(NodeContext& node) : m_node(node) {} - bool validateHeaders(const BlockHeader& header) override + bool validateHeaders(const interfaces::BlockHeader& header) override { //BlockValidationState state; //m_node.chainman->ProcessNewBlockHeaders(headers, state, Params());