mirror of
https://github.com/status-im/evmc.git
synced 2025-02-23 08:28:15 +00:00
Merge pull request #218 from ethereum/tests-refactoring
Tests and examples refactoring
This commit is contained in:
commit
1b5a67cfff
@ -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.
|
||||
@ -31,7 +32,7 @@ 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();
|
||||
}
|
||||
|
||||
@ -39,32 +40,29 @@ 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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
|
@ -2,6 +2,7 @@
|
||||
// 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>
|
||||
@ -9,6 +10,26 @@
|
||||
#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;
|
||||
|
@ -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"
|
||||
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user