Skip to content

Commit 24a3214

Browse files
authored
Move precompiles' stubs from JSON to C++ (#853)
Create individual stubs for precompiles with missing implementation. They can be enabled selectively and by default. Remove cache layer and JSON stubs.
2 parents 940b61a + 2f05dc8 commit 24a3214

File tree

9 files changed

+593
-965
lines changed

9 files changed

+593
-965
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,12 @@ To build the evmone EVMC module (shared library), test, and benchmark:
9191

9292
### Precompiles
9393

94-
Ethereum Precompiled Contracts (_precompiles_ for short) are not directly supported by evmone.
94+
Ethereum Precompiled Contracts (_precompiles_ for short) are only partly supported by evmone.
9595

9696
However, there are options to enable limited precompiles support for testing.
9797

98-
1. The [test/state/precompiles_stub.json](./test/state/precompiles_stub.json) contains
99-
precompiles execution results for inputs commonly used in tests.
100-
You can use the precompiles STUB by setting the environment variable
101-
`EVMONE_PRECOMPILES_STUB=./test/state/precompiles_stub.json`.
98+
1. For precompiles with missing implementation stubs are enabled by default.
99+
They will correctly respond to known inputs.
102100
2. The CMake option `EVMONE_PRECOMPILES_SILKPRE=1` enables building of
103101
the [silkpre] third party library with the implementation of the precompiles.
104102
This library also requires [GMP] (e.g. libgmp-dev) library for building and execution.

circle.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,13 +410,11 @@ jobs:
410410
name: "Execution spec tests (blockchain_tests)"
411411
working_directory: ~/build
412412
command: >
413-
EVMONE_PRECOMPILES_STUB=~/project/test/state/precompiles_stub.json
414413
bin/evmone-blockchaintest ~/spec-tests/fixtures/blockchain_tests
415414
- run:
416415
name: "Execution spec tests (state_tests)"
417416
working_directory: ~/build
418417
command: >
419-
EVMONE_PRECOMPILES_STUB=~/project/test/state/precompiles_stub.json
420418
bin/evmone-statetest ~/spec-tests/fixtures/state_tests
421419
422420
- download_execution_tests:
@@ -427,31 +425,27 @@ jobs:
427425
name: "State tests"
428426
working_directory: ~/build
429427
command: >
430-
EVMONE_PRECOMPILES_STUB=~/project/test/state/precompiles_stub.json
431428
bin/evmone-statetest
432429
~/tests/GeneralStateTests
433430
~/tests/LegacyTests/Constantinople/GeneralStateTests
434431
- run:
435432
name: "Blockchain tests (GeneralStateTests)"
436433
working_directory: ~/build
437434
command: >
438-
EVMONE_PRECOMPILES_STUB=~/project/test/state/precompiles_stub.json
439435
bin/evmone-blockchaintest
440436
--gtest_filter='*:-VMTests/vmPerformance.*:*.*Call50000_sha256:*.CALLBlake2f_MaxRounds'
441437
~/tests/BlockchainTests/GeneralStateTests
442438
- run:
443439
name: "Blockchain tests (ValidBlocks)"
444440
working_directory: ~/build
445441
command: >
446-
EVMONE_PRECOMPILES_STUB=~/project/test/state/precompiles_stub.json
447442
bin/evmone-blockchaintest
448443
--gtest_filter='*:-bcMultiChainTest.*:bcTotalDifficultyTest.*:bcForkStressTest.ForkStressTest:bcGasPricerTest.RPC_API_Test:bcValidBlockTest.SimpleTx3LowS'
449444
~/tests/BlockchainTests/ValidBlocks
450445
- run:
451446
name: "Blockchain tests (EIPs)"
452447
working_directory: ~/build
453448
command: >
454-
EVMONE_PRECOMPILES_STUB=~/project/test/state/precompiles_stub.json
455449
bin/evmone-blockchaintest
456450
--gtest_filter='-*StateTests/stEOF/*.*'
457451
~/tests/EIPTests/BlockchainTests/

