Bitcoin Core  29.1.0
P2P Digital Currency
common-types.h
Go to the documentation of this file.
1 // Copyright (c) 2023 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_IPC_CAPNP_COMMON_TYPES_H
6 #define BITCOIN_IPC_CAPNP_COMMON_TYPES_H
7 
8 #include <clientversion.h>
9 #include <interfaces/types.h>
10 #include <primitives/transaction.h>
11 #include <serialize.h>
12 #include <streams.h>
13 #include <univalue.h>
14 
15 #include <cstddef>
16 #include <mp/proxy-types.h>
17 #include <mp/type-chrono.h>
18 #include <mp/type-context.h>
19 #include <mp/type-data.h>
20 #include <mp/type-decay.h>
21 #include <mp/type-interface.h>
22 #include <mp/type-message.h>
23 #include <mp/type-number.h>
24 #include <mp/type-optional.h>
25 #include <mp/type-pointer.h>
26 #include <mp/type-string.h>
27 #include <mp/type-struct.h>
28 #include <mp/type-threadmap.h>
29 #include <mp/type-vector.h>
30 #include <type_traits>
31 #include <utility>
32 
33 namespace ipc {
34 namespace capnp {
42 template <typename S>
43 auto Wrap(S& s)
44 {
46 }
47 
51 template <typename T>
52 concept Deserializable = std::is_constructible_v<T, ::deserialize_type, ::DataStream&>;
53 } // namespace capnp
54 } // namespace ipc
55 
57 namespace mp {
62 template <typename LocalType, typename Value, typename Output>
63 void CustomBuildField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output)
64 // Enable if serializeable and if LocalType is not cv or reference qualified. If
65 // LocalType is cv or reference qualified, it is important to fall back to
66 // lower-priority Priority<0> implementation of this function that strips cv
67 // references, to prevent this CustomBuildField overload from taking precedence
68 // over more narrow overloads for specific LocalTypes.
69 requires Serializable<LocalType, DataStream> && std::is_same_v<LocalType, std::remove_cv_t<std::remove_reference_t<LocalType>>>
70 {
71  DataStream stream;
72  auto wrapper{ipc::capnp::Wrap(stream)};
73  value.Serialize(wrapper);
74  auto result = output.init(stream.size());
75  memcpy(result.begin(), stream.data(), stream.size());
76 }
77 
82 template <typename LocalType, typename Input, typename ReadDest>
83 decltype(auto) CustomReadField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest)
84 requires Unserializable<LocalType, DataStream> && (!ipc::capnp::Deserializable<LocalType>)
85 {
86  return read_dest.update([&](auto& value) {
87  if (!input.has()) return;
88  auto data = input.get();
89  SpanReader stream({data.begin(), data.end()});
90  auto wrapper{ipc::capnp::Wrap(stream)};
91  value.Unserialize(wrapper);
92  });
93 }
94 
99 template <typename LocalType, typename Input, typename ReadDest>
100 decltype(auto) CustomReadField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest)
101 requires ipc::capnp::Deserializable<LocalType>
102 {
103  assert(input.has());
104  auto data = input.get();
105  SpanReader stream({data.begin(), data.end()});
106  auto wrapper{ipc::capnp::Wrap(stream)};
107  return read_dest.construct(::deserialize, wrapper);
108 }
109 
112 template <typename Value, typename Output>
113 void CustomBuildField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output)
114 {
115  std::string str = value.write();
116  auto result = output.init(str.size());
117  memcpy(result.begin(), str.data(), str.size());
118 }
119 
120 template <typename Input, typename ReadDest>
121 decltype(auto) CustomReadField(TypeList<UniValue>, Priority<1>, InvokeContext& invoke_context, Input&& input,
122  ReadDest&& read_dest)
123 {
124  return read_dest.update([&](auto& value) {
125  auto data = input.get();
126  value.read(std::string_view{data.begin(), data.size()});
127  });
128 }
129 
130 } // namespace mp
131 
132 #endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H
Wrapper that overrides the GetParams() function of a stream.
Definition: serialize.h:1114
assert(!tx.IsCoinBase())
constexpr deserialize_type deserialize
Definition: serialize.h:49
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
concept Deserializable
Detect if type has a deserialize_type constructor, which is used to deserialize types like CTransacti...
Definition: common-types.h:52
memcpy(result.begin(), stream.data(), stream.size())
value_type * data()
Definition: streams.h:188
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:100
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
Definition: ipc.h:12
size_type size() const
Definition: streams.h:181
auto result
Definition: common-types.h:74
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest) requires Unserializable< LocalType
Overload multiprocess library&#39;s CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:100
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output) requires Serializable< LocalType
Overload multiprocess library&#39;s CustomBuildField hook to allow any serializable object to be stored i...
concept Unserializable
Definition: serialize.h:758
auto Wrap(S &s)
Construct a ParamStream wrapping a data stream with serialization parameters needed to pass transacti...
Definition: common-types.h:43
auto wrapper
Definition: common-types.h:72
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:195