22 std::shared_ptr<ccf::RpcContextImpl> fwd_ctx) = 0;
25 template <
typename ChannelProxy>
29 std::weak_ptr<ccf::AbstractRPCResponder> rpcresponder;
30 std::shared_ptr<ChannelProxy> n2n_channels;
31 std::weak_ptr<ccf::RPCMap> rpc_map;
35 ForwardedCommandId next_command_id = 0;
43 std::unordered_map<ForwardedCommandId, TimeoutTask> timeout_tasks;
46 using IsCallerCertForwarded = bool;
48 struct SendTimeoutErrorMsg
53 size_t client_session_id_,
54 const std::chrono::milliseconds& timeout_) :
55 forwarder(forwarder_),
57 client_session_id(client_session_id_),
63 size_t client_session_id;
64 std::chrono::milliseconds timeout;
72 std::unique_ptr<::threading::Tmsg<SendTimeoutErrorMsg>>
73 create_timeout_error_task(
75 size_t client_session_id,
76 const std::chrono::milliseconds& timeout)
78 return std::make_unique<::threading::Tmsg<SendTimeoutErrorMsg>>(
79 [](std::unique_ptr<::threading::Tmsg<SendTimeoutErrorMsg>> msg) {
80 msg->data.forwarder->send_timeout_error_response(
81 msg->data.to, msg->data.client_session_id, msg->data.timeout);
89 void send_timeout_error_response(
91 size_t client_session_id,
92 const std::chrono::milliseconds& timeout)
94 auto rpc_responder_shared = rpcresponder.lock();
95 if (rpc_responder_shared)
98 auto body = fmt::format(
99 "Request was forwarded to node {}, but no response was received "
103 response.set_body(body);
105 http::headers::CONTENT_TYPE, http::headervalues::contenttype::TEXT);
106 rpc_responder_shared->reply_async(
107 client_session_id,
false, response.build_response());
111 static void cancel_forwarding_task_cb(
114 cancel_forwarding_task(msg->data.timer_entry);
117 static void cancel_forwarding_task(
125 std::weak_ptr<ccf::AbstractRPCResponder> rpcresponder,
126 std::shared_ptr<ChannelProxy> n2n_channels,
127 std::weak_ptr<ccf::RPCMap> rpc_map_) :
128 rpcresponder(rpcresponder),
129 n2n_channels(n2n_channels),
139 std::shared_ptr<ccf::RpcContextImpl> rpc_ctx,
141 const std::vector<uint8_t>& caller_cert,
142 const std::chrono::milliseconds& timeout)
override
144 auto session_ctx = rpc_ctx->get_session_context();
146 IsCallerCertForwarded include_caller =
false;
147 const auto method = rpc_ctx->get_method();
148 const auto& raw_request = rpc_ctx->get_serialised_request();
149 auto client_session_id = session_ctx->client_session_id;
150 size_t size =
sizeof(client_session_id) +
sizeof(IsCallerCertForwarded) +
152 if (!caller_cert.empty())
154 size +=
sizeof(size_t) + caller_cert.size();
155 include_caller =
true;
158 std::vector<uint8_t> plain(size);
159 auto data_ = plain.data();
160 auto size_ = plain.size();
170 ForwardedCommandId command_id;
172 std::lock_guard<ccf::pal::Mutex> guard(timeout_tasks_lock);
173 command_id = next_command_id++;
174 timeout_tasks[command_id] = {
176 create_timeout_error_task(to, client_session_id, timeout), timeout),
180 const auto view_opt = session_ctx->active_view;
181 if (!view_opt.has_value())
183 throw std::logic_error(
184 "Expected active_view to be set before forwarding");
188 return n2n_channels->send_encrypted(
192 template <
typename TFwdHdr>
194 const NodeId& from,
const uint8_t* data,
size_t size)
196 std::pair<TFwdHdr, std::vector<uint8_t>> r;
199 LOG_TRACE_FMT(
"Receiving forwarded command of {} bytes", size);
200 LOG_TRACE_FMT(
" => {:02x}", fmt::join(data, data + size,
""));
202 r = n2n_channels->template recv_encrypted<TFwdHdr>(from, data, size);
204 catch (
const std::logic_error& err)
211 std::vector<uint8_t> caller_cert;
212 const auto& plain_ = r.second;
213 auto data_ = plain_.data();
214 auto size_ = plain_.size();
215 auto client_session_id = serialized::read<size_t>(data_, size_);
216 auto includes_caller =
217 serialized::read<IsCallerCertForwarded>(data_, size_);
220 auto caller_size = serialized::read<size_t>(data_, size_);
226 std::make_shared<ccf::SessionContext>(client_session_id, caller_cert);
227 session->is_forwarded =
true;
229 if constexpr (std::is_same_v<TFwdHdr, ForwardedCommandHeader_v3>)
232 session->active_view =
view;
238 session, raw_request, r.first.frame_format);
240 catch (const ::http::RequestTooLargeException& rexc)
242 LOG_FAIL_FMT(
"Forwarded request exceeded limit: {}", rexc.what());
245 catch (
const std::exception& err)
253 template <
typename TFwdHdr>
255 size_t client_session_id,
257 const TFwdHdr& header,
258 const std::vector<uint8_t>& data)
260 std::vector<uint8_t> plain(
sizeof(client_session_id) + data.size());
261 auto data_ = plain.data();
262 auto size_ = plain.size();
266 if (!n2n_channels->send_encrypted(
269 LOG_FAIL_FMT(
"Failed to send forwarded response to {}", from_node);
280 template <
typename TFwdHdr>
282 const NodeId& from,
const uint8_t* data,
size_t size)
284 std::pair<TFwdHdr, std::vector<uint8_t>> r;
288 LOG_TRACE_FMT(
" => {:02x}", fmt::join(data, data + size,
""));
290 r = n2n_channels->template recv_encrypted<TFwdHdr>(from, data, size);
292 catch (
const std::logic_error& err)
300 if constexpr (std::is_same_v<TFwdHdr, ForwardedResponseHeader_v3>)
305 const auto& plain_ = r.second;
306 auto data_ = plain_.data();
307 auto size_ = plain_.size();
315 std::shared_ptr<::http::HttpRpcContext>& ctx)
323 std::shared_ptr<ccf::RPCMap> rpc_map_shared = rpc_map.lock();
324 if (rpc_map_shared ==
nullptr)
330 std::shared_ptr<ccf::RpcHandler> search =
331 ::http::fetch_rpc_handler(ctx, rpc_map_shared);
333 auto fwd_handler = std::dynamic_pointer_cast<ForwardedRpcHandler>(search);
337 "Failed to process forwarded command: handler is not a "
338 "ForwardedRpcHandler");
349 const auto forwarded_msg = serialized::peek<ForwardedMsg>(data, size);
351 "recv_message({}, {} bytes) (type={})",
361 recv_forwarded_command<ForwardedHeader_v1>(from, data, size);
364 if (fwd_handler ==
nullptr)
375 fwd_handler->process_forwarded(ctx);
378 ctx->get_session_context()->client_session_id,
381 ctx->serialise_response());
388 recv_forwarded_command<ForwardedHeader_v2>(from, data, size);
391 if (fwd_handler ==
nullptr)
396 const auto forwarded_hdr_v2 =
397 serialized::peek<ForwardedHeader_v2>(data, size);
398 const auto cmd_id = forwarded_hdr_v2.id;
400 fwd_handler->process_forwarded(ctx);
410 ctx->get_session_context()->client_session_id,
413 ctx->serialise_response());
419 auto ctx = recv_forwarded_command<ForwardedCommandHeader_v3>(
423 if (fwd_handler ==
nullptr)
428 const auto forwarded_hdr_v3 =
429 serialized::peek<ForwardedCommandHeader_v3>(data, size);
430 const auto cmd_id = forwarded_hdr_v3.id;
432 fwd_handler->process_forwarded(ctx);
437 cmd_id, ctx->terminate_session);
442 ctx->get_session_context()->client_session_id,
445 ctx->serialise_response());
452 const auto forwarded_hdr_v2 =
453 serialized::peek<ForwardedHeader_v2>(data, size);
454 const auto cmd_id = forwarded_hdr_v2.id;
458 std::lock_guard<ccf::pal::Mutex> guard(timeout_tasks_lock);
459 auto it = timeout_tasks.find(cmd_id);
460 if (it != timeout_tasks.end())
465 auto msg = std::make_unique<::threading::Tmsg<CancelTimerMsg>>(
466 &cancel_forwarding_task_cb);
467 msg->data.timer_entry = it->second.timer_entry;
470 it->second.thread_id, std::move(msg));
474 cancel_forwarding_task(it->second.timer_entry);
476 it = timeout_tasks.erase(it);
481 "Response for {} received too late - already sent timeout "
491 std::optional<ForwardedResponseResult> rep;
494 rep = recv_forwarded_response<ForwardedResponseHeader_v3>(
500 recv_forwarded_response<ForwardedHeader_v2>(from, data, size);
505 recv_forwarded_response<ForwardedHeader_v1>(from, data, size);
508 if (!rep.has_value())
514 "Sending forwarded response to RPC endpoint {}",
515 rep->client_session_id);
517 auto rpc_responder_shared = rpcresponder.lock();
519 rpc_responder_shared &&
520 !rpc_responder_shared->reply_async(
521 rep->client_session_id,
522 rep->should_terminate_session,
523 std::move(rep->response_body)))
538 catch (
const std::exception& e)
Definition forwarder_types.h:22
Definition forwarder.h:17
virtual ~ForwardedRpcHandler()
Definition forwarder.h:19
virtual void process_forwarded(std::shared_ptr< ccf::RpcContextImpl > fwd_ctx)=0
Definition forwarder.h:27
void initialize(const NodeId &self_)
Definition forwarder.h:133
std::shared_ptr<::http::HttpRpcContext > recv_forwarded_command(const NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:193
void recv_message(const ccf::NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:345
Forwarder(std::weak_ptr< ccf::AbstractRPCResponder > rpcresponder, std::shared_ptr< ChannelProxy > n2n_channels, std::weak_ptr< ccf::RPCMap > rpc_map_)
Definition forwarder.h:124
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:254
std::shared_ptr< ForwardedRpcHandler > get_forwarder_handler(std::shared_ptr<::http::HttpRpcContext > &ctx)
Definition forwarder.h:314
std::optional< ForwardedResponseResult > recv_forwarded_response(const NodeId &from, const uint8_t *data, size_t size)
Definition forwarder.h:281
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:138
Definition http_builder.h:200
static ThreadMessaging & instance()
Definition thread_messaging.h:283
bool cancel_timer_task(TaskQueue::TimerEntry timer_entry)
Definition thread_messaging.h:333
TaskQueue::TimerEntry add_task_after(std::unique_ptr< Tmsg< Payload > > msg, std::chrono::milliseconds ms)
Definition thread_messaging.h:326
void add_task(uint16_t tid, std::unique_ptr< Tmsg< Payload > > msg)
Definition thread_messaging.h:318
#define LOG_TRACE_FMT
Definition logger.h:356
#define LOG_DEBUG_FMT
Definition logger.h:357
#define LOG_FAIL_FMT
Definition logger.h:363
std::mutex Mutex
Definition locking.h:12
uint16_t get_current_thread_id()
Definition thread_local.cpp:15
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:420
view
Definition signatures.h:54
@ forwarded_cmd_v3
Definition node_types.h:50
@ forwarded_cmd_v2
Definition node_types.h:44
@ forwarded_response_v1
Definition node_types.h:40
@ forwarded_response_v3
Definition node_types.h:51
@ forwarded_response_v2
Definition node_types.h:45
@ forwarded_cmd_v1
Definition node_types.h:39
uint64_t View
Definition tx_id.h:23
@ forwarded_msg
Definition node_types.h:22
void write(uint8_t *&data, size_t &size, const T &v)
Definition serialized.h:106
T read(const uint8_t *&data, size_t &size)
Definition serialized.h:59
Definition node_types.h:76
Definition forwarder.h:274
size_t client_session_id
Definition forwarder.h:275
std::vector< uint8_t > response_body
Definition forwarder.h:276
bool should_terminate_session
Definition forwarder.h:277
Definition thread_messaging.h:85
Definition thread_messaging.h:27