Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Pass options to EVMC class constructor instead of using globall… (#5875)
Browse files Browse the repository at this point in the history
Pass options to EVMC class constructor instead of using globally set options
  • Loading branch information
gumb0 authored Dec 23, 2019
2 parents 31db6db + f0f2341 commit 2fe1bec
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
5 changes: 3 additions & 2 deletions libevm/EVMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ evmc_revision toRevision(EVMSchedule const& _schedule) noexcept
}
} // namespace

EVMC::EVMC(evmc_vm* _vm) noexcept : evmc::VM(_vm)
EVMC::EVMC(evmc_vm* _vm, std::vector<std::pair<std::string, std::string>> const& _options) noexcept
: evmc::VM(_vm)
{
assert(_vm != nullptr);
assert(is_abi_compatible());

// Set the options.
for (auto& pair : evmcOptions())
for (auto& pair : _options)
{
auto result = set_option(pair.first.c_str(), pair.second.c_str());
switch (result)
Expand Down
5 changes: 4 additions & 1 deletion libevm/EVMC.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#pragma once

#include <libevm/VMFace.h>
#include <string>
#include <utility>
#include <vector>

namespace dev
{
Expand All @@ -14,7 +17,7 @@ namespace eth
class EVMC : public evmc::VM, public VMFace
{
public:
explicit EVMC(evmc_vm* _vm) noexcept;
EVMC(evmc_vm* _vm, std::vector<std::pair<std::string, std::string>> const& _options) noexcept;

owning_bytes_ref exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp) final;
};
Expand Down
15 changes: 5 additions & 10 deletions libevm/VMFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ auto g_kind = VMKind::Legacy;
/// so access is thread-safe.
std::unique_ptr<EVMC> g_evmcDll;

/// The list of EVMC options stored as pairs of (name, value).
std::vector<std::pair<std::string, std::string>> s_evmcOptions;

/// A helper type to build the tabled of VM implementations.
///
/// More readable than std::tuple.
Expand Down Expand Up @@ -86,7 +89,7 @@ void setVMKind(const std::string& _name)
"loading " + _name + " failed"));
}

g_evmcDll.reset(new EVMC{vm});
g_evmcDll.reset(new EVMC{vm, s_evmcOptions});

cnote << "Loaded EVMC module: " << g_evmcDll->name() << " " << g_evmcDll->version() << " ("
<< _name << ")";
Expand All @@ -99,9 +102,6 @@ namespace
/// space and we can reuse this variable in exception message.
const char c_evmcPrefix[] = "evmc ";

/// The list of EVMC options stored as pairs of (name, value).
std::vector<std::pair<std::string, std::string>> s_evmcOptions;

/// The additional parser for EVMC options. The options should look like
/// `--evmc name=value` or `--evmc=name=value`. The boost pass the strings
/// of `name=value` here. This function splits the name and value or reports
Expand All @@ -120,11 +120,6 @@ void parseEvmcOptions(const std::vector<std::string>& _opts)
}
} // namespace

std::vector<std::pair<std::string, std::string>>& evmcOptions() noexcept
{
return s_evmcOptions;
};

po::options_description vmProgramOptions(unsigned _lineLength)
{
// It must be a static object because boost expects const char*.
Expand Down Expand Up @@ -174,7 +169,7 @@ VMPtr VMFactory::create(VMKind _kind)
switch (_kind)
{
case VMKind::Interpreter:
return {new EVMC{evmc_create_aleth_interpreter()}, default_delete};
return {new EVMC{evmc_create_aleth_interpreter(), s_evmcOptions}, default_delete};
case VMKind::DLL:
assert(g_evmcDll != nullptr);
// Return "fake" owning pointer to global EVMC DLL VM.
Expand Down
3 changes: 0 additions & 3 deletions libevm/VMFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ enum class VMKind
DLL
};

/// Returns the EVMC options parsed from command line.
std::vector<std::pair<std::string, std::string>>& evmcOptions() noexcept;

/// Provide a set of program options related to VMs.
///
/// @param _lineLength The line length for description text wrapping, the same as in
Expand Down
16 changes: 11 additions & 5 deletions test/unittests/libevm/VMTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ class LegacyVMCreate2TestFixture: public Create2TestFixture
class AlethInterpreterCreate2TestFixture: public Create2TestFixture
{
public:
AlethInterpreterCreate2TestFixture(): Create2TestFixture{new EVMC{evmc_create_aleth_interpreter()}} {}
AlethInterpreterCreate2TestFixture()
: Create2TestFixture{new EVMC{evmc_create_aleth_interpreter(), {}}}
{}
};

class ExtcodehashTestFixture : public TestOutputHelperFixture
Expand Down Expand Up @@ -376,7 +378,7 @@ class AlethInterpreterExtcodehashTestFixture : public ExtcodehashTestFixture
{
public:
AlethInterpreterExtcodehashTestFixture()
: ExtcodehashTestFixture{new EVMC{evmc_create_aleth_interpreter()}}
: ExtcodehashTestFixture{new EVMC{evmc_create_aleth_interpreter(), {}}}
{}
};

Expand Down Expand Up @@ -546,7 +548,9 @@ class LegacyVMSstoreTestFixture : public SstoreTestFixture
class AlethInterpreterSstoreTestFixture : public SstoreTestFixture
{
public:
AlethInterpreterSstoreTestFixture() : SstoreTestFixture{new EVMC{evmc_create_aleth_interpreter()}} {}
AlethInterpreterSstoreTestFixture()
: SstoreTestFixture{new EVMC{evmc_create_aleth_interpreter(), {}}}
{}
};

class ChainIDTestFixture : public TestOutputHelperFixture
Expand Down Expand Up @@ -630,7 +634,8 @@ class LegacyVMChainIDTestFixture : public ChainIDTestFixture
class AlethInterpreterChainIDTestFixture : public ChainIDTestFixture
{
public:
AlethInterpreterChainIDTestFixture() : ChainIDTestFixture{new EVMC{evmc_create_aleth_interpreter()}}
AlethInterpreterChainIDTestFixture()
: ChainIDTestFixture{new EVMC{evmc_create_aleth_interpreter(), {}}}
{}
};

Expand Down Expand Up @@ -749,7 +754,8 @@ class LegacyVMBalanceFixture : public BalanceFixture
class AlethInterpreterBalanceFixture : public BalanceFixture
{
public:
AlethInterpreterBalanceFixture() : BalanceFixture{new EVMC{evmc_create_aleth_interpreter()}} {}
AlethInterpreterBalanceFixture() : BalanceFixture{new EVMC{evmc_create_aleth_interpreter(), {}}}
{}
};

class PrecompileCallFixture : public TestOutputHelperFixture
Expand Down

0 comments on commit 2fe1bec

Please sign in to comment.