test/state/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ target_sources(
2424
mpt_hash.cpp
2525
precompiles.hpp
2626
precompiles.cpp
27-
precompiles_cache.hpp
28-
precompiles_cache.cpp
2927
precompiles_internal.hpp
28+
precompiles_stubs.hpp
29+
precompiles_stubs.cpp
3030
rlp.hpp
3131
state.hpp
3232
state.cpp

test/state/precompiles.cpp

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
#include "precompiles.hpp"
6-
#include "precompiles_cache.hpp"
76
#include "precompiles_internal.hpp"
7+
#include "precompiles_stubs.hpp"
88
#include <evmone_precompiles/blake2b.hpp>
99
#include <evmone_precompiles/bn254.hpp>
1010
#include <evmone_precompiles/ripemd160.hpp>
1111
#include <evmone_precompiles/secp256k1.hpp>
1212
#include <intx/intx.hpp>
13+
#include <array>
1314
#include <bit>
1415
#include <cassert>
15-
#include <iostream>
1616
#include <limits>
17-
#include <unordered_map>
1817

1918
#ifdef EVMONE_PRECOMPILES_SILKPRE
2019
#include "precompiles_silkpre.hpp"
2120
#endif
2221

2322
namespace evmone::state
2423
{
24+
using evmc::bytes;
25+
using evmc::bytes_view;
2526
using namespace evmc::literals;
2627

2728
namespace
@@ -292,26 +293,19 @@ struct PrecompileTraits
292293
decltype(identity_execute)* execute = nullptr;
293294
};
294295

295-
template <PrecompileId Id>
296-
ExecutionResult dummy_execute(const uint8_t*, size_t, uint8_t*, size_t) noexcept
297-
{
298-
std::cerr << "Precompile " << static_cast<int>(Id) << " not implemented!\n";
299-
return ExecutionResult{EVMC_INTERNAL_ERROR, 0};
300-
}
301-
302296
inline constexpr auto traits = []() noexcept {
303297
std::array<PrecompileTraits, NumPrecompiles> tbl{{
304298
{}, // undefined for 0
305299
{ecrecover_analyze, ecrecover_execute},
306-
{sha256_analyze, dummy_execute<PrecompileId::sha256>},
300+
{sha256_analyze, sha256_stub},
307301
{ripemd160_analyze, ripemd160_execute},
308302
{identity_analyze, identity_execute},
309-
{expmod_analyze, dummy_execute<PrecompileId::expmod>},
303+
{expmod_analyze, expmod_stub},
310304
{ecadd_analyze, ecadd_execute},
311305
{ecmul_analyze, ecmul_execute},
312-
{ecpairing_analyze, dummy_execute<PrecompileId::ecpairing>},
306+
{ecpairing_analyze, ecpairing_stub},
313307
{blake2bf_analyze, blake2bf_execute},
314-
{point_evaluation_analyze, dummy_execute<PrecompileId::point_evaluation>},
308+
{point_evaluation_analyze, point_evaluation_stub},
315309
}};
316310
#ifdef EVMONE_PRECOMPILES_SILKPRE
317311
// tbl[static_cast<size_t>(PrecompileId::ecrecover)].execute = silkpre_ecrecover_execute;
@@ -363,10 +357,6 @@ evmc::Result call_precompile(evmc_revision rev, const evmc_message& msg) noexcep
363357
if (gas_left < 0)
364358
return evmc::Result{EVMC_OUT_OF_GAS};
365359

366-
static Cache cache;
367-
if (auto r = cache.find(static_cast<PrecompileId>(id), input, gas_left); r.has_value())
368-
return std::move(*r);
369-
370360
// Buffer for the precompile's output.
371361
// Big enough to handle all "expmod" tests, but in case does not match the size requirement
372362
// from analysis, the result will likely be incorrect.
@@ -380,8 +370,6 @@ evmc::Result call_precompile(evmc_revision rev, const evmc_message& msg) noexcep
380370
evmc::Result result{
381371
status_code, status_code == EVMC_SUCCESS ? gas_left : 0, 0, output_buf, output_size};
382372

383-
cache.insert(static_cast<PrecompileId>(id), input, result);
384-
385373
return result;
386374
}
387375
} // namespace evmone::state

test/state/precompiles_cache.cpp

Lines changed: 0 additions & 102 deletions
This file was deleted.

test/state/precompiles_cache.hpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)