23 std::shared_ptr<ccf::RpcContextImpl> fwd_ctx) = 0;
26 template <
typename ChannelProxy>
30 std::weak_ptr<ccf::AbstractRPCResponder> rpcresponder;
31 std::shared_ptr<ChannelProxy> n2n_channels;
32 std::weak_ptr<ccf::RPCMap> rpc_map;
36 ForwardedCommandId next_command_id = 0;
38 std::unordered_map<ForwardedCommandId, ccf::tasks::Task> timeout_tasks;
41 using IsCallerCertForwarded = bool;
43 void send_timeout_error_response(
45 size_t client_session_id,
46 const std::chrono::milliseconds& timeout)
48 auto rpc_responder_shared = rpcresponder.lock();
49 if (rpc_responder_shared)
52 auto body = fmt::format(
53 "Request was forwarded to node {}, but no response was received "
57 response.set_body(body);
59 http::headers::CONTENT_TYPE, http::headervalues::contenttype::TEXT);
60 rpc_responder_shared->reply_async(
61 client_session_id,
false, response.build_response());
67 std::weak_ptr<ccf::AbstractRPCResponder> rpcresponder,
68 std::shared_ptr<ChannelProxy> n2n_channels,
69 std::weak_ptr<ccf::RPCMap> rpc_map_) :
70 rpcresponder(
std::move(rpcresponder)),
71 n2n_channels(
std::move(n2n_channels)),
72 rpc_map(
std::move(rpc_map_))
81 std::shared_ptr<ccf::RpcContextImpl> rpc_ctx,
83 const std::vector<uint8_t>& caller_cert,
84 const std::chrono::milliseconds& timeout)
override
86 auto session_ctx = rpc_ctx->get_session_context();
88 IsCallerCertForwarded include_caller =
false;
89 const auto& raw_request = rpc_ctx->get_serialised_request();
90 auto client_session_id = session_ctx->client_session_id;
91 size_t size =
sizeof(client_session_id) +
sizeof(IsCallerCertForwarded) +
93 if (!caller_cert.empty())
95 size +=
sizeof(size_t) + caller_cert.size();
96 include_caller =
true;
99 std::vector<uint8_t> plain(size);
100 auto* data_ = plain.data();
101 auto size_ = plain.size();
111 ForwardedCommandId command_id = 0;
113 std::lock_guard<ccf::pal::Mutex> guard(timeout_tasks_lock);
114 command_id = next_command_id++;
117 this->send_timeout_error_response(to, client_session_id, timeout);
119 timeout_tasks[command_id] = task;
123 const auto view_opt = session_ctx->active_view;
124 if (!view_opt.has_value())
126 throw std::logic_error(
127 "Expected active_view to be set before forwarding");
131 return n2n_channels->send_encrypted(
135 template <
typename TFwdHdr>
137 const NodeId& from,
const uint8_t* data,
size_t size)
139 std::pair<TFwdHdr, std::vector<uint8_t>> r;
142 LOG_TRACE_FMT(
"Receiving forwarded command of {} bytes", size);
143 LOG_TRACE_FMT(
" => {:02x}", fmt::join(data, data + size,
""));
145 r = n2n_channels->template recv_encrypted<TFwdHdr>(from, data, size);
147 catch (
const std::logic_error& err)
154 std::vector<uint8_t> caller_cert;
155 const auto& plain_ = r.second;
156 auto data_ = plain_.data();
157 auto size_ = plain_.size();
158 auto client_session_id = serialized::read<size_t>(data_, size_);
159 auto includes_caller =
160 serialized::read<IsCallerCertForwarded>(data_, size_);
163 auto caller_size = serialized::read<size_t>(data_, size_);
170 std::make_shared<ccf::SessionContext>(client_session_id, caller_cert);
171 session->is_forwarded =
true;
173 if constexpr (std::is_same_v<TFwdHdr, ForwardedCommandHeader_v3>)
176 session->active_view = view;
182 session, raw_request, r.first.frame_format);
184 catch (const ::http::RequestTooLargeException& rexc)
186 LOG_FAIL_FMT(
"Forwarded request exceeded limit: {}", rexc.what());
189 catch (
const std::exception& err)
197 template <
typename TFwdHdr>
199 size_t client_session_id,
201 const TFwdHdr& header,
202 const std::vector<uint8_t>& data)
204 std::vector<uint8_t> plain(
sizeof(client_session_id) + data.size());
205 auto* data_ = plain.data();
206 auto size_ = plain.size();
210 if (!n2n_channels->send_encrypted(
213 LOG_FAIL_FMT(
"Failed to send forwarded response to {}", from_node);
224 template <
typename TFwdHdr>
226 const NodeId& from,
const uint8_t* data,
size_t size)
228 std::pair<TFwdHdr, std::vector<uint8_t>> r;
232 LOG_TRACE_FMT(
" => {:02x}", fmt::join(data, data + size,
""));
234 r = n2n_channels->template recv_encrypted<TFwdHdr>(from, data, size);
236 catch (
const std::logic_error& err)
244 if constexpr (std::is_same_v<TFwdHdr, ForwardedResponseHeader_v3>)
249 const auto& plain_ = r.second;
250 auto data_ = plain_.data();
251 auto size_ = plain_.size();
259 std::shared_ptr<::http::HttpRpcContext>& ctx)
267 std::shared_ptr<ccf::RPCMap> rpc_map_shared = rpc_map.lock();
268 if (rpc_map_shared ==
nullptr)
274 std::shared_ptr<ccf::RpcHandler> search =
275 ::http::fetch_rpc_handler(ctx, rpc_map_shared);
277 auto fwd_handler = std::dynamic_pointer_cast<ForwardedRpcHandler>(search);
281 "Failed to process forwarded command: handler is not a "
282 "ForwardedRpcHandler");
293 const auto forwarded_msg = serialized::peek<ForwardedMsg>(data, size);
295 "recv_message({}, {} bytes) (type={})",
305 recv_forwarded_command<ForwardedHeader_v1>(from, data, size);
308 if (fwd_handler ==
nullptr)
319 fwd_handler->process_forwarded(ctx);
322 ctx->get_session_context()->client_session_id,
325 ctx->serialise_response());
332 recv_forwarded_command<ForwardedHeader_v2>(from, data, size);
335 if (fwd_handler ==
nullptr)
340 const auto forwarded_hdr_v2 =
341 serialized::peek<ForwardedHeader_v2>(data, size);
342 const auto cmd_id = forwarded_hdr_v2.id;
344 fwd_handler->process_forwarded(ctx);
354 ctx->get_session_context()->client_session_id,
357 ctx->serialise_response());
363 auto ctx = recv_forwarded_command<ForwardedCommandHeader_v3>(
367 if (fwd_handler ==
nullptr)
372 const auto forwarded_hdr_v3 =
373 serialized::peek<ForwardedCommandHeader_v3>(data, size);
374 const auto cmd_id = forwarded_hdr_v3.id;
376 fwd_handler->process_forwarded(ctx);
381 cmd_id, ctx->terminate_session);
386 ctx->get_session_context()->client_session_id,
389 ctx->serialise_response());
396 const auto forwarded_hdr_v2 =
397 serialized::peek<ForwardedHeader_v2>(data, size);
398 const auto cmd_id = forwarded_hdr_v2.id;
402 std::lock_guard<ccf::pal::Mutex> guard(timeout_tasks_lock);
403 auto it = timeout_tasks.find(cmd_id);
404 if (it != timeout_tasks.end())
406 it->second->cancel_task();
407 it = timeout_tasks.erase(it);
412 "Response for {} received too late - already sent timeout "
422 std::optional<ForwardedResponseResult> rep;
425 rep = recv_forwarded_response<ForwardedResponseHeader_v3>(
431 recv_forwarded_response<ForwardedHeader_v2>(from, data, size);
436 recv_forwarded_response<ForwardedHeader_v1>(from, data, size);
439 if (!rep.has_value())
445 "Sending forwarded response to RPC endpoint {}",
446 rep->client_session_id);
448 auto rpc_responder_shared = rpcresponder.lock();
450 rpc_responder_shared &&
451 !rpc_responder_shared->reply_async(
452 rep->client_session_id,
453 rep->should_terminate_session,
454 std::move(rep->response_body)))
469 catch (
const std::exception& e)
Definition forwarder_types.h:22
Definition forwarder.h:18
virtual ~ForwardedRpcHandler()=default
virtual void process_forwarded(std::shared_ptr< ccf::RpcContextImpl > fwd_ctx)=0
Definition forwarder.h:28
void initialize(const NodeId &self_)
Definition forwarder.h:75
std::shared_ptr<::http::HttpRpcContext > recv_forwarded_command(const NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:136
void recv_message(const ccf::NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:289
Forwarder(std::weak_ptr< ccf::AbstractRPCResponder > rpcresponder, std::shared_ptr< ChannelProxy > n2n_channels, std::weak_ptr< ccf::RPCMap > rpc_map_)
Definition forwarder.h:66
void send_forwarded_response(size_t client_session_id, const NodeId &from_node, const TFwdHdr &header, const std::vector< uint8_t > &data)
Definition forwarder.h:198
std::shared_ptr< ForwardedRpcHandler > get_forwarder_handler(std::shared_ptr<::http::HttpRpcContext > &ctx)
Definition forwarder.h:258
std::optional< ForwardedResponseResult > recv_forwarded_response(const NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:225
bool forward_command(std::shared_ptr< ccf::RpcContextImpl > rpc_ctx, const NodeId &to, const std::vector< uint8_t > &caller_cert, const std::chrono::milliseconds &timeout) override
Definition forwarder.h:80
Definition http_builder.h:200
#define LOG_TRACE_FMT
Definition internal_logger.h:13
#define LOG_DEBUG_FMT
Definition internal_logger.h:14
#define LOG_FAIL_FMT
Definition internal_logger.h:16
std::mutex Mutex
Definition locking.h:12
Task make_basic_task(Ts &&... ts)
Definition basic_task.h:33
void add_delayed_task(Task task, std::chrono::milliseconds delay)
Definition task_system.cpp:70
Definition app_interface.h:13
std::shared_ptr<::http::HttpRpcContext > make_fwd_rpc_context(std::shared_ptr< ccf::SessionContext > s, const std::vector< uint8_t > &packed, ccf::FrameFormat frame_format)
Definition http_rpc_context.h:391
@ forwarded_cmd_v3
Definition node_types.h:52
@ forwarded_cmd_v2
Definition node_types.h:46
@ forwarded_response_v1
Definition node_types.h:42
@ forwarded_response_v3
Definition node_types.h:53
@ forwarded_response_v2
Definition node_types.h:47
@ forwarded_cmd_v1
Definition node_types.h:41
uint64_t View
Definition tx_id.h:23
@ forwarded_msg
Definition node_types.h:24
void write(uint8_t *&data, size_t &size, const T &v)
Definition serialized.h:105
T read(const uint8_t *&data, size_t &size)
Definition serialized.h:58
Definition node_types.h:80
Definition forwarder.h:218
size_t client_session_id
Definition forwarder.h:219
std::vector< uint8_t > response_body
Definition forwarder.h:220
bool should_terminate_session
Definition forwarder.h:221