2016-06-24 09:35:33 +00:00
|
|
|
/// EVM-C -- C interface to Ethereum Virtual Machine
|
|
|
|
///
|
|
|
|
/// ## High level design rules
|
2017-08-18 13:11:13 +00:00
|
|
|
///
|
2016-06-24 09:35:33 +00:00
|
|
|
/// 1. Pass function arguments and results by value.
|
|
|
|
/// This rule comes from modern C++ and tries to avoid costly alias analysis
|
|
|
|
/// needed for optimization. As the result we have a lots of complex structs
|
|
|
|
/// and unions. And variable sized arrays of bytes cannot be passed by copy.
|
|
|
|
/// 2. The EVM operates on integers so it prefers values to be host-endian.
|
|
|
|
/// On the other hand, LLVM can generate good code for byte swaping.
|
|
|
|
/// The interface also tries to match host application "natural" endianess.
|
|
|
|
/// I would like to know what endianess you use and where.
|
|
|
|
///
|
2017-08-18 13:11:13 +00:00
|
|
|
/// ## Terms
|
|
|
|
///
|
|
|
|
/// 1. EVM -- an Ethereum Virtual Machine instance/implementation.
|
|
|
|
/// 2. Host -- an entity controlling the EVM. The Host requests code execution
|
|
|
|
/// and responses to EVM queries by callback functions.
|
|
|
|
///
|
2016-06-24 09:35:33 +00:00
|
|
|
/// @defgroup EVMC EVM-C
|
|
|
|
/// @{
|
2016-08-23 18:04:08 +00:00
|
|
|
#ifndef EVM_H
|
|
|
|
#define EVM_H
|
2016-06-24 09:35:33 +00:00
|
|
|
|
|
|
|
#include <stdint.h> // Definition of int64_t, uint64_t.
|
|
|
|
#include <stddef.h> // Definition of size_t.
|
|
|
|
|
|
|
|
#if __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-08-31 10:01:28 +00:00
|
|
|
// BEGIN Python CFFI declarations
|
|
|
|
|
2017-04-24 11:54:05 +00:00
|
|
|
enum {
|
|
|
|
/// The EVM-C ABI version number of the interface declared in this file.
|
|
|
|
EVM_ABI_VERSION = 0
|
|
|
|
};
|
2016-08-25 14:03:22 +00:00
|
|
|
|
2016-08-31 09:40:02 +00:00
|
|
|
/// Big-endian 256-bit integer.
|
2016-06-24 09:35:33 +00:00
|
|
|
///
|
2016-08-31 09:40:02 +00:00
|
|
|
/// 32 bytes of data representing big-endian 256-bit integer. I.e. bytes[0] is
|
|
|
|
/// the most significant byte, bytes[31] is the least significant byte.
|
|
|
|
/// This type is used to transfer to/from the VM values interpreted by the user
|
|
|
|
/// as both 256-bit integers and 256-bit hashes.
|
|
|
|
struct evm_uint256be {
|
|
|
|
/// The 32 bytes of the big-endian integer or hash.
|
|
|
|
uint8_t bytes[32];
|
2016-06-24 09:35:33 +00:00
|
|
|
};
|
|
|
|
|
2016-08-22 17:26:37 +00:00
|
|
|
/// Big-endian 160-bit hash suitable for keeping an Ethereum address.
|
2017-09-26 12:11:53 +00:00
|
|
|
struct evm_address {
|
2016-06-24 09:35:33 +00:00
|
|
|
/// The 20 bytes of the hash.
|
2016-07-26 12:00:59 +00:00
|
|
|
uint8_t bytes[20];
|
2016-06-24 09:35:33 +00:00
|
|
|
};
|
|
|
|
|
2017-01-27 16:49:24 +00:00
|
|
|
/// The kind of call-like instruction.
|
|
|
|
enum evm_call_kind {
|
2017-06-23 18:08:53 +00:00
|
|
|
EVM_CALL = 0, ///< Request CALL.
|
|
|
|
EVM_DELEGATECALL = 1, ///< Request DELEGATECALL. The value param ignored.
|
|
|
|
EVM_CALLCODE = 2, ///< Request CALLCODE.
|
|
|
|
EVM_CREATE = 3, ///< Request CREATE. Semantic of some params changes.
|
2017-01-27 16:49:24 +00:00
|
|
|
};
|
|
|
|
|
2018-01-11 09:54:23 +00:00
|
|
|
/// The flags for ::evm_message.
|
2017-06-23 15:08:51 +00:00
|
|
|
enum evm_flags {
|
2018-01-11 09:54:23 +00:00
|
|
|
EVM_STATIC = 1 ///< Static call mode.
|
2017-06-23 15:08:51 +00:00
|
|
|
};
|
|
|
|
|
2018-01-11 09:54:23 +00:00
|
|
|
/// The message describing an EVM call,
|
|
|
|
/// including a zero-depth calls from a transaction origin.
|
2017-01-12 15:45:14 +00:00
|
|
|
struct evm_message {
|
2018-01-23 10:00:55 +00:00
|
|
|
struct evm_address destination; ///< The destination of the message.
|
|
|
|
struct evm_address sender; ///< The sender of the message.
|
2018-01-11 09:54:23 +00:00
|
|
|
|
|
|
|
/// The amount of Ether transferred with the message.
|
2017-01-12 15:45:14 +00:00
|
|
|
struct evm_uint256be value;
|
2018-01-11 09:54:23 +00:00
|
|
|
|
2018-01-23 05:18:03 +00:00
|
|
|
/// The message input data.
|
|
|
|
///
|
|
|
|
/// This MAY be NULL.
|
2018-01-23 13:21:56 +00:00
|
|
|
const uint8_t* input_data;
|
|
|
|
|
2018-01-23 05:18:03 +00:00
|
|
|
/// The size of the message input data.
|
|
|
|
///
|
2018-01-23 13:21:56 +00:00
|
|
|
/// If input_data is NULL this MUST be 0.
|
2018-01-23 05:18:03 +00:00
|
|
|
size_t input_size;
|
2018-01-11 09:54:23 +00:00
|
|
|
|
|
|
|
/// The optional hash of the code of the destination account.
|
|
|
|
/// The null hash MUST be used when not specified.
|
2017-01-27 15:27:34 +00:00
|
|
|
struct evm_uint256be code_hash;
|
2018-01-11 09:54:23 +00:00
|
|
|
|
|
|
|
int64_t gas; ///< The amount of gas for message execution.
|
|
|
|
int32_t depth; ///< The call depth.
|
|
|
|
|
|
|
|
/// The kind of the call. For zero-depth calls ::EVM_CALL SHOULD be used.
|
2017-01-27 16:49:24 +00:00
|
|
|
enum evm_call_kind kind;
|
2018-01-11 09:54:23 +00:00
|
|
|
|
2018-01-23 13:21:56 +00:00
|
|
|
/// Additional flags modifying the call execution behavior.
|
|
|
|
/// In the current version the only valid values are ::EVM_STATIC or 0.
|
2017-06-23 15:08:51 +00:00
|
|
|
uint32_t flags;
|
2017-01-12 15:45:14 +00:00
|
|
|
};
|
|
|
|
|
2018-01-11 09:54:23 +00:00
|
|
|
|
|
|
|
/// The transaction and block data for execution.
|
2017-01-17 11:22:53 +00:00
|
|
|
struct evm_tx_context {
|
2018-01-11 09:54:23 +00:00
|
|
|
struct evm_uint256be tx_gas_price; ///< The transaction gas price.
|
|
|
|
struct evm_address tx_origin; ///< The transaction origin account.
|
|
|
|
struct evm_address block_coinbase; ///< The miner of the block.
|
|
|
|
int64_t block_number; ///< The block number.
|
|
|
|
int64_t block_timestamp; ///< The block timestamp.
|
|
|
|
int64_t block_gas_limit; ///< The block gas limit.
|
|
|
|
struct evm_uint256be block_difficulty; ///< The block difficulty.
|
2017-01-17 11:22:53 +00:00
|
|
|
};
|
|
|
|
|
2017-09-14 15:28:58 +00:00
|
|
|
struct evm_context;
|
|
|
|
|
2018-01-14 13:36:42 +00:00
|
|
|
/// Get transaction context callback function.
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to retrieve the transaction and
|
|
|
|
/// block context.
|
|
|
|
///
|
|
|
|
/// @param[out] result The returned transaction context.
|
|
|
|
/// @see ::evm_tx_context.
|
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-01-18 21:58:30 +00:00
|
|
|
typedef void (*evm_get_tx_context_fn)(struct evm_tx_context* result,
|
2017-08-18 13:11:13 +00:00
|
|
|
struct evm_context* context);
|
2017-01-18 21:58:30 +00:00
|
|
|
|
2018-01-14 13:36:42 +00:00
|
|
|
/// Get block hash callback function..
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to query the block hash of
|
|
|
|
/// a given block.
|
|
|
|
///
|
|
|
|
/// @param[out] result The returned block hash value.
|
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @param number The block number. Must be a value between
|
|
|
|
// (and including) 0 and 255.
|
2017-01-19 21:39:21 +00:00
|
|
|
typedef void (*evm_get_block_hash_fn)(struct evm_uint256be* result,
|
2017-08-18 13:11:13 +00:00
|
|
|
struct evm_context* context,
|
2017-01-19 21:39:21 +00:00
|
|
|
int64_t number);
|
|
|
|
|
2017-09-22 17:27:30 +00:00
|
|
|
/// The execution status code.
|
2018-03-21 14:50:26 +00:00
|
|
|
///
|
|
|
|
/// @todo: Add status code for out-of-buffer access.
|
2017-09-22 17:27:30 +00:00
|
|
|
enum evm_status_code {
|
2016-08-25 10:48:54 +00:00
|
|
|
EVM_SUCCESS = 0, ///< Execution finished with success.
|
|
|
|
EVM_FAILURE = 1, ///< Generic execution failure.
|
2016-08-24 11:30:45 +00:00
|
|
|
EVM_OUT_OF_GAS = 2,
|
2018-03-26 15:10:54 +00:00
|
|
|
EVM_UNDEFINED_INSTRUCTION = 3, ///< Unknown instruction encountered by the VM.
|
2016-08-24 11:30:45 +00:00
|
|
|
EVM_BAD_JUMP_DESTINATION = 4,
|
|
|
|
EVM_STACK_OVERFLOW = 5,
|
|
|
|
EVM_STACK_UNDERFLOW = 6,
|
2017-09-22 17:27:30 +00:00
|
|
|
EVM_REVERT = 7, ///< Execution terminated with REVERT opcode.
|
2018-03-21 14:50:26 +00:00
|
|
|
|
2018-03-26 15:10:54 +00:00
|
|
|
/// Tried to execute an operation which is restricted in static mode.
|
|
|
|
///
|
2018-03-21 14:50:26 +00:00
|
|
|
/// @todo Avoid _ERROR suffix that suggests fatal error.
|
2018-01-23 05:19:52 +00:00
|
|
|
EVM_STATIC_MODE_ERROR = 8,
|
2017-04-25 15:01:46 +00:00
|
|
|
|
2018-01-18 16:36:42 +00:00
|
|
|
/// The EVM rejected the execution of the given code or message.
|
2018-01-13 17:57:47 +00:00
|
|
|
///
|
2018-01-18 16:36:42 +00:00
|
|
|
/// This error SHOULD be used to signal that the EVM is not able to or
|
|
|
|
/// willing to execute the given code type or message.
|
|
|
|
/// If an EVM returns the ::EVM_REJECTED status code,
|
|
|
|
/// the Client MAY try to execute it in other EVM implementation.
|
2018-01-13 17:57:47 +00:00
|
|
|
/// For example, the Client tries running a code in the EVM 1.5. If the
|
2018-01-18 16:36:42 +00:00
|
|
|
/// code is not supported there, the execution falls back to the EVM 1.0.
|
|
|
|
EVM_REJECTED = -1,
|
2018-01-13 17:57:47 +00:00
|
|
|
|
2017-04-25 15:01:46 +00:00
|
|
|
/// EVM implementation internal error.
|
|
|
|
///
|
2017-09-26 14:51:10 +00:00
|
|
|
/// @todo We should rethink reporting internal errors. One of the options
|
|
|
|
/// it to allow using any negative value to represent internal errors.
|
2018-01-13 17:57:47 +00:00
|
|
|
EVM_INTERNAL_ERROR = -2,
|
2016-08-23 21:58:39 +00:00
|
|
|
};
|
|
|
|
|
2016-12-20 15:50:23 +00:00
|
|
|
struct evm_result; ///< Forward declaration.
|
2016-09-27 15:14:19 +00:00
|
|
|
|
|
|
|
/// Releases resources assigned to an execution result.
|
|
|
|
///
|
|
|
|
/// This function releases memory (and other resources, if any) assigned to the
|
|
|
|
/// specified execution result making the result object invalid.
|
|
|
|
///
|
|
|
|
/// @param result The execution result which resource are to be released. The
|
|
|
|
/// result itself it not modified by this function, but becomes
|
|
|
|
/// invalid and user should discard it as well.
|
2017-12-12 11:57:35 +00:00
|
|
|
typedef void (*evm_release_result_fn)(const struct evm_result* result);
|
2016-09-27 15:14:19 +00:00
|
|
|
|
2016-08-25 10:11:24 +00:00
|
|
|
/// The EVM code execution result.
|
2016-06-24 09:35:33 +00:00
|
|
|
struct evm_result {
|
2017-09-26 14:51:10 +00:00
|
|
|
/// The execution status code.
|
|
|
|
enum evm_status_code status_code;
|
2016-08-23 21:58:39 +00:00
|
|
|
|
2016-08-25 10:11:24 +00:00
|
|
|
/// The amount of gas left after the execution.
|
|
|
|
///
|
2017-08-07 16:19:05 +00:00
|
|
|
/// If evm_result::code is not ::EVM_SUCCESS nor ::EVM_REVERT
|
|
|
|
/// the value MUST be 0.
|
2016-07-04 22:54:51 +00:00
|
|
|
int64_t gas_left;
|
|
|
|
|
2017-04-24 11:54:05 +00:00
|
|
|
/// The reference to output data.
|
|
|
|
///
|
|
|
|
/// The output contains data coming from RETURN opcode (iff evm_result::code
|
2017-08-07 16:19:05 +00:00
|
|
|
/// field is ::EVM_SUCCESS) or from REVERT opcode.
|
2017-04-24 11:54:05 +00:00
|
|
|
///
|
|
|
|
/// The memory containing the output data is owned by EVM and has to be
|
|
|
|
/// freed with evm_result::release().
|
2017-12-14 14:23:21 +00:00
|
|
|
///
|
2018-01-15 17:55:22 +00:00
|
|
|
/// This MAY be NULL.
|
2018-01-23 13:21:56 +00:00
|
|
|
const uint8_t* output_data;
|
2016-12-20 14:28:43 +00:00
|
|
|
|
|
|
|
/// The size of the output data.
|
2018-01-15 17:55:22 +00:00
|
|
|
///
|
|
|
|
/// If output_data is NULL this MUST be 0.
|
2016-12-20 14:28:43 +00:00
|
|
|
size_t output_size;
|
2016-07-20 12:15:04 +00:00
|
|
|
|
2017-04-25 17:05:49 +00:00
|
|
|
/// The pointer to a function releasing all resources associated with
|
|
|
|
/// the result object.
|
2016-09-28 11:19:02 +00:00
|
|
|
///
|
2017-04-25 17:05:49 +00:00
|
|
|
/// This function pointer is optional (MAY be NULL) and MAY be set by
|
|
|
|
/// the EVM implementation. If set it MUST be used by the user to
|
|
|
|
/// release memory and other resources associated with the result object.
|
|
|
|
/// After the result resources are released the result object
|
|
|
|
/// MUST NOT be used any more.
|
|
|
|
///
|
|
|
|
/// The suggested code pattern for releasing EVM results:
|
|
|
|
/// @code
|
|
|
|
/// struct evm_result result = ...;
|
|
|
|
/// if (result.release)
|
|
|
|
/// result.release(&result);
|
|
|
|
/// @endcode
|
2017-04-24 16:10:30 +00:00
|
|
|
///
|
|
|
|
/// @note
|
|
|
|
/// It works similarly to C++ virtual destructor. Attaching the release
|
|
|
|
/// function to the result itself allows EVM composition.
|
2016-09-28 11:19:02 +00:00
|
|
|
evm_release_result_fn release;
|
2016-07-04 22:54:51 +00:00
|
|
|
|
2017-10-23 15:28:53 +00:00
|
|
|
/// The address of the contract created by CREATE opcode.
|
|
|
|
///
|
|
|
|
/// This field has valid value only if the result describes successful
|
|
|
|
/// CREATE (evm_result::status_code is ::EVM_SUCCESS).
|
|
|
|
struct evm_address create_address;
|
|
|
|
|
2017-05-29 14:24:56 +00:00
|
|
|
/// Reserved data that MAY be used by a evm_result object creator.
|
|
|
|
///
|
2017-10-23 15:28:53 +00:00
|
|
|
/// This reserved 4 bytes together with 20 bytes from create_address form
|
|
|
|
/// 24 bytes of memory called "optional data" within evm_result struct
|
|
|
|
/// to be optionally used by the evm_result object creator.
|
|
|
|
///
|
|
|
|
/// @see evm_result_optional_data, evm_get_optional_data().
|
|
|
|
///
|
|
|
|
/// Also extends the size of the evm_result to 64 bytes (full cache line).
|
|
|
|
uint8_t padding[4];
|
2016-06-24 09:35:33 +00:00
|
|
|
};
|
|
|
|
|
2017-10-23 15:28:53 +00:00
|
|
|
|
|
|
|
/// The union representing evm_result "optional data".
|
|
|
|
///
|
|
|
|
/// The evm_result struct contains 24 bytes of optional data that can be
|
|
|
|
/// reused by the obejct creator if the object does not contain
|
|
|
|
/// evm_result::create_address.
|
|
|
|
///
|
|
|
|
/// An EVM implementation MAY use this memory to keep additional data
|
|
|
|
/// when returning result from ::evm_execute_fn.
|
|
|
|
/// The host application MAY use this memory to keep additional data
|
|
|
|
/// when returning result of performed calls from ::evm_call_fn.
|
|
|
|
///
|
|
|
|
/// @see evm_get_optional_data(), evm_get_const_optional_data().
|
|
|
|
union evm_result_optional_data
|
|
|
|
{
|
|
|
|
uint8_t bytes[24];
|
|
|
|
void* pointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Provides read-write access to evm_result "optional data".
|
|
|
|
static inline union evm_result_optional_data* evm_get_optional_data(
|
|
|
|
struct evm_result* result)
|
|
|
|
{
|
|
|
|
return (union evm_result_optional_data*) &result->create_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provides read-only access to evm_result "optional data".
|
|
|
|
static inline const union evm_result_optional_data* evm_get_const_optional_data(
|
|
|
|
const struct evm_result* result)
|
|
|
|
{
|
|
|
|
return (const union evm_result_optional_data*) &result->create_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 08:26:52 +00:00
|
|
|
/// Check account existence callback function
|
2016-06-24 09:35:33 +00:00
|
|
|
///
|
2017-08-08 08:26:52 +00:00
|
|
|
/// This callback function is used by the EVM to check if
|
|
|
|
/// there exists an account at given address.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-05-05 16:56:36 +00:00
|
|
|
/// @param address The address of the account the query is about.
|
2017-08-08 08:26:52 +00:00
|
|
|
/// @return 1 if exists, 0 otherwise.
|
2017-08-18 13:11:13 +00:00
|
|
|
typedef int (*evm_account_exists_fn)(struct evm_context* context,
|
2017-09-26 12:11:53 +00:00
|
|
|
const struct evm_address* address);
|
2017-06-09 14:16:17 +00:00
|
|
|
|
|
|
|
/// Get storage callback function.
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to query the given contract
|
|
|
|
/// storage entry.
|
|
|
|
/// @param[out] result The returned storage value.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-06-09 14:16:17 +00:00
|
|
|
/// @param address The address of the contract.
|
|
|
|
/// @param key The index of the storage entry.
|
|
|
|
typedef void (*evm_get_storage_fn)(struct evm_uint256be* result,
|
2017-08-18 13:11:13 +00:00
|
|
|
struct evm_context* context,
|
2017-09-26 12:11:53 +00:00
|
|
|
const struct evm_address* address,
|
2017-06-09 14:16:17 +00:00
|
|
|
const struct evm_uint256be* key);
|
2016-06-24 09:35:33 +00:00
|
|
|
|
2017-06-08 10:00:04 +00:00
|
|
|
/// Set storage callback function.
|
2016-08-21 18:08:42 +00:00
|
|
|
///
|
2017-06-08 10:00:04 +00:00
|
|
|
/// This callback function is used by an EVM to update the given contract
|
2017-06-09 14:16:17 +00:00
|
|
|
/// storage entry.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-06-08 10:00:04 +00:00
|
|
|
/// @param address The address of the contract.
|
|
|
|
/// @param key The index of the storage entry.
|
|
|
|
/// @param value The value to be stored.
|
2017-08-18 13:11:13 +00:00
|
|
|
typedef void (*evm_set_storage_fn)(struct evm_context* context,
|
2017-09-26 12:11:53 +00:00
|
|
|
const struct evm_address* address,
|
2017-06-08 10:00:04 +00:00
|
|
|
const struct evm_uint256be* key,
|
|
|
|
const struct evm_uint256be* value);
|
2016-06-24 09:35:33 +00:00
|
|
|
|
2017-08-07 16:44:43 +00:00
|
|
|
/// Get balance callback function.
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to query the balance of the given
|
|
|
|
/// address.
|
|
|
|
/// @param[out] result The returned balance value.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-08-07 16:44:43 +00:00
|
|
|
/// @param address The address.
|
|
|
|
typedef void (*evm_get_balance_fn)(struct evm_uint256be* result,
|
2017-08-18 13:11:13 +00:00
|
|
|
struct evm_context* context,
|
2017-09-26 12:11:53 +00:00
|
|
|
const struct evm_address* address);
|
2017-08-07 16:44:43 +00:00
|
|
|
|
2017-08-07 18:44:05 +00:00
|
|
|
/// Get code callback function.
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to get the code of a contract of
|
|
|
|
/// given address.
|
2018-01-14 13:33:38 +00:00
|
|
|
///
|
2017-08-18 12:09:55 +00:00
|
|
|
/// @param[out] result_code The pointer to the contract code. This argument is
|
|
|
|
/// optional. If NULL is provided, the host MUST only
|
2018-01-14 13:33:38 +00:00
|
|
|
/// return the code size. It will be freed by the Client.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-08-18 12:09:55 +00:00
|
|
|
/// @param address The address of the contract.
|
|
|
|
/// @return The size of the code.
|
2017-08-07 18:44:05 +00:00
|
|
|
typedef size_t (*evm_get_code_fn)(const uint8_t** result_code,
|
2017-08-18 13:11:13 +00:00
|
|
|
struct evm_context* context,
|
2017-09-26 12:11:53 +00:00
|
|
|
const struct evm_address* address);
|
2017-08-07 18:44:05 +00:00
|
|
|
|
2017-06-08 09:38:08 +00:00
|
|
|
/// Selfdestruct callback function.
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to SELFDESTRUCT given contract.
|
2018-01-14 13:32:49 +00:00
|
|
|
/// The execution of the contract will not be stopped, that is up to the EVM.
|
|
|
|
///
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-06-08 09:38:08 +00:00
|
|
|
/// @param address The address of the contract to be selfdestructed.
|
|
|
|
/// @param beneficiary The address where the remaining ETH is going to be
|
2017-06-08 12:12:52 +00:00
|
|
|
/// transferred.
|
2017-08-18 13:11:13 +00:00
|
|
|
typedef void (*evm_selfdestruct_fn)(struct evm_context* context,
|
2017-09-26 12:11:53 +00:00
|
|
|
const struct evm_address* address,
|
|
|
|
const struct evm_address* beneficiary);
|
2017-06-08 09:38:08 +00:00
|
|
|
|
2017-05-30 13:05:27 +00:00
|
|
|
/// Log callback function.
|
|
|
|
///
|
|
|
|
/// This callback function is used by an EVM to inform about a LOG that happened
|
|
|
|
/// during an EVM bytecode execution.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2017-06-08 12:12:52 +00:00
|
|
|
/// @param address The address of the contract that generated the log.
|
|
|
|
/// @param data The pointer to unindexed data attached to the log.
|
|
|
|
/// @param data_size The length of the data.
|
|
|
|
/// @param topics The pointer to the array of topics attached to the log.
|
|
|
|
/// @param topics_count The number of the topics. Valid values are between
|
|
|
|
/// 0 and 4 inclusively.
|
2018-01-23 09:54:49 +00:00
|
|
|
typedef void (*evm_emit_log_fn)(struct evm_context* context,
|
|
|
|
const struct evm_address* address,
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
const struct evm_uint256be topics[],
|
|
|
|
size_t topics_count);
|
2017-05-30 13:05:27 +00:00
|
|
|
|
2016-06-24 09:35:33 +00:00
|
|
|
/// Pointer to the callback function supporting EVM calls.
|
|
|
|
///
|
2017-12-12 11:57:35 +00:00
|
|
|
/// @param[out] result The result of the call. The result object is not
|
|
|
|
/// initialized by the EVM, the Client MUST correctly
|
|
|
|
/// initialize all expected fields of the structure.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context.
|
|
|
|
/// @see ::evm_context.
|
2018-01-23 13:48:37 +00:00
|
|
|
/// @param msg Call parameters. @see ::evm_message.
|
2017-08-18 13:11:13 +00:00
|
|
|
typedef void (*evm_call_fn)(struct evm_result* result,
|
|
|
|
struct evm_context* context,
|
|
|
|
const struct evm_message* msg);
|
2016-06-24 09:35:33 +00:00
|
|
|
|
2017-09-22 17:27:30 +00:00
|
|
|
/// The context interface.
|
2017-08-01 11:34:25 +00:00
|
|
|
///
|
|
|
|
/// The set of all callback functions expected by EVM instances. This is C
|
2017-09-22 17:27:30 +00:00
|
|
|
/// realisation of vtable for OOP interface (only virtual methods, no data).
|
2017-08-18 13:11:13 +00:00
|
|
|
/// Host implementations SHOULD create constant singletons of this (similarly
|
2017-08-01 11:34:25 +00:00
|
|
|
/// to vtables) to lower the maintenance and memory management cost.
|
2017-09-22 17:27:30 +00:00
|
|
|
struct evm_context_fn_table {
|
2017-08-08 08:26:52 +00:00
|
|
|
evm_account_exists_fn account_exists;
|
2017-08-01 11:34:25 +00:00
|
|
|
evm_get_storage_fn get_storage;
|
|
|
|
evm_set_storage_fn set_storage;
|
2017-08-07 16:44:43 +00:00
|
|
|
evm_get_balance_fn get_balance;
|
2017-08-07 18:44:05 +00:00
|
|
|
evm_get_code_fn get_code;
|
2017-08-01 11:34:25 +00:00
|
|
|
evm_selfdestruct_fn selfdestruct;
|
|
|
|
evm_call_fn call;
|
|
|
|
evm_get_tx_context_fn get_tx_context;
|
|
|
|
evm_get_block_hash_fn get_block_hash;
|
2018-01-23 09:54:49 +00:00
|
|
|
evm_emit_log_fn emit_log;
|
2017-08-01 11:34:25 +00:00
|
|
|
};
|
|
|
|
|
2016-06-24 09:35:33 +00:00
|
|
|
|
2017-09-14 15:28:58 +00:00
|
|
|
/// Execution context managed by the Host.
|
|
|
|
///
|
|
|
|
/// The Host MUST pass the pointer to the execution context to
|
|
|
|
/// ::evm_execute_fn. The EVM MUST pass the same pointer back to the Host in
|
|
|
|
/// every callback function.
|
|
|
|
/// The context MUST contain at least the function table defining the context
|
|
|
|
/// callback interface.
|
|
|
|
/// Optionally, The Host MAY include in the context additional data.
|
|
|
|
struct evm_context {
|
|
|
|
|
2017-09-22 17:27:30 +00:00
|
|
|
/// Function table defining the context interface (vtable).
|
|
|
|
const struct evm_context_fn_table* fn_table;
|
2017-09-14 15:28:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-12-20 15:50:23 +00:00
|
|
|
struct evm_instance; ///< Forward declaration.
|
2016-06-24 09:35:33 +00:00
|
|
|
|
|
|
|
/// Destroys the EVM instance.
|
|
|
|
///
|
|
|
|
/// @param evm The EVM instance to be destroyed.
|
2016-08-23 10:09:39 +00:00
|
|
|
typedef void (*evm_destroy_fn)(struct evm_instance* evm);
|
2016-06-24 09:35:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// Configures the EVM instance.
|
|
|
|
///
|
|
|
|
/// Allows modifying options of the EVM instance.
|
|
|
|
/// Options:
|
|
|
|
/// - code cache behavior: on, off, read-only, ...
|
|
|
|
/// - optimizations,
|
|
|
|
///
|
|
|
|
/// @param evm The EVM instance to be configured.
|
2016-08-31 09:58:19 +00:00
|
|
|
/// @param name The option name. NULL-terminated string. Cannot be NULL.
|
|
|
|
/// @param value The new option value. NULL-terminated string. Cannot be NULL.
|
2016-08-23 09:55:06 +00:00
|
|
|
/// @return 1 if the option set successfully, 0 otherwise.
|
2016-08-23 10:09:39 +00:00
|
|
|
typedef int (*evm_set_option_fn)(struct evm_instance* evm,
|
|
|
|
char const* name,
|
|
|
|
char const* value);
|
2016-06-24 09:35:33 +00:00
|
|
|
|
|
|
|
|
2017-08-20 14:14:35 +00:00
|
|
|
/// EVM revision.
|
|
|
|
///
|
|
|
|
/// The revision of the EVM specification based on the Ethereum
|
|
|
|
/// upgrade / hard fork codenames.
|
2017-08-18 16:32:26 +00:00
|
|
|
enum evm_revision {
|
2016-07-29 09:25:40 +00:00
|
|
|
EVM_FRONTIER = 0,
|
2016-11-22 16:39:15 +00:00
|
|
|
EVM_HOMESTEAD = 1,
|
2017-08-18 16:38:41 +00:00
|
|
|
EVM_TANGERINE_WHISTLE = 2,
|
|
|
|
EVM_SPURIOUS_DRAGON = 3,
|
2017-08-01 14:40:52 +00:00
|
|
|
EVM_BYZANTIUM = 4,
|
|
|
|
EVM_CONSTANTINOPLE = 5,
|
2016-07-28 10:45:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-24 09:35:33 +00:00
|
|
|
/// Generates and executes machine code for given EVM bytecode.
|
|
|
|
///
|
|
|
|
/// All the fun is here. This function actually does something useful.
|
|
|
|
///
|
|
|
|
/// @param instance A EVM instance.
|
2017-08-18 13:11:13 +00:00
|
|
|
/// @param context The pointer to the Host execution context to be passed
|
|
|
|
/// to callback functions. @see ::evm_context.
|
2017-08-20 14:14:35 +00:00
|
|
|
/// @param rev Requested EVM specification revision.
|
2018-01-23 13:48:37 +00:00
|
|
|
/// @param msg Call parameters. @see ::evm_message.
|
2016-06-24 09:35:33 +00:00
|
|
|
/// @param code Reference to the bytecode to be executed.
|
2016-07-20 12:15:04 +00:00
|
|
|
/// @param code_size The length of the bytecode.
|
2016-06-24 09:35:33 +00:00
|
|
|
/// @return All execution results.
|
2016-08-23 10:09:39 +00:00
|
|
|
typedef struct evm_result (*evm_execute_fn)(struct evm_instance* instance,
|
2017-08-18 13:11:13 +00:00
|
|
|
struct evm_context* context,
|
2017-08-20 14:14:35 +00:00
|
|
|
enum evm_revision rev,
|
2017-01-27 15:27:34 +00:00
|
|
|
const struct evm_message* msg,
|
2016-08-23 10:09:39 +00:00
|
|
|
uint8_t const* code,
|
2017-01-27 15:27:34 +00:00
|
|
|
size_t code_size);
|
2016-06-24 09:35:33 +00:00
|
|
|
|
|
|
|
|
2016-12-20 15:50:23 +00:00
|
|
|
/// The EVM instance.
|
2016-08-23 11:50:25 +00:00
|
|
|
///
|
2016-12-20 15:50:23 +00:00
|
|
|
/// Defines the base struct of the EVM implementation.
|
|
|
|
struct evm_instance {
|
2017-09-22 16:50:20 +00:00
|
|
|
|
|
|
|
/// EVM-C ABI version implemented by the EVM instance.
|
|
|
|
///
|
|
|
|
/// For future use to detect ABI incompatibilities. The EVM-C ABI version
|
|
|
|
/// represented by this file is in ::EVM_ABI_VERSION.
|
2017-09-22 17:27:30 +00:00
|
|
|
///
|
|
|
|
/// @todo Consider removing this field.
|
2017-09-22 16:50:20 +00:00
|
|
|
const int abi_version;
|
|
|
|
|
2016-12-20 15:50:23 +00:00
|
|
|
/// Pointer to function destroying the EVM instance.
|
2016-08-23 10:09:39 +00:00
|
|
|
evm_destroy_fn destroy;
|
2016-08-23 11:50:25 +00:00
|
|
|
|
2016-12-20 15:50:23 +00:00
|
|
|
/// Pointer to function executing a code by the EVM instance.
|
2016-08-23 10:09:39 +00:00
|
|
|
evm_execute_fn execute;
|
2016-08-23 11:50:25 +00:00
|
|
|
|
|
|
|
/// Optional pointer to function modifying VM's options.
|
|
|
|
///
|
|
|
|
/// If the VM does not support this feature the pointer can be NULL.
|
2016-08-23 10:09:39 +00:00
|
|
|
evm_set_option_fn set_option;
|
|
|
|
};
|
|
|
|
|
2016-08-31 10:01:28 +00:00
|
|
|
// END Python CFFI declarations
|
|
|
|
|
2017-09-22 17:16:52 +00:00
|
|
|
/// Example of a function creating an instance of an example EVM implementation.
|
2016-08-23 11:50:25 +00:00
|
|
|
///
|
2017-09-22 17:16:52 +00:00
|
|
|
/// Each EVM implementation MUST provide a function returning an EVM instance.
|
|
|
|
/// The function SHOULD be named `<vm-name>_create(void)`.
|
2016-08-23 11:50:25 +00:00
|
|
|
///
|
2017-10-02 16:02:23 +00:00
|
|
|
/// @return EVM instance or NULL indicating instance creation failure.
|
2018-01-24 22:38:17 +00:00
|
|
|
///
|
|
|
|
/// struct evm_instance* examplevm_create(void);
|
2016-08-23 10:58:09 +00:00
|
|
|
|
|
|
|
|
2016-06-24 09:35:33 +00:00
|
|
|
#if __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-23 18:04:08 +00:00
|
|
|
|
|
|
|
#endif // EVM_H
|
2016-06-24 09:35:33 +00:00
|
|
|
/// @}
|