mirror of https://github.com/status-im/evmc.git
Merge pull request #443 from ethereum/example_fixes
Fixes for examples and unit tests
This commit is contained in:
commit
f284e4292b
|
@ -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}")
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
@ -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{};
|
||||
|
@ -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,
|
||||
|
|
|
@ -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
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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/)
|
||||
|
|
|
@ -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;
|
||||
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue