Skip to content

Commit

Permalink
Merge pull request #133 from eosnetworkfoundation/yarkin/hash_as_index
Browse files Browse the repository at this point in the history
Use hash as consensus paramter index.
  • Loading branch information
yarkinwho authored Mar 16, 2024
2 parents 9e1cbda + a2911a4 commit 6367598
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions eosevm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_library(eos_evm ${EOS_EVM_SRC})
target_include_directories(eos_evm PUBLIC ${SILKWORM_MAIN_DIR})

set(EOS_EVM_PUBLIC_LIBS
ethash::ethash
intx::intx
evmc
tl::expected
Expand Down
7 changes: 7 additions & 0 deletions eosevm/consensus_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if not defined(ANTELOPE)
#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/core/common/util.hpp>
#endif

namespace eosevm {
Expand Down Expand Up @@ -73,6 +74,12 @@ std::optional<ConsensusParameters> ConsensusParameters::decode(silkworm::ByteVie

return config;
}

[[nodiscard]] evmc::bytes32 ConsensusParameters::hash() const noexcept {
auto encoded = this->encode();
evmc::bytes32 header_hash = std::bit_cast<evmc_bytes32>(silkworm::keccak256(encoded));
return header_hash;
}
#endif

} // namespace eosevm
1 change: 1 addition & 0 deletions eosevm/consensus_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct ConsensusParameters {

// Decode from storage in db.
static std::optional<ConsensusParameters> decode(silkworm::ByteView encoded) noexcept;
evmc::bytes32 hash() const noexcept;
#endif

friend bool operator==(const ConsensusParameters&, const ConsensusParameters&);
Expand Down
4 changes: 2 additions & 2 deletions silkworm/core/types/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ namespace rlp {

to.consensus_parameter_index = std::nullopt;
if (from.length() > leftover) {
uint64_t consensus_parameter_index;
evmc::bytes32 consensus_parameter_index;
if (DecodingResult res{decode(from, consensus_parameter_index, Leftover::kAllow)}; !res) {
return res;
}
Expand Down Expand Up @@ -327,7 +327,7 @@ namespace rlp {

to.consensus_parameter_index = std::nullopt;
if (from.length() > leftover) {
uint64_t consensus_parameter_index;
evmc::bytes32 consensus_parameter_index;
if (DecodingResult res{decode(from, consensus_parameter_index, Leftover::kAllow)}; !res) {
return res;
}
Expand Down
2 changes: 1 addition & 1 deletion silkworm/core/types/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct BlockBody {
std::optional<std::vector<Withdrawal>> withdrawals{std::nullopt};

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

friend bool operator==(const BlockBody&, const BlockBody&);
};
Expand Down
2 changes: 1 addition & 1 deletion silkworm/core/types/block_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TEST_CASE("BlockBody RLP 2") {
body.ommers[0].prev_randao = 0xf0a53dfdd6c2f2a661e718ef29092de60d81d45f84044bec7bf4b36630b2bc08_bytes32;
body.ommers[0].nonce[7] = 35;

body.consensus_parameter_index = 1234;
body.consensus_parameter_index = evmc::bytes32(1234);

Bytes rlp{};
rlp::encode(rlp, body);
Expand Down
8 changes: 4 additions & 4 deletions silkworm/node/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1219,9 +1219,9 @@ void write_runtime_states_u64(RWTxn& txn, uint64_t num, RuntimeState runtime_sta
write_runtime_states_bytes(txn, value, runtime_state);
}

std::optional<eosevm::ConsensusParameters> read_consensus_parameters(ROTxn& txn, BlockNum index) {
std::optional<eosevm::ConsensusParameters> read_consensus_parameters(ROTxn& txn, const evmc::bytes32& index) {
auto cursor = txn.ro_cursor(table::kConsensusParameters);
auto key{db::block_key(index)};
auto key{db::block_key(index.bytes)};
auto data{cursor->find(to_slice(key), /*throw_notfound=*/false)};
if (!data) {
return std::nullopt;
Expand All @@ -1230,9 +1230,9 @@ std::optional<eosevm::ConsensusParameters> read_consensus_parameters(ROTxn& txn,
return eosevm::ConsensusParameters::decode(encoded);
}

void update_consensus_parameters(RWTxn& txn, BlockNum index, const eosevm::ConsensusParameters& config) {
void update_consensus_parameters(RWTxn& txn, const evmc::bytes32& index, const eosevm::ConsensusParameters& config) {
auto cursor = txn.rw_cursor(table::kConsensusParameters);
auto key{db::block_key(index)};
auto key{db::block_key(index.bytes)};

cursor->upsert(to_slice(key), mdbx::slice(config.encode()));
}
Expand Down
4 changes: 2 additions & 2 deletions silkworm/node/db/access_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ std::optional<uint64_t> read_runtime_states_u64(ROTxn& txn, RuntimeState runtime
void write_runtime_states_u64(RWTxn& txn, uint64_t num, RuntimeState runtime_state);

//! \brief Read ConsensusParameters indexed by blocknum that it is added.
std::optional<eosevm::ConsensusParameters> read_consensus_parameters(ROTxn& txn, BlockNum index);
std::optional<eosevm::ConsensusParameters> read_consensus_parameters(ROTxn& txn, const evmc::bytes32& index);

//! \brief Write ConsensusParameters indexed by blocknum that it is added. Can overwrite during forks.
void update_consensus_parameters(RWTxn& txn, BlockNum index, const eosevm::ConsensusParameters& config);
void update_consensus_parameters(RWTxn& txn, const evmc::bytes32&, const eosevm::ConsensusParameters& config);

class DataModel {
public:
Expand Down
30 changes: 18 additions & 12 deletions silkworm/node/db/access_layer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +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;
body.consensus_parameter_index = evmc::bytes32(1234);
return body;
}

Expand Down Expand Up @@ -529,7 +529,7 @@ TEST_CASE("Headers and bodies") {
REQUIRE(b == header.number);
REQUIRE(h == header.hash());

CHECK(block.consensus_parameter_index == 1234);
CHECK(block.consensus_parameter_index == evmc::bytes32(1234));
}

SECTION("process_blocks_at_height") {
Expand Down Expand Up @@ -940,21 +940,27 @@ TEST_CASE("ConsensusParameters") {
},
};

CHECK(read_consensus_parameters(txn, 0) == std::nullopt);
CHECK(read_consensus_parameters(txn, evmc::bytes32(0)) == std::nullopt);

update_consensus_parameters(txn, 0, value1 );
CHECK(read_consensus_parameters(txn, 0) == value1);
update_consensus_parameters(txn, evmc::bytes32(0), value1 );
CHECK(read_consensus_parameters(txn, evmc::bytes32(0)) == value1);

update_consensus_parameters(txn, 0, value2 );
CHECK(read_consensus_parameters(txn, 0) == value2);
update_consensus_parameters(txn, evmc::bytes32(0), value2 );
CHECK(read_consensus_parameters(txn, evmc::bytes32(0)) == value2);

CHECK(read_consensus_parameters(txn, 1) == std::nullopt);
CHECK(read_consensus_parameters(txn, evmc::bytes32(1)) == std::nullopt);

update_consensus_parameters(txn, 1, value2 );
CHECK(read_consensus_parameters(txn, 1) == value2);
update_consensus_parameters(txn, evmc::bytes32(1), value2 );
CHECK(read_consensus_parameters(txn, evmc::bytes32(1)) == value2);

update_consensus_parameters(txn, 1, value1 );
CHECK(read_consensus_parameters(txn, 1) == value1);
update_consensus_parameters(txn, evmc::bytes32(1), value1 );
CHECK(read_consensus_parameters(txn, evmc::bytes32(1)) == value1);

update_consensus_parameters(txn, value1.hash(), value1 );
CHECK(read_consensus_parameters(txn, value1.hash()) == value1);

update_consensus_parameters(txn, value2.hash(), value2 );
CHECK(read_consensus_parameters(txn, value2.hash()) == value2);
}


Expand Down
8 changes: 7 additions & 1 deletion silkworm/node/db/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Bytes block_key(BlockNum block_number, std::span<const uint8_t, kHashLength> has
return key;
}

Bytes block_key(std::span<const uint8_t, kHashLength> hash) {
Bytes key(kHashLength, '\0');
std::memcpy(&key[0], hash.data(), kHashLength);
return key;
}

auto split_block_key(ByteView key) -> std::tuple<BlockNum, evmc::bytes32> {
SILKWORM_ASSERT(key.size() == sizeof(BlockNum) + kHashLength);

Expand Down Expand Up @@ -195,7 +201,7 @@ namespace detail {

to.consensus_parameter_index = std::nullopt;
if (from.length() > leftover) {
uint64_t consensus_parameter_index;
evmc::bytes32 consensus_parameter_index;
if (DecodingResult res{rlp::decode(from, consensus_parameter_index, rlp::Leftover::kAllow)}; !res) {
return res;
}
Expand Down
4 changes: 3 additions & 1 deletion silkworm/node/db/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ Bytes block_key(BlockNum block_number);
// Erigon HeaderKey & BlockBodyKey
Bytes block_key(BlockNum block_number, std::span<const uint8_t, kHashLength> hash);

Bytes block_key(std::span<const uint8_t, kHashLength> hash);

// Split a block key in BlockNum and Hash
auto split_block_key(ByteView key) -> std::tuple<BlockNum, evmc::bytes32>;

Expand Down Expand Up @@ -131,7 +133,7 @@ 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};
std::optional<evmc::bytes32> consensus_parameter_index{std::nullopt};

[[nodiscard]] Bytes encode() const;

Expand Down
2 changes: 1 addition & 1 deletion silkworm/node/db/util_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST_CASE("BlockBodyForStorage encoding") {
CHECK(decoded == body);

// No withdrawals
body.consensus_parameter_index = 1234;
body.consensus_parameter_index = evmc::bytes32(1234);
encoded = body.encode();
view = encoded;
decoded = decode_stored_block_body(view);
Expand Down

0 comments on commit 6367598

Please sign in to comment.