diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8aba0ba..65263af 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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() diff --git a/examples/capi.c b/examples/capi.c index c16ceb8..3b4b984 100644 --- a/examples/capi.c +++ b/examples/capi.c @@ -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 #include #include #include -#include -#include #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() { diff --git a/examples/example_host/CMakeLists.txt b/examples/example_host/CMakeLists.txt new file mode 100644 index 0000000..119c575 --- /dev/null +++ b/examples/example_host/CMakeLists.txt @@ -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 .. +) diff --git a/test/vmtester/mock_context.cpp b/examples/example_host/example_host.cpp similarity index 91% rename from test/vmtester/mock_context.cpp rename to examples/example_host/example_host.cpp index c4a42fb..47fddc6 100644 --- a/test/vmtester/mock_context.cpp +++ b/examples/example_host/example_host.cpp @@ -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 #include -#include -#include -#include -#include - -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, diff --git a/test/vmtester/CMakeLists.txt b/test/vmtester/CMakeLists.txt index 1986b71..8f262c3 100644 --- a/test/vmtester/CMakeLists.txt +++ b/test/vmtester/CMakeLists.txt @@ -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})