Skip to content

Commit

Permalink
eof: Add trait since when an instruction is in EOF (#989)
Browse files Browse the repository at this point in the history
Add `std::optional<evmc_revision> eof_since` to the `instr::traits`
with the information in which EVM revision an instruction has become
valid in EOF.

Using `std::optional` has some inconvenience:
the `std::nullopt` is less-than any concrete value (think "Frontier-1")
while for undefined instructions we rather want to assign +infinity.
  • Loading branch information
chfast authored Sep 9, 2024
1 parent fdc48da commit 07d2f7a
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 253 deletions.
99 changes: 10 additions & 89 deletions lib/evmone/baseline_instruction_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,108 +9,29 @@ namespace evmone::baseline
{
namespace
{
constexpr auto common_cost_tables = []() noexcept {
consteval auto build_cost_tables(bool eof) noexcept
{
std::array<CostTable, EVMC_MAX_REVISION + 1> tables{};
for (size_t r = EVMC_FRONTIER; r <= EVMC_MAX_REVISION; ++r)
{
auto& table = tables[r];
for (size_t i = 0; i < table.size(); ++i)
for (size_t op = 0; op < table.size(); ++op)
{
table[i] = instr::gas_costs[r][i]; // Include instr::undefined in the table.
const auto& tr = instr::traits[op];
const auto since = eof ? tr.eof_since : tr.since;
table[op] = (since && r >= *since) ? instr::gas_costs[r][op] : instr::undefined;
}
}
return tables;
}();

constexpr auto legacy_cost_tables = []() noexcept {
auto tables = common_cost_tables;
tables[EVMC_PRAGUE][OP_RJUMP] = instr::undefined;
tables[EVMC_PRAGUE][OP_RJUMPI] = instr::undefined;
tables[EVMC_PRAGUE][OP_RJUMPV] = instr::undefined;
tables[EVMC_PRAGUE][OP_CALLF] = instr::undefined;
tables[EVMC_PRAGUE][OP_RETF] = instr::undefined;
tables[EVMC_PRAGUE][OP_JUMPF] = instr::undefined;
tables[EVMC_PRAGUE][OP_DATALOAD] = instr::undefined;
tables[EVMC_PRAGUE][OP_DATALOADN] = instr::undefined;
tables[EVMC_PRAGUE][OP_DATASIZE] = instr::undefined;
tables[EVMC_PRAGUE][OP_DATACOPY] = instr::undefined;
tables[EVMC_PRAGUE][OP_DUPN] = instr::undefined;
tables[EVMC_PRAGUE][OP_SWAPN] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXCHANGE] = instr::undefined;
tables[EVMC_PRAGUE][OP_RETURNDATALOAD] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXTCALL] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXTSTATICCALL] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXTDELEGATECALL] = instr::undefined;
tables[EVMC_PRAGUE][OP_EOFCREATE] = instr::undefined;
tables[EVMC_PRAGUE][OP_RETURNCONTRACT] = instr::undefined;

tables[EVMC_OSAKA][OP_RJUMP] = instr::undefined;
tables[EVMC_OSAKA][OP_RJUMPI] = instr::undefined;
tables[EVMC_OSAKA][OP_RJUMPV] = instr::undefined;
tables[EVMC_OSAKA][OP_CALLF] = instr::undefined;
tables[EVMC_OSAKA][OP_RETF] = instr::undefined;
tables[EVMC_OSAKA][OP_JUMPF] = instr::undefined;
tables[EVMC_OSAKA][OP_DATALOAD] = instr::undefined;
tables[EVMC_OSAKA][OP_DATALOADN] = instr::undefined;
tables[EVMC_OSAKA][OP_DATASIZE] = instr::undefined;
tables[EVMC_OSAKA][OP_DATACOPY] = instr::undefined;
tables[EVMC_OSAKA][OP_DUPN] = instr::undefined;
tables[EVMC_OSAKA][OP_SWAPN] = instr::undefined;
tables[EVMC_OSAKA][OP_EXCHANGE] = instr::undefined;
tables[EVMC_OSAKA][OP_RETURNDATALOAD] = instr::undefined;
tables[EVMC_OSAKA][OP_EXTCALL] = instr::undefined;
tables[EVMC_OSAKA][OP_EXTSTATICCALL] = instr::undefined;
tables[EVMC_OSAKA][OP_EXTDELEGATECALL] = instr::undefined;
tables[EVMC_OSAKA][OP_EOFCREATE] = instr::undefined;
tables[EVMC_OSAKA][OP_RETURNCONTRACT] = instr::undefined;
tables[EVMC_OSAKA][OP_TXCREATE] = instr::undefined;

return tables;
}();

