CCF
Loading...
Searching...
No Matches
node_info_network.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3
4#pragma once
5
6#include "ccf/ds/json.h"
7#include "ccf/ds/nonstd.h"
10
11#include <string>
12
13namespace ccf
14{
15 enum class Authority : uint8_t
16 {
17 NODE,
18 SERVICE,
19 ACME, // DEPRECATED
21 };
24 {{Authority::NODE, "Node"},
25 {Authority::SERVICE, "Service"},
26 {Authority::ACME, "ACME"}, // DEPRECATED
27 {Authority::UNSECURED, "Unsecured"}});
28
29 using ApplicationProtocol = std::string;
30
32 {
34 bool operator==(const Endorsement& other) const
35 {
36 return authority == other.authority;
37 }
38 };
41
43 {
44 std::string rpchost;
45 std::string pubhost;
46 std::string nodehost;
47 std::string nodeport;
48 std::string rpcport;
49 std::string pubport;
50 };
53 NodeInfoNetwork_v1, rpchost, pubhost, nodehost, nodeport, rpcport, pubport);
54
55 static constexpr auto PRIMARY_RPC_INTERFACE = "primary_rpc_interface";
56
57 enum class RedirectionResolutionKind : uint8_t
58 {
61 };
66
77
80 {
81 using NetAddress = std::string;
82 using RpcInterfaceID = std::string;
83 using NetProtocol = std::string;
84
87 {
91 std::optional<ApplicationProtocol> app_protocol = std::nullopt;
92
94 std::optional<size_t> max_open_sessions_soft = std::nullopt;
95
97 std::optional<size_t> max_open_sessions_hard = std::nullopt;
98
100 std::optional<http::ParserConfiguration> http_configuration =
101 std::nullopt;
102
104 std::optional<Endorsement> endorsement = std::nullopt;
105
108 std::optional<std::vector<std::string>> accepted_endpoints = std::nullopt;
109
111 std::optional<size_t> forwarding_timeout_ms = std::nullopt;
112
116 std::set<ccf::endpoints::OperatorFeature> enabled_operator_features;
117
125
126 std::optional<Redirections> redirections = std::nullopt;
127
142 };
143
145 using RpcInterfaces = std::map<RpcInterfaceID, NetInterface>;
146
149
152 };
153
163 endorsement,
164 max_open_sessions_soft,
165 max_open_sessions_hard,
166 published_address,
167 protocol,
168 app_protocol,
169 http_configuration,
170 accepted_endpoints,
171 forwarding_timeout_ms,
172 enabled_operator_features,
173 redirections);
176 NodeInfoNetwork_v2, node_to_node_interface, rpc_interfaces);
177
179 {
180 NodeInfoNetwork() = default;
183
184 bool operator==(const NodeInfoNetwork& other) const
185 {
188 }
189 };
190
191 inline static std::pair<std::string, std::string> split_net_address(
192 const NodeInfoNetwork::NetAddress& addr)
193 {
194 auto [host, port] = ccf::nonstd::rsplit_1(addr, ":");
195 return std::make_pair(std::string(host), std::string(port));
196 }
197
198 inline static NodeInfoNetwork::NetAddress make_net_address(
199 const std::string& host, const std::string& port)
200 {
201 return fmt::format("{}:{}", host, port);
202 }
203
204 // All NodeInfoNetwork read that may lead to re-serialization for
205 // write purposes must be v2 by now, so we only serialize as v2.
206 // If any v1 NodeInfoNetwork is read at all, it must be for historical
207 // query purposes, and does not need to be re-serialized to v2.
208 inline void to_json(nlohmann::json& j, const NodeInfoNetwork& nin)
209 {
210 to_json(j, (const NodeInfoNetwork_v2&)nin);
211 }
212
213 inline void from_json(const nlohmann::json& j, NodeInfoNetwork& nin)
214 {
215 try
216 {
218 from_json(j, v2);
219 nin = NodeInfoNetwork(v2);
220 }
221 catch (const ccf::JsonParseError& jpe)
222 {
224 try
225 {
226 if (
227 j.contains("rpc_interfaces") || j.contains("node_to_node_interface"))
228 {
229 // If these v2 fields are present, rethrow the error - the JSON is
230 // malformed for v2. Only proceed to parse as v1 if these fields are
231 // absent and the NodeInfoNetwork is a pure v1.
232 throw jpe;
233 }
234 from_json(j, v1);
235 }
236 catch (const ccf::JsonParseError& _)
237 {
238 // If this also fails to parse as a v1, then rethrow the earlier error.
239 // Configs should now be using v2, and this v1 parsing is just a
240 // backwards-compatibility shim, which does not get to return errors.
241 throw jpe;
242 }
243
245 make_net_address(v1.nodehost, v1.nodeport);
246 // If published address is not explicitly set, default to bind address
248 make_net_address(v1.nodehost, v1.nodeport);
249
250 NodeInfoNetwork::NetInterface primary_interface;
251 primary_interface.bind_address = make_net_address(v1.rpchost, v1.rpcport);
252 primary_interface.published_address =
253 make_net_address(v1.pubhost, v1.pubport);
254
255 nin.rpc_interfaces.emplace(
256 PRIMARY_RPC_INTERFACE, std::move(primary_interface));
257 }
258 }
259}
260
261FMT_BEGIN_NAMESPACE template <>
262struct formatter<ccf::Authority>
263{
264 template <typename ParseContext>
265 constexpr auto parse(ParseContext& ctx)
266 {
267 return ctx.begin();
268 }
269
270 template <typename FormatContext>
271 auto format(const ccf::Authority& authority, FormatContext& ctx) const
272 -> decltype(ctx.out())
273 {
274 switch (authority)
275 {
277 {
278 return format_to(ctx.out(), "Node");
279 }
281 {
282 return format_to(ctx.out(), "Service");
283 }
285 {
286 return format_to(ctx.out(), "ACME");
287 }
289 {
290 return format_to(ctx.out(), "Unsecured");
291 }
292 }
293 }
294};
295FMT_END_NAMESPACE
Definition json.h:26
#define DECLARE_JSON_REQUIRED_FIELDS(TYPE,...)
Definition json.h:736
#define DECLARE_JSON_TYPE(TYPE)
Definition json.h:685
#define DECLARE_JSON_TYPE_WITH_OPTIONAL_FIELDS(TYPE)
Definition json.h:712
#define DECLARE_JSON_OPTIONAL_FIELDS(TYPE,...)
Definition json.h:811
#define DECLARE_JSON_ENUM(TYPE,...)
Definition json.h:864
Definition app_interface.h:13
std::string ApplicationProtocol
Definition node_info_network.h:29
Authority
Definition node_info_network.h:16
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
RedirectionResolutionKind
Definition node_info_network.h:58
void to_json(nlohmann::json &j, const ClaimsDigest &hash)
Definition claims_digest.h:49
Definition configuration.h:14
Definition node_info_network.h:32
Authority authority
Definition node_info_network.h:33
bool operator==(const Endorsement &other) const
Definition node_info_network.h:34
Definition node_info_network.h:43
std::string pubport
Definition node_info_network.h:49
std::string nodehost
Definition node_info_network.h:46
std::string rpchost
Definition node_info_network.h:44
std::string rpcport
Definition node_info_network.h:48
std::string nodeport
Definition node_info_network.h:47
std::string pubhost
Definition node_info_network.h:45
Definition node_info_network.h:119
bool operator==(const Redirections &other) const =default
RedirectionResolverConfig to_primary
Definition node_info_network.h:120
RedirectionResolverConfig to_backup
Definition node_info_network.h:121
Network interface description.
Definition node_info_network.h:87
std::optional< size_t > max_open_sessions_hard
Maximum open sessions hard limit.
Definition node_info_network.h:97
std::optional< Redirections > redirections
Definition node_info_network.h:126
NetProtocol protocol
Definition node_info_network.h:90
std::optional< size_t > max_open_sessions_soft
Maximum open sessions soft limit.
Definition node_info_network.h:94
std::optional< size_t > forwarding_timeout_ms
Timeout for forwarded RPC calls (in milliseconds)
Definition node_info_network.h:111
std::optional< ApplicationProtocol > app_protocol
Definition node_info_network.h:91
std::optional< http::ParserConfiguration > http_configuration
HTTP configuration.
Definition node_info_network.h:100
std::set< ccf::endpoints::OperatorFeature > enabled_operator_features
Definition node_info_network.h:116
NetAddress bind_address
Definition node_info_network.h:88
std::optional< std::vector< std::string > > accepted_endpoints
Definition node_info_network.h:108
NetAddress published_address
Definition node_info_network.h:89
bool operator==(const NetInterface &other) const
Definition node_info_network.h:128
std::optional< Endorsement > endorsement
Interface endorsement.
Definition node_info_network.h:104
Node network information.
Definition node_info_network.h:80
NetInterface node_to_node_interface
Node-to-node network interface.
Definition node_info_network.h:148
std::string RpcInterfaceID
Definition node_info_network.h:82
std::string NetProtocol
Definition node_info_network.h:83
std::string NetAddress
Definition node_info_network.h:81
std::map< RpcInterfaceID, NetInterface > RpcInterfaces
RPC interface mapping.
Definition node_info_network.h:145
RpcInterfaces rpc_interfaces
RPC interfaces.
Definition node_info_network.h:151
Definition node_info_network.h:179
bool operator==(const NodeInfoNetwork &other) const
Definition node_info_network.h:184
NodeInfoNetwork(const NodeInfoNetwork_v2 &other)
Definition node_info_network.h:181
NodeInfoNetwork()=default
Definition node_info_network.h:68
bool operator==(const RedirectionResolverConfig &) const =default
RedirectionResolutionKind kind
Definition node_info_network.h:69
nlohmann::json target
Definition node_info_network.h:70
constexpr auto parse(ParseContext &ctx)
Definition node_info_network.h:265
auto format(const ccf::Authority &authority, FormatContext &ctx) const -> decltype(ctx.out())
Definition node_info_network.h:271