Fabcoin Core  0.16.2
P2P Digital Currency
examplevm.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include "evm.h"
4 
5 
6 struct examplevm
7 {
12 };
13 
14 static void evm_destroy(struct evm_instance* evm)
15 {
16  free(evm);
17 }
18 
22 int evm_set_option(struct evm_instance* evm,
23  char const* name,
24  char const* value)
25 {
26  if (strcmp(name, "example-option") == 0)
27  return 1;
28  return 0;
29 }
30 
31 static void evm_release_result(struct evm_result const* result)
32 {
33 }
34 
35 static struct evm_result evm_execute(struct evm_instance* instance,
36  struct evm_env* env,
37  enum evm_mode mode,
38  struct evm_uint256be code_hash,
39  uint8_t const* code,
40  size_t code_size,
41  int64_t gas,
42  uint8_t const* input,
43  size_t input_size,
44  struct evm_uint256be value)
45 {
46  struct evm_result ret = {};
47 
48  // Execute code and refer to callbacks: instance->query_fn()
49 
50  ret.release = evm_release_result;
51  ret.code = EVM_FAILURE;
52  ret.gas_left = 0;
53 
54  return ret;
55 }
56 
57 static struct evm_instance* evm_create(evm_query_fn query_fn,
60 {
61  struct examplevm* vm = calloc(1, sizeof(struct examplevm));
62  struct evm_instance* interface = &vm->instance;
63  interface->destroy = evm_destroy;
64  interface->execute = evm_execute;
65  interface->set_option = evm_set_option;
66  vm->query_fn = query_fn;
67  vm->update_fn = update_fn;
68  vm->call_fn = call_fn;
69  return interface;
70 }
71 
73 {
74  struct evm_factory factory = {EVM_ABI_VERSION, evm_create};
75  return factory;
76 }
evm_update_fn update_fn
Definition: examplevm.c:10
int evm_set_option(struct evm_instance *evm, char const *name, char const *value)
Example options.
Definition: examplevm.c:22
Big-endian 256-bit integer.
Definition: evm.h:36
evm_mode mode
Definition: SmartVM.cpp:47
struct evm_instance instance
Definition: examplevm.c:8
int64_t gas_left
The amount of gas left after the execution.
Definition: evm.h:78
evm_release_result_fn release
The pointer to the result release implementation.
Definition: evm.h:92
bytes code
Definition: SmartVM.cpp:45
struct evm_factory examplevm_get_factory()
Example of a function creating uninitialized instance of an example VM.
Definition: examplevm.c:72
The EVM instance factory.
Definition: evm.h:455
enum evm_result_code code
The execution result code.
Definition: evm.h:73
const char * name
Definition: rest.cpp:36
evm_destroy_fn destroy
Pointer to function destroying the EVM instance.
Definition: evm.h:431
The EVM instance.
Definition: evm.h:429
evm_mode
EVM compatibility mode aka chain mode.
Definition: evm.h:359
evm_call_fn call_fn
Definition: examplevm.c:11
The EVM code execution result.
Definition: evm.h:71
void(* evm_update_fn)(struct evm_env *env, enum evm_update_key key, const union evm_variant *arg1, const union evm_variant *arg2)
Update callback function.
Definition: evm.h:271
void(* evm_query_fn)(union evm_variant *result, struct evm_env *env, enum evm_query_key key, const union evm_variant *arg)
Query callback function.
Definition: evm.h:232
int64_t(* evm_call_fn)(struct evm_env *env, enum evm_call_kind kind, int64_t gas, const struct evm_uint160be *address, const struct evm_uint256be *value, uint8_t const *input, size_t input_size, uint8_t *output, size_t output_size)
Pointer to the callback function supporting EVM calls.
Definition: evm.h:308
Generic execution failure.
Definition: evm.h:50
evm_query_fn query_fn
Definition: examplevm.c:9