Skip to content

Commit

Permalink
Merge pull request ethereum#116 from ethereum/host-example
Browse files Browse the repository at this point in the history
Host example
  • Loading branch information
chfast authored Aug 31, 2018
2 parents 297ce06 + 759e67a commit 3a3aef2
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 275 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if(TARGET evmc)
endif()

option(EVMC_TESTING "Build EVMC tests and test tools" OFF)
option(EVMC_EXAMPLES "Build EVMC examples" OFF)
option(EVMC_EXAMPLES "Build EVMC examples" ${EVMC_TESTING})
option(HUNTER_ENABLED "Enable Hunter package manager support" "${EVMC_TESTING}")

include(cmake/cable/bootstrap.cmake)
Expand Down Expand Up @@ -46,7 +46,7 @@ if(EVMC_TESTING)
add_subdirectory(test)
endif()

if(EVMC_EXAMPLES)
if(EVMC_EXAMPLES OR EVMC_TESTING)
add_subdirectory(examples)
endif()

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ after_build:
if ($env:GENERATOR) {
cd C:\projects\evmc\build\test
Release\evmc-test.exe
C:\install\bin\evmc-vmtester.exe C:\install\bin\evmc-examplevm.dll
C:\install\bin\evmc-vmtester.exe C:\install\bin\evmc-example-vm.dll
}
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
cmake ~/project/examples/use_instructions_in_cmake -DCMAKE_PREFIX_PATH=~/install
cmake --build .
- run:
name: "Run evmc-vmtester libevmc-examplevm.so"
command: ~/install/bin/evmc-vmtester ~/install/lib/libevmc-examplevm.so
name: "Run evmc-vmtester libevmc-example-vm.so"
command: ~/install/bin/evmc-vmtester ~/install/lib/libevmc-example-vm.so

build-clang-3.8:
<<: *build
Expand Down
33 changes: 28 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
# EVMC: Ethereum Client-VM Connector API.
# Copyright 2018 The EVMC Authors.
# Licensed under the Apache License, Version 2.0. See the LICENSE file.

add_subdirectory(examplevm)
include(GNUInstallDirs)

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)
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
set(CMAKE_DEBUG_POSTFIX "")


add_library(evmc-example-host STATIC example_host.cpp)
target_link_libraries(evmc-example-host PRIVATE evmc)


add_library(evmc-example-vm example_vm.c)
target_link_libraries(evmc-example-vm PRIVATE evmc)
set_source_files_properties(example_vm.c PROPERTIES COMPILE_DEFINITIONS PROJECT_VERSION=${PROJECT_VERSION})


add_executable(evmc-example example.c)
target_link_libraries(evmc-example PRIVATE evmc-example-host evmc-example-vm evmc)


install(TARGETS evmc-example-vm
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
219 changes: 0 additions & 219 deletions examples/capi.c

This file was deleted.

58 changes: 58 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/

#include "example_host.h"
#include "example_vm.h"

#include <evmc/helpers.h>

#include <inttypes.h>
#include <stdio.h>

int main()
{
struct evmc_instance* vm = evmc_create_example_vm();
if (!evmc_is_abi_compatible(vm))
return 1;
// EVM bytecode goes here. This is one of the examples.
const uint8_t code[] = "\x30\x60\x00\x52\x59\x60\x00\xf3";
const size_t code_size = sizeof(code);
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;
struct evmc_context* ctx = example_host_create_context();
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;
struct evmc_result result = evmc_execute(vm, ctx, EVMC_HOMESTEAD, &msg, code, code_size);
printf("Execution result:\n");
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;
for (i = 0; i < result.output_size; i++)
printf("%02x ", result.output_data[i]);
printf("\n");
}
evmc_release_result(&result);
example_host_destroy_context(ctx);
evmc_destroy(vm);
return 0;
}
Loading

0 comments on commit 3a3aef2

Please sign in to comment.