constexpr auto eof_cost_tables = []() noexcept {
auto tables = common_cost_tables;
tables[EVMC_PRAGUE][OP_JUMP] = instr::undefined;
tables[EVMC_PRAGUE][OP_JUMPI] = instr::undefined;
tables[EVMC_PRAGUE][OP_PC] = instr::undefined;
tables[EVMC_PRAGUE][OP_CALLCODE] = instr::undefined;
tables[EVMC_PRAGUE][OP_SELFDESTRUCT] = instr::undefined;
tables[EVMC_PRAGUE][OP_CALL] = instr::undefined;
tables[EVMC_PRAGUE][OP_STATICCALL] = instr::undefined;
tables[EVMC_PRAGUE][OP_DELEGATECALL] = instr::undefined;
tables[EVMC_PRAGUE][OP_CREATE] = instr::undefined;
tables[EVMC_PRAGUE][OP_CREATE2] = instr::undefined;
tables[EVMC_PRAGUE][OP_CODESIZE] = instr::undefined;
tables[EVMC_PRAGUE][OP_CODECOPY] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXTCODESIZE] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXTCODECOPY] = instr::undefined;
tables[EVMC_PRAGUE][OP_EXTCODEHASH] = instr::undefined;
tables[EVMC_PRAGUE][OP_GAS] = instr::undefined;

tables[EVMC_OSAKA][OP_JUMP] = instr::undefined;
tables[EVMC_OSAKA][OP_JUMPI] = instr::undefined;
tables[EVMC_OSAKA][OP_PC] = instr::undefined;
tables[EVMC_OSAKA][OP_CALLCODE] = instr::undefined;
tables[EVMC_OSAKA][OP_SELFDESTRUCT] = instr::undefined;
tables[EVMC_OSAKA][OP_CALL] = instr::undefined;
tables[EVMC_OSAKA][OP_STATICCALL] = instr::undefined;
tables[EVMC_OSAKA][OP_DELEGATECALL] = instr::undefined;
tables[EVMC_OSAKA][OP_CREATE] = instr::undefined;
tables[EVMC_OSAKA][OP_CREATE2] = instr::undefined;
tables[EVMC_OSAKA][OP_CODESIZE] = instr::undefined;
tables[EVMC_OSAKA][OP_CODECOPY] = instr::undefined;
tables[EVMC_OSAKA][OP_EXTCODESIZE] = instr::undefined;
tables[EVMC_OSAKA][OP_EXTCODECOPY] = instr::undefined;
tables[EVMC_OSAKA][OP_EXTCODEHASH] = instr::undefined;
tables[EVMC_OSAKA][OP_GAS] = instr::undefined;
return tables;
}();
}

constexpr auto LEGACY_COST_TABLES = build_cost_tables(false);
constexpr auto EOF_COST_TABLES = build_cost_tables(true);
} // namespace

const CostTable& get_baseline_cost_table(evmc_revision rev, uint8_t eof_version) noexcept
{
const auto& tables = (eof_version == 0) ? legacy_cost_tables : eof_cost_tables;
const auto& tables = (eof_version == 0) ? LEGACY_COST_TABLES : EOF_COST_TABLES;
return tables[rev];
}
} // namespace evmone::baseline
Loading

0 comments on commit 07d2f7a

Please sign in to comment.