Skip to content

Instantly share code, notes, and snippets.

@MahatiC
Created September 8, 2022 14:25
Show Gist options
  • Save MahatiC/2c29ab357ea3f52cf7f1703f4dd87486 to your computer and use it in GitHub Desktop.
Save MahatiC/2c29ab357ea3f52cf7f1703f4dd87486 to your computer and use it in GitHub Desktop.
diff --git a/src/node/rpc/forwarder.h b/src/node/rpc/forwarder.h
index ae408746..7c856a5e 100644
--- a/src/node/rpc/forwarder.h
+++ b/src/node/rpc/forwarder.h
@@ -290,82 +290,15 @@ namespace ccf
switch (forwarded_msg)
{
case ForwardedMsg::forwarded_cmd_v1:
- case ForwardedMsg::forwarded_cmd_v2:
{
std::shared_ptr<ccf::RPCMap> rpc_map_shared = rpc_map.lock();
if (rpc_map_shared)
{
- std::shared_ptr<http::HttpRpcContext> ctx;
- if (forwarded_msg == ForwardedMsg::forwarded_cmd_v2)
- {
- ctx =
- recv_forwarded_command<ForwardedHeader_v2>(from, data, size);
- }
- else
- {
- ctx =
- recv_forwarded_command<ForwardedHeader_v1>(from, data, size);
- }
-
- if (ctx == nullptr)
- {
- LOG_FAIL_FMT("Failed to receive forwarded command");
- return;
- }
-
- const auto actor_opt = http::extract_actor(*ctx);
- if (!actor_opt.has_value())
- {
- LOG_FAIL_FMT("Failed to extract actor from forwarded context.");
- LOG_DEBUG_FMT(
- "Failed to extract actor from forwarded context. Method is "
- "'{}'",
- ctx->get_method());
- }
-
- const auto& actor_s = actor_opt.value();
- auto actor = rpc_map_shared->resolve(actor_s);
- auto handler = rpc_map_shared->find(actor);
- if (actor == ccf::ActorsType::unknown || !handler.has_value())
- {
- LOG_FAIL_FMT(
- "Failed to process forwarded command: unknown actor");
- LOG_DEBUG_FMT(
- "Failed to process forwarded command: unknown actor {}",
- actor_s);
- return;
- }
-
- auto fwd_handler =
- dynamic_cast<ForwardedRpcHandler*>(handler.value().get());
- if (!fwd_handler)
- {
- LOG_FAIL_FMT(
- "Failed to process forwarded command: handler is not a "
- "ForwardedRpcHandler");
- return;
- }
-
- if (forwarded_msg == ForwardedMsg::forwarded_cmd_v2)
- {
- const auto forwarded_hdr_v2 =
- serialized::peek<ForwardedHeader_v2>(data, size);
- const auto cmd_id = forwarded_hdr_v2.id;
+ std::shared_ptr<http::HttpRpcContext> ctx =
+ recv_forwarded_command<ForwardedHeader_v1>(from, data, size);
+ //call common code
+ handle_errors(ctx);
- // frame_format is deliberately unset, the forwarder ignores it
- // and expects the same format they forwarded.
- ForwardedHeader_v2 response_header{
- {ForwardedMsg::forwarded_response_v2, {}}, cmd_id};
-
- // Ignore return value - false only means it is pending
- send_forwarded_response(
- ctx->get_session_context()->client_session_id,
- from,
- response_header,
- fwd_handler->process_forwarded(ctx));
- }
- else
- {
// frame_format is deliberately unset, the forwarder ignores it
// and expects the same format they forwarded.
ForwardedHeader_v1 response_header{
@@ -377,12 +310,38 @@ namespace ccf
from,
response_header,
fwd_handler->process_forwarded(ctx));
- }
LOG_DEBUG_FMT("Sending forwarded response to {}", from);
}
break;
}
-
+ case ForwardedMsg::forwarded_cmd_v2:
+ {
+ std::shared_ptr<ccf::RPCMap> rpc_map_shared = rpc_map.lock();
+ if (rpc_map_shared)
+ {
+ std::shared_ptr<http::HttpRpcContext> ctx =
+ recv_forwarded_command<ForwardedHeader_v2>(from, data, size);
+ //call common code
+ handle_erros(ctx);
+
+ const auto forwarded_hdr_v2 =
+ serialized::peek<ForwardedHeader_v2>(data, size);
+ const auto cmd_id = forwarded_hdr_v2.id;
+
+ // frame_format is deliberately unset, the forwarder ignores it
+ // and expects the same format they forwarded.
+ ForwardedHeader_v2 response_header{
+ {ForwardedMsg::forwarded_response_v2, {}}, cmd_id};
+
+ // Ignore return value - false only means it is pending
+ send_forwarded_response(
+ ctx->get_session_context()->client_session_id,
+ from,
+ response_header,
+ fwd_handler->process_forwarded(ctx));
+ }
+ break;
+ }
case ForwardedMsg::forwarded_response_v2:
{
const auto forwarded_hdr_v2 =
@@ -440,7 +399,6 @@ namespace ccf
{
return;
}
-
break;
}
@@ -450,12 +408,53 @@ namespace ccf
break;
}
}
- }
catch (const std::exception& e)
{
LOG_FAIL_EXC(e.what());
return;
}
}
- };
+ }
+
+ bool handle_errors(std::shared_ptr<http::HttpRpcContext> &ctx) {
+ if (ctx == nullptr)
+ {
+ LOG_FAIL_FMT("Failed to receive forwarded command");
+ return false;
+ }
+
+ const auto actor_opt = http::extract_actor(*ctx);
+ if (!actor_opt.has_value())
+ {
+ LOG_FAIL_FMT("Failed to extract actor from forwarded context.");
+ LOG_DEBUG_FMT(
+ "Failed to extract actor from forwarded context. Method is "
+ "'{}'",
+ ctx->get_method());
+ }
+
+ const auto& actor_s = actor_opt.value();
+ auto actor = rpc_map_shared->resolve(actor_s);
+ auto handler = rpc_map_shared->find(actor);
+ if (actor == ccf::ActorsType::unknown || !handler.has_value())
+ {
+ LOG_FAIL_FMT(
+ "Failed to process forwarded command: unknown actor");
+ LOG_DEBUG_FMT(
+ "Failed to process forwarded command: unknown actor {}",
+ actor_s);
+ return false;
+ }
+
+ auto fwd_handler =
+ dynamic_cast<ForwardedRpcHandler*>(handler.value().get());
+ if (!fwd_handler)
+ {
+ LOG_FAIL_FMT(
+ "Failed to process forwarded command: handler is not a "
+ "ForwardedRpcHandler");
+ return false;
+ }
+ }
+};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment