Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EIP-2929 consensus tests #298

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement EIP-2929 changes to SLOAD and SSTORE
  • Loading branch information
yperbasis authored and chfast committed Apr 7, 2021
commit 09bcf241f6ac8d30b3ebe00e2189f40f2108344a
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[submodule "evmc"]
path = evmc
url = https://github.com/ethereum/evmc
url = https://github.com/torquem-ch/evmc
branch = eip-2929
9 changes: 8 additions & 1 deletion lib/evmone/baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,15 @@ evmc_result baseline_execute(ExecutionState& state) noexcept
msize(state);
break;
case OP_SLOAD:
sload(state);
{
const auto status_code = sload(state);
if (status_code != EVMC_SUCCESS)
{
state.status = status_code;
goto exit;
}
break;
}
case OP_SSTORE:
{
const auto status_code = sstore(state);
Expand Down
6 changes: 5 additions & 1 deletion lib/evmone/instruction_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,5 +333,9 @@ constexpr inline std::array<int16_t, 256> gas_costs<EVMC_ISTANBUL> = []() noexce
}();

template <>
constexpr inline auto gas_costs<EVMC_BERLIN> = gas_costs<EVMC_ISTANBUL>;
constexpr inline std::array<int16_t, 256> gas_costs<EVMC_BERLIN> = []() noexcept {
auto table = gas_costs<EVMC_ISTANBUL>;
table[OP_SLOAD] = 100; // EIP-2929 WARM_STORAGE_READ_COST
return table;
}();
} // namespace evmone::instr
50 changes: 34 additions & 16 deletions lib/evmone/instructions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,22 @@ inline evmc_status_code mstore8(ExecutionState& state) noexcept
return EVMC_SUCCESS;
}

inline void sload(ExecutionState& state) noexcept
inline evmc_status_code sload(ExecutionState& state) noexcept
{
auto& x = state.stack.top();
x = intx::be::load<uint256>(
state.host.get_storage(state.msg->destination, intx::be::store<evmc::bytes32>(x)));
const auto key = intx::be::store<evmc::bytes32>(x);

if (state.rev >= EVMC_BERLIN &&
state.host.access_storage(state.msg->destination, key) == EVMC_ACCESS_COLD)
{
// EIP-2929: COLD_SLOAD_COST - WARM_STORAGE_READ_COST = 2000
if ((state.gas_left -= 2000) < 0)
return EVMC_OUT_OF_GAS;
}

x = intx::be::load<uint256>(state.host.get_storage(state.msg->destination, key));

return EVMC_SUCCESS;
}

inline evmc_status_code sstore(ExecutionState& state) noexcept
Expand All @@ -578,34 +589,41 @@ inline evmc_status_code sstore(ExecutionState& state) noexcept

const auto key = intx::be::store<evmc::bytes32>(state.stack.pop());
const auto value = intx::be::store<evmc::bytes32>(state.stack.pop());
const auto status = state.host.set_storage(state.msg->destination, key, value);

// EIP-2929
static constexpr int COLD_SLOAD_COST = 2100;
static constexpr int WARM_STORAGE_READ_COST = 100;
int cost = 0;
if (state.rev >= EVMC_BERLIN &&
state.host.access_storage(state.msg->destination, key) == EVMC_ACCESS_COLD)
{
cost = COLD_SLOAD_COST;
}

const auto status = state.host.set_storage(state.msg->destination, key, value);

switch (status)
{
case EVMC_STORAGE_UNCHANGED:
if (state.rev >= EVMC_ISTANBUL)
case EVMC_STORAGE_MODIFIED_AGAIN:
if (state.rev >= EVMC_BERLIN)
cost += WARM_STORAGE_READ_COST;
else if (state.rev == EVMC_ISTANBUL)
cost = 800;
else if (state.rev == EVMC_CONSTANTINOPLE)
cost = 200;
else
cost = 5000;
break;
case EVMC_STORAGE_MODIFIED:
cost = 5000;
break;
case EVMC_STORAGE_MODIFIED_AGAIN:
if (state.rev >= EVMC_ISTANBUL)
cost = 800;
else if (state.rev == EVMC_CONSTANTINOPLE)
cost = 200;
case EVMC_STORAGE_DELETED:
if (state.rev >= EVMC_BERLIN)
cost += 5000 - COLD_SLOAD_COST;
else
cost = 5000;
break;
case EVMC_STORAGE_ADDED:
cost = 20000;
break;
case EVMC_STORAGE_DELETED:
cost = 5000;
cost += 20000;
break;
}
if ((state.gas_left -= cost) < 0)
Expand Down