Skip to content

Commit

Permalink
rpcdaemon: erigon_getBlockByTimestamp using glaze (#1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored Jan 5, 2024
1 parent e52c41b commit 3c670a8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
12 changes: 6 additions & 6 deletions silkworm/rpc/commands/erigon_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ Task<void> ErigonRpcApi::handle_erigon_get_balance_changes_in_block(const nlohma
}

// https://eth.wiki/json-rpc/API#erigon_getBlockByTimestamp
Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::json& request, nlohmann::json& reply) {
Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::json& request, std::string& reply) {
// Decode request parameters
const auto& params = request["params"];
if (params.size() != 2) {
auto error_msg = "invalid erigon_getBlockByTimestamp params: " + params.dump();
SILK_ERROR << error_msg;
reply = make_json_error(request, 100, error_msg);
make_glaze_json_error(request, 100, error_msg, reply);
co_return;
}
const auto block_timestamp = params[0].get<std::string>();
Expand Down Expand Up @@ -164,20 +164,20 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
if (!block_with_hash) {
const std::string error_msg = "block not found ";
SILK_ERROR << "erigon_get_block_by_timestamè: core::read_block_by_number: " << error_msg << request.dump();
reply = make_json_error(request, 100, error_msg);
make_glaze_json_error(request, 100, error_msg, reply);
co_await tx->close(); // RAII not (yet) available with coroutines
co_return;
}
const auto total_difficulty = co_await chain_storage->read_total_difficulty(block_with_hash->hash, block_number);
const Block extended_block{block_with_hash, *total_difficulty, full_tx};

reply = make_json_content(request, extended_block);
make_glaze_json_content(request, extended_block, reply);
} catch (const std::exception& e) {
SILK_ERROR << "exception: " << e.what() << " processing request: " << request.dump();
reply = make_json_error(request, 100, e.what());
make_glaze_json_error(request, 100, e.what(), reply);
} catch (...) {
SILK_ERROR << "unexpected exception processing request: " << request.dump();
reply = make_json_error(request, 100, "unexpected exception");
make_glaze_json_error(request, 100, "unexpected exception", reply);
}

