evmc/examples/capi.c

67 lines
1.8 KiB
C
Raw Normal View History

/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/
2018-04-18 14:22:24 +00:00
#include <evmc/evmc.h>
2018-08-20 04:24:21 +00:00
#include <evmc/helpers.h>
#include <inttypes.h>
#include <stdio.h>
2016-06-24 09:40:06 +00:00
#include "examplevm/examplevm.h"
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
{
struct evmc_instance* vm = evmc_create_examplevm();
2018-08-20 13:46:31 +00:00
if (!evmc_is_abi_compatible(vm))
return 1;
// EVM bytecode goes here. This is one of the examples examplevm.c
const uint8_t code[] = "\x30\x60\x00\x52\x59\x60\x00\xf3";
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};
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 13:41:02 +00:00
struct evmc_result result = evmc_execute(vm, &ctx, EVMC_HOMESTEAD, &msg, code, code_size);
2016-06-24 09:40:06 +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
{
printf(" Gas used: %" PRId64 "\n", gas - result.gas_left);
printf(" Gas left: %" PRId64 "\n", result.gas_left);
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++)
printf("%02x ", result.output_data[i]);
printf("\n");
}
2018-08-20 04:24:21 +00:00
evmc_release_result(&result);
evmc_destroy(vm);
2017-10-24 11:50:35 +00:00
return 0;
2016-06-24 09:40:06 +00:00
}