Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcademon: fix block header serialization for erigon_getHeaderByHash #1985

Merged
merged 7 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions silkworm/rpc/commands/erigon_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Task<void> ErigonRpcApi::handle_erigon_get_block_by_timestamp(const nlohmann::js
const auto block_with_hash = co_await core::read_block_by_number(*block_cache_, *chain_storage, block_number);
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();
SILK_ERROR << "erigon_get_block_by_timestamp: core::read_block_by_number: " << error_msg << request.dump();
make_glaze_json_error(request, 100, error_msg, reply);
co_await tx->close(); // RAII not (yet) available with coroutines
co_return;
Expand Down Expand Up @@ -259,7 +259,8 @@ Task<void> ErigonRpcApi::handle_erigon_get_header_by_hash(const nlohmann::json&

const auto header{co_await chain_storage->read_header(block_hash)};
if (!header) {
reply = make_json_error(request, 100, "block not found");
auto error_msg = "block header not found: 0x" + silkworm::to_hex(block_hash);
reply = make_json_error(request, -32000, error_msg);
} else {
reply = make_json_content(request, *header);
}
Expand Down Expand Up @@ -305,7 +306,8 @@ Task<void> ErigonRpcApi::handle_erigon_get_header_by_number(const nlohmann::json
const auto header{co_await chain_storage->read_canonical_header(block_number)};

if (!header) {
reply = make_json_error(request, 100, "block not found");
const auto error_msg = "block header not found: " + std::to_string(block_number);
reply = make_json_error(request, -32000, error_msg);
} else {
reply = make_json_content(request, *header);
}
Expand Down
17 changes: 16 additions & 1 deletion silkworm/rpc/json/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <silkworm/core/types/address.hpp>
#include <silkworm/core/types/evmc_bytes32.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/rpc/common/compatibility.hpp>
#include <silkworm/rpc/common/util.hpp>

namespace silkworm::rpc {
Expand Down Expand Up @@ -226,7 +227,21 @@ void to_json(nlohmann::json& json, const BlockHeader& header) {
} else {
json["baseFeePerGas"] = nullptr;
}
json["withdrawalsRoot"] = nullptr; // waiting EIP-4895
if (rpc::compatibility::is_erigon_json_api_compatibility_required()) {
json["AuRaSeal"] = nullptr;
json["AuRaStep"] = 0;
json["Verkle"] = false;
json["VerkleKeyVals"] = nullptr;
json["VerkleProof"] = nullptr;
}
json["blobGasUsed"] = nullptr;
json["excessBlobGas"] = nullptr;
json["parentBeaconBlockRoot"] = nullptr;
if (header.withdrawals_root) {
json["withdrawalsRoot"] = *header.withdrawals_root;
} else {
json["withdrawalsRoot"] = nullptr;
}
}

} // namespace silkworm
Expand Down
34 changes: 31 additions & 3 deletions silkworm/rpc/json/types_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <silkworm/db/access_layer.hpp>
#include <silkworm/infra/common/decoding_exception.hpp>
#include <silkworm/infra/test_util/log.hpp>
#include <silkworm/rpc/common/compatibility.hpp>

namespace silkworm::rpc {

Expand Down Expand Up @@ -162,6 +163,7 @@ TEST_CASE("serialize non-empty bytes32", "[rpc][to_json]") {

TEST_CASE("serialize empty block header", "[rpc][to_json]") {
silkworm::BlockHeader header{};
rpc::compatibility::set_erigon_json_api_compatibility_required(true);
nlohmann::json j = header;
CHECK(j == R"({
"baseFeePerGas":null,
Expand All @@ -187,11 +189,20 @@ TEST_CASE("serialize empty block header", "[rpc][to_json]") {
"timestamp":"0x0",
"extraData":"0x",
"mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000",
"withdrawalsRoot":null
"withdrawalsRoot":null,
"AuRaSeal":null,
"AuRaStep":0,
"Verkle":false,
"VerkleKeyVals":null,
"VerkleProof":null,
"blobGasUsed":null,
"excessBlobGas": null,
"parentBeaconBlockRoot": null
})"_json);
}

TEST_CASE("serialize block header", "[rpc][to_json]") {
rpc::compatibility::set_erigon_json_api_compatibility_required(true);
silkworm::BlockHeader header{
0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c_bytes32,
0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d_bytes32,
Expand Down Expand Up @@ -233,11 +244,20 @@ TEST_CASE("serialize block header", "[rpc][to_json]") {
"extraData":"0x0001ff0100",
"mixHash":"0x0000000000000000000000000000000000000000000000000000000000000001",
"nonce":"0x00000000000000ff",
"withdrawalsRoot":null
"withdrawalsRoot":null,
"AuRaSeal":null,
"AuRaStep":0,
"Verkle":false,
"VerkleKeyVals":null,
"VerkleProof":null,
"blobGasUsed":null,
"excessBlobGas": null,
"parentBeaconBlockRoot": null
})"_json);
}

TEST_CASE("serialize block header with baseFeePerGas", "[rpc][to_json]") {
rpc::compatibility::set_erigon_json_api_compatibility_required(true);
silkworm::BlockHeader header{
0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c_bytes32,
0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d_bytes32,
Expand Down Expand Up @@ -282,7 +302,15 @@ TEST_CASE("serialize block header with baseFeePerGas", "[rpc][to_json]") {
"mixHash":"0x0000000000000000000000000000000000000000000000000000000000000001",
"nonce":"0x0102030405060708",
"baseFeePerGas":"0x3e8",
"withdrawalsRoot":null
"withdrawalsRoot":null,
"AuRaSeal":null,
"AuRaStep":0,
"Verkle":false,
"VerkleKeyVals":null,
"VerkleProof":null,
"blobGasUsed":null,
"excessBlobGas": null,
"parentBeaconBlockRoot": null
})"_json);
}

Expand Down
Loading