From 14ee5d25f2a71c9265faf8f772770c7938dcca16 Mon Sep 17 00:00:00 2001 From: yarkin Date: Mon, 5 Feb 2024 11:47:54 +0800 Subject: [PATCH] Add consensus_parameter_index field to block --- silkworm/core/types/block.cpp | 17 ++++++++- silkworm/core/types/block.hpp | 3 ++ silkworm/core/types/block_test.cpp | 19 ++++++++++ silkworm/node/db/util_test.cpp | 1 + silkworm/silkrpc/json/types.cpp | 5 +++ silkworm/silkrpc/json/types_test.cpp | 55 +++++++++++++++++++++++++++- 6 files changed, 98 insertions(+), 2 deletions(-) diff --git a/silkworm/core/types/block.cpp b/silkworm/core/types/block.cpp index e28d1e65e..ad48ab964 100644 --- a/silkworm/core/types/block.cpp +++ b/silkworm/core/types/block.cpp @@ -29,7 +29,7 @@ bool operator==(const BlockHeader& a, const BlockHeader& b) { a.receipts_root == b.receipts_root && a.logs_bloom == b.logs_bloom && a.difficulty == b.difficulty && a.number == b.number && a.gas_limit == b.gas_limit && a.gas_used == b.gas_used && a.timestamp == b.timestamp && a.extra_data == b.extra_data && a.prev_randao == b.prev_randao && - a.nonce == b.nonce && a.base_fee_per_gas == b.base_fee_per_gas; + a.nonce == b.nonce && a.base_fee_per_gas == b.base_fee_per_gas && a.consensus_parameter_index == b.consensus_parameter_index; } bool operator==(const BlockBody& a, const BlockBody& b) { @@ -110,6 +110,9 @@ namespace rlp { rlp_head.payload_length += kHashLength + 1; // prev_randao rlp_head.payload_length += 8 + 1; // nonce } + if (header.consensus_parameter_index) { + rlp_head.payload_length += length(*header.consensus_parameter_index); + } if (header.base_fee_per_gas) { rlp_head.payload_length += length(*header.base_fee_per_gas); } @@ -155,6 +158,9 @@ namespace rlp { encode(to, header.prev_randao); encode(to, header.nonce); } + if (header.consensus_parameter_index) { + encode(to, *header.consensus_parameter_index); + } if (header.base_fee_per_gas) { encode(to, *header.base_fee_per_gas); } @@ -202,6 +208,15 @@ namespace rlp { return res; } + if (from.length() > leftover) { + to.consensus_parameter_index = 0; + if (DecodingResult res{decode(from, *to.consensus_parameter_index, Leftover::kAllow)}; !res) { + return res; + } + } else { + to.consensus_parameter_index = std::nullopt; + } + if (from.length() > leftover) { to.base_fee_per_gas = 0; if (DecodingResult res{decode(from, *to.base_fee_per_gas, Leftover::kAllow)}; !res) { diff --git a/silkworm/core/types/block.hpp b/silkworm/core/types/block.hpp index 74e97a961..67757f9c1 100644 --- a/silkworm/core/types/block.hpp +++ b/silkworm/core/types/block.hpp @@ -70,6 +70,9 @@ struct BlockHeader { evmc::bytes32 prev_randao{}; // mix hash prior to EIP-4399 NonceType nonce{}; + // EOS-EVM + std::optional consensus_parameter_index{std::nullopt}; + std::optional base_fee_per_gas{std::nullopt}; // EIP-1559 std::optional withdrawals_root{std::nullopt}; // EIP-4895 diff --git a/silkworm/core/types/block_test.cpp b/silkworm/core/types/block_test.cpp index 674ed9426..7b4c98432 100644 --- a/silkworm/core/types/block_test.cpp +++ b/silkworm/core/types/block_test.cpp @@ -172,9 +172,27 @@ TEST_CASE("EIP-2718 Block RLP") { CHECK(block.transactions[1].access_list.size() == 1); } +TEST_CASE("Consensus Parameters Header RLP") { + BlockHeader h{ + .number = 13'500'000, + .consensus_parameter_index= 1234, + }; + + Bytes rlp; + rlp::encode(rlp, h); + + ByteView view{rlp}; + BlockHeader decoded; + REQUIRE(rlp::decode(view, decoded)); + + CHECK(view.empty()); + CHECK(decoded == h); +} + TEST_CASE("EIP-1559 Header RLP") { BlockHeader h{ .number = 13'500'000, + .consensus_parameter_index= 0, .base_fee_per_gas = 2'700'000'000, }; @@ -194,6 +212,7 @@ TEST_CASE("EIP-4844 Header RLP") { .ommers_hash = kEmptyListHash, .number = 17'000'000, .prev_randao = 0xd01681d2b3acdebff0288a02a1648b3910500961982d5ecdbef064af7c34090b_bytes32, + .consensus_parameter_index= 0, .base_fee_per_gas = 2'700'000'000, .withdrawals_root = 0xbac9348581b0ee244d6eb61076b63c4e4afa70430c804ab0e6a0ab69d9a9d323_bytes32, .data_gas_used = 456, diff --git a/silkworm/node/db/util_test.cpp b/silkworm/node/db/util_test.cpp index 0d28ec8f9..38a4c24ce 100644 --- a/silkworm/node/db/util_test.cpp +++ b/silkworm/node/db/util_test.cpp @@ -36,6 +36,7 @@ TEST_CASE("BlockBodyForStorage encoding") { .extra_data = *from_hex("0001FF0100"), .prev_randao = 0x0000000000000000000000000000000000000000000000000000000000000001_bytes32, .nonce = {0, 0, 0, 0, 0, 0, 0, 255}, + .consensus_parameter_index = 0x1234, .base_fee_per_gas = 0x244428, }; diff --git a/silkworm/silkrpc/json/types.cpp b/silkworm/silkrpc/json/types.cpp index 34c7776c5..2359b6f19 100644 --- a/silkworm/silkrpc/json/types.cpp +++ b/silkworm/silkrpc/json/types.cpp @@ -211,6 +211,11 @@ void to_json(nlohmann::json& json, const BlockHeader& header) { json["gasLimit"] = rpc::to_quantity(header.gas_limit); json["gasUsed"] = rpc::to_quantity(header.gas_used); json["timestamp"] = rpc::to_quantity(header.timestamp); + if (header.consensus_parameter_index.has_value()) { + json["consensusParameterIndex"] = rpc::to_quantity(header.consensus_parameter_index.value_or(0)); + } else { + json["consensusParameterIndex"] = nullptr; + } if (header.base_fee_per_gas.has_value()) { json["baseFeePerGas"] = rpc::to_quantity(header.base_fee_per_gas.value_or(0)); } else { diff --git a/silkworm/silkrpc/json/types_test.cpp b/silkworm/silkrpc/json/types_test.cpp index 593f75d0d..749f9d6d2 100644 --- a/silkworm/silkrpc/json/types_test.cpp +++ b/silkworm/silkrpc/json/types_test.cpp @@ -164,6 +164,7 @@ TEST_CASE("serialize empty block header", "[silkrpc][to_json]") { nlohmann::json j = header; CHECK(j == R"({ "baseFeePerGas":null, + "consensusParameterIndex":null, "hash": "0xc3bd2d00745c03048a5616146a96f5ff78e54efb9e5b04af208cdaff6f3830ee", "parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000", "sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000", @@ -210,6 +211,7 @@ TEST_CASE("serialize block header", "[silkrpc][to_json]") { nlohmann::json j = header; CHECK(j == R"({ "baseFeePerGas":null, + "consensusParameterIndex":null, "hash": "0x5e053b099d472a3fc02394243961937ffa008bad0daa81a984a0830ba0beee01", "parentHash":"0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c", "sha3Uncles":"0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d", @@ -236,6 +238,55 @@ TEST_CASE("serialize block header", "[silkrpc][to_json]") { })"_json); } +TEST_CASE("serialize block header with consensus parameter index", "[silkrpc][to_json]") { + silkworm::BlockHeader header{ + 0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c_bytes32, + 0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d_bytes32, + 0x0715a7794a1dc8e42615f059dd6e406a6594651a_address, + 0xb02a3b0ee16c858afaa34bcd6770b3c20ee56aa2f75858733eb0e927b5b7126d_bytes32, + 0xb02a3b0ee16c858afaa34bcd6770b3c20ee56aa2f75858733eb0e927b5b7126e_bytes32, + 0xb02a3b0ee16c858afaa34bcd6770b3c20ee56aa2f75858733eb0e927b5b7126f_bytes32, + silkworm::Bloom{}, + intx::uint256{0}, + uint64_t(5), + uint64_t(1000000), + uint64_t(1000000), + uint64_t(5405021), + *silkworm::from_hex("0001FF0100"), // extradata + 0x0000000000000000000000000000000000000000000000000000000000000001_bytes32, // mixhash + {1, 2, 3, 4, 5, 6, 7, 8}, // nonce + std::optional(1001), // consensus_parameter_index // base_fee_per_gas + }; + nlohmann::json j = header; + CHECK(j == R"({ + "baseFeePerGas":null, + "hash": "0x74b164b2a76408c5c0a69c99325438a90ece1353817ef6e239fc0dcb7ac76d09", + "parentHash":"0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c", + "sha3Uncles":"0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d", + "miner":"0x0715a7794a1dc8e42615f059dd6e406a6594651a", + "stateRoot":"0xb02a3b0ee16c858afaa34bcd6770b3c20ee56aa2f75858733eb0e927b5b7126d", + "transactionsRoot":"0xb02a3b0ee16c858afaa34bcd6770b3c20ee56aa2f75858733eb0e927b5b7126e", + "receiptsRoot":"0xb02a3b0ee16c858afaa34bcd6770b3c20ee56aa2f75858733eb0e927b5b7126f", + "logsBloom":"0x000000000000000000000000000000000000000000000000000000000000000000000000)" + R"(000000000000000000000000000000000000000000000000000000000000000000000000)" + R"(000000000000000000000000000000000000000000000000000000000000000000000000)" + R"(000000000000000000000000000000000000000000000000000000000000000000000000)" + R"(000000000000000000000000000000000000000000000000000000000000000000000000)" + R"(000000000000000000000000000000000000000000000000000000000000000000000000)" + R"(00000000000000000000000000000000000000000000000000000000000000000000000000000000", + "difficulty":"0x0", + "number":"0x5", + "gasLimit":"0xf4240", + "gasUsed":"0xf4240", + "timestamp":"0x52795d", + "extraData":"0x0001ff0100", + "mixHash":"0x0000000000000000000000000000000000000000000000000000000000000001", + "nonce":"0x0102030405060708", + "consensusParameterIndex":"0x3e9", + "withdrawalsRoot":null + })"_json); +} + TEST_CASE("serialize block header with baseFeePerGas", "[silkrpc][to_json]") { silkworm::BlockHeader header{ 0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c_bytes32, @@ -253,12 +304,13 @@ TEST_CASE("serialize block header with baseFeePerGas", "[silkrpc][to_json]") { *silkworm::from_hex("0001FF0100"), // extradata 0x0000000000000000000000000000000000000000000000000000000000000001_bytes32, // mixhash {1, 2, 3, 4, 5, 6, 7, 8}, // nonce + std::optional(1001), // consensus_parameter_index std::optional(1000), // base_fee_per_gas }; nlohmann::json j = header; CHECK(j == R"({ "baseFeePerGas":"0x3e8", - "hash": "0x5e3a9484b3ee70cc9ae7673051efd0369cfa4126430075921c70255cbdefbe6", + "hash": "0xa88494b78d8cb3ad596637990fa04fd4e9068a9271f044e4d3cdb313a24973b9", "parentHash":"0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c", "sha3Uncles":"0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d", "miner":"0x0715a7794a1dc8e42615f059dd6e406a6594651a", @@ -280,6 +332,7 @@ TEST_CASE("serialize block header with baseFeePerGas", "[silkrpc][to_json]") { "extraData":"0x0001ff0100", "mixHash":"0x0000000000000000000000000000000000000000000000000000000000000001", "nonce":"0x0102030405060708", + "consensusParameterIndex":"0x3e9", "baseFeePerGas":"0x3e8", "withdrawalsRoot":null })"_json);