Skip to content

Commit

Permalink
Merge pull request #443 from ethereum/example_fixes
Browse files Browse the repository at this point in the history
Fixes for examples and unit tests
  • Loading branch information
chfast committed Nov 5, 2019
2 parents a15e493 + a94829a commit f284e42
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
6 changes: 5 additions & 1 deletion examples/example_precompiles_vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
# Copyright 2019 The EVMC Authors.
# Licensed under the Apache License, Version 2.0.

add_library(example-precompiles-vm SHARED example_precompiles_vm.cpp)
add_library(example-precompiles-vm SHARED example_precompiles_vm.cpp example_precompiles_vm.h)
add_library(evmc::example-precompiles-vm ALIAS example-precompiles-vm)
target_link_libraries(example-precompiles-vm PRIVATE evmc)

add_library(example-precompiles-vm-static STATIC example_precompiles_vm.cpp example_precompiles_vm.h)
add_library(evmc::example-precompiles-vm-static ALIAS example-precompiles-vm-static)
target_link_libraries(example-precompiles-vm-static PRIVATE evmc)

set_source_files_properties(example_precompiles_vm.cpp PROPERTIES
COMPILE_DEFINITIONS PROJECT_VERSION="${PROJECT_VERSION}")

Expand Down
7 changes: 3 additions & 4 deletions examples/example_precompiles_vm/example_precompiles_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Licensed under the Apache License, Version 2.0.
*/

#include <evmc/evmc.h>
#include <evmc/utils.h>
#include "example_precompiles_vm.h"
#include <algorithm>

static evmc_result execute_identity(const evmc_message* msg)
Expand Down Expand Up @@ -63,7 +62,7 @@ static evmc_result execute(evmc_vm* /*unused*/,
constexpr auto prefix_size = sizeof(evmc_address) - 2;
const auto& dst = msg->destination;
// Check if the address prefix is all zeros.
if (std::all_of(&dst.bytes[0], &dst.bytes[prefix_size], [](uint8_t x) { return x == 0; }))
if (std::any_of(&dst.bytes[0], &dst.bytes[prefix_size], [](uint8_t x) { return x != 0; }))
{
// If not, reject the execution request.
auto result = evmc_result{};
Expand Down Expand Up @@ -112,7 +111,7 @@ static evmc_result execute(evmc_vm* /*unused*/,
}
}

extern "C" EVMC_EXPORT evmc_vm* evmc_create_example_precompiles_vm()
evmc_vm* evmc_create_example_precompiles_vm()
{
static struct evmc_vm vm = {
EVMC_ABI_VERSION,
Expand Down
21 changes: 21 additions & 0 deletions examples/example_precompiles_vm/example_precompiles_vm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* EVMC: Ethereum Client-VM Connector API.
* Copyright 2019 The EVMC Authors.
* Licensed under the Apache License, Version 2.0.
*/
#pragma once

#include <evmc/evmc.h>
#include <evmc/utils.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* Creates EVMC Example Precompiles VM.
*/
EVMC_EXPORT struct evmc_vm* evmc_create_example_precompiles_vm(void);

#ifdef __cplusplus
}
#endif
4 changes: 2 additions & 2 deletions examples/example_vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Copyright 2019 The EVMC Authors.
# Licensed under the Apache License, Version 2.0.

add_library(example-vm SHARED example_vm.c)
add_library(example-vm SHARED example_vm.c example_vm.h)
add_library(evmc::example-vm ALIAS example-vm)
target_link_libraries(example-vm PRIVATE evmc)

add_library(example-vm-static STATIC example_vm.c)
add_library(example-vm-static STATIC example_vm.c example_vm.h)
add_library(evmc::example-vm-static ALIAS example-vm-static)
target_link_libraries(example-vm-static PRIVATE evmc)

Expand Down
11 changes: 10 additions & 1 deletion test/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ add_executable(
test_loader.cpp
)

target_link_libraries(evmc-unittests PRIVATE loader-mocked evmc::example-vm-static evmc-example-host instructions GTest::gtest_main)
target_link_libraries(
evmc-unittests
PRIVATE
loader-mocked
evmc::example-vm-static
evmc::example-precompiles-vm-static
evmc-example-host
instructions
GTest::gtest_main
)
set_target_properties(evmc-unittests PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..)

gtest_add_tests(TARGET evmc-unittests TEST_PREFIX ${PROJECT_NAME}/unittests/)
24 changes: 23 additions & 1 deletion test/unittests/test_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
#include <vector>

#include "../../examples/example_host.h"
#include "../../examples/example_precompiles_vm/example_precompiles_vm.h"
#include "../../examples/example_vm/example_vm.h"

#include <evmc/evmc.hpp>

#include <gtest/gtest.h>

#include <array>
#include <cstring>
#include <map>
#include <unordered_map>


TEST(cpp, address)
{
evmc::address a;
Expand Down Expand Up @@ -344,6 +345,27 @@ TEST(cpp, vm_move)
EXPECT_EQ(destroy_counter, 5);
}

TEST(cpp, vm_execute_precompiles)
{
auto vm = evmc::VM{evmc_create_example_precompiles_vm()};
EXPECT_EQ(vm.get_capabilities(), evmc_capabilities_flagset{EVMC_CAPABILITY_PRECOMPILES});

constexpr std::array<uint8_t, 3> input{{1, 2, 3}};

evmc_message msg{};
msg.destination.bytes[19] = 4; // Call Identify precompile at address 0x4.
msg.input_data = input.data();
msg.input_size = input.size();
msg.gas = 18;

constexpr evmc_host_interface null_interface{};
auto res = vm.execute(null_interface, nullptr, EVMC_MAX_REVISION, msg, nullptr, 0);
EXPECT_EQ(res.status_code, EVMC_SUCCESS);
EXPECT_EQ(res.gas_left, 0);
ASSERT_EQ(res.output_size, input.size());
EXPECT_TRUE(std::equal(input.begin(), input.end(), res.output_data));
}

TEST(cpp, host)
{
// Use example host to execute all methods from the C++ host wrapper.
Expand Down
3 changes: 1 addition & 2 deletions test/vmtester/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ TEST_F(evmc_vm_test, precompile_test)
{
EXPECT_EQ(result.output_size, size_t{0});
}
else
else if (result.output_size != 0)
{
EXPECT_NE(result.output_size, size_t{0});
read_buffer(result.output_data, result.output_size);
}

Expand Down

0 comments on commit f284e42

Please sign in to comment.