Skip to content

Commit c05e57f

Browse files
authored
rpcdaemon: manage http_compression as erigon (#2763)
1 parent 8dd192d commit c05e57f

File tree

6 files changed

+37
-15
lines changed

6 files changed

+37
-15
lines changed

silkworm/rpc/daemon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ void Daemon::start() {
348348

349349
return std::make_unique<http::Server>(
350350
end_point, std::move(make_jsonrpc_handler), ioc, worker_pool_, settings_.cors_domain, std::move(jwt_secret),
351-
settings_.use_websocket, settings_.ws_compression, settings_.http_compression);
351+
settings_.use_websocket, settings_.ws_compression, settings_.http_compression, settings_.erigon_json_rpc_compatibility);
352352
};
353353

354354
// Put the interface logs into the data folder

silkworm/rpc/http/connection.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ Connection::Connection(boost::asio::ip::tcp::socket socket,
5858
bool ws_upgrade_enabled,
5959
bool ws_compression,
6060
bool http_compression,
61-
WorkerPool& workers)
61+
WorkerPool& workers,
62+
bool erigon_json_rpc_compatibility)
6263
: socket_{std::move(socket)},
6364
handler_factory_{handler_factory},
6465
handler_{handler_factory_(this)},
@@ -67,7 +68,8 @@ Connection::Connection(boost::asio::ip::tcp::socket socket,
6768
ws_upgrade_enabled_{ws_upgrade_enabled},
6869
ws_compression_{ws_compression},
6970
http_compression_{http_compression},
70-
workers_{workers} {
71+
workers_{workers},
72+
erigon_json_rpc_compatibility_{erigon_json_rpc_compatibility} {
7173
socket_.set_option(boost::asio::ip::tcp::socket::keep_alive(true));
7274
SILK_TRACE << "Connection::Connection created for " << socket_.remote_endpoint();
7375
}
@@ -185,12 +187,12 @@ Task<void> Connection::handle_actual_request(const RequestWithStringBody& req) {
185187
}
186188

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

193-
gzip_encoding_requested_ = accept_encoding.contains(kGzipEncoding);
195+
gzip_encoding_requested_ = accept_encoding.contains(kGzipEncoding) && http_compression_;
194196
if (http_compression_ && !accept_encoding.empty() && !accept_encoding.contains(kIdentity) && !gzip_encoding_requested_) {
195197
co_await do_write("unsupported requested compression\n", boost::beast::http::status::unsupported_media_type, kGzipEncoding);
196198
co_return;

silkworm/rpc/http/connection.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class Connection : public StreamWriter {
6060
bool ws_upgrade_enabled,
6161
bool ws_compression,
6262
bool http_compression,
63-
WorkerPool& workers);
63+
WorkerPool& workers,
64+
bool erigon_json_rpc_compatibility);
6465
~Connection() override;
6566

6667
/* StreamWriter Interface */
@@ -128,6 +129,8 @@ class Connection : public StreamWriter {
128129
std::string vary_;
129130
std::string origin_;
130131
boost::beast::http::verb method_{boost::beast::http::verb::unknown};
132+
133+
bool erigon_json_rpc_compatibility_{false};
131134
};
132135

133136
} // namespace silkworm::rpc::http

silkworm/rpc/http/connection_test.cpp

+15-5
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ TEST_CASE("connection creation", "[rpc][http][connection]") {
5151
handler_factory,
5252
allowed_origins,
5353
std::move(jwt_secret),
54-
false,
55-
false,
56-
false,
57-
workers});
54+
/*ws_upgrade_enabled=*/false,
55+
/*ws_compression=*/false,
56+
/*http_compression=*/false,
57+
workers,
58+
/*erigon_json_rpc_compatibility=*/true});
5859
}
5960
}
6061

@@ -92,7 +93,16 @@ TEST_CASE("is_request_authorized", "[rpc][http][connection]") {
9293
ConnectionForTest connection = [&]() -> ConnectionForTest {
9394
boost::asio::ip::tcp::socket socket{ioc};
9495
socket.open(boost::asio::ip::tcp::v4());
95-
return {std::move(socket), handler_factory, allowed_origins, jwt_secret, false, false, false, workers};
96+
return {
97+
std::move(socket),
98+
handler_factory,
99+
allowed_origins,
100+
jwt_secret,
101+
/*ws_upgrade_enabled=*/false,
102+
/*ws_compression=*/false,
103+
/*http_compression=*/false,
104+
workers,
105+
/*erigon_json_rpc_compatibility=*/true};
96106
}();
97107

98108
SECTION("no HTTP Authorization header") {

silkworm/rpc/http/server.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ Server::Server(const std::string& end_point,
4848
std::optional<std::string> jwt_secret,
4949
bool use_websocket,
5050
bool ws_compression,
51-
bool http_compression)
51+
bool http_compression,
52+
bool erigon_json_rpc_compatibility)
5253
: handler_factory_{std::move(handler_factory)},
5354
acceptor_{ioc},
5455
allowed_origins_{std::move(allowed_origins)},
5556
jwt_secret_(std::move(jwt_secret)),
5657
use_websocket_{use_websocket},
5758
ws_compression_{ws_compression},
5859
http_compression_{http_compression},
59-
workers_{workers} {
60+
workers_{workers},
61+
erigon_json_rpc_compatibility_{erigon_json_rpc_compatibility} {
6062
const auto [host, port] = parse_endpoint(end_point);
6163

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

9395
auto new_connection = std::make_shared<Connection>(
94-
std::move(socket), handler_factory_, allowed_origins_, jwt_secret_, use_websocket_, ws_compression_, http_compression_, workers_);
96+
std::move(socket), handler_factory_, allowed_origins_, jwt_secret_,
97+
use_websocket_, ws_compression_, http_compression_, workers_, erigon_json_rpc_compatibility_);
9598
boost::asio::co_spawn(this_executor, Connection::run_read_loop(new_connection), boost::asio::detached);
9699
}
97100
} catch (const boost::system::system_error& se) {

silkworm/rpc/http/server.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class Server {
4848
std::optional<std::string> jwt_secret,
4949
bool use_websocket,
5050
bool ws_compression,
51-
bool http_compression);
51+
bool http_compression,
52+
bool erigon_json_rpc_compatibility);
5253

5354
void start();
5455

@@ -80,6 +81,9 @@ class Server {
8081

8182
//! The configured workers
8283
WorkerPool& workers_;
84+
85+
//! Flag indicating if JSON-RPC compatibility with Erigon is enabled or not
86+
bool erigon_json_rpc_compatibility_;
8387
};
8488

8589
} // namespace silkworm::rpc::http

0 commit comments

Comments
 (0)