Update EVM-C examples

This commit is contained in:
Paweł Bylica 2016-08-23 12:58:09 +02:00
parent 5defa1d222
commit efb73335c2
3 changed files with 29 additions and 31 deletions

View File

@ -1,8 +1,12 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h>
#include "evm.h" #include "evm.h"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wunused-parameter"
struct evm_uint256 balance(struct evm_env* env, struct evm_hash160 address) struct evm_uint256 balance(struct evm_env* env, struct evm_hash160 address)
{ {
struct evm_uint256 ret = { .words = { 1 } }; struct evm_uint256 ret = { .words = { 1 } };
@ -64,7 +68,8 @@ int64_t call(
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
printf("Using VM: %s (%s)\n", evm_get_info(EVM_NAME), evm_get_info(EVM_VERSION)); printf("Using VM: %s (%s)\n", evm_get_info(EVM_NAME), evm_get_info(EVM_VERSION));
struct evm_instance* jit = evm_create(query, update, call); struct evm_fn_table ftab = examplevm_get_fn_table();
struct evm_instance* jit = ftab.create(query, update, call);
char const code[] = "exec()"; char const code[] = "exec()";
const size_t code_size = sizeof(code); const size_t code_size = sizeof(code);
@ -74,23 +79,24 @@ int main(int argc, char *argv[]) {
int64_t gas = 200000; int64_t gas = 200000;
struct evm_result result = struct evm_result result =
evm_execute(jit, NULL, EVM_HOMESTEAD, code_hash, (const uint8_t *)code, code_size, gas, (const uint8_t *)input, ftab.execute(jit, NULL, EVM_HOMESTEAD, code_hash, (const uint8_t *)code, code_size, gas, (const uint8_t *)input,
sizeof(input), value); sizeof(input), value);
printf("Execution result:\n"); printf("Execution result:\n");
if (result.gas_left & EVM_EXCEPTION) { if (result.gas_left & EVM_EXCEPTION) {
printf(" EVM eception\n"); printf(" EVM eception\n");
} }
printf(" Gas used: %lld\n", gas - result.gas_left); printf(" Gas used: " PRId64 "\n", gas - result.gas_left);
printf(" Gas left: %lld\n", result.gas_left); printf(" Gas left: " PRId64 "\n", result.gas_left);
printf(" Output size: %zd\n", result.output_size); printf(" Output size: %zd\n", result.output_size);
printf(" Output: "); printf(" Output: ");
for (int i = 0; i < result.output_size; i++) { size_t i = 0;
for (i = 0; i < result.output_size; i++) {
printf("%02x ", result.output_data[i]); printf("%02x ", result.output_data[i]);
} }
printf("\n"); printf("\n");
evm_destroy_result(result); ftab.release_result(&result);
evm_destroy(jit); ftab.destroy(jit);
} }

View File

@ -3,6 +3,9 @@
#include <stdbool.h> #include <stdbool.h>
#include "evm.h" #include "evm.h"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wunused-parameter"
struct evm_instance { struct evm_instance {
evm_query_fn query_fn; evm_query_fn query_fn;
evm_update_fn update_fn; evm_update_fn update_fn;
@ -19,7 +22,7 @@ EXPORT char const* evm_get_info(enum evm_info_key key)
return ""; return "";
} }
EXPORT struct evm_instance* evm_create(evm_query_fn query_fn, static struct evm_instance* evm_create(evm_query_fn query_fn,
evm_update_fn update_fn, evm_update_fn update_fn,
evm_call_fn call_fn) evm_call_fn call_fn)
{ {
@ -34,19 +37,12 @@ EXPORT struct evm_instance* evm_create(evm_query_fn query_fn,
return ret; return ret;
} }
EXPORT void evm_destroy(struct evm_instance* evm) static void evm_destroy(struct evm_instance* evm)
{ {
free(evm); free(evm);
} }
EXPORT int evm_set_option(struct evm_instance* evm, static struct evm_result evm_execute(struct evm_instance* instance,
char const* name,
char const* value)
{
return 0;
}
EXPORT struct evm_result evm_execute(struct evm_instance* instance,
struct evm_env* env, struct evm_env* env,
enum evm_mode mode, enum evm_mode mode,
struct evm_hash256 code_hash, struct evm_hash256 code_hash,
@ -68,21 +64,13 @@ EXPORT struct evm_result evm_execute(struct evm_instance* instance,
return ret; return ret;
} }
EXPORT void evm_destroy_result(struct evm_result result) static void evm_release_result(struct evm_result const* result)
{ {
} }
EXPORT bool evmjit_is_code_ready(struct evm_instance* instance, EXPORT struct evm_fn_table examplevm_get_fn_table()
enum evm_mode mode,
struct evm_hash256 code_hash)
{
return true;
}
EXPORT void evmjit_compile(struct evm_instance* instance,
enum evm_mode mode,
uint8_t const* code,
size_t code_size,
struct evm_hash256 code_hash)
{ {
struct evm_fn_table ftab = {evm_create, evm_destroy, evm_execute,
evm_release_result, 0, 0, 0};
return ftab;
} }

View File

@ -384,6 +384,10 @@ struct evm_fn_table {
evm_set_option_fn set_option; evm_set_option_fn set_option;
}; };
struct evm_fn_table examplevm_get_fn_table();
EXPORT struct evm_fn_table evmjit_get_fn_table(); EXPORT struct evm_fn_table evmjit_get_fn_table();