From 9eab6dd9af40a27aafb396a0fcd7c9bd9076881b Mon Sep 17 00:00:00 2001 From: yarkin Date: Fri, 9 Feb 2024 09:19:45 +0800 Subject: [PATCH] Add missing parts and tests. --- silkworm/core/types/block_test.cpp | 2 ++ silkworm/node/db/access_layer.cpp | 2 ++ silkworm/node/db/access_layer_test.cpp | 3 +++ silkworm/node/db/util.cpp | 15 +++++++++++++++ silkworm/node/db/util.hpp | 2 ++ silkworm/node/db/util_test.cpp | 9 ++++++++- 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/silkworm/core/types/block_test.cpp b/silkworm/core/types/block_test.cpp index 674ed9426..bc86ddb34 100644 --- a/silkworm/core/types/block_test.cpp +++ b/silkworm/core/types/block_test.cpp @@ -104,6 +104,8 @@ TEST_CASE("BlockBody RLP 2") { body.ommers[0].prev_randao = 0xf0a53dfdd6c2f2a661e718ef29092de60d81d45f84044bec7bf4b36630b2bc08_bytes32; body.ommers[0].nonce[7] = 35; + body.consensus_parameter_index = 1234; + Bytes rlp{}; rlp::encode(rlp, body); diff --git a/silkworm/node/db/access_layer.cpp b/silkworm/node/db/access_layer.cpp index 6a8dda69e..c1a18ba4a 100644 --- a/silkworm/node/db/access_layer.cpp +++ b/silkworm/node/db/access_layer.cpp @@ -443,6 +443,7 @@ bool read_body(ROTxn& txn, const Bytes& key, bool read_senders, BlockBody& out) if (!out.transactions.empty() && read_senders) { parse_senders(txn, key, out.transactions); } + out.consensus_parameter_index = body.consensus_parameter_index; return true; } @@ -498,6 +499,7 @@ void write_body(RWTxn& txn, const BlockBody& body, const uint8_t (&hash)[kHashLe body_for_storage.txn_count = body.transactions.size(); body_for_storage.base_txn_id = increment_map_sequence(txn, table::kBlockTransactions.name, body_for_storage.txn_count); + body_for_storage.consensus_parameter_index = body.consensus_parameter_index; Bytes value{body_for_storage.encode()}; auto key{db::block_key(number, hash)}; diff --git a/silkworm/node/db/access_layer_test.cpp b/silkworm/node/db/access_layer_test.cpp index 74ded4b10..c67c76c7a 100644 --- a/silkworm/node/db/access_layer_test.cpp +++ b/silkworm/node/db/access_layer_test.cpp @@ -81,6 +81,7 @@ static BlockBody sample_block_body() { body.ommers[0].prev_randao = 0xf0a53dfdd6c2f2a661e718ef29092de60d81d45f84044bec7bf4b36630b2bc08_bytes32; body.ommers[0].nonce[7] = 35; + body.consensus_parameter_index = 1234; return body; } @@ -527,6 +528,8 @@ TEST_CASE("Headers and bodies") { auto [b, h] = split_block_key(key); REQUIRE(b == header.number); REQUIRE(h == header.hash()); + + CHECK(block.consensus_parameter_index == 1234); } SECTION("process_blocks_at_height") { diff --git a/silkworm/node/db/util.cpp b/silkworm/node/db/util.cpp index 2758728a7..fa0cbc730 100644 --- a/silkworm/node/db/util.cpp +++ b/silkworm/node/db/util.cpp @@ -154,6 +154,9 @@ namespace detail { header.payload_length += rlp::length(base_txn_id); header.payload_length += rlp::length(txn_count); header.payload_length += rlp::length(ommers); + if (consensus_parameter_index) { + header.payload_length += rlp::length(*consensus_parameter_index); + } if (withdrawals) { header.payload_length += rlp::length(*withdrawals); } @@ -163,6 +166,9 @@ namespace detail { rlp::encode(to, base_txn_id); rlp::encode(to, txn_count); rlp::encode(to, ommers); + if (consensus_parameter_index) { + rlp::encode(to, *consensus_parameter_index); + } if (withdrawals) { rlp::encode(to, *withdrawals); } @@ -187,6 +193,15 @@ namespace detail { return res; } + to.consensus_parameter_index = std::nullopt; + if (from.length() > leftover) { + uint64_t consensus_parameter_index; + if (DecodingResult res{rlp::decode(from, consensus_parameter_index, rlp::Leftover::kAllow)}; !res) { + return res; + } + to.consensus_parameter_index = consensus_parameter_index; + } + to.withdrawals = std::nullopt; if (from.length() > leftover) { std::vector withdrawals; diff --git a/silkworm/node/db/util.hpp b/silkworm/node/db/util.hpp index ff63210d4..89f174621 100644 --- a/silkworm/node/db/util.hpp +++ b/silkworm/node/db/util.hpp @@ -131,6 +131,8 @@ namespace detail { std::vector ommers; std::optional> withdrawals{std::nullopt}; // EIP-4895 + std::optional consensus_parameter_index{std::nullopt}; + [[nodiscard]] Bytes encode() const; friend bool operator==(const BlockBodyForStorage&, const BlockBodyForStorage&) = default; diff --git a/silkworm/node/db/util_test.cpp b/silkworm/node/db/util_test.cpp index 0d28ec8f9..82dce395d 100644 --- a/silkworm/node/db/util_test.cpp +++ b/silkworm/node/db/util_test.cpp @@ -39,13 +39,20 @@ TEST_CASE("BlockBodyForStorage encoding") { .base_fee_per_gas = 0x244428, }; - // No withdrawals + // No consensus_parameter_index and withdraws BlockBodyForStorage body{.base_txn_id = 15, .txn_count = 3, .ommers = {header}}; Bytes encoded{body.encode()}; ByteView view{encoded}; BlockBodyForStorage decoded{decode_stored_block_body(view)}; CHECK(decoded == body); + // No withdrawals + body.consensus_parameter_index = 1234; + encoded = body.encode(); + view = encoded; + decoded = decode_stored_block_body(view); + CHECK(decoded == body); + // With withdrawals body.ommers.clear(); // no uncles after The Merge body.withdrawals = {{