Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
ipc
capnp
common-types.h
Go to the documentation of this file.
1
// Copyright (c) 2023-present 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 <stdexcept>
31
#include <type_traits>
32
#include <utility>
33
34
namespace
ipc
{
35
namespace
capnp
{
43
template
<
typename
S>
44
auto
Wrap
(
S
& s)
45
{
46
return
ParamsStream
{s,
TX_WITH_WITNESS
};
47
}
48
52
template
<
typename
T>
53
concept
Deserializable
= std::is_constructible_v<T, ::deserialize_type, ::DataStream&>;
54
}
// namespace capnp
55
}
// namespace ipc
56
58
namespace
mp
{
63
template
<
typename
LocalType,
typename
Value,
typename
Output>
64
void
CustomBuildField
(
TypeList<LocalType>
,
Priority<1>
,
InvokeContext
& invoke_context, Value&& value, Output&& output)
65
// Enable if serializeable and if LocalType is not cv or reference qualified. If
66
// LocalType is cv or reference qualified, it is important to fall back to
67
// lower-priority Priority<0> implementation of this function that strips cv
68
// references, to prevent this CustomBuildField overload from taking precedence
69
// over more narrow overloads for specific LocalTypes.
70
requires
Serializable<LocalType, DataStream>
&& std::is_same_v<LocalType, std::remove_cv_t<std::remove_reference_t<LocalType>>>
71
{
72
DataStream
stream;
73
auto
wrapper{
ipc::capnp::Wrap
(stream)};
74
value.Serialize(wrapper);
75
auto
result = output.init(stream.
size
());
76
memcpy(result.begin(), stream.
data
(), stream.
size
());
77
}
78
83
template
<
typename
LocalType,
typename
Input,
typename
ReadDest>
84
decltype
(
auto
)
CustomReadField
(
TypeList<LocalType>
,
Priority<1>
,
InvokeContext
& invoke_context, Input&& input, ReadDest&& read_dest)
85
requires
Unserializable<LocalType, DataStream>
&& (!
ipc::capnp::Deserializable<LocalType>
)
86
{
87
return
read_dest.update([&](
auto
& value) {
88
if
(!input.has())
return
;
89
auto
data = input.get();
90
SpanReader
stream({data.begin(), data.end()});
91
auto
wrapper{
ipc::capnp::Wrap
(stream)};
92
value.Unserialize(wrapper);
93
});
94
}
95
100
template
<
typename
LocalType,
typename
Input,
typename
ReadDest>
101
decltype
(
auto
)
CustomReadField
(
TypeList<LocalType>
,
Priority<1>
,
InvokeContext
& invoke_context, Input&& input, ReadDest&& read_dest)
102
requires
ipc::capnp::Deserializable<LocalType>
103
{
104
assert
(input.has());
105
auto
data = input.get();
106
SpanReader
stream({data.begin(), data.end()});
107
auto
wrapper{
ipc::capnp::Wrap
(stream)};
108
return
read_dest.construct(
::deserialize
, wrapper);
109
}
110
113
template
<
typename
Value,
typename
Output>
114
void
CustomBuildField
(
TypeList<UniValue>
,
Priority<1>
,
InvokeContext
& invoke_context, Value&& value, Output&& output)
115
{
116
std::string str = value.write();
117
auto
result = output.init(str.size());
118
memcpy(result.begin(), str.data(), str.size());
119
}
120
121
template
<
typename
Input,
typename
ReadDest>
122
decltype
(
auto
)
CustomReadField
(
TypeList<UniValue>
,
Priority<1>
,
InvokeContext
& invoke_context, Input&& input,
123
ReadDest&& read_dest)
124
{
125
return
read_dest.update([&](
auto
& value) {
126
auto
data = input.get();
127
if
(!value.read(std::string_view{data.begin(), data.size()})) {
128
throw
std::runtime_error{
"invalid JSON received over IPC"
};
129
}
130
});
131
}
132
140
template
<
typename
Input>
141
bool
CustomHasField
(
TypeList<CTransaction>
,
InvokeContext
& invoke_context,
const
Input& input)
142
{
143
return
input.get().size() > 0;
144
}
145
}
// namespace mp
146
147
#endif
// BITCOIN_IPC_CAPNP_COMMON_TYPES_H
DataStream
Double ended buffer combining vector and stream-like interfaces.
Definition
streams.h:165
DataStream::size
size_type size() const
Definition
streams.h:198
DataStream::data
value_type * data()
Definition
streams.h:205
ParamsStream
Wrapper that overrides the GetParams() function of a stream.
Definition
serialize.h:1169
SpanReader
Minimal stream for reading from an existing byte array by std::span.
Definition
streams.h:83
clientversion.h
Serializable
If none of the specialized versions above matched, default to calling member function.
Definition
serialize.h:763
Unserializable
Definition
serialize.h:772
ipc::capnp::Deserializable
Definition
common-types.h:53
types.h
ipc::capnp
Definition
common-types.h:35
ipc::capnp::Wrap
auto Wrap(S &s)
Definition
common-types.h:44
ipc
Definition
ipc.h:14
mp
Functions to serialize / deserialize common bitcoin types.
Definition
common-types.h:58
mp::CustomReadField
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest)
Definition
common-types.h:84
mp::CustomBuildField
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output)
Definition
common-types.h:64
mp::CustomHasField
bool CustomHasField(TypeList< CTransaction >, InvokeContext &invoke_context, const Input &input)
Definition
common-types.h:141
S
#define S(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
Definition
precomputed_ecmult.c:8
transaction.h
TX_WITH_WITNESS
constexpr TransactionSerParams TX_WITH_WITNESS
Definition
transaction.h:181
proxy-types.h
serialize.h
deserialize
constexpr deserialize_type deserialize
Definition
serialize.h:52
streams.h
mp::InvokeContext
Definition
proxy-io.h:33
mp::Priority
Definition
util.h:115
mp::TypeList
Generic utility functions used by capnp code.
Definition
util.h:39
type-chrono.h
type-context.h
type-data.h
type-decay.h
type-interface.h
type-message.h
type-number.h
type-optional.h
type-pointer.h
type-string.h
type-struct.h
type-threadmap.h
type-vector.h
univalue.h
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.18.0