2016-08-22 17:22:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "evm.h"
|
|
|
|
|
|
|
|
struct evm_instance {
|
|
|
|
evm_query_fn query_fn;
|
|
|
|
evm_update_fn update_fn;
|
|
|
|
evm_call_fn call_fn;
|
|
|
|
};
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
static struct evm_instance* evm_create(evm_query_fn query_fn,
|
2016-08-22 17:22:34 +00:00
|
|
|
evm_update_fn update_fn,
|
|
|
|
evm_call_fn call_fn)
|
|
|
|
{
|
|
|
|
struct evm_instance *ret = calloc(1, sizeof(struct evm_instance));
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
ret->query_fn = query_fn;
|
|
|
|
ret->update_fn = update_fn;
|
|
|
|
ret->call_fn = call_fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
static void evm_destroy(struct evm_instance* evm)
|
2016-08-22 17:22:34 +00:00
|
|
|
{
|
|
|
|
free(evm);
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:24:22 +00:00
|
|
|
/// Example options.
|
|
|
|
///
|
|
|
|
/// VMs are allowed to omit this function implementation.
|
|
|
|
int evm_set_option(struct evm_instance* evm,
|
|
|
|
char const* name,
|
|
|
|
char const* value)
|
|
|
|
{
|
|
|
|
if (strcmp(name, "example-option") == 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
static struct evm_result evm_execute(struct evm_instance* instance,
|
2016-08-22 17:22:34 +00:00
|
|
|
struct evm_env* env,
|
|
|
|
enum evm_mode mode,
|
2016-08-31 09:40:02 +00:00
|
|
|
struct evm_uint256be code_hash,
|
2016-08-22 17:22:34 +00:00
|
|
|
uint8_t const* code,
|
|
|
|
size_t code_size,
|
|
|
|
int64_t gas,
|
|
|
|
uint8_t const* input,
|
|
|
|
size_t input_size,
|
2016-08-31 09:40:02 +00:00
|
|
|
struct evm_uint256be value)
|
2016-08-22 17:22:34 +00:00
|
|
|
{
|
|
|
|
struct evm_result ret;
|
|
|
|
|
|
|
|
memset(&ret, 0, sizeof(struct evm_result));
|
|
|
|
|
|
|
|
// Execute code and refer to callbacks: instance->query_fn()
|
|
|
|
|
2016-08-25 11:44:34 +00:00
|
|
|
ret.code = EVM_FAILURE;
|
2016-08-22 17:22:34 +00:00
|
|
|
ret.gas_left = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
static void evm_release_result(struct evm_result const* result)
|
2016-08-22 17:22:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-25 13:01:49 +00:00
|
|
|
struct evm_interface examplevm_get_interface()
|
2016-08-22 17:22:34 +00:00
|
|
|
{
|
2016-08-23 19:51:42 +00:00
|
|
|
struct evm_interface intf;
|
|
|
|
memset(&intf, 0, sizeof(struct evm_result));
|
2016-08-25 14:03:22 +00:00
|
|
|
intf.abi_version = EVM_ABI_VERSION;
|
2016-08-23 19:51:42 +00:00
|
|
|
intf.create = evm_create;
|
|
|
|
intf.destroy = evm_destroy;
|
|
|
|
intf.execute = evm_execute;
|
|
|
|
intf.release_result = evm_release_result;
|
|
|
|
intf.set_option = evm_set_option;
|
|
|
|
return intf;
|
2016-08-23 10:58:09 +00:00
|
|
|
}
|