Skip to content

Commit

Permalink
host: Make EOF opaque for EXTCODE* instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
axic authored and pdobacz committed Feb 6, 2024
1 parent e4bcf7c commit 586fed1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions test/state/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,35 @@ uint256be Host::get_balance(const address& addr) const noexcept
return (acc != nullptr) ? intx::be::store<uint256be>(acc->balance) : uint256be{};
}

namespace
{
/// For EXTCODE* instructions if the target is an EOF account, then only return EF00.
/// While we only do this if the caller is legacy, it is not a problem doing this
/// unconditionally, because EOF contracts dot no have EXTCODE* instructions.
bytes_view extcode(bytes_view code) noexcept
{
return is_eof_container(code) ? code.substr(0, 2) : code;
}
} // namespace

size_t Host::get_code_size(const address& addr) const noexcept
{
const auto* const acc = m_state.find(addr);
return (acc != nullptr) ? acc->code.size() : 0;
return (acc != nullptr) ? extcode(acc->code).size() : 0;
}

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(acc->code) : bytes32{};
return (acc != nullptr && !acc->is_empty()) ? keccak256(extcode(acc->code)) : bytes32{};
}

size_t Host::copy_code(const address& addr, size_t code_offset, uint8_t* buffer_data,
size_t buffer_size) const noexcept
{
const auto* const acc = m_state.find(addr);
const auto code = (acc != nullptr) ? bytes_view{acc->code} : bytes_view{};
const auto code = (acc != nullptr) ? extcode(acc->code) : bytes_view{};
const auto code_slice = code.substr(std::min(code_offset, code.size()));
const auto num_bytes = std::min(buffer_size, code_slice.size());
std::copy_n(code_slice.begin(), num_bytes, buffer_data);
Expand Down

0 comments on commit 586fed1

Please sign in to comment.