Skip to content

Commit

Permalink
WIP support 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 5e9c460
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 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));

Check warning on line 146 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L146

Added line #L146 was not covered by tests

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

Check warning on line 150 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L148-L150

Added lines #L148 - L150 were not covered by tests
}

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)

Check warning on line 556 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L555-L556

Added lines #L555 - L556 were not covered by tests
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};

Check warning on line 559 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L558-L559

Added lines #L558 - L559 were not covered by tests
}
}
}

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)

Check warning on line 593 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L592-L593

Added lines #L592 - L593 were not covered by tests
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};

Check warning on line 596 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L595-L596

Added lines #L595 - L596 were not covered by tests
}
}
}

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)

Check warning on line 700 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L699-L700

Added lines #L699 - L700 were not covered by tests
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};

Check warning on line 703 in lib/evmone/instructions.hpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions.hpp#L702-L703

Added lines #L702 - L703 were not covered by tests
}
}
}

x = intx::be::load<uint256>(state.host.get_code_hash(addr));
return {EVMC_SUCCESS, gas_left};
}
Expand Down
15 changes: 14 additions & 1 deletion 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)

Check warning on line 75 in lib/evmone/instructions_calls.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions_calls.cpp#L74-L75

Added lines #L74 - L75 were not covered by tests
{
if ((gas_left -= instr::additional_cold_account_access_cost) < 0)
return {EVMC_OUT_OF_GAS, gas_left};

Check warning on line 78 in lib/evmone/instructions_calls.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/instructions_calls.cpp#L77-L78

Added lines #L77 - L78 were not covered by tests
}
}
}

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 5e9c460

Please sign in to comment.