Skip to content

Commit

Permalink
Add back main example that uses example Host and VM
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 31, 2018
1 parent 78888b5 commit 98213f9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ endif()
add_subdirectory(example_host)
add_subdirectory(examplevm)

add_executable(evmc-example example.c)
target_link_libraries(evmc-example PRIVATE evmc-example-host evmc-examplevm evmc)
20 changes: 6 additions & 14 deletions examples/capi.c → examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
*/

#include <evmc/evmc.h>
#include "example_host/example_host.h"
#include "examplevm/examplevm.h"

#include <evmc/helpers.h>

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

#include "examplevm/examplevm.h"

/// Example how the API is supposed to be used.
int main()
{
struct evmc_instance* vm = evmc_create_examplevm();
if (!evmc_is_abi_compatible(vm))
return 1;

// EVM bytecode goes here. This is one of the examples examplevm.c
const uint8_t code[] = "\x30\x60\x00\x52\x59\x60\x00\xf3";
const size_t code_size = sizeof(code);
Expand All @@ -26,9 +24,7 @@ int main()
const struct evmc_uint256be value = {{1, 0}};
const struct evmc_address addr = {{0, 1, 2}};
const int64_t gas = 200000;

struct evmc_context ctx = {&ctx_fn_table};

struct evmc_context* ctx = example_host_create_context();
struct evmc_message msg;
msg.sender = addr;
msg.destination = addr;
Expand All @@ -38,9 +34,7 @@ int main()
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);

struct evmc_result result = evmc_execute(vm, ctx, EVMC_HOMESTEAD, &msg, code, code_size);
printf("Execution result:\n");
if (result.status_code != EVMC_SUCCESS)
{
Expand All @@ -51,16 +45,14 @@ int main()
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;
}

0 comments on commit 98213f9

Please sign in to comment.