2016-08-22 20:51:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-06-24 09:40:06 +00:00
|
|
|
#include "evm.h"
|
|
|
|
|
2016-08-23 10:58:09 +00:00
|
|
|
|
2017-01-20 12:38:11 +00:00
|
|
|
struct evm_uint256be balance(struct evm_env* env,
|
|
|
|
const struct evm_uint160be* address)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2016-08-31 09:40:02 +00:00
|
|
|
struct evm_uint256be ret = {.bytes = {1, 2, 3, 4}};
|
2016-08-22 20:51:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-31 09:40:02 +00:00
|
|
|
struct evm_uint160be address(struct evm_env* env)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2016-08-31 09:40:02 +00:00
|
|
|
struct evm_uint160be ret = {.bytes = {1, 2, 3, 4}};
|
2016-08-22 20:51:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2017-06-08 12:12:52 +00:00
|
|
|
static void print_address(const struct evm_uint160be* address)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < sizeof(address->bytes); ++i)
|
|
|
|
printf("%x", address->bytes[i] & 0xff);
|
|
|
|
}
|
|
|
|
|
2016-12-22 11:55:49 +00:00
|
|
|
static void query(union evm_variant* result,
|
|
|
|
struct evm_env* env,
|
|
|
|
enum evm_query_key key,
|
2017-06-09 14:16:17 +00:00
|
|
|
const struct evm_uint160be* address) {
|
2016-08-22 20:51:43 +00:00
|
|
|
printf("EVM-C: QUERY %d\n", key);
|
2016-06-24 09:40:06 +00:00
|
|
|
switch (key) {
|
2017-01-18 22:26:48 +00:00
|
|
|
case EVM_CODE_BY_ADDRESS:
|
|
|
|
result->data = NULL;
|
|
|
|
result->data_size = 0;
|
2016-12-22 11:55:49 +00:00
|
|
|
break;
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2017-01-18 22:26:48 +00:00
|
|
|
case EVM_ACCOUNT_EXISTS:
|
|
|
|
result->int64 = 0;
|
2016-08-22 20:51:43 +00:00
|
|
|
break;
|
|
|
|
|
2016-12-22 11:55:49 +00:00
|
|
|
default:
|
|
|
|
result->int64 = 0;
|
2016-06-24 09:40:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 14:16:17 +00:00
|
|
|
static void get_storage(struct evm_uint256be* result,
|
|
|
|
struct evm_env* env,
|
|
|
|
const struct evm_uint160be* address,
|
|
|
|
const struct evm_uint256be* key)
|
|
|
|
{
|
|
|
|
printf("EVM-C: SLOAD @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-06-08 10:00:04 +00:00
|
|
|
static void set_storage(struct evm_env* env,
|
2017-06-08 12:12:52 +00:00
|
|
|
const struct evm_uint160be* address,
|
2017-06-08 10:00:04 +00:00
|
|
|
const struct evm_uint256be* key,
|
|
|
|
const struct evm_uint256be* value)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2017-06-08 12:12:52 +00:00
|
|
|
printf("EVM-C: SSTORE @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:44:43 +00:00
|
|
|
static void get_balance(struct evm_uint256be* result,
|
|
|
|
struct evm_env* env,
|
|
|
|
const struct evm_uint160be* address)
|
|
|
|
{
|
|
|
|
printf("EVM-C: BALANCE @");
|
|
|
|
print_address(address);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2017-06-08 12:12:52 +00:00
|
|
|
static void selfdestruct(struct evm_env* env,
|
|
|
|
const struct evm_uint160be* address,
|
|
|
|
const struct evm_uint160be* beneficiary)
|
|
|
|
{
|
|
|
|
printf("EVM-C: SELFDESTRUCT ");
|
|
|
|
print_address(address);
|
|
|
|
printf(" -> ");
|
|
|
|
print_address(beneficiary);
|
|
|
|
printf("\n");
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:26:44 +00:00
|
|
|
static void call(struct evm_result* result,
|
|
|
|
struct evm_env* env,
|
|
|
|
const struct evm_message* msg)
|
2016-08-22 20:51:43 +00:00
|
|
|
{
|
2017-04-28 13:26:44 +00:00
|
|
|
printf("EVM-C: CALL (depth: %d)\n", msg->depth);
|
|
|
|
result->code = EVM_FAILURE;
|
2016-08-22 20:51:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 22:26:48 +00:00
|
|
|
static void get_tx_context(struct evm_tx_context* result, struct evm_env* env)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-19 21:39:21 +00:00
|
|
|
static void get_block_hash(struct evm_uint256be* result, struct evm_env* env,
|
|
|
|
int64_t number)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-08 12:12:52 +00:00
|
|
|
/// EVM log callback.
|
|
|
|
///
|
|
|
|
/// @note The `evm_log` name is used to avoid conflict with `log()` C function.
|
|
|
|
static void evm_log(struct evm_env* env, const struct evm_uint160be* address,
|
|
|
|
const uint8_t* data, size_t data_size,
|
|
|
|
const struct evm_uint256be topics[], size_t topics_count)
|
|
|
|
{
|
|
|
|
printf("EVM-C: LOG%d\n", (int)topics_count);
|
|
|
|
}
|
|
|
|
|
2017-08-01 11:34:25 +00:00
|
|
|
static const struct evm_host example_host = {
|
|
|
|
query,
|
|
|
|
get_storage,
|
|
|
|
set_storage,
|
2017-08-07 16:44:43 +00:00
|
|
|
get_balance,
|
2017-08-01 11:34:25 +00:00
|
|
|
selfdestruct,
|
|
|
|
call,
|
|
|
|
get_tx_context,
|
|
|
|
get_block_hash,
|
|
|
|
evm_log
|
|
|
|
};
|
|
|
|
|
2016-06-24 09:40:06 +00:00
|
|
|
/// Example how the API is supposed to be used.
|
2016-08-22 20:51:43 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
2016-12-20 15:50:23 +00:00
|
|
|
struct evm_factory factory = examplevm_get_factory();
|
|
|
|
if (factory.abi_version != EVM_ABI_VERSION)
|
|
|
|
return 1; // Incompatible ABI version.
|
2016-06-24 09:40:06 +00:00
|
|
|
|
2017-08-01 11:34:25 +00:00
|
|
|
struct evm_instance* jit = factory.create(&example_host);
|
2016-12-20 15:50:23 +00:00
|
|
|
|
|
|
|
uint8_t const code[] = "Place some EVM bytecode here";
|
2016-08-22 20:51:43 +00:00
|
|
|
const size_t code_size = sizeof(code);
|
2016-08-31 09:40:02 +00:00
|
|
|
struct evm_uint256be code_hash = {.bytes = {1, 2, 3,}};
|
2016-12-20 15:50:23 +00:00
|
|
|
uint8_t const input[] = "Hello World!";
|
2017-01-27 15:27:34 +00:00
|
|
|
struct evm_uint256be value = {{1, 0,}};
|
2017-01-12 15:45:14 +00:00
|
|
|
struct evm_uint160be addr = {{0, 1, 2,}};
|
2016-06-24 09:40:06 +00:00
|
|
|
int64_t gas = 200000;
|
2017-01-12 15:45:14 +00:00
|
|
|
|
2017-01-27 15:27:34 +00:00
|
|
|
struct evm_message msg = {addr, addr, value, input, sizeof(input),
|
|
|
|
code_hash, gas, 0};
|
2017-01-12 15:45:14 +00:00
|
|
|
|
2016-06-24 09:40:06 +00:00
|
|
|
struct evm_result result =
|
2017-01-27 15:27:34 +00:00
|
|
|
jit->execute(jit, NULL, EVM_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");
|
2016-08-25 11:44:34 +00:00
|
|
|
if (result.code != EVM_SUCCESS) {
|
|
|
|
printf(" EVM execution failure: %d\n", result.code);
|
2016-08-24 11:43:35 +00:00
|
|
|
} else {
|
|
|
|
printf(" Gas used: %ld\n", gas - result.gas_left);
|
|
|
|
printf(" Gas left: %ld\n", result.gas_left);
|
|
|
|
printf(" Output size: %zd\n", result.output_size);
|
|
|
|
|
|
|
|
printf(" Output: ");
|
|
|
|
size_t i = 0;
|
|
|
|
for (i = 0; i < result.output_size; i++) {
|
|
|
|
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);
|
2016-12-20 15:50:23 +00:00
|
|
|
jit->destroy(jit);
|
2016-06-24 09:40:06 +00:00
|
|
|
}
|