Skip to content

Commit

Permalink
Add consensus_parameter_index field to block
Browse files Browse the repository at this point in the history
  • Loading branch information
yarkinwho committed Feb 5, 2024
1 parent 660190a commit 14ee5d2
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
17 changes: 16 additions & 1 deletion silkworm/core/types/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions silkworm/core/types/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ struct BlockHeader {
evmc::bytes32 prev_randao{}; // mix hash prior to EIP-4399
NonceType nonce{};

// EOS-EVM
std::optional<uint64_t> consensus_parameter_index{std::nullopt};

std::optional<intx::uint256> base_fee_per_gas{std::nullopt}; // EIP-1559
std::optional<evmc::bytes32> withdrawals_root{std::nullopt}; // EIP-4895

Expand Down
19 changes: 19 additions & 0 deletions silkworm/core/types/block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions silkworm/node/db/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
5 changes: 5 additions & 0 deletions silkworm/silkrpc/json/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
55 changes: 54 additions & 1 deletion silkworm/silkrpc/json/types_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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<uint64_t>(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,
Expand All @@ -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<uint64_t>(1001), // consensus_parameter_index
std::optional<intx::uint256>(1000), // base_fee_per_gas
};
nlohmann::json j = header;
CHECK(j == R"({
"baseFeePerGas":"0x3e8",
"hash": "0x5e3a9484b3ee70cc9ae7673051efd0369cfa4126430075921c70255cbdefbe6",
"hash": "0xa88494b78d8cb3ad596637990fa04fd4e9068a9271f044e4d3cdb313a24973b9",
"parentHash":"0x374f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126c",
"sha3Uncles":"0x474f3a049e006f36f6cf91b02a3b0ee16c858af2f75858733eb0e927b5b7126d",
"miner":"0x0715a7794a1dc8e42615f059dd6e406a6594651a",
Expand All @@ -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);
Expand Down

0 comments on commit 14ee5d2

Please sign in to comment.