EVMC modules for implementations of precompiled contracts
EIP-2003, titled "EVMC modules for implementations of precompiled contracts," proposes a way to provide implementations of Ethereum precompiled contracts using the Ethereum Virtual Machine Connector (EVMC) VM API. This proposal is compatible with EVMC ABI version 6.
The EVMC module with implementations of precompiled contracts should advertise the EVMC_CAPABILITY_PRECOMPILES capability in the get_capabilities() method and implement the execute() method in a specific way. The method should validate the incoming execution request requirements, check if the call destination address targets an existing precompiled contract, and emulate execution of empty code if not.
The implementation should inspect the input data and calculate the gas cost of the execution, compute the amount of gas left after execution, and execute the code of the precompiled contract, returning the EVMC_SUCCESS status code, the output, and gas left.
The EIP also specifies that precompiled contract implementations are allowed to return two more EVMC error codes: EVMC_FAILURE for failures caused by something other than out of gas, and EVMC_REVERT if the precompile doesn't want to forfeit all supplied gas.
The client is not required to provide the Host interface, so the precompiled contracts implementation must not access the evmc_context. This removes a significant portion of work needed for full EVMC integration.
Video
Original
Abstract
EVMC specifies a generic API for Ethereum execution engines. This EIP specifies a way of providing implementations of Ethereum precompiled contracts using the EVMC VM API.
Specification
For the complete EVMC specification visit the EVMC documentation first. This EIP is based on and is compatible with EVMC ABI version 6.
The EVMC module with implementations of precompiled contracts SHOULD:
-
Advertise the
EVMC_CAPABILITY_PRECOMPILES
capability in theget_capabilities()
method. -
Implement the
execute()
method in the following way:-
Validate the incoming execution request requirements:
-
The message kind (
evmc_message::kind
) is a call (EVMC_CALL
). -
The call destination address (
evmc_message::destination
) is within the range of precompiled contracts defined by EIP-1352. -
There is no code provided (the
code
argument isNULL
andcode_size
argument is0
).
If the requirements are not fulfilled, abort execution with the
EVMC_REJECTED
status code. -
-
Check if the call destination address (
evmc_message::destination
) targets existing precompiled contract. Consider the EVM revision (evmc_revision
) requested by therev
parameter ofexecute()
.If yes, execute as follows:
-
Inspect the input data (
evmc_message::input_data
,evmc_message::input_size
) and calculate the gas cost of the execution. -
Compute the amount of gas left after execution by subtracting the gas cost from the call gas limit (
evmc_message::gas
). -
If gas left is negative, abort execution with the
EVMC_OUT_OF_GAS
status code. -
Otherwise, execute the code of the precompiled contract, return the
EVMC_SUCCESS
status code, the output and gas left (evmc_result::output_data
,evmc_result::output_size
,evmc_result::gas_left
).
-
-
Otherwise, emulate execution of empty code by returning the
EVMC_SUCCESS
status code and gas left equal the call gas limit (evmc_message::gas
).
-
Precompiled contract implementations are allowed to return two more EVMC error codes:
EVMC_FAILURE
if the failure was caused due to something other than out of gas (e.g. input validation error)EVMC_REVERT
if the precompile doesn't want to forfeit all supplied gas (as of May 2019 no such precompile exists)
The Client is not required to provide the Host interface ([evmc_context
] argument of execute()
is set to NULL).
Therefore, the precompiled contracts implementation MUST NOT access the evmc_context
.
Rationale
It is very unlikely that any precompile will need to access or modify a contract state. Not requiring the Client to implement the EVMC Host interface removes the big portion of work needed for full EVMC integration.
Test Cases
EVMC provides the evmc-vmtester tool for checking compatibility with the EVMC specification.
Implementations
- Example of Precompiles VM implementation
- ewasm precompiles
- Aleth code for precompiles
- Parity code for precompiles
- EIP-1962 implemented as an EVMC precompile module
References
- EVMC – Ethereum Client-VM Connector API
- EVMC documentation
- EVMC VM Implementation Guide
- EIP 1352: Specify restricted address range for precompiles/system contracts
Copyright
Copyright and related rights waived via CC0.
Adopted by projects
Not miss a beat of EIPs' update?
Subscribe EIPs Fun to receive the latest updates of EIPs Good for Buidlers to follow up.
View all