Skip to content

Commit

Permalink
Support code delegation in instructions implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jul 29, 2024
1 parent 133d2a7 commit 7f9d123
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
67 changes: 64 additions & 3 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "baseline.hpp"
#include "constants.hpp"
#include "eof.hpp"
#include "execution_state.hpp"
#include "instructions_traits.hpp"
Expand Down Expand Up @@ -128,6 +129,27 @@ inline bool check_memory(
return check_memory(gas_left, memory, offset, static_cast<uint64_t>(size));
}

constexpr bool is_code_delegated(bytes_view code) noexcept
{
return code.starts_with(DELEGATION_MAGIC);
}

inline std::optional<evmc::address> get_delegate_address(
const evmc::address& addr, const evmc::HostContext& host) noexcept
{
std::array<uint8_t, std::size(DELEGATION_MAGIC)> prefix;
host.copy_code(addr, 0, prefix.data(), prefix.size());

if (!is_code_delegated(bytes_view{prefix.data(), prefix.size()}))
return {};

assert(host.get_code_size(addr) == std::size(DELEGATION_MAGIC) + std::size(addr.bytes));

evmc::address delegate_address;
host.copy_code(addr, prefix.size(), delegate_address.bytes, std::size(delegate_address.bytes));
return delegate_address;
}

namespace instr::core
{

Expand Down Expand Up @@ -518,21 +540,34 @@ inline void blobbasefee(StackTop stack, ExecutionState& state) noexcept
inline Result extcodesize(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
{
auto& x = stack.top();
const auto addr = intx::be::trunc<evmc::address>(x);
auto addr = intx::be::trunc<evmc::address>(x);

if (state.rev >= EVMC_BERLIN && state.host.access_account(addr) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}

if (state.rev >= EVMC_PRAGUE)
{
if (const auto delegate_addr = get_delegate_address(addr, state.host))
{
addr = *delegate_addr;
if (state.host.access_account(addr) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}
}
}

x = state.host.get_code_size(addr);
return {EVMC_SUCCESS, gas_left};
}

inline Result extcodecopy(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
{
const auto addr = intx::be::trunc<evmc::address>(stack.pop());
auto addr = intx::be::trunc<evmc::address>(stack.pop());
const auto& mem_index = stack.pop();
const auto& input_index = stack.pop();
const auto& size = stack.pop();
Expand All @@ -550,6 +585,19 @@ inline Result extcodecopy(StackTop stack, int64_t gas_left, ExecutionState& stat
return {EVMC_OUT_OF_GAS, gas_left};
}

if (state.rev >= EVMC_PRAGUE)
{
if (const auto delegate_addr = get_delegate_address(addr, state.host))
{
addr = *delegate_addr;
if (state.host.access_account(addr) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}
}
}

if (s > 0)
{
const auto src =
Expand Down Expand Up @@ -636,14 +684,27 @@ inline Result returndatacopy(StackTop stack, int64_t gas_left, ExecutionState& s
inline Result extcodehash(StackTop stack, int64_t gas_left, ExecutionState& state) noexcept
{
auto& x = stack.top();
const auto addr = intx::be::trunc<evmc::address>(x);
auto addr = intx::be::trunc<evmc::address>(x);

if (state.rev >= EVMC_BERLIN && state.host.access_account(addr) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}

if (state.rev >= EVMC_PRAGUE)
{
if (const auto delegate_addr = get_delegate_address(addr, state.host))
{
addr = *delegate_addr;
if (state.host.access_account(addr) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}
}
}

x = intx::be::load<uint256>(state.host.get_code_hash(addr));
return {EVMC_SUCCESS, gas_left};
}
Expand Down
30 changes: 28 additions & 2 deletions lib/evmone/instructions_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Result call_impl(StackTop stack, int64_t gas_left, ExecutionState& state) noexce
Op == OP_CALL || Op == OP_CALLCODE || Op == OP_DELEGATECALL || Op == OP_STATICCALL);

const auto gas = stack.pop();
const auto dst = intx::be::trunc<evmc::address>(stack.pop());
auto dst = intx::be::trunc<evmc::address>(stack.pop());
const auto value = (Op == OP_STATICCALL || Op == OP_DELEGATECALL) ? 0 : stack.pop();
const auto has_value = value != 0;
const auto input_offset_u256 = stack.pop();
Expand All @@ -67,6 +67,19 @@ Result call_impl(StackTop stack, int64_t gas_left, ExecutionState& state) noexce
return {EVMC_OUT_OF_GAS, gas_left};
}

if (state.rev >= EVMC_PRAGUE)
{
if (const auto delegate_addr = get_delegate_address(dst, state.host))
{
dst = *delegate_addr;
if (state.host.access_account(dst) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}
}
}

if (!check_memory(gas_left, state.memory, input_offset_u256, input_size_u256))
return {EVMC_OUT_OF_GAS, gas_left};

Expand Down Expand Up @@ -170,14 +183,27 @@ Result extcall_impl(StackTop stack, int64_t gas_left, ExecutionState& state) noe
if (dst_u256 > ADDRESS_MAX)
return {EVMC_ARGUMENT_OUT_OF_RANGE, gas_left};

const auto dst = intx::be::trunc<evmc::address>(dst_u256);
auto dst = intx::be::trunc<evmc::address>(dst_u256);

if (state.host.access_account(dst) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}

if (state.rev >= EVMC_PRAGUE)
{
if (const auto delegate_addr = get_delegate_address(dst, state.host))
{
dst = *delegate_addr;
if (state.host.access_account(dst) == EVMC_ACCESS_COLD)
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};
}
}
}

if (!check_memory(gas_left, state.memory, input_offset_u256, input_size_u256))
return {EVMC_OUT_OF_GAS, gas_left};

Expand Down

0 comments on commit 7f9d123

Please sign in to comment.