Replace capi.c example with example_host.cpp

This commit is contained in:
Paweł Bylica 2018-08-30 14:49:20 +02:00
parent 4d4ff37c1c
commit 5aefc4faf3
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
5 changed files with 35 additions and 172 deletions

View File

@ -1,8 +1,12 @@
# EVMC: Ethereum Client-VM Connector API.
# Copyright 2018 The EVMC Authors.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
add_subdirectory(example_host)
add_subdirectory(examplevm)
add_executable(example-capi capi.c)
target_link_libraries(example-capi PRIVATE evmc evmc-examplevm)
if(NOT MSVC)
target_compile_options(example-capi PRIVATE -Wno-extra)
endif()

View File

@ -1,169 +1,16 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/
#include <evmc/evmc.h>
#include <evmc/helpers.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "examplevm/examplevm.h"
struct evmc_uint256be balance(struct evmc_context* context, const struct evmc_address* address)
{
(void)context;
(void)address;
struct evmc_uint256be ret = {.bytes = {1, 2, 3, 4}};
return ret;
}
static void print_address(const struct evmc_address* address)
{
int i = 0;
for (i = 0; i < sizeof(address->bytes); ++i)
printf("%x", address->bytes[i] & 0xff);
}
static int account_exists(struct evmc_context* context, const struct evmc_address* address)
{
(void)context;
printf("EVM-C: EXISTS @");
print_address(address);
printf("\n");
return 0;
}
static void get_storage(struct evmc_uint256be* result,
struct evmc_context* context,
const struct evmc_address* address,
const struct evmc_uint256be* key)
{
(void)result;
(void)context;
(void)key;
printf("EVM-C: SLOAD @");
print_address(address);
printf("\n");
}
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)
{
(void)context;
(void)key;
(void)value;
printf("EVM-C: SSTORE @");
print_address(address);
printf("\n");
return EVMC_STORAGE_UNCHANGED;
}
static void get_balance(struct evmc_uint256be* result,
struct evmc_context* context,
const struct evmc_address* address)
{
printf("EVM-C: BALANCE @");
print_address(address);
printf("\n");
*result = balance(context, address);
}
static size_t get_code_size(struct evmc_context* context, const struct evmc_address* address)
{
(void)context;
printf("EVM-C: CODESIZE @");
print_address(address);
printf("\n");
return 0;
}
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");
}
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)
{
(void)context;
(void)code_offset;
(void)buffer_data;
(void)buffer_size;
printf("EVM-C: COPYCODE @");
print_address(address);
printf("\n");
return 0;
}
static void selfdestruct(struct evmc_context* context,
const struct evmc_address* address,
const struct evmc_address* beneficiary)
{
(void)context;
printf("EVM-C: SELFDESTRUCT ");
print_address(address);
printf(" -> ");
print_address(beneficiary);
printf("\n");
}
static struct evmc_result call(struct evmc_context* context, const struct evmc_message* msg)
{
(void)context;
printf("EVM-C: CALL (depth: %d)\n", msg->depth);
struct evmc_result result = {.status_code = EVMC_FAILURE};
return result;
}
static struct evmc_tx_context get_tx_context(struct evmc_context* context)
{
(void)context;
struct evmc_tx_context result;
memset(&result, 0, sizeof(struct evmc_tx_context));
return result;
}
static void get_block_hash(struct evmc_uint256be* result,
struct evmc_context* context,
int64_t number)
{
(void)result;
(void)context;
(void)number;
}
/// EVM log callback.
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)
{
(void)context;
(void)address;
(void)data;
(void)data_size;
(void)topics;
printf("EVM-C: LOG%d\n", (int)topics_count);
}
static const struct evmc_context_fn_table ctx_fn_table = {
account_exists, get_storage, set_storage, get_balance, get_code_size, get_code_hash,
copy_code, selfdestruct, call, get_tx_context, get_block_hash, emit_log,
};
/// Example how the API is supposed to be used.
int main()
{

View File

@ -0,0 +1,11 @@
# EVMC: Ethereum Client-VM Connector API
# Copyright 2018 The EVMC Authors.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.
add_library(evmc-example-host STATIC example_host.cpp)
target_link_libraries(evmc-example-host PRIVATE evmc::evmc)
set_target_properties(evmc-example-host PROPERTIES
DEBUG_POSTFIX ""
RUNTIME_OUTPUT_DIRECTORY ..
LIBRARY_OUTPUT_DIRECTORY ..
)

View File

@ -1,12 +1,14 @@
// EVMC -- Ethereum Client-VM Connector API
// Copyright 2018 The EVMC Authors.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
/// @file
/// Example implementation of an EVMC Host.
#include <evmc/evmc.h>
#include <evmc/helpers.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
evmc_uint256be balance(evmc_context* context, const evmc_address* address)
static evmc_uint256be balance(evmc_context* context, const evmc_address* address)
{
(void)context;
(void)address;
@ -111,7 +113,6 @@ static void get_block_hash(evmc_uint256be* result, evmc_context* context, int64_
(void)number;
}
/// EVM log callback.
static void emit_log(evmc_context* context,
const evmc_address* address,
const uint8_t* data,

View File

@ -7,9 +7,9 @@ include(GNUInstallDirs)
hunter_add_package(CLI11)
find_package(CLI11)
add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp mock_context.cpp)
add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp)
set_target_properties(evmc-vmtester PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..)
target_link_libraries(evmc-vmtester PRIVATE evmc loader GTest::gtest CLI11::CLI11)
target_link_libraries(evmc-vmtester PRIVATE evmc loader evmc-example-host GTest::gtest CLI11::CLI11)
install(TARGETS evmc-vmtester RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})