Skip to content

Commit 02b6951

Browse files
committed
eof: Return constant hash of EXTCODEHASH of EOF
Replace runtime evaluated `keccak256("EF00")` with a constant returned for an account with EOF code.
1 parent e4f65da commit 02b6951

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/evmone/eof.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#pragma once
55

66
#include <evmc/bytes.hpp>
7-
#include <evmc/evmc.h>
7+
#include <evmc/evmc.hpp>
88
#include <evmc/utils.h>
99
#include <cassert>
1010
#include <cstddef>
@@ -17,10 +17,16 @@ namespace evmone
1717
{
1818
using evmc::bytes;
1919
using evmc::bytes_view;
20+
using namespace evmc::literals;
2021

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

25+
/// The value returned by EXTCODEHASH of an address with EOF code.
26+
/// See EIP-3540: https://eips.ethereum.org/EIPS/eip-3540#changes-to-execution-semantics.
27+
static constexpr auto EOF_CODE_HASH_SENTINEL =
28+
0x9dbf3648db8210552e9c4f75c6a1c3057c0ca432043bd648be15fe7be05646f5_bytes32;
29+
2430
struct EOFCodeType
2531
{
2632
uint8_t inputs; ///< Number of code inputs.

test/state/host.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ size_t Host::get_code_size(const address& addr) const noexcept
113113

114114
bytes32 Host::get_code_hash(const address& addr) const noexcept
115115
{
116-
// TODO: Cache code hash. It will be needed also to compute the MPT hash.
117116
const auto* const acc = m_state.find(addr);
118-
return (acc != nullptr && !acc->is_empty()) ? keccak256(extcode(acc->code)) : bytes32{};
117+
if (acc == nullptr || acc->is_empty())
118+
return {};
119+
if (is_eof_container(acc->code))
120+
return EOF_CODE_HASH_SENTINEL;
121+
// TODO: Cache code hash. It will be needed also to compute the MPT hash.
122+
return keccak256(acc->code);
119123
}
120124

121125
size_t Host::copy_code(const address& addr, size_t code_offset, uint8_t* buffer_data,

test/unittests/eof_test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <evmone/eof.hpp>
66
#include <gtest/gtest.h>
7+
#include <test/state/hash_utils.hpp>
78
#include <test/utils/bytecode.hpp>
89
#include <test/utils/utils.hpp>
910

@@ -115,3 +116,8 @@ TEST(eof, get_error_message)
115116
// NOLINTNEXTLINE(*.EnumCastOutOfRange)
116117
EXPECT_EQ(evmone::get_error_message(static_cast<EOFValidationError>(-1)), "<unknown>");
117118
}
119+
120+
TEST(eof, extcodehash_sentinel)
121+
{
122+
EXPECT_EQ(keccak256(EOF_MAGIC), EOF_CODE_HASH_SENTINEL);
123+
}

0 commit comments

Comments
 (0)