Browse Source

Extract interface of VM into new VMFace class

cl-refactor
Paweł Bylica 10 years ago
parent
commit
4f69964b2a
  1. 12
      libevm/VM.cpp
  2. 38
      libevm/VM.h
  3. 19
      libevm/VMFace.cpp
  4. 75
      libevm/VMFace.h
  5. 2
      windows/LibEthereum.vcxproj
  6. 6
      windows/LibEthereum.vcxproj.filters

12
libevm/VM.cpp

@ -20,13 +20,21 @@
*/ */
#include "VM.h" #include "VM.h"
#include <libethereum/ExtVM.h>
using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
void VM::reset(u256 _gas) void VM::reset(u256 _gas)
{ {
m_gas = _gas; VMFace::reset(_gas);
m_curPC = 0; m_curPC = 0;
} }
bytesConstRef VM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps)
{
if (auto defaultExt = dynamic_cast<ExtVM*>(&_ext))
return go<ExtVM>(*defaultExt, _onOp, _steps);
else
return go<ExtVMFace>(_ext, _onOp, _steps);
}

38
libevm/VM.h

@ -27,6 +27,7 @@
#include <libevmface/Instruction.h> #include <libevmface/Instruction.h>
#include <libdevcrypto/SHA3.h> #include <libdevcrypto/SHA3.h>
#include <libethcore/BlockInfo.h> #include <libethcore/BlockInfo.h>
#include "VMFace.h"
#include "FeeStructure.h" #include "FeeStructure.h"
#include "ExtVMFace.h" #include "ExtVMFace.h"
@ -35,52 +36,29 @@ namespace dev
namespace eth namespace eth
{ {
struct VMException: virtual Exception {};
struct StepsDone: virtual VMException {};
struct BreakPointHit: virtual VMException {};
struct BadInstruction: virtual VMException {};
struct BadJumpDestination: virtual VMException {};
struct OutOfGas: virtual VMException {};
class StackTooSmall: virtual public VMException { public: StackTooSmall(u256 _req, u256 _got): req(_req), got(_got) {} u256 req; u256 got; };
// Convert from a 256-bit integer stack/memory entry into a 160-bit Address hash.
// Currently we just pull out the right (low-order in BE) 160-bits.
inline Address asAddress(u256 _item)
{
return right160(h256(_item));
}
inline u256 fromAddress(Address _a)
{
return (u160)_a;
// h256 ret;
// memcpy(&ret, &_a, sizeof(_a));
// return ret;
}
/** /**
*/ */
class VM class VM : public VMFace
{ {
public: public:
/// Construct VM object. /// Construct VM object.
explicit VM(u256 _gas = 0) { reset(_gas); } explicit VM(u256 _gas = 0): VMFace(_gas) {}
void reset(u256 _gas = 0); virtual void reset(u256 _gas = 0) override final;
template <class Ext> virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final;
bytesConstRef go(Ext& _ext, OnOpFunc const& _onOp = OnOpFunc(), uint64_t _steps = (uint64_t)-1);
void require(u256 _n) { if (m_stack.size() < _n) BOOST_THROW_EXCEPTION(StackTooSmall(_n, m_stack.size())); } void require(u256 _n) { if (m_stack.size() < _n) BOOST_THROW_EXCEPTION(StackTooSmall(_n, m_stack.size())); }
void requireMem(unsigned _n) { if (m_temp.size() < _n) { m_temp.resize(_n); } } void requireMem(unsigned _n) { if (m_temp.size() < _n) { m_temp.resize(_n); } }
u256 gas() const { return m_gas; }
u256 curPC() const { return m_curPC; } u256 curPC() const { return m_curPC; }
bytes const& memory() const { return m_temp; } bytes const& memory() const { return m_temp; }
u256s const& stack() const { return m_stack; } u256s const& stack() const { return m_stack; }
private: private:
u256 m_gas = 0; template <class Ext>
bytesConstRef go(Ext& _ext, OnOpFunc const& _onOp, uint64_t _steps);
u256 m_curPC = 0; u256 m_curPC = 0;
bytes m_temp; bytes m_temp;
u256s m_stack; u256s m_stack;

19
libevm/VMFace.cpp

@ -0,0 +1,19 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#include "VM.h"

75
libevm/VMFace.h

@ -0,0 +1,75 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <libdevcore/Exceptions.h>
#include "ExtVMFace.h"
namespace dev
{
namespace eth
{
struct VMException: virtual Exception {};
struct StepsDone: virtual VMException {};
struct BreakPointHit: virtual VMException {};
struct BadInstruction: virtual VMException {};
struct BadJumpDestination: virtual VMException {};
struct OutOfGas: virtual VMException {};
class StackTooSmall: virtual public VMException { public: StackTooSmall(u256 _req, u256 _got): req(_req), got(_got) {} u256 req; u256 got; };
// Convert from a 256-bit integer stack/memory entry into a 160-bit Address hash.
// Currently we just pull out the right (low-order in BE) 160-bits.
inline Address asAddress(u256 _item)
{
return right160(h256(_item));
}
inline u256 fromAddress(Address _a)
{
return (u160)_a;
// h256 ret;
// memcpy(&ret, &_a, sizeof(_a));
// return ret;
}
/**
*/
class VMFace
{
public:
/// Construct VM object.
explicit VMFace(u256 _gas = 0): m_gas(_gas) {}
virtual ~VMFace() = default;
VMFace(VMFace const&) = delete;
void operator=(VMFace const&) = delete;
virtual void reset(u256 _gas = 0) { m_gas = _gas; }
virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) = 0;
u256 gas() const { return m_gas; }
protected:
u256 m_gas = 0;
};
}
}

2
windows/LibEthereum.vcxproj

@ -141,6 +141,7 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile> </ClCompile>
<ClCompile Include="..\libevm\VMFace.cpp" />
<ClCompile Include="..\libevm\_libevm.cpp" /> <ClCompile Include="..\libevm\_libevm.cpp" />
<ClCompile Include="..\liblll\Assembly.cpp" /> <ClCompile Include="..\liblll\Assembly.cpp" />
<ClCompile Include="..\liblll\CodeFragment.cpp" /> <ClCompile Include="..\liblll\CodeFragment.cpp" />
@ -360,6 +361,7 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild> <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClInclude> </ClInclude>
<ClInclude Include="..\libevm\VMFace.h" />
<ClInclude Include="..\liblll\All.h" /> <ClInclude Include="..\liblll\All.h" />
<ClInclude Include="..\liblll\Assembly.h" /> <ClInclude Include="..\liblll\Assembly.h" />
<ClInclude Include="..\liblll\CodeFragment.h" /> <ClInclude Include="..\liblll\CodeFragment.h" />

6
windows/LibEthereum.vcxproj.filters

@ -193,6 +193,9 @@
<ClCompile Include="..\libethcore\Exceptions.cpp"> <ClCompile Include="..\libethcore\Exceptions.cpp">
<Filter>libethcore</Filter> <Filter>libethcore</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\libevm\VMFace.cpp">
<Filter>libevm</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="stdafx.h"> <ClInclude Include="stdafx.h">
@ -417,6 +420,9 @@
<ClInclude Include="..\libwebthree\webthree.h"> <ClInclude Include="..\libwebthree\webthree.h">
<Filter>libwebthree</Filter> <Filter>libwebthree</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\libevm\VMFace.h">
<Filter>libevm</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="Windows"> <Filter Include="Windows">

Loading…
Cancel
Save