EVMC
helpers.h
Go to the documentation of this file.
1 /* EVMC: Ethereum Client-VM Connector API.
2  * Copyright 2018-2019 The EVMC Authors.
3  * Licensed under the Apache License, Version 2.0.
4  */
5 
18 #pragma once
19 
20 #include <evmc/evmc.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
27 static inline bool evmc_is_abi_compatible(struct evmc_vm* vm)
28 {
29  return vm->abi_version == EVMC_ABI_VERSION;
30 }
31 
35 static inline const char* evmc_vm_name(struct evmc_vm* vm)
36 {
37  return vm->name;
38 }
39 
43 static inline const char* evmc_vm_version(struct evmc_vm* vm)
44 {
45  return vm->version;
46 }
47 
53 static inline bool evmc_vm_has_capability(struct evmc_vm* vm, enum evmc_capabilities capability)
54 {
55  return (vm->get_capabilities(vm) & (evmc_capabilities_flagset)capability) != 0;
56 }
57 
63 static inline void evmc_destroy(struct evmc_vm* vm)
64 {
65  vm->destroy(vm);
66 }
67 
73 static inline enum evmc_set_option_result evmc_set_option(struct evmc_vm* vm,
74  char const* name,
75  char const* value)
76 {
77  if (vm->set_option)
78  return vm->set_option(vm, name, value);
79  return EVMC_SET_OPTION_INVALID_NAME;
80 }
81 
87 static inline struct evmc_result evmc_execute(struct evmc_vm* vm,
88  const struct evmc_host_interface* host,
89  struct evmc_host_context* context,
90  enum evmc_revision rev,
91  const struct evmc_message* msg,
92  uint8_t const* code,
93  size_t code_size)
94 {
95  return vm->execute(vm, host, context, rev, msg, code, code_size);
96 }
97 
104 static void evmc_free_result_memory(const struct evmc_result* result)
105 {
106  free((uint8_t*)result->output_data);
107 }
108 
122  int64_t gas_left,
123  const uint8_t* output_data,
124  size_t output_size)
125 {
126  struct evmc_result result;
127  memset(&result, 0, sizeof(result));
128 
129  if (output_size != 0)
130  {
131  uint8_t* buffer = (uint8_t*)malloc(output_size);
132 
133  if (!buffer)
134  {
136  return result;
137  }
138 
139  memcpy(buffer, output_data, output_size);
140  result.output_data = buffer;
141  result.output_size = output_size;
143  }
144 
145  result.status_code = status_code;
146  result.gas_left = gas_left;
147  return result;
148 }
149 
157 static inline void evmc_release_result(struct evmc_result* result)
158 {
159  if (result->release)
160  result->release(result);
161 }
162 
163 
191 {
192  uint8_t bytes[24];
193  void* pointer;
194 };
195 
198  struct evmc_result* result)
199 {
200  return (union evmc_result_optional_storage*)&result->create_address;
201 }
202 
205  const struct evmc_result* result)
206 {
207  return (const union evmc_result_optional_storage*)&result->create_address;
208 }
209 
static bool evmc_vm_has_capability(struct evmc_vm *vm, enum evmc_capabilities capability)
Checks if the VM has the given capability.
Definition: helpers.h:53
static struct evmc_result evmc_make_result(enum evmc_status_code status_code, int64_t gas_left, const uint8_t *output_data, size_t output_size)
Creates the result from the provided arguments.
Definition: helpers.h:121
static const union evmc_result_optional_storage * evmc_get_const_optional_storage(const struct evmc_result *result)
Provides read-only access to evmc_result "optional storage".
Definition: helpers.h:204
static const char * evmc_vm_name(struct evmc_vm *vm)
Returns the name of the VM.
Definition: helpers.h:35
static const char * evmc_vm_version(struct evmc_vm *vm)
Returns the version of the VM.
Definition: helpers.h:43
The union representing evmc_result "optional storage".
Definition: helpers.h:190
evmc_status_code
The execution status code.
Definition: evmc.h:211
const char * name
The name of the EVMC VM implementation.
Definition: evmc.h:871
The VM failed to allocate the amount of memory needed for execution.
Definition: evmc.h:315
enum evmc_status_code status_code
The execution status code.
Definition: evmc.h:343
const int abi_version
EVMC ABI version implemented by the VM instance.
Definition: evmc.h:863
const char * version
The version of the EVMC VM implementation, e.g.
Definition: evmc.h:879
static void evmc_release_result(struct evmc_result *result)
Releases the resources allocated to the execution result.
Definition: helpers.h:157
evmc_release_result_fn release
The method releasing all resources associated with the result object.
Definition: evmc.h:392
EVMC: Ethereum Client-VM Connector API.
uint8_t bytes[24]
24 bytes of optional storage.
Definition: helpers.h:192
size_t output_size
The size of the output data.
Definition: evmc.h:371
int64_t gas_left
The amount of gas left after the execution.
Definition: evmc.h:351
The EVMC ABI version number of the interface declared in this file.
Definition: evmc.h:47
evmc_address create_address
The address of the contract created by create instructions.
Definition: evmc.h:403
The Host interface.
Definition: evmc.h:621
static void evmc_destroy(struct evmc_vm *vm)
Destroys the VM instance.
Definition: helpers.h:63
The VM instance.
Definition: evmc.h:855
uint32_t evmc_capabilities_flagset
Alias for unsigned integer representing a set of bit flags of EVMC capabilities.
Definition: evmc.h:836
The opaque data type representing the Host execution context.
static enum evmc_set_option_result evmc_set_option(struct evmc_vm *vm, char const *name, char const *value)
Sets the option for the VM, if the feature is supported by the VM.
Definition: helpers.h:73
static struct evmc_result evmc_execute(struct evmc_vm *vm, const struct evmc_host_interface *host, struct evmc_host_context *context, enum evmc_revision rev, const struct evmc_message *msg, uint8_t const *code, size_t code_size)
Executes code in the VM instance.
Definition: helpers.h:87
static bool evmc_is_abi_compatible(struct evmc_vm *vm)
Returns true if the VM has a compatible ABI version.
Definition: helpers.h:27
The message describing an EVM call, including a zero-depth calls from a transaction origin...
Definition: evmc.h:95
evmc_set_option_result
Possible outcomes of evmc_set_option.
Definition: evmc.h:674
evmc_set_option_fn set_option
Optional pointer to function modifying VM&#39;s options.
Definition: evmc.h:912
void * pointer
Optional pointer.
Definition: helpers.h:193
The EVM code execution result.
Definition: evmc.h:340
evmc_destroy_fn destroy
Pointer to function destroying the VM instance.
Definition: evmc.h:886
const uint8_t * output_data
The reference to output data.
Definition: evmc.h:364
static union evmc_result_optional_storage * evmc_get_optional_storage(struct evmc_result *result)
Provides read-write access to evmc_result "optional storage".
Definition: helpers.h:197
static void evmc_free_result_memory(const struct evmc_result *result)
The evmc_result release function using free() for releasing the memory.
Definition: helpers.h:104
evmc_revision
EVM revision.
Definition: evmc.h:705
evmc_get_capabilities_fn get_capabilities
A method returning capabilities supported by the VM instance.
Definition: evmc.h:905
evmc_capabilities
Possible capabilities of a VM.
Definition: evmc.h:806