2016-08-22 18:22:34 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-04-25 20:07:50 +02:00
|
|
|
#include <limits.h>
|
2017-04-27 00:51:12 +02:00
|
|
|
#include <evm.h>
|
2016-08-22 18:22:34 +01:00
|
|
|
|
2016-12-20 16:50:23 +01:00
|
|
|
|
|
|
|
struct examplevm
|
|
|
|
{
|
|
|
|
struct evm_instance instance;
|
2017-01-20 13:38:11 +01:00
|
|
|
evm_query_state_fn query_fn;
|
2017-06-08 12:00:04 +02:00
|
|
|
evm_set_storage_fn set_storage_fn;
|
2016-08-22 18:22:34 +01:00
|
|
|
evm_call_fn call_fn;
|
2017-01-18 23:26:48 +01:00
|
|
|
evm_get_tx_context_fn get_tx_context_fn;
|
2017-01-19 22:39:21 +01:00
|
|
|
evm_get_block_hash_fn get_block_hash_fn;
|
2017-04-25 20:07:50 +02:00
|
|
|
|
|
|
|
int example_option;
|
2016-08-22 18:22:34 +01:00
|
|
|
};
|
|
|
|
|
2016-08-23 12:58:09 +02:00
|
|
|
static void evm_destroy(struct evm_instance* evm)
|
2016-08-22 18:22:34 +01:00
|
|
|
{
|
|
|
|
free(evm);
|
|
|
|
}
|
|
|
|
|
2016-08-23 21:24:22 +02:00
|
|
|
/// Example options.
|
|
|
|
///
|
|
|
|
/// VMs are allowed to omit this function implementation.
|
2017-04-25 20:07:50 +02:00
|
|
|
int evm_set_option(struct evm_instance* instance,
|
2016-08-23 21:24:22 +02:00
|
|
|
char const* name,
|
|
|
|
char const* value)
|
|
|
|
{
|
2017-04-25 20:07:50 +02:00
|
|
|
struct examplevm* vm = (struct examplevm*)instance;
|
|
|
|
if (strcmp(name, "example-option") == 0) {
|
|
|
|
long int v = strtol(value, NULL, 0);
|
|
|
|
if (v > INT_MAX || v < INT_MIN)
|
|
|
|
return 0;
|
|
|
|
vm->example_option = (int)v;
|
2016-08-23 21:24:22 +02:00
|
|
|
return 1;
|
2017-04-25 20:07:50 +02:00
|
|
|
}
|
|
|
|
|
2016-08-23 21:24:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-27 17:14:19 +02:00
|
|
|
static void evm_release_result(struct evm_result const* result)
|
|
|
|
{
|
2017-04-28 15:26:44 +02:00
|
|
|
(void)result;
|
2016-09-27 17:14:19 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 17:32:28 +02:00
|
|
|
static void free_result_output_data(struct evm_result const* result)
|
|
|
|
{
|
|
|
|
free((uint8_t*)result->output_data);
|
|
|
|
}
|
|
|
|
|
2017-01-27 16:27:34 +01:00
|
|
|
static struct evm_result execute(struct evm_instance* instance,
|
|
|
|
struct evm_env* env,
|
|
|
|
enum evm_mode mode,
|
|
|
|
const struct evm_message* msg,
|
|
|
|
const uint8_t* code,
|
|
|
|
size_t code_size)
|
2016-08-22 18:22:34 +01:00
|
|
|
{
|
2016-09-28 16:50:13 +02:00
|
|
|
struct evm_result ret = {};
|
2017-04-24 17:32:28 +02:00
|
|
|
if (code_size == 0) {
|
|
|
|
// In case of empty code return a fancy error message.
|
2017-01-27 16:27:34 +01:00
|
|
|
const char* error = mode == EVM_METROPOLIS ?
|
|
|
|
"Welcome to Metropolis!" : "Hello Ethereum!";
|
|
|
|
ret.output_data = (const uint8_t*)error;
|
|
|
|
ret.output_size = strlen(error);
|
2017-04-24 17:32:28 +02:00
|
|
|
ret.code = EVM_FAILURE;
|
|
|
|
ret.release = NULL; // We don't need to release the constant messages.
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct examplevm* vm = (struct examplevm*)instance;
|
|
|
|
|
2017-04-25 17:01:46 +02:00
|
|
|
// Simulate executing by checking for some code patterns.
|
|
|
|
// Solidity inline assembly is used in the examples instead of EVM bytecode.
|
2016-08-22 18:22:34 +01:00
|
|
|
|
2017-04-25 17:01:46 +02:00
|
|
|
// Assembly: `{ mstore(0, address()) return(0, msize()) }`.
|
2017-04-25 20:07:50 +02:00
|
|
|
const char return_address[] = "30600052596000f3";
|
|
|
|
|
|
|
|
// Assembly: `{ sstore(0, add(sload(0), 1)) }`
|
|
|
|
const char counter[] = "600160005401600055";
|
|
|
|
|
|
|
|
if (code_size == strlen(return_address) &&
|
|
|
|
strncmp((const char*)code, return_address, code_size)) {
|
|
|
|
static const size_t address_size = sizeof(msg->address);
|
2017-04-24 17:32:28 +02:00
|
|
|
uint8_t* output_data = (uint8_t*)malloc(address_size);
|
2017-04-25 17:01:46 +02:00
|
|
|
if (!output_data) {
|
|
|
|
// malloc failed, report internal error.
|
|
|
|
ret.code = EVM_INTERNAL_ERROR;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-27 16:27:34 +01:00
|
|
|
memcpy(output_data, &msg->address, address_size);
|
2017-04-24 17:32:28 +02:00
|
|
|
ret.code = EVM_SUCCESS;
|
|
|
|
ret.output_data = output_data;
|
|
|
|
ret.output_size = address_size;
|
|
|
|
ret.release = &free_result_output_data;
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-25 20:07:50 +02:00
|
|
|
else if (code_size == strlen(counter) &&
|
|
|
|
strncmp((const char*)code, counter, code_size)) {
|
|
|
|
union evm_variant value;
|
2017-05-08 14:55:04 +02:00
|
|
|
const struct evm_uint256be index = {{0,}};
|
2017-04-25 20:07:50 +02:00
|
|
|
vm->query_fn(&value, env, EVM_SLOAD, &msg->address, &index);
|
|
|
|
value.uint256be.bytes[31] += 1;
|
2017-06-08 12:00:04 +02:00
|
|
|
vm->set_storage_fn(env, &msg->address, &index, &value.uint256be);
|
2017-04-25 20:07:50 +02:00
|
|
|
ret.code = EVM_SUCCESS;
|
|
|
|
return ret;
|
|
|
|
}
|
2016-08-22 18:22:34 +01:00
|
|
|
|
2016-09-27 17:14:19 +02:00
|
|
|
ret.release = evm_release_result;
|
2016-08-25 13:44:34 +02:00
|
|
|
ret.code = EVM_FAILURE;
|
2016-08-22 18:22:34 +01:00
|
|
|
ret.gas_left = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-01-20 13:38:11 +01:00
|
|
|
static struct evm_instance* evm_create(evm_query_state_fn query_fn,
|
2017-06-08 12:00:04 +02:00
|
|
|
evm_set_storage_fn update_fn,
|
2017-06-08 11:38:08 +02:00
|
|
|
evm_selfdestruct_fn selfdestruct_fn,
|
2017-01-18 23:26:48 +01:00
|
|
|
evm_call_fn call_fn,
|
2017-01-19 22:39:21 +01:00
|
|
|
evm_get_tx_context_fn get_tx_context_fn,
|
2017-05-30 15:05:27 +02:00
|
|
|
evm_get_block_hash_fn get_block_hash_fn,
|
|
|
|
evm_log_fn log_fn)
|
2016-12-20 16:50:23 +01:00
|
|
|
{
|
|
|
|
struct examplevm* vm = calloc(1, sizeof(struct examplevm));
|
|
|
|
struct evm_instance* interface = &vm->instance;
|
|
|
|
interface->destroy = evm_destroy;
|
2017-01-27 16:27:34 +01:00
|
|
|
interface->execute = execute;
|
2016-12-20 16:50:23 +01:00
|
|
|
interface->set_option = evm_set_option;
|
|
|
|
vm->query_fn = query_fn;
|
2017-06-08 12:00:04 +02:00
|
|
|
vm->set_storage_fn = update_fn;
|
2016-12-20 16:50:23 +01:00
|
|
|
vm->call_fn = call_fn;
|
2017-01-18 23:26:48 +01:00
|
|
|
vm->get_tx_context_fn = get_tx_context_fn;
|
2017-01-19 22:39:21 +01:00
|
|
|
vm->get_block_hash_fn = get_block_hash_fn;
|
2016-12-20 16:50:23 +01:00
|
|
|
return interface;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct evm_factory examplevm_get_factory()
|
2016-08-22 18:22:34 +01:00
|
|
|
{
|
2016-12-20 16:50:23 +01:00
|
|
|
struct evm_factory factory = {EVM_ABI_VERSION, evm_create};
|
|
|
|
return factory;
|
2016-09-28 16:50:13 +02:00
|
|
|
}
|