Fabcoin Core  0.16.2
P2P Digital Currency
capi.c
Go to the documentation of this file.
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "evm.h"
5 
6 
7 struct evm_uint256be balance(struct evm_env* env, struct evm_uint160be address)
8 {
9  struct evm_uint256be ret = {.bytes = {1, 2, 3, 4}};
10  return ret;
11 }
12 
13 struct evm_uint160be address(struct evm_env* env)
14 {
15  struct evm_uint160be ret = {.bytes = {1, 2, 3, 4}};
16  return ret;
17 }
18 
19 static void query(union evm_variant* result,
20  struct evm_env* env,
21  enum evm_query_key key,
22  const union evm_variant* arg) {
23  printf("EVM-C: QUERY %d\n", key);
24  switch (key) {
25  case EVM_GAS_LIMIT:
26  result->int64 = 314;
27  break;
28 
29  case EVM_BALANCE:
30  result->uint256be = balance(env, arg->address);
31  break;
32 
33  case EVM_ADDRESS:
34  result->address = address(env);
35  break;
36 
37  default:
38  result->int64 = 0;
39  }
40 }
41 
42 static void update(struct evm_env* env,
43  enum evm_update_key key,
44  const union evm_variant* arg1,
45  const union evm_variant* arg2)
46 {
47  printf("EVM-C: UPDATE %d\n", key);
48 }
49 
50 static int64_t call(
51  struct evm_env* _opaqueEnv,
52  enum evm_call_kind _kind,
53  int64_t _gas,
54  const struct evm_uint160be* _address,
55  const struct evm_uint256be* _value,
56  uint8_t const* _inputData,
57  size_t _inputSize,
58  uint8_t* _outputData,
59  size_t _outputSize
60 )
61 {
62  printf("EVM-C: CALL %d\n", _kind);
63  return EVM_CALL_FAILURE;
64 }
65 
67 int main(int argc, char *argv[]) {
68  struct evm_factory factory = examplevm_get_factory();
69  if (factory.abi_version != EVM_ABI_VERSION)
70  return 1; // Incompatible ABI version.
71 
72  struct evm_instance* jit = factory.create(query, update, call);
73 
74  uint8_t const code[] = "Place some EVM bytecode here";
75  const size_t code_size = sizeof(code);
76  struct evm_uint256be code_hash = {.bytes = {1, 2, 3,}};
77  uint8_t const input[] = "Hello World!";
78  struct evm_uint256be value = {{1, 0, 0, 0}};
79 
80  int64_t gas = 200000;
81  struct evm_result result =
82  jit->execute(jit, NULL, EVM_HOMESTEAD, code_hash, code, code_size, gas,
83  input, sizeof(input), value);
84 
85  printf("Execution result:\n");
86  if (result.code != EVM_SUCCESS) {
87  printf(" EVM execution failure: %d\n", result.code);
88  } else {
89  printf(" Gas used: %ld\n", gas - result.gas_left);
90  printf(" Gas left: %ld\n", result.gas_left);
91  printf(" Output size: %zd\n", result.output_size);
92 
93  printf(" Output: ");
94  size_t i = 0;
95  for (i = 0; i < result.output_size; i++) {
96  printf("%02x ", result.output_data[i]);
97  }
98  printf("\n");
99  }
100 
101  result.release(&result);
102  jit->destroy(jit);
103 }
uint8_t bytes[32]
The 32 bytes of the big-endian integer or hash.
Definition: evm.h:38
Big-endian 256-bit integer.
Definition: evm.h:36
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:972
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
Definition: capi.c:7
uint8_t const * output_data
The reference to output data.
Definition: evm.h:82
struct evm_uint160be address
An Ethereum address.
Definition: evm.h:151
int64_t gas_left
The amount of gas left after the execution.
Definition: evm.h:78
int main(int argc, char *argv[])
Example how the API is supposed to be used.
Definition: capi.c:67
evm_release_result_fn release
The pointer to the result release implementation.
Definition: evm.h:92
Execution finished with success.
Definition: evm.h:49
bytes code
Definition: SmartVM.cpp:45
struct evm_uint256be uint256be
A big-endian 256-bit integer or hash.
Definition: evm.h:143
int64_t int64
A host-endian 64-bit integer.
Definition: evm.h:140
struct evm_factory examplevm_get_factory()
Example of a function creating uninitialized instance of an example VM.
Definition: examplevm.c:72
Address of the contract for ADDRESS.
Definition: evm.h:111
evm_create_fn create
Pointer to function creating and initializing the EVM instance.
Definition: evm.h:463
evm_update_key
The update callback key.
Definition: evm.h:238
Variant type to represent possible types of values used in EVM.
Definition: evm.h:138
The EVM instance factory.
Definition: evm.h:455
enum evm_result_code code
The execution result code.
Definition: evm.h:73
size_t output_size
The size of the output data.
Definition: evm.h:85
Balance of a given address for BALANCE.
Definition: evm.h:122
evm_destroy_fn destroy
Pointer to function destroying the EVM instance.
Definition: evm.h:431
Big-endian 160-bit hash suitable for keeping an Ethereum address.
Definition: evm.h:42
int abi_version
EVM-C ABI version implemented by the EVM factory and instance.
Definition: evm.h:460
The EVM instance.
Definition: evm.h:429
evm_call_kind
The kind of call-like instruction.
Definition: evm.h:277
evm_execute_fn execute
Pointer to function executing a code by the EVM instance.
Definition: evm.h:434
The EVM code execution result.
Definition: evm.h:71
Current block gas limit for GASLIMIT.
Definition: evm.h:117
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
evm_query_key
The query callback key.
Definition: evm.h:109
uint8_t bytes[20]
The 20 bytes of the hash.
Definition: evm.h:44