2018-04-18 14:22:24 +00:00
|
|
|
#include <evmc/evmc.h>
|
2018-01-24 22:38:17 +00:00
|
|
|
|
2017-12-11 19:12:39 +00:00
|
|
|
#include <inttypes.h>
|
2016-08-22 20:51:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-05-11 11:24:33 +00:00
|
|
|
#include <string.h>
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2018-04-17 09:02:00 +00:00
|
|
|
#include "examplevm/examplevm.h"
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
|
2018-05-11 11:24:33 +00:00
|
|
|
struct evmc_uint256be balance(struct evmc_context* context, const struct evmc_address* address)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
|
|
|
(void)address;
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_uint256be ret = {.bytes = {1, 2, 3, 4}};
|
2016-08-22 20:51:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void print_address(const struct evmc_address* address)
|
2017-06-08 12:12:52 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < sizeof(address->bytes); ++i)
|
|
|
|
printf("%x", address->bytes[i] & 0xff);
|
|
|
|
}
|
|
|
|
|
2018-05-11 11:24:33 +00:00
|
|
|
static int account_exists(struct evmc_context* context, const struct evmc_address* address)
|
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
2017-08-08 08:26:52 +00:00
|
|
|
printf("EVM-C: EXISTS @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
2016-06-24 09:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void get_storage(struct evmc_uint256be* result,
|
|
|
|
struct evmc_context* context,
|
|
|
|
const struct evmc_address* address,
|
|
|
|
const struct evmc_uint256be* key)
|
2017-06-09 14:16:17 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)result;
|
|
|
|
(void)context;
|
|
|
|
(void)key;
|
2017-06-09 14:16:17 +00:00
|
|
|
printf("EVM-C: SLOAD @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:34:15 +00:00
|
|
|
static enum evmc_storage_status set_storage(struct evmc_context* context,
|
|
|
|
const struct evmc_address* address,
|
|
|
|
const struct evmc_uint256be* key,
|
|
|
|
const struct evmc_uint256be* value)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
|
|
|
(void)key;
|
|
|
|
(void)value;
|
2017-06-08 12:12:52 +00:00
|
|
|
printf("EVM-C: SSTORE @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
2018-08-08 13:34:15 +00:00
|
|
|
return EVMC_STORAGE_UNCHANGED;
|
2017-06-08 12:12:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void get_balance(struct evmc_uint256be* result,
|
|
|
|
struct evmc_context* context,
|
|
|
|
const struct evmc_address* address)
|
2017-08-07 16:44:43 +00:00
|
|
|
{
|
|
|
|
printf("EVM-C: BALANCE @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
2017-08-18 13:11:13 +00:00
|
|
|
*result = balance(context, address);
|
2017-08-07 16:44:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static size_t get_code_size(struct evmc_context* context, const struct evmc_address* address)
|
2018-03-26 10:44:34 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
2018-03-26 10:44:34 +00:00
|
|
|
printf("EVM-C: CODESIZE @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-31 10:49:08 +00:00
|
|
|
static void get_code_hash(struct evmc_uint256be* result,
|
|
|
|
struct evmc_context* context,
|
|
|
|
const struct evmc_address* address)
|
|
|
|
{
|
|
|
|
(void)result;
|
|
|
|
(void)context;
|
|
|
|
printf("EVM-C: CODEHASH @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2018-05-11 11:24:33 +00:00
|
|
|
static size_t copy_code(struct evmc_context* context,
|
|
|
|
const struct evmc_address* address,
|
|
|
|
size_t code_offset,
|
|
|
|
uint8_t* buffer_data,
|
|
|
|
size_t buffer_size)
|
2017-08-07 18:44:05 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
|
|
|
(void)code_offset;
|
|
|
|
(void)buffer_data;
|
|
|
|
(void)buffer_size;
|
2018-03-26 13:03:19 +00:00
|
|
|
printf("EVM-C: COPYCODE @");
|
2017-08-07 18:44:05 +00:00
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void selfdestruct(struct evmc_context* context,
|
|
|
|
const struct evmc_address* address,
|
|
|
|
const struct evmc_address* beneficiary)
|
2017-06-08 12:12:52 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
2017-06-08 12:12:52 +00:00
|
|
|
printf("EVM-C: SELFDESTRUCT ");
|
|
|
|
print_address(address);
|
|
|
|
printf(" -> ");
|
|
|
|
print_address(beneficiary);
|
|
|
|
printf("\n");
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static void call(struct evmc_result* result,
|
|
|
|
struct evmc_context* context,
|
|
|
|
const struct evmc_message* msg)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
2017-04-28 13:26:44 +00:00
|
|
|
printf("EVM-C: CALL (depth: %d)\n", msg->depth);
|
2018-03-28 13:09:07 +00:00
|
|
|
result->status_code = EVMC_FAILURE;
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 11:20:18 +00:00
|
|
|
static void get_tx_context(struct evmc_tx_context* result, struct evmc_context* context)
|
|
|
|
{
|
|
|
|
(void)result;
|
|
|
|
(void)context;
|
|
|
|
}
|
2017-01-18 22:26:48 +00:00
|
|
|
|
2018-05-11 11:24:33 +00:00
|
|
|
static void get_block_hash(struct evmc_uint256be* result,
|
|
|
|
struct evmc_context* context,
|
2017-01-19 21:39:21 +00:00
|
|
|
int64_t number)
|
2018-06-25 11:20:18 +00:00
|
|
|
{
|
|
|
|
(void)result;
|
|
|
|
(void)context;
|
|
|
|
(void)number;
|
|
|
|
}
|
2017-01-19 21:39:21 +00:00
|
|
|
|
2017-06-08 12:12:52 +00:00
|
|
|
/// EVM log callback.
|
2018-08-08 13:39:40 +00:00
|
|
|
static void emit_log(struct evmc_context* context,
|
|
|
|
const struct evmc_address* address,
|
|
|
|
const uint8_t* data,
|
|
|
|
size_t data_size,
|
|
|
|
const struct evmc_uint256be topics[],
|
|
|
|
size_t topics_count)
|
2017-06-08 12:12:52 +00:00
|
|
|
{
|
2018-06-25 11:20:18 +00:00
|
|
|
(void)context;
|
|
|
|
(void)address;
|
|
|
|
(void)data;
|
|
|
|
(void)data_size;
|
|
|
|
(void)topics;
|
2017-06-08 12:12:52 +00:00
|
|
|
printf("EVM-C: LOG%d\n", (int)topics_count);
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
static const struct evmc_context_fn_table ctx_fn_table = {
|
2018-07-31 10:49:08 +00:00
|
|
|
account_exists, get_storage, set_storage, get_balance, get_code_size, get_code_hash,
|
2018-08-08 13:39:40 +00:00
|
|
|
copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log,
|
2017-08-01 11:34:25 +00:00
|
|
|
};
|
|
|
|
|
2016-06-24 09:40:06 +00:00
|
|
|
/// Example how the API is supposed to be used.
|
2018-06-25 11:20:18 +00:00
|
|
|
int main()
|
2018-05-11 11:24:33 +00:00
|
|
|
{
|
2018-08-20 04:23:01 +00:00
|
|
|
struct evmc_instance* vm = evmc_create_examplevm();
|
|
|
|
if (vm->abi_version != EVMC_ABI_VERSION)
|
2017-09-22 16:50:20 +00:00
|
|
|
return 1; // Incompatible ABI version.
|
2016-12-20 15:50:23 +00:00
|
|
|
|
2018-06-25 11:20:18 +00:00
|
|
|
const uint8_t code[] = "Place some EVM bytecode here";
|
2016-08-22 20:51:43 +00:00
|
|
|
const size_t code_size = sizeof(code);
|
2018-06-25 11:20:18 +00:00
|
|
|
const struct evmc_uint256be code_hash = {.bytes = {1, 2, 3}};
|
|
|
|
const uint8_t input[] = "Hello World!";
|
|
|
|
const struct evmc_uint256be value = {{1, 0}};
|
|
|
|
const struct evmc_address addr = {{0, 1, 2}};
|
|
|
|
const int64_t gas = 200000;
|
2017-01-12 15:45:14 +00:00
|
|
|
|
2018-03-28 13:09:07 +00:00
|
|
|
struct evmc_context ctx = {&ctx_fn_table};
|
2017-09-22 17:27:30 +00:00
|
|
|
|
2018-06-25 11:20:18 +00:00
|
|
|
struct evmc_message msg;
|
|
|
|
msg.sender = addr;
|
|
|
|
msg.destination = addr;
|
|
|
|
msg.value = value;
|
|
|
|
msg.input_data = input;
|
|
|
|
msg.input_size = sizeof(input);
|
|
|
|
msg.code_hash = code_hash;
|
|
|
|
msg.gas = gas;
|
|
|
|
msg.depth = 0;
|
2017-01-12 15:45:14 +00:00
|
|
|
|
2018-08-20 04:23:01 +00:00
|
|
|
struct evmc_result result = vm->execute(vm, &ctx, EVMC_HOMESTEAD, &msg, code, code_size);
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2016-08-22 20:51:43 +00:00
|
|
|
printf("Execution result:\n");
|
2018-05-11 11:24:33 +00:00
|
|
|
if (result.status_code != EVMC_SUCCESS)
|
|
|
|
{
|
|
|
|
printf(" EVM execution failure: %d\n", result.status_code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-11 19:12:39 +00:00
|
|
|
printf(" Gas used: %" PRId64 "\n", gas - result.gas_left);
|
|
|
|
printf(" Gas left: %" PRId64 "\n", result.gas_left);
|
2016-08-24 11:43:35 +00:00
|
|
|
printf(" Output size: %zd\n", result.output_size);
|
|
|
|
|
|
|
|
printf(" Output: ");
|
|
|
|
size_t i = 0;
|
2018-05-11 11:24:33 +00:00
|
|
|
for (i = 0; i < result.output_size; i++)
|
2016-08-24 11:43:35 +00:00
|
|
|
printf("%02x ", result.output_data[i]);
|
|
|
|
printf("\n");
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-24 16:10:30 +00:00
|
|
|
if (result.release)
|
|
|
|
result.release(&result);
|
2018-08-20 04:23:01 +00:00
|
|
|
vm->destroy(vm);
|
2017-10-24 11:50:35 +00:00
|
|
|
|
|
|
|
return 0;
|
2016-06-24 09:40:06 +00:00
|
|
|
}
|