Skip to content

Commit

Permalink
Add missing parts and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yarkinwho committed Feb 9, 2024
1 parent eb3f482 commit 9eab6dd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions silkworm/core/types/block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions silkworm/node/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)};

Expand Down
3 changes: 3 additions & 0 deletions silkworm/node/db/access_layer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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") {
Expand Down
15 changes: 15 additions & 0 deletions silkworm/node/db/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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<Withdrawal> withdrawals;
Expand Down
2 changes: 2 additions & 0 deletions silkworm/node/db/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ namespace detail {
std::vector<BlockHeader> ommers;
std::optional<std::vector<Withdrawal>> withdrawals{std::nullopt}; // EIP-4895

std::optional<uint64_t> consensus_parameter_index{std::nullopt};

[[nodiscard]] Bytes encode() const;

friend bool operator==(const BlockBodyForStorage&, const BlockBodyForStorage&) = default;
Expand Down
9 changes: 8 additions & 1 deletion silkworm/node/db/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {{
Expand Down

0 comments on commit 9eab6dd

Please sign in to comment.