Skip to content

Commit

Permalink
rpcdaemon: manage http_compression as erigon (#2763)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored Mar 4, 2025
1 parent 8dd192d commit c05e57f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion silkworm/rpc/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void Daemon::start() {

return std::make_unique<http::Server>(
end_point, std::move(make_jsonrpc_handler), ioc, worker_pool_, settings_.cors_domain, std::move(jwt_secret),
settings_.use_websocket, settings_.ws_compression, settings_.http_compression);
settings_.use_websocket, settings_.ws_compression, settings_.http_compression, settings_.erigon_json_rpc_compatibility);
};

// Put the interface logs into the data folder
Expand Down
10 changes: 6 additions & 4 deletions silkworm/rpc/http/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ Connection::Connection(boost::asio::ip::tcp::socket socket,
bool ws_upgrade_enabled,
bool ws_compression,
bool http_compression,
WorkerPool& workers)
WorkerPool& workers,
bool erigon_json_rpc_compatibility)
: socket_{std::move(socket)},
handler_factory_{handler_factory},
handler_{handler_factory_(this)},
Expand All @@ -67,7 +68,8 @@ Connection::Connection(boost::asio::ip::tcp::socket socket,
ws_upgrade_enabled_{ws_upgrade_enabled},
ws_compression_{ws_compression},
http_compression_{http_compression},
workers_{workers} {
workers_{workers},
erigon_json_rpc_compatibility_{erigon_json_rpc_compatibility} {
socket_.set_option(boost::asio::ip::tcp::socket::keep_alive(true));
SILK_TRACE << "Connection::Connection created for " << socket_.remote_endpoint();
}
Expand Down Expand Up @@ -185,12 +187,12 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req) {
}

const auto accept_encoding = req[boost::beast::http::field::accept_encoding];
if (!http_compression_ && !accept_encoding.empty()) {
if (!http_compression_ && !accept_encoding.empty() && !erigon_json_rpc_compatibility_) {
co_await do_write("unsupported compression\n", boost::beast::http::status::unsupported_media_type, "identity");
co_return;
}

gzip_encoding_requested_ = accept_encoding.contains(kGzipEncoding);
gzip_encoding_requested_ = accept_encoding.contains(kGzipEncoding) && http_compression_;
if (http_compression_ && !accept_encoding.empty() && !accept_encoding.contains(kIdentity) && !gzip_encoding_requested_) {
co_await do_write("unsupported requested compression\n", boost::beast::http::status::unsupported_media_type, kGzipEncoding);
co_return;
Expand Down
5 changes: 4 additions & 1 deletion silkworm/rpc/http/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Connection : public StreamWriter {
bool ws_upgrade_enabled,
bool ws_compression,
bool http_compression,
WorkerPool& workers);
WorkerPool& workers,
bool erigon_json_rpc_compatibility);
~Connection() override;

/* StreamWriter Interface */
Expand Down Expand Up @@ -128,6 +129,8 @@ class Connection : public StreamWriter {
std::string vary_;
std::string origin_;
boost::beast::http::verb method_{boost::beast::http::verb::unknown};

bool erigon_json_rpc_compatibility_{false};
};

} // namespace silkworm::rpc::http
20 changes: 15 additions & 5 deletions silkworm/rpc/http/connection_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ TEST_CASE("connection creation", "[rpc][http][connection]") {
handler_factory,
allowed_origins,
std::move(jwt_secret),
false,
false,
false,
workers});
/*ws_upgrade_enabled=*/false,
/*ws_compression=*/false,
/*http_compression=*/false,
workers,
/*erigon_json_rpc_compatibility=*/true});
}
}

Expand Down Expand Up @@ -92,7 +93,16 @@ TEST_CASE("is_request_authorized", "[rpc][http][connection]") {
ConnectionForTest connection = [&]() -> ConnectionForTest {
boost::asio::ip::tcp::socket socket{ioc};
socket.open(boost::asio::ip::tcp::v4());
return {std::move(socket), handler_factory, allowed_origins, jwt_secret, false, false, false, workers};
return {
std::move(socket),
handler_factory,
allowed_origins,
jwt_secret,
/*ws_upgrade_enabled=*/false,
/*ws_compression=*/false,
/*http_compression=*/false,
workers,
/*erigon_json_rpc_compatibility=*/true};
}();

SECTION("no HTTP Authorization header") {
Expand Down
9 changes: 6 additions & 3 deletions silkworm/rpc/http/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ Server::Server(const std::string& end_point,
std::optional<std::string> jwt_secret,
bool use_websocket,
bool ws_compression,
bool http_compression)
bool http_compression,
bool erigon_json_rpc_compatibility)
: handler_factory_{std::move(handler_factory)},
acceptor_{ioc},
allowed_origins_{std::move(allowed_origins)},
jwt_secret_(std::move(jwt_secret)),
use_websocket_{use_websocket},
ws_compression_{ws_compression},
http_compression_{http_compression},
workers_{workers} {
workers_{workers},
erigon_json_rpc_compatibility_{erigon_json_rpc_compatibility} {
const auto [host, port] = parse_endpoint(end_point);

// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
Expand Down Expand Up @@ -91,7 +93,8 @@ Task<void> Server::run() {
SILK_TRACE << "Server::run accepted connection from " << socket.remote_endpoint();

auto new_connection = std::make_shared<Connection>(
std::move(socket), handler_factory_, allowed_origins_, jwt_secret_, use_websocket_, ws_compression_, http_compression_, workers_);
std::move(socket), handler_factory_, allowed_origins_, jwt_secret_,
use_websocket_, ws_compression_, http_compression_, workers_, erigon_json_rpc_compatibility_);
boost::asio::co_spawn(this_executor, Connection::run_read_loop(new_connection), boost::asio::detached);
}
} catch (const boost::system::system_error& se) {
Expand Down
6 changes: 5 additions & 1 deletion silkworm/rpc/http/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class Server {
std::optional<std::string> jwt_secret,
bool use_websocket,
bool ws_compression,
bool http_compression);
bool http_compression,
bool erigon_json_rpc_compatibility);

void start();

Expand Down Expand Up @@ -80,6 +81,9 @@ class Server {

//! The configured workers
WorkerPool& workers_;

//! Flag indicating if JSON-RPC compatibility with Erigon is enabled or not
bool erigon_json_rpc_compatibility_;
};

} // namespace silkworm::rpc::http

0 comments on commit c05e57f

Please # to comment.