Merge pull request #15 from ethereum/examplevm

EVMC Example VM
This commit is contained in:
Paweł Bylica 2018-04-13 15:04:26 +02:00 committed by GitHub
commit 5a7478ec7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 29 deletions

View File

@ -10,6 +10,7 @@ if(TARGET evmc)
endif()
include(cmake/cable/bootstrap.cmake)
include(CableCompilerSettings)
include(HunterGate)
include(defaults/HunterCacheServers)
@ -23,6 +24,8 @@ HunterGate(
project(evmc)
cable_configure_compiler()
add_library(evmc INTERFACE)
target_include_directories(evmc INTERFACE include)

View File

@ -9,13 +9,19 @@ jobs:
- run:
name: "Configure"
working_directory: ~/build
command: cmake ../project -DEVMC_BUILD_EXAMPLES=ON -DEVMC_BUILD_TESTS=ON
command: cmake ../project -DCMAKE_INSTALL_PREFIX=~/install -DBUILD_SHARED_LIBS=ON -DEVMC_BUILD_EXAMPLES=ON -DEVMC_BUILD_TESTS=ON
- run:
name: "Build"
command: cmake --build ~/build
- run:
name: "Test"
command: cmake --build ~/build --target test
- run:
name: "Install"
command: cmake --build ~/build --target install
- run:
name: "Run evmc-vmtester libevmc-examplevm.so"
command: ~/install/bin/evmc-vmtester ~/install/lib/libevmc-examplevm.so
test-docs:
docker:

View File

@ -1,11 +1,8 @@
add_library(example-vm STATIC examplevm.c)
target_link_libraries(example-vm PRIVATE evmc)
if(NOT MSVC)
target_compile_options(example-vm PRIVATE -Wno-extra)
endif()
add_subdirectory(examplevm)
add_executable(example-capi capi.c)
target_link_libraries(example-capi PRIVATE evmc example-vm)
target_link_libraries(example-capi PRIVATE evmc evmc-examplevm)
if(NOT MSVC)
target_compile_options(example-capi PRIVATE -Wno-extra)
endif()

View File

@ -0,0 +1,14 @@
# EVMC -- Ethereum Client-VM Connector API
# Copyright 2018 Pawel Bylica.
# Licensed under the MIT License. See the LICENSE file.
include(GNUInstallDirs)
add_library(evmc-examplevm examplevm.c)
target_link_libraries(evmc-examplevm PRIVATE evmc)
set_target_properties(evmc-examplevm PROPERTIES DEBUG_POSTFIX "")
install(TARGETS evmc-examplevm
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -19,12 +19,11 @@ static void evmc_destroy(struct evmc_instance* evm)
/// Example options.
///
/// VMs are allowed to omit this function implementation.
int evmc_set_option(struct evmc_instance* instance,
char const* name,
char const* value)
int evmc_set_option(struct evmc_instance* instance, char const* name, char const* value)
{
struct examplevm* vm = (struct examplevm*)instance;
if (strcmp(name, "verbose") == 0) {
if (strcmp(name, "verbose") == 0)
{
long int v = strtol(value, NULL, 0);
if (v > INT_MAX || v < INT_MIN)
return 0;
@ -45,18 +44,14 @@ static void free_result_output_data(struct evmc_result const* result)
free((uint8_t*)result->output_data);
}
static struct evmc_result execute(struct evmc_instance* instance,
struct evmc_context* context,
enum evmc_revision rev,
const struct evmc_message* msg,
const uint8_t* code,
size_t code_size)
static struct evmc_result execute(struct evmc_instance* instance, struct evmc_context* context,
enum evmc_revision rev, const struct evmc_message* msg, const uint8_t* code, size_t code_size)
{
struct evmc_result ret = {};
if (code_size == 0) {
struct evmc_result ret = {.status_code = EVMC_INTERNAL_ERROR};
if (code_size == 0)
{
// In case of empty code return a fancy error message.
const char* error = rev == EVMC_BYZANTIUM ?
"Welcome to Byzantium!" : "Hello Ethereum!";
const char* error = rev == EVMC_BYZANTIUM ? "Welcome to Byzantium!" : "Hello Ethereum!";
ret.output_data = (const uint8_t*)error;
ret.output_size = strlen(error);
ret.status_code = EVMC_FAILURE;
@ -76,10 +71,12 @@ static struct evmc_result execute(struct evmc_instance* instance,
const char counter[] = "600160005401600055";
if (code_size == strlen(return_address) &&
strncmp((const char*)code, return_address, code_size) == 0) {
strncmp((const char*)code, return_address, code_size) == 0)
{
static const size_t address_size = sizeof(msg->destination);
uint8_t* output_data = (uint8_t*)malloc(address_size);
if (!output_data) {
if (!output_data)
{
// malloc failed, report internal error.
ret.status_code = EVMC_INTERNAL_ERROR;
return ret;
@ -91,10 +88,10 @@ static struct evmc_result execute(struct evmc_instance* instance,
ret.release = &free_result_output_data;
return ret;
}
else if (code_size == strlen(counter) &&
strncmp((const char*)code, counter, code_size) == 0) {
else if (code_size == strlen(counter) && strncmp((const char*)code, counter, code_size) == 0)
{
struct evmc_uint256be value;
const struct evmc_uint256be index = {{0,}};
const struct evmc_uint256be index = {{0}};
context->fn_table->get_storage(&value, context, &msg->destination, &index);
value.bytes[31] += 1;
context->fn_table->set_storage(context, &msg->destination, &index, &value);
@ -118,7 +115,7 @@ struct evmc_instance* examplevm_create()
.abi_version = EVMC_ABI_VERSION,
.destroy = evmc_destroy,
.execute = execute,
.set_option = evmc_set_option
.set_option = evmc_set_option,
};
struct examplevm* vm = calloc(1, sizeof(struct examplevm));
struct evmc_instance* interface = &vm->instance;

View File

@ -2,6 +2,8 @@
# Copyright 2018 Pawel Bylica.
# Licensed under the MIT License. See the LICENSE file.
include(GNUInstallDirs)
hunter_add_package(Boost COMPONENTS program_options filesystem system)
find_package(Boost CONFIG REQUIRED program_options filesystem system)
@ -21,6 +23,8 @@ target_link_libraries(
${CMAKE_DL_LIBS}
)
install(TARGETS evmc-vmtester RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
add_test(NAME vmtester-help
COMMAND evmc-vmtester --help
)
@ -43,5 +47,3 @@ set_tests_properties(vmtester-novm PROPERTIES
WILL_FAIL TRUE
FAIL_REGULAR_EXPRESSION "required but missing"
)
# TODO: Add test for `evmc-vmtester examplevm.so`