2018-08-30 12:49:20 +00:00
|
|
|
/* EVMC: Ethereum Client-VM Connector API.
|
2019-04-24 14:12:13 +00:00
|
|
|
* Copyright 2016-2019 The EVMC Authors.
|
|
|
|
* Licensed under the Apache License, Version 2.0.
|
2018-08-30 12:49:20 +00:00
|
|
|
*/
|
|
|
|
|
2018-08-30 16:16:15 +00:00
|
|
|
#include "example_host.h"
|
2019-08-08 10:42:43 +00:00
|
|
|
#ifdef STATICALLY_LINKED_EXAMPLE
|
2019-06-28 13:29:28 +00:00
|
|
|
#include "example_vm/example_vm.h"
|
2019-08-08 10:42:43 +00:00
|
|
|
#endif
|
2018-08-31 11:08:26 +00:00
|
|
|
|
2018-08-20 04:24:21 +00:00
|
|
|
#include <evmc/helpers.h>
|
2019-08-08 10:42:43 +00:00
|
|
|
#include <evmc/loader.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>
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2019-08-08 10:51:41 +00:00
|
|
|
int main(int argc, char* argv[])
|
2018-05-11 11:24:33 +00:00
|
|
|
{
|
2019-08-08 10:42:43 +00:00
|
|
|
#ifdef STATICALLY_LINKED_EXAMPLE
|
2019-08-08 10:51:41 +00:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2019-09-24 21:59:08 +00:00
|
|
|
struct evmc_vm* vm = evmc_create_example_vm();
|
2019-08-08 10:42:43 +00:00
|
|
|
if (!vm)
|
2019-09-24 21:59:08 +00:00
|
|
|
return EVMC_LOADER_VM_CREATION_FAILURE;
|
2018-08-20 13:46:31 +00:00
|
|
|
if (!evmc_is_abi_compatible(vm))
|
2019-08-08 10:42:43 +00:00
|
|
|
return EVMC_LOADER_ABI_VERSION_MISMATCH;
|
|
|
|
#else
|
2019-08-08 10:51:41 +00:00
|
|
|
const char* config_string = (argc > 1) ? argv[1] : "example-vm.so";
|
2019-08-08 10:42:43 +00:00
|
|
|
enum evmc_loader_error_code error_code;
|
2019-09-24 21:59:08 +00:00
|
|
|
struct evmc_vm* vm = evmc_load_and_configure(config_string, &error_code);
|
2019-08-08 10:42:43 +00:00
|
|
|
if (!vm)
|
|
|
|
{
|
|
|
|
printf("Loading error: %d\n", error_code);
|
|
|
|
// NOTE: the values are small enough for casting
|
|
|
|
return (int)error_code;
|
|
|
|
}
|
|
|
|
#endif
|
2019-07-23 12:00:20 +00:00
|
|
|
|
2018-08-30 16:27:44 +00:00
|
|
|
// EVM bytecode goes here. This is one of the examples.
|
2019-08-08 15:31:02 +00:00
|
|
|
const uint8_t code[] = "\x43\x60\x00\x55\x43\x60\x00\x52\x59\x60\x00\xf3";
|
2019-07-23 12:00:20 +00:00
|
|
|
const size_t code_size = sizeof(code) - 1;
|
2018-06-25 11:20:18 +00:00
|
|
|
const uint8_t input[] = "Hello World!";
|
2018-09-06 17:01:46 +00:00
|
|
|
const evmc_uint256be value = {{1, 0}};
|
2018-09-06 21:30:48 +00:00
|
|
|
const evmc_address addr = {{0, 1, 2}};
|
2018-06-25 11:20:18 +00:00
|
|
|
const int64_t gas = 200000;
|
2019-08-08 15:18:20 +00:00
|
|
|
struct evmc_tx_context tx_context;
|
|
|
|
memset(&tx_context, 0, sizeof(tx_context));
|
2019-08-08 15:21:05 +00:00
|
|
|
tx_context.block_number = 42;
|
|
|
|
tx_context.block_timestamp = 66;
|
|
|
|
tx_context.block_gas_limit = gas * 2;
|
2019-10-31 09:38:17 +00:00
|
|
|
const struct evmc_host_interface* host = example_host_get_interface();
|
2019-09-20 09:29:46 +00:00
|
|
|
struct evmc_host_context* ctx = example_host_create_context(tx_context);
|
2018-06-25 11:20:18 +00:00
|
|
|
struct evmc_message msg;
|
2019-08-08 19:36:39 +00:00
|
|
|
msg.kind = EVMC_CALL;
|
2018-06-25 11:20:18 +00:00
|
|
|
msg.sender = addr;
|
|
|
|
msg.destination = addr;
|
|
|
|
msg.value = value;
|
|
|
|
msg.input_data = input;
|
|
|
|
msg.input_size = sizeof(input);
|
|
|
|
msg.gas = gas;
|
|
|
|
msg.depth = 0;
|
2019-10-31 09:38:17 +00:00
|
|
|
struct evmc_result result = evmc_execute(vm, host, ctx, EVMC_HOMESTEAD, &msg, code, code_size);
|
2016-08-22 20:51:43 +00:00
|
|
|
printf("Execution result:\n");
|
2019-07-23 12:00:20 +00:00
|
|
|
int exit_code = 0;
|
2018-05-11 11:24:33 +00:00
|
|
|
if (result.status_code != EVMC_SUCCESS)
|
|
|
|
{
|
|
|
|
printf(" EVM execution failure: %d\n", result.status_code);
|
2019-07-23 12:00:20 +00:00
|
|
|
exit_code = result.status_code;
|
2018-05-11 11:24:33 +00:00
|
|
|
}
|
|
|
|
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++)
|
2019-07-23 12:00:20 +00:00
|
|
|
printf("%02x", result.output_data[i]);
|
2016-08-24 11:43:35 +00:00
|
|
|
printf("\n");
|
2019-08-08 15:18:58 +00:00
|
|
|
const evmc_bytes32 storage_key = {{0}};
|
2019-10-31 09:38:17 +00:00
|
|
|
evmc_bytes32 storage_value = host->get_storage(ctx, &msg.destination, &storage_key);
|
2019-08-08 15:18:58 +00:00
|
|
|
printf(" Storage at 0x00..00: ");
|
|
|
|
for (i = 0; i < sizeof(storage_value.bytes) / sizeof(storage_value.bytes[0]); i++)
|
|
|
|
printf("%02x", storage_value.bytes[i]);
|
|
|
|
printf("\n");
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
2018-08-20 04:24:21 +00:00
|
|
|
evmc_release_result(&result);
|
2018-08-31 11:08:26 +00:00
|
|
|
example_host_destroy_context(ctx);
|
2018-08-20 04:24:21 +00:00
|
|
|
evmc_destroy(vm);
|
2019-07-23 12:00:20 +00:00
|
|
|
return exit_code;
|
2016-06-24 09:40:06 +00:00
|
|
|
}
|