Skip to content

Commit

Permalink
Create context in example host
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Aug 30, 2018
1 parent 07f2f73 commit c7ddcf2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 16 additions & 1 deletion examples/example_host/example_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,22 @@ static void emit_log(evmc_context* context,
(void)topics_count;
}

extern const evmc_context_fn_table mock_context_fn_table = {
static const evmc_context_fn_table methods = {
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,
};

struct example_host_context : evmc_context
{
example_host_context() : evmc_context{&methods} {}
};

evmc_context* example_host_create_context()
{
return new example_host_context;
}

void example_host_destroy_context(evmc_context* context)
{
delete context;
}
13 changes: 10 additions & 3 deletions test/vmtester/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#include <array>
#include <cstring>

// Declarations of functions from example host:

evmc_context* example_host_create_context();
void example_host_destroy_context(evmc_context* context);


// Compile time checks:

static_assert(sizeof(evmc_uint256be) == 32, "evmc_uint256be is too big");
Expand All @@ -27,7 +33,6 @@ static constexpr size_t optionalDataSize =
sizeof(evmc_result) - offsetof(evmc_result, create_address);
static_assert(optionalDataSize == sizeof(evmc_result_optional_storage), "");

extern const struct evmc_context_fn_table mock_context_fn_table;

TEST_F(evmc_vm_test, abi_version_match)
{
Expand All @@ -36,12 +41,12 @@ TEST_F(evmc_vm_test, abi_version_match)

TEST_F(evmc_vm_test, execute)
{
evmc_context context = {&mock_context_fn_table};
evmc_context* context = example_host_create_context();
evmc_message msg{};
std::array<uint8_t, 2> code = {{0xfe, 0x00}};

evmc_result result =
vm->execute(vm, &context, EVMC_LATEST_REVISION, &msg, code.data(), code.size());
vm->execute(vm, context, EVMC_LATEST_REVISION, &msg, code.data(), code.size());

// Validate some constraints
if (result.status_code != EVMC_SUCCESS && result.status_code != EVMC_REVERT)
Expand All @@ -56,6 +61,8 @@ TEST_F(evmc_vm_test, execute)

if (result.release)
result.release(&result);

example_host_destroy_context(context);
}

TEST_F(evmc_vm_test, set_option_unknown)
Expand Down

0 comments on commit c7ddcf2

Please sign in to comment.