Skip to content

Commit

Permalink
use PrecompileId
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Dec 13, 2022
1 parent b07d6f4 commit 3fce7ed
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
10 changes: 7 additions & 3 deletions test/state/precompiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ inline constexpr auto traits = []() noexcept {

std::optional<evmc::Result> call_precompile(evmc_revision rev, const evmc_message& msg) noexcept
{
if (evmc::is_zero(msg.code_address) || msg.code_address >= address{NumPrecompiles})
// Define compile-time constant,
// TODO: workaround for Clang Analyzer bug https://github.com/llvm/llvm-project/issues/59493.
static constexpr address address_boundary{NumPrecompiles};

if (evmc::is_zero(msg.code_address) || msg.code_address >= address_boundary)
return {};

const auto id = msg.code_address.bytes[19];
Expand All @@ -200,7 +204,7 @@ std::optional<evmc::Result> call_precompile(evmc_revision rev, const evmc_messag
return evmc::Result{EVMC_OUT_OF_GAS};

static Cache cache;
if (auto r = cache.find(id, input, gas_left); r.has_value())
if (auto r = cache.find(static_cast<PrecompileId>(id), input, gas_left); r.has_value())
return r;

uint8_t output_buf[256]; // Big enough to handle all "expmod" tests.
Expand All @@ -212,7 +216,7 @@ std::optional<evmc::Result> call_precompile(evmc_revision rev, const evmc_messag
evmc::Result result{
status_code, status_code == EVMC_SUCCESS ? gas_left : 0, 0, output_buf, output_size};

cache.insert(id, input, result);
cache.insert(static_cast<PrecompileId>(id), input, result);

return result;
}
Expand Down
10 changes: 10 additions & 0 deletions test/state/precompiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

#include <evmc/evmc.hpp>
#include <optional>
#include <type_traits>

namespace stdx
{
template <typename EnumT>
inline constexpr auto to_underlying(EnumT e) noexcept
{
return static_cast<std::underlying_type_t<EnumT>>(e);
}
} // namespace stdx

namespace evmone::state
{
Expand Down
10 changes: 5 additions & 5 deletions test/state/precompiles_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

namespace evmone::state
{
std::optional<evmc::Result> Cache::find(uint8_t id, bytes_view input, int64_t gas_left) const
std::optional<evmc::Result> Cache::find(PrecompileId id, bytes_view input, int64_t gas_left) const
{
if (const auto& cache = m_cache.at(id); !cache.empty())
if (const auto& cache = m_cache.at(stdx::to_underlying(id)); !cache.empty())
{
const auto input_hash = keccak256(input);
if (const auto it = cache.find(input_hash); it != cache.end())
Expand All @@ -31,15 +31,15 @@ std::optional<evmc::Result> Cache::find(uint8_t id, bytes_view input, int64_t ga
return {};
}

void Cache::insert(uint8_t id, bytes_view input, const evmc::Result& result)
void Cache::insert(PrecompileId id, bytes_view input, const evmc::Result& result)
{
if (id == 4) // Do not cache "identity".
if (id == PrecompileId::identity) // Do not cache "identity".
return;
const auto input_hash = keccak256(input);
std::optional<bytes> cached_output;
if (result.status_code == EVMC_SUCCESS)
cached_output = bytes{result.output_data, result.output_size};
m_cache.at(id).insert({input_hash, std::move(cached_output)});
m_cache.at(stdx::to_underlying(id)).insert({input_hash, std::move(cached_output)});
}

Cache::Cache() noexcept
Expand Down
4 changes: 2 additions & 2 deletions test/state/precompiles_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class Cache
/// used for constructing the result for successful execution.
/// @return The cached execution result
/// or std::nullopt if the matching cache entry is not found.
std::optional<evmc::Result> find(uint8_t id, bytes_view input, int64_t gas_left) const;
std::optional<evmc::Result> find(PrecompileId id, bytes_view input, int64_t gas_left) const;

/// Inserts new precompiles cache entry.
void insert(uint8_t id, bytes_view input, const evmc::Result& result);
void insert(PrecompileId id, bytes_view input, const evmc::Result& result);
};
} // namespace evmone::state

0 comments on commit 3fce7ed

Please sign in to comment.