Skip to content

Commit

Permalink
rpcdaemon: fix JSON for empty logs in erigon_getLogsByHash (#1997)
Browse files Browse the repository at this point in the history
  • Loading branch information
lupin012 authored May 3, 2024
1 parent 68f8a3f commit 00a9daf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
5 changes: 2 additions & 3 deletions silkworm/rpc/commands/erigon_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,8 @@ Task<void> ErigonRpcApi::handle_erigon_get_logs_by_hash(const nlohmann::json& re

const auto block_with_hash = co_await core::read_block_by_hash(*block_cache_, *chain_storage, block_hash);
if (!block_with_hash) {
const std::string error_msg = "block not found ";
SILK_ERROR << "erigon_get_logs_by_hash: core::read_block_by_hash: " << error_msg << request.dump();
reply = make_json_error(request, 100, error_msg);
std::vector<Logs> logs{};
reply = make_json_content(request, logs);
co_await tx->close(); // RAII not (yet) available with coroutines
co_return;
}
Expand Down
16 changes: 16 additions & 0 deletions silkworm/rpc/json/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@

namespace silkworm::rpc {

void to_json(nlohmann::json& json, const std::vector<Logs>& logs) {
json = nlohmann::json::array();
for (std::size_t k{0}; k < logs.size(); k++) {
auto& inner_logs{logs[k]};
nlohmann::basic_json inner_json = nlohmann::json::array();
if (inner_logs.empty()) {
inner_json = nullptr;
} else {
for (std::size_t i{0}; i < inner_logs.size(); i++) {
inner_json.push_back(inner_logs[i]);
}
}
json.push_back(inner_json);
}
}

void to_json(nlohmann::json& json, const Log& log) {
json["address"] = log.address;
json["topics"] = log.topics;
Expand Down
1 change: 1 addition & 0 deletions silkworm/rpc/json/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace silkworm::rpc {

void from_json(const nlohmann::json& json, Log& log);
void to_json(nlohmann::json& json, const Log& log);
void to_json(nlohmann::json& json, const std::vector<Logs>& logs);

void make_glaze_json_content(const nlohmann::json& request_json, const Logs& logs, std::string& json_reply);

Expand Down

0 comments on commit 00a9daf

Please sign in to comment.