24 std::shared_ptr<ccf::RpcContextImpl> fwd_ctx) = 0;
27 template <
typename ChannelProxy>
31 std::weak_ptr<ccf::AbstractRPCResponder> rpcresponder;
32 std::shared_ptr<ChannelProxy> n2n_channels;
33 std::weak_ptr<ccf::RPCMap> rpc_map;
37 ForwardedCommandId next_command_id = 0;
39 std::unordered_map<ForwardedCommandId, ccf::tasks::Task> timeout_tasks;
42 using IsCallerCertForwarded = bool;
44 void send_timeout_error_response(
46 size_t client_session_id,
47 const std::chrono::milliseconds& timeout)
49 auto rpc_responder_shared = rpcresponder.lock();
50 if (rpc_responder_shared)
53 auto body = fmt::format(
54 "Request was forwarded to node {}, but no response was received "
58 response.set_body(body);
60 http::headers::CONTENT_TYPE, http::headervalues::contenttype::TEXT);
61 rpc_responder_shared->reply_async(
62 client_session_id,
false, response.build_response());
68 std::weak_ptr<ccf::AbstractRPCResponder> rpcresponder,
69 std::shared_ptr<ChannelProxy> n2n_channels,
70 std::weak_ptr<ccf::RPCMap> rpc_map_) :
71 rpcresponder(
std::move(rpcresponder)),
72 n2n_channels(
std::move(n2n_channels)),
73 rpc_map(
std::move(rpc_map_))
82 std::shared_ptr<ccf::RpcContextImpl> rpc_ctx,
84 const std::vector<uint8_t>& caller_cert,
85 const std::chrono::milliseconds& timeout)
override
87 auto session_ctx = rpc_ctx->get_session_context();
89 IsCallerCertForwarded include_caller =
false;
90 const auto& raw_request = rpc_ctx->get_serialised_request();
91 auto client_session_id = session_ctx->client_session_id;
92 size_t size =
sizeof(client_session_id) +
sizeof(IsCallerCertForwarded) +
94 if (!caller_cert.empty())
96 size +=
sizeof(size_t) + caller_cert.size();
97 include_caller =
true;
100 std::vector<uint8_t> plain(size);
101 auto* data_ = plain.data();
102 auto size_ = plain.size();
112 ForwardedCommandId command_id = 0;
114 std::lock_guard<ccf::pal::Mutex> guard(timeout_tasks_lock);
115 command_id = next_command_id++;
118 this->send_timeout_error_response(to, client_session_id, timeout);
120 timeout_tasks[command_id] = task;
124 const auto view_opt = session_ctx->active_view;
125 if (!view_opt.has_value())
127 throw std::logic_error(
128 "Expected active_view to be set before forwarding");
132 return n2n_channels->send_encrypted(
136 template <
typename TFwdHdr>
138 const NodeId& from,
const uint8_t* data,
size_t size)
140 std::pair<TFwdHdr, std::vector<uint8_t>> r;
143 LOG_TRACE_FMT(
"Receiving forwarded command of {} bytes", size);
144 LOG_TRACE_FMT(
" => {:02x}", fmt::join(data, data + size,
""));
146 r = n2n_channels->template recv_encrypted<TFwdHdr>(from, data, size);
148 catch (
const std::logic_error& err)
155 std::vector<uint8_t> caller_cert;
156 const auto& plain_ = r.second;
157 auto data_ = plain_.data();
158 auto size_ = plain_.size();
159 auto client_session_id = serialized::read<size_t>(data_, size_);
160 auto includes_caller =
161 serialized::read<IsCallerCertForwarded>(data_, size_);
164 auto caller_size = serialized::read<size_t>(data_, size_);
171 std::make_shared<ccf::SessionContext>(client_session_id, caller_cert);
172 session->is_forwarded =
true;
174 if constexpr (std::is_same_v<TFwdHdr, ForwardedCommandHeader_v3>)
177 session->active_view =
view;
183 session, raw_request, r.first.frame_format);
185 catch (const ::http::RequestTooLargeException& rexc)
187 LOG_FAIL_FMT(
"Forwarded request exceeded limit: {}", rexc.what());
190 catch (
const std::exception& err)
198 template <
typename TFwdHdr>
200 size_t client_session_id,
202 const TFwdHdr& header,
203 const std::vector<uint8_t>& data)
205 std::vector<uint8_t> plain(
sizeof(client_session_id) + data.size());
206 auto* data_ = plain.data();
207 auto size_ = plain.size();
211 if (!n2n_channels->send_encrypted(
214 LOG_FAIL_FMT(
"Failed to send forwarded response to {}", from_node);
225 template <
typename TFwdHdr>
227 const NodeId& from,
const uint8_t* data,
size_t size)
229 std::pair<TFwdHdr, std::vector<uint8_t>> r;
233 LOG_TRACE_FMT(
" => {:02x}", fmt::join(data, data + size,
""));
235 r = n2n_channels->template recv_encrypted<TFwdHdr>(from, data, size);
237 catch (
const std::logic_error& err)
245 if constexpr (std::is_same_v<TFwdHdr, ForwardedResponseHeader_v3>)
250 const auto& plain_ = r.second;
251 auto data_ = plain_.data();
252 auto size_ = plain_.size();
260 std::shared_ptr<::http::HttpRpcContext>& ctx)
268 std::shared_ptr<ccf::RPCMap> rpc_map_shared = rpc_map.lock();
269 if (rpc_map_shared ==
nullptr)
275 std::shared_ptr<ccf::RpcHandler> search =
276 ::http::fetch_rpc_handler(ctx, rpc_map_shared);
278 auto fwd_handler = std::dynamic_pointer_cast<ForwardedRpcHandler>(search);
282 "Failed to process forwarded command: handler is not a "
283 "ForwardedRpcHandler");
294 const auto forwarded_msg = serialized::peek<ForwardedMsg>(data, size);
296 "recv_message({}, {} bytes) (type={})",
306 recv_forwarded_command<ForwardedHeader_v1>(from, data, size);
309 if (fwd_handler ==
nullptr)
320 fwd_handler->process_forwarded(ctx);
323 ctx->get_session_context()->client_session_id,
326 ctx->serialise_response());
333 recv_forwarded_command<ForwardedHeader_v2>(from, data, size);
336 if (fwd_handler ==
nullptr)
341 const auto forwarded_hdr_v2 =
342 serialized::peek<ForwardedHeader_v2>(data, size);
343 const auto cmd_id = forwarded_hdr_v2.id;
345 fwd_handler->process_forwarded(ctx);
355 ctx->get_session_context()->client_session_id,
358 ctx->serialise_response());
364 auto ctx = recv_forwarded_command<ForwardedCommandHeader_v3>(
368 if (fwd_handler ==
nullptr)
373 const auto forwarded_hdr_v3 =
374 serialized::peek<ForwardedCommandHeader_v3>(data, size);
375 const auto cmd_id = forwarded_hdr_v3.id;
377 fwd_handler->process_forwarded(ctx);
382 cmd_id, ctx->terminate_session);
387 ctx->get_session_context()->client_session_id,
390 ctx->serialise_response());
397 const auto forwarded_hdr_v2 =
398 serialized::peek<ForwardedHeader_v2>(data, size);
399 const auto cmd_id = forwarded_hdr_v2.id;
403 std::lock_guard<ccf::pal::Mutex> guard(timeout_tasks_lock);
404 auto it = timeout_tasks.find(cmd_id);
405 if (it != timeout_tasks.end())
407 it->second->cancel_task();
408 it = timeout_tasks.erase(it);
413 "Response for {} received too late - already sent timeout "
423 std::optional<ForwardedResponseResult> rep;
426 rep = recv_forwarded_response<ForwardedResponseHeader_v3>(
432 recv_forwarded_response<ForwardedHeader_v2>(from, data, size);
437 recv_forwarded_response<ForwardedHeader_v1>(from, data, size);
440 if (!rep.has_value())
446 "Sending forwarded response to RPC endpoint {}",
447 rep->client_session_id);
449 auto rpc_responder_shared = rpcresponder.lock();
451 rpc_responder_shared &&
452 !rpc_responder_shared->reply_async(
453 rep->client_session_id,
454 rep->should_terminate_session,
455 std::move(rep->response_body)))
470 catch (
const std::exception& e)
Definition forwarder_types.h:22
Definition forwarder.h:19
virtual ~ForwardedRpcHandler()=default
virtual void process_forwarded(std::shared_ptr< ccf::RpcContextImpl > fwd_ctx)=0
Definition forwarder.h:29
void initialize(const NodeId &self_)
Definition forwarder.h:76
std::shared_ptr<::http::HttpRpcContext > recv_forwarded_command(const NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:137
void recv_message(const ccf::NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:290
Forwarder(std::weak_ptr< ccf::AbstractRPCResponder > rpcresponder, std::shared_ptr< ChannelProxy > n2n_channels, std::weak_ptr< ccf::RPCMap > rpc_map_)
Definition forwarder.h:67
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:199
std::shared_ptr< ForwardedRpcHandler > get_forwarder_handler(std::shared_ptr<::http::HttpRpcContext > &ctx)
Definition forwarder.h:259
std::optional< ForwardedResponseResult > recv_forwarded_response(const NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:226
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:81
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:14
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:402
view
Definition signatures.h:54
@ 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:219
size_t client_session_id
Definition forwarder.h:220
std::vector< uint8_t > response_body
Definition forwarder.h:221
bool should_terminate_session
Definition forwarder.h:222