Skip to content

Commit

Permalink
Merge pull request ethereum#218 from ethereum/tests-refactoring
Browse files Browse the repository at this point in the history
Tests and examples refactoring
  • Loading branch information
chfast authored Mar 15, 2019
2 parents e951ab2 + 4683047 commit 1b5a67c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 45 deletions.
40 changes: 18 additions & 22 deletions examples/example_host.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// EVMC -- Ethereum Client-VM Connector API
// Copyright 2018 The EVMC Authors.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/

/// @file
/// Example implementation of an EVMC Host.
Expand Down Expand Up @@ -31,40 +32,37 @@ struct example_host_context : evmc_context

static bool account_exists(evmc_context* context, const evmc_address* address)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
return host->accounts.find(*address) != host->accounts.end();
}

static evmc_bytes32 get_storage(evmc_context* context,
const evmc_address* address,
const evmc_bytes32* key)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
return it->second.storage[*key];
return {};
}

static enum evmc_storage_status set_storage(evmc_context* context,
const evmc_address* address,
const evmc_bytes32* key,
const evmc_bytes32* value)
static evmc_storage_status set_storage(evmc_context* context,
const evmc_address* address,
const evmc_bytes32* key,
const evmc_bytes32* value)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
auto& account = host->accounts[*address];
auto prevValue = account.storage[*key];
auto prev_value = account.storage[*key];
account.storage[*key] = *value;

if (prevValue == *value)
return EVMC_STORAGE_UNCHANGED;
else
return EVMC_STORAGE_MODIFIED;
return (prev_value == *value) ? EVMC_STORAGE_UNCHANGED : EVMC_STORAGE_MODIFIED;
}

static evmc_uint256be get_balance(evmc_context* context, const evmc_address* address)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
return it->second.balance;
Expand All @@ -73,7 +71,7 @@ static evmc_uint256be get_balance(evmc_context* context, const evmc_address* add

static size_t get_code_size(evmc_context* context, const evmc_address* address)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
return it->second.code_size;
Expand All @@ -82,7 +80,7 @@ static size_t get_code_size(evmc_context* context, const evmc_address* address)

static evmc_bytes32 get_code_hash(evmc_context* context, const evmc_address* address)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
auto it = host->accounts.find(*address);
if (it != host->accounts.end())
return it->second.code_hash;
Expand Down Expand Up @@ -130,14 +128,12 @@ static evmc_tx_context get_tx_context(evmc_context* context)

static evmc_bytes32 get_block_hash(evmc_context* context, int64_t number)
{
example_host_context* host = static_cast<example_host_context*>(context);
auto* host = static_cast<example_host_context*>(context);
int64_t current_block_number = host->tx_context.block_number;

evmc_bytes32 example_block_hash;
auto example_block_hash = evmc_bytes32{};
if (number < current_block_number && number >= current_block_number - 256)
example_block_hash = {{1, 1, 1, 1}};
else
example_block_hash = {};
return example_block_hash;
}

Expand Down
4 changes: 2 additions & 2 deletions examples/example_host.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2018 The EVMC Authors.
* Licensed under the Apache License, Version 2.0. See the LICENSE file.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/

#include <evmc/evmc.h>
Expand Down
21 changes: 21 additions & 0 deletions test/unittests/test_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
// Copyright 2019 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.

#include <evmc/helpers.h>
#include <evmc/helpers.hpp>

#include <gtest/gtest.h>

#include <map>
#include <unordered_map>


// Compile time checks:

static_assert(sizeof(evmc_bytes32) == 32, "evmc_bytes32 is too big");
static_assert(sizeof(evmc_address) == 20, "evmc_address is too big");
static_assert(sizeof(evmc_result) <= 64, "evmc_result does not fit cache line");
static_assert(sizeof(evmc_instance) <= 64, "evmc_instance does not fit cache line");
static_assert(offsetof(evmc_message, value) % 8 == 0, "evmc_message.value not aligned");

// Check enums match int size.
// On GCC/clang the underlying type should be unsigned int, on MSVC int
static_assert(sizeof(evmc_call_kind) == sizeof(int),
"Enum `evmc_call_kind` is not the size of int");
static_assert(sizeof(evmc_revision) == sizeof(int), "Enum `evmc_revision` is not the size of int");

static constexpr size_t optionalDataSize =
sizeof(evmc_result) - offsetof(evmc_result, create_address);
static_assert(optionalDataSize == sizeof(evmc_result_optional_storage), "");


TEST(helpers, maps)
{
std::map<evmc_address, bool> addresses;
Expand Down
23 changes: 2 additions & 21 deletions test/vmtester/tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// EVMC: Ethereum Client-VM Connector API
// Copyright 2018 The EVMC Authors.
// Licensed under the Apache License, Version 2.0. See the LICENSE file.
// Copyright 2019 The EVMC Authors.
// Licensed under the Apache License, Version 2.0.

#include "../../examples/example_host.h"
#include "vmtester.hpp"
Expand All @@ -10,25 +10,6 @@
#include <array>
#include <cstring>

// Compile time checks:

static_assert(sizeof(evmc_bytes32) == 32, "evmc_bytes32 is too big");
static_assert(sizeof(evmc_address) == 20, "evmc_address is too big");
static_assert(sizeof(evmc_result) <= 64, "evmc_result does not fit cache line");
static_assert(sizeof(evmc_instance) <= 64, "evmc_instance does not fit cache line");
static_assert(offsetof(evmc_message, value) % 8 == 0, "evmc_message.value not aligned");

// Check enums match int size.
// On GCC/clang the underlying type should be unsigned int, on MSVC int
static_assert(sizeof(evmc_call_kind) == sizeof(int),
"Enum `evmc_call_kind` is not the size of int");
static_assert(sizeof(evmc_revision) == sizeof(int), "Enum `evmc_revision` is not the size of int");

static constexpr size_t optionalDataSize =
sizeof(evmc_result) - offsetof(evmc_result, create_address);
static_assert(optionalDataSize == sizeof(evmc_result_optional_storage), "");


TEST_F(evmc_vm_test, abi_version_match)
{
ASSERT_EQ(vm->abi_version, EVMC_ABI_VERSION);
Expand Down

0 comments on commit 1b5a67c

Please sign in to comment.