Skip to content

Commit

Permalink
Move host implementation to assigner library
Browse files Browse the repository at this point in the history
  • Loading branch information
makxenov committed Apr 19, 2024
1 parent adfa2a4 commit 3bae30f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
clang_17
ethash
deps.intx
deps.evmc
crypto3
blueprint
];
Expand Down
6 changes: 3 additions & 3 deletions lib/assigner/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ cmake_policy(SET CMP0063 NEW)

option(BUILD_ASSIGNER_TESTS "Build unit tests" FALSE)

add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME} STATIC src/vm_host.cpp)

target_include_directories(${PROJECT_NAME} INTERFACE
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${include_dir}/evmone
${CMAKE_CURRENT_SOURCE_DIR}/../evmone
Expand All @@ -16,7 +16,7 @@ target_include_directories(${PROJECT_NAME} INTERFACE

# TODO: link to blueprint and crypto3 here after fixing this in crypto3:
# https://github.com/NilFoundation/crypto3/issues/175
target_link_libraries(${PROJECT_NAME} INTERFACE
target_link_libraries(${PROJECT_NAME} PUBLIC
evmone)

install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/nil/blueprint/
Expand Down
18 changes: 9 additions & 9 deletions lib/assigner/test/test_host.h → lib/assigner/include/vm_host.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef EVM1_ASSIGNER_LIB_ASSIGNER_TEST_TEST_HOST_H_
#define EVM1_ASSIGNER_LIB_ASSIGNER_TEST_TEST_HOST_H_
#ifndef EVM_ASSIGNER_LIB_ASSIGNER_INCLUDE_VM_HOST_H_
#define EVM_ASSIGNER_LIB_ASSIGNER_INCLUDE_VM_HOST_H_

// Based on example host

Expand Down Expand Up @@ -40,19 +40,19 @@ using accounts = std::map<evmc::address, account>;
using BlueprintFieldType = typename nil::crypto3::algebra::curves::pallas::base_field_type;
using AssignerType = nil::blueprint::assigner<BlueprintFieldType>;

class TestHost : public evmc::Host
class VMHost : public evmc::Host
{
evmc::accounts accounts;
evmc_tx_context tx_context{};
AssignerType *assigner;

public:
TestHost() = default;
explicit TestHost(evmc_tx_context& _tx_context, AssignerType* _assigner) noexcept
VMHost() = default;
explicit VMHost(evmc_tx_context& _tx_context, AssignerType* _assigner) noexcept
: tx_context{_tx_context}, assigner(_assigner)
{}

TestHost(evmc_tx_context& _tx_context, AssignerType* _assigner, evmc::accounts& _accounts) noexcept
VMHost(evmc_tx_context& _tx_context, AssignerType* _assigner, evmc::accounts& _accounts) noexcept
: accounts{_accounts}, tx_context{_tx_context}, assigner(_assigner)
{}

Expand Down Expand Up @@ -222,8 +222,8 @@ class TestHost : public evmc::Host

extern "C" {

evmc_host_context* test_host_create_context(evmc_tx_context tx_context, AssignerType *assigner);
void test_host_destroy_context(evmc_host_context* context);
evmc_host_context* vm_host_create_context(evmc_tx_context tx_context, AssignerType *assigner);
void vm_host_destroy_context(evmc_host_context* context);
}

#endif // EVM1_ASSIGNER_LIB_ASSIGNER_TEST_TEST_HOST_H_
#endif // EVM_ASSIGNER_LIB_ASSIGNER_INCLUDE_VM_HOST_H_
21 changes: 13 additions & 8 deletions lib/assigner/test/test_host.cpp → lib/assigner/src/vm_host.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "test_host.h"
#include "vm_host.h"

#include <intx/intx.hpp>
#include <ethash/keccak.hpp>

extern "C" {

evmc_host_context* test_host_create_context(evmc_tx_context tx_context, AssignerType *assigner)
evmc_host_context* vm_host_create_context(evmc_tx_context tx_context, AssignerType *assigner)
{
auto host = new TestHost{tx_context, assigner};
auto host = new VMHost{tx_context, assigner};
return host->to_context();
}

void test_host_destroy_context(evmc_host_context* context)
void vm_host_destroy_context(evmc_host_context* context)
{
delete evmc::Host::from_context<TestHost>(context);
delete evmc::Host::from_context<VMHost>(context);
}
}

evmc::Result TestHost::handle_call(const evmc_message& msg)
evmc::Result VMHost::handle_call(const evmc_message& msg)
{
evmc_vm * vm = evmc_create_evmone();
auto account_iter = accounts.find(msg.code_address);
Expand Down Expand Up @@ -46,9 +46,14 @@ evmc::Result TestHost::handle_call(const evmc_message& msg)
return res;
}

evmc::Result TestHost::handle_create(const evmc_message& msg)
evmc::Result VMHost::handle_create(const evmc_message& msg)
{
evmc::address new_contract_address = calculate_address(msg);
if (accounts.find(new_contract_address) != accounts.end())
{
// Address collision
return evmc::Result{EVMC_FAILURE};
}
accounts[new_contract_address] = {};
if (msg.input_size == 0)
{
Expand All @@ -72,7 +77,7 @@ evmc::Result TestHost::handle_create(const evmc_message& msg)
return res;
}

evmc::address TestHost::calculate_address(const evmc_message& msg)
evmc::address VMHost::calculate_address(const evmc_message& msg)
{
// TODO: Implement for CREATE opcode, for now the result is only correct for CREATE2
// CREATE requires rlp encoding
Expand Down
3 changes: 1 addition & 2 deletions lib/assigner/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ include(GoogleTest)
find_package(GTest CONFIG REQUIRED)

add_executable(assigner_tests
assigner_test.cpp
test_host.cpp)
assigner_test.cpp)

target_link_libraries(
assigner_tests
Expand Down
6 changes: 3 additions & 3 deletions lib/assigner/test/assigner_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <evmc/evmc.hpp>
#include <evmc/mocked_host.hpp>
#include "instructions_opcodes.hpp"
#include "test_host.h"
#include "vm_host.h"

#include <gtest/gtest.h>

Expand Down Expand Up @@ -59,13 +59,13 @@ class AssignerTest : public testing::Test


host_interface = &evmc::Host::get_interface();
ctx = test_host_create_context(tx_context, assigner_ptr.get());
ctx = vm_host_create_context(tx_context, assigner_ptr.get());
}

static void TearDownTestSuite()
{
assigner_ptr.reset();
test_host_destroy_context(ctx);
vm_host_destroy_context(ctx);
}

static std::unique_ptr<nil::blueprint::assigner<BlueprintFieldType>> assigner_ptr;
Expand Down

0 comments on commit 3bae30f

Please sign in to comment.