Skip to content

Commit

Permalink
eof: Return constant hash of EXTCODEHASH of EOF
Browse files Browse the repository at this point in the history
Replace runtime evaluated `keccak256("EF00")` with a constant returned
for an account with EOF code.
  • Loading branch information
chfast committed Sep 28, 2024
1 parent e4f65da commit 02b6951
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/evmone/eof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#pragma once

#include <evmc/bytes.hpp>
#include <evmc/evmc.h>
#include <evmc/evmc.hpp>
#include <evmc/utils.h>
#include <cassert>
#include <cstddef>
Expand All @@ -17,10 +17,16 @@ namespace evmone
{
using evmc::bytes;
using evmc::bytes_view;
using namespace evmc::literals;

constexpr uint8_t EOF_MAGIC_BYTES[] = {0xef, 0x00};
constexpr bytes_view EOF_MAGIC{EOF_MAGIC_BYTES, std::size(EOF_MAGIC_BYTES)};

/// The value returned by EXTCODEHASH of an address with EOF code.
/// See EIP-3540: https://eips.ethereum.org/EIPS/eip-3540#changes-to-execution-semantics.
static constexpr auto EOF_CODE_HASH_SENTINEL =
0x9dbf3648db8210552e9c4f75c6a1c3057c0ca432043bd648be15fe7be05646f5_bytes32;

struct EOFCodeType
{
uint8_t inputs; ///< Number of code inputs.
Expand Down
8 changes: 6 additions & 2 deletions test/state/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ size_t Host::get_code_size(const address& addr) const noexcept

bytes32 Host::get_code_hash(const address& addr) const noexcept
{
// TODO: Cache code hash. It will be needed also to compute the MPT hash.
const auto* const acc = m_state.find(addr);
return (acc != nullptr && !acc->is_empty()) ? keccak256(extcode(acc->code)) : bytes32{};
if (acc == nullptr || acc->is_empty())
return {};
if (is_eof_container(acc->code))
return EOF_CODE_HASH_SENTINEL;
// TODO: Cache code hash. It will be needed also to compute the MPT hash.
return keccak256(acc->code);
}

size_t Host::copy_code(const address& addr, size_t code_offset, uint8_t* buffer_data,
Expand Down
6 changes: 6 additions & 0 deletions test/unittests/eof_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <evmone/eof.hpp>
#include <gtest/gtest.h>
#include <test/state/hash_utils.hpp>
#include <test/utils/bytecode.hpp>
#include <test/utils/utils.hpp>

Expand Down Expand Up @@ -115,3 +116,8 @@ TEST(eof, get_error_message)
// NOLINTNEXTLINE(*.EnumCastOutOfRange)
EXPECT_EQ(evmone::get_error_message(static_cast<EOFValidationError>(-1)), "<unknown>");
}

TEST(eof, extcodehash_sentinel)
{
EXPECT_EQ(keccak256(EOF_MAGIC), EOF_CODE_HASH_SENTINEL);
}

0 comments on commit 02b6951

Please sign in to comment.