// Close remote database transaction, RAII not available with coroutines
Expand Down
4 changes: 3 additions & 1 deletion silkworm/rpc/commands/erigon_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class ErigonRpcApi {
Task<void> handle_erigon_block_number(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_cache_check(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_get_balance_changes_in_block(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_get_block_by_timestamp(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_get_block_receipts_by_block_hash(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_get_header_by_hash(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_get_header_by_number(const nlohmann::json& request, nlohmann::json& reply);
Expand All @@ -62,6 +61,9 @@ class ErigonRpcApi {
Task<void> handle_erigon_cumulative_chain_traffic(const nlohmann::json& request, nlohmann::json& reply);
Task<void> handle_erigon_node_info(const nlohmann::json& request, nlohmann::json& reply);

// GLAZE
Task<void> handle_erigon_get_block_by_timestamp(const nlohmann::json& request, std::string& reply);

private:
BlockCache* block_cache_;
ethdb::Database* database_;
Expand Down
20 changes: 8 additions & 12 deletions silkworm/rpc/commands/erigon_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ErigonRpcApi_ForTest : public ErigonRpcApi {
explicit ErigonRpcApi_ForTest(boost::asio::io_context& io_context) : ErigonRpcApi{io_context} {}

// MSVC doesn't support using access declarations properly, so explicitly forward these public accessors
Task<void> erigon_get_block_by_timestamp(const nlohmann::json& request, nlohmann::json& reply) {
Task<void> erigon_get_block_by_timestamp(const nlohmann::json& request, std::string& reply) {
co_return co_await ErigonRpcApi::handle_erigon_get_block_by_timestamp(request, reply);
}
Task<void> erigon_get_header_by_hash(const nlohmann::json& request, nlohmann::json& reply) {
Expand Down Expand Up @@ -62,7 +62,8 @@ using ErigonRpcApiTest = test::JsonApiTestBase<ErigonRpcApi_ForTest>;

#ifndef SILKWORM_SANITIZE
TEST_CASE_METHOD(ErigonRpcApiTest, "ErigonRpcApi::handle_erigon_get_block_by_timestamp", "[rpc][erigon_api]") {
nlohmann::json reply;
std::string reply;
nlohmann::json exp_rsp;

SECTION("request params is empty: return error") {
CHECK_NOTHROW(run<&ErigonRpcApi_ForTest::erigon_get_block_by_timestamp>(
Expand All @@ -73,11 +74,9 @@ TEST_CASE_METHOD(ErigonRpcApiTest, "ErigonRpcApi::handle_erigon_get_block_by_tim
"params":[]
})"_json,
reply));
CHECK(reply == R"({
"jsonrpc":"2.0",
"id":1,
"error":{"code":100,"message":"invalid erigon_getBlockByTimestamp params: []"}
})"_json);

std::string expected_rsp{"{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":100,\"message\":\"invalid erigon_getBlockByTimestamp params: []\"}}"};
CHECK(reply == expected_rsp);
}
SECTION("request params are incomplete: return error") {
CHECK_NOTHROW(run<&ErigonRpcApi_ForTest::erigon_get_block_by_timestamp>(
Expand All @@ -93,11 +92,8 @@ TEST_CASE_METHOD(ErigonRpcApiTest, "ErigonRpcApi::handle_erigon_get_block_by_tim
"id":1,
"error":{"code":100,"message":"invalid erigon_getBlockByTimestamp params: [\"1658865942\"]"}
})"_json;
CHECK(reply == R"({
"jsonrpc":"2.0",
"id":1,
"error":{"code":100,"message":"invalid erigon_getBlockByTimestamp params: [\"1658865942\"]"}
})"_json);
std::string expected_rsp{"{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":100,\"message\":\"invalid erigon_getBlockByTimestamp params: [\\\"1658865942\\\"]\"}}"};
CHECK(reply == expected_rsp);
}
SECTION("request 1st param is invalid: return error") {
CHECK_THROWS_AS(run<&ErigonRpcApi_ForTest::erigon_get_block_by_timestamp>(
Expand Down
4 changes: 3 additions & 1 deletion silkworm/rpc/commands/rpc_api_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ void RpcApiTable::add_erigon_handlers() {
method_handlers_[http::method::k_erigon_blockNumber] = &commands::RpcApi::handle_erigon_block_number;
method_handlers_[http::method::k_erigon_cacheCheck] = &commands::RpcApi::handle_erigon_cache_check;
method_handlers_[http::method::k_erigon_getBalanceChangesInBlock] = &commands::RpcApi::handle_erigon_get_balance_changes_in_block;
method_handlers_[http::method::k_erigon_getBlockByTimestamp] = &commands::RpcApi::handle_erigon_get_block_by_timestamp;
method_handlers_[http::method::k_erigon_getBlockReceiptsByBlockHash] = &commands::RpcApi::handle_erigon_get_block_receipts_by_block_hash;
method_handlers_[http::method::k_erigon_getHeaderByHash] = &commands::RpcApi::handle_erigon_get_header_by_hash;
method_handlers_[http::method::k_erigon_getHeaderByNumber] = &commands::RpcApi::handle_erigon_get_header_by_number;
Expand All @@ -196,6 +195,9 @@ void RpcApiTable::add_erigon_handlers() {
method_handlers_[http::method::k_erigon_watchTheBurn] = &commands::RpcApi::handle_erigon_watch_the_burn;
method_handlers_[http::method::k_erigon_cumulative_chain_traffic] = &commands::RpcApi::handle_erigon_cumulative_chain_traffic;
method_handlers_[http::method::k_erigon_nodeInfo] = &commands::RpcApi::handle_erigon_node_info;

// GLAZE methods
method_handlers_glaze_[http::method::k_erigon_getBlockByTimestamp] = &commands::RpcApi::handle_erigon_get_block_by_timestamp;
}

void RpcApiTable::add_trace_handlers() {
Expand Down

0 comments on commit 3c670a8

Please sign in to comment.