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 committed Mar 18, 2023
1 parent c94614c commit 3b8e703
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions test/state/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,39 @@ 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
{
if (code.size() >= 2 && code[0] == 0xEF && code[1] == 0x00)
{
return code.substr(2);
}
return 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 3b8e703

Please sign in to comment.