Browse Source

Change the way VMs are created (mostly for tracking where are created)

cl-refactor
Paweł Bylica 10 years ago
parent
commit
900fd04f1e
  1. 4
      libethereum/Executive.cpp
  2. 6
      libethereum/State.cpp
  3. 8
      libevm/VM.h
  4. 11
      libevm/VMFace.cpp
  5. 7
      libevm/VMFace.h
  6. 8
      libevmjit/VM.h
  7. 7
      test/vm.cpp
  8. 3
      windows/Eth.vcxproj

4
libethereum/Executive.cpp

@ -118,7 +118,7 @@ bool Executive::call(Address _receiveAddress, Address _senderAddress, u256 _valu
if (m_s.addressHasCode(_receiveAddress)) if (m_s.addressHasCode(_receiveAddress))
{ {
m_vm = new VM(_gas); m_vm = VMFace::create(VMFace::Interpreter, _gas).release();
bytes const& c = m_s.code(_receiveAddress); bytes const& c = m_s.code(_receiveAddress);
m_ext = new ExtVM(m_s, _receiveAddress, _senderAddress, _originAddress, _value, _gasPrice, _data, &c, m_ms); m_ext = new ExtVM(m_s, _receiveAddress, _senderAddress, _originAddress, _value, _gasPrice, _data, &c, m_ms);
} }
@ -137,7 +137,7 @@ bool Executive::create(Address _sender, u256 _endowment, u256 _gasPrice, u256 _g
m_s.m_cache[m_newAddress] = AddressState(0, m_s.balance(m_newAddress) + _endowment, h256(), h256()); m_s.m_cache[m_newAddress] = AddressState(0, m_s.balance(m_newAddress) + _endowment, h256(), h256());
// Execute _init. // Execute _init.
m_vm = new VM(_gas); m_vm = VMFace::create(VMFace::JIT, _gas).release();
m_ext = new ExtVM(m_s, m_newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _init, m_ms); m_ext = new ExtVM(m_s, m_newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _init, m_ms);
return _init.empty(); return _init.empty();
} }

6
libethereum/State.cpp

@ -1146,7 +1146,8 @@ bool State::call(Address _receiveAddress, Address _codeAddress, Address _senderA
} }
else if (addressHasCode(_codeAddress)) else if (addressHasCode(_codeAddress))
{ {
VM vm(*_gas); auto vmObj = VMFace::create(VMFace::Interpreter, *_gas);
VMFace& vm = *vmObj;
ExtVM evm(*this, _receiveAddress, _senderAddress, _originAddress, _value, _gasPrice, _data, &code(_codeAddress), o_ms, _level); ExtVM evm(*this, _receiveAddress, _senderAddress, _originAddress, _value, _gasPrice, _data, &code(_codeAddress), o_ms, _level);
bool revert = false; bool revert = false;
@ -1208,7 +1209,8 @@ h160 State::create(Address _sender, u256 _endowment, u256 _gasPrice, u256* _gas,
m_cache[newAddress] = AddressState(0, balance(newAddress) + _endowment, h256(), h256()); m_cache[newAddress] = AddressState(0, balance(newAddress) + _endowment, h256(), h256());
// Execute init code. // Execute init code.
VM vm(*_gas); auto vmObj = VMFace::create(VMFace::Interpreter, *_gas);
VMFace& vm = *vmObj;
ExtVM evm(*this, newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _code, o_ms, _level); ExtVM evm(*this, newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _code, o_ms, _level);
bool revert = false; bool revert = false;
bytesConstRef out; bytesConstRef out;

8
libevm/VM.h

@ -41,10 +41,7 @@ namespace eth
class VM : public VMFace class VM : public VMFace
{ {
public: public:
/// Construct VM object. virtual void reset(u256 _gas = 0) noexcept override final;
explicit VM(u256 _gas = 0): VMFace(_gas) {}
virtual void reset(u256 _gas = 0) override final;
virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final; virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final;
@ -56,6 +53,9 @@ public:
u256s const& stack() const { return m_stack; } u256s const& stack() const { return m_stack; }
private: private:
friend VMFace;
explicit VM(u256 _gas = 0): VMFace(_gas) {}
template <class Ext> template <class Ext>
bytesConstRef go(Ext& _ext, OnOpFunc const& _onOp, uint64_t _steps); bytesConstRef go(Ext& _ext, OnOpFunc const& _onOp, uint64_t _steps);

11
libevm/VMFace.cpp

@ -15,5 +15,16 @@
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "VMFace.h"
#include "VM.h" #include "VM.h"
#include <libevmjit/VM.h>
using namespace dev;
using namespace dev::eth;
std::unique_ptr<VMFace> VMFace::create(VMFace::Kind _kind, u256 _gas)
{
std::unique_ptr<VMFace> vm(_kind == Kind::JIT ? static_cast<VMFace*>(new jit::VM) : new VM);
vm->reset(_gas);
return vm;
}

7
libevm/VMFace.h

@ -17,6 +17,7 @@
#pragma once #pragma once
#include <memory>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include "ExtVMFace.h" #include "ExtVMFace.h"
@ -61,12 +62,16 @@ public:
VMFace(VMFace const&) = delete; VMFace(VMFace const&) = delete;
void operator=(VMFace const&) = delete; void operator=(VMFace const&) = delete;
virtual void reset(u256 _gas = 0) { m_gas = _gas; } virtual void reset(u256 _gas = 0) noexcept { m_gas = _gas; }
virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) = 0; virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) = 0;
u256 gas() const { return m_gas; } u256 gas() const { return m_gas; }
enum Kind: bool { Interpreter, JIT };
static std::unique_ptr<VMFace> create(Kind, u256 _gas = 0);
protected: protected:
u256 m_gas = 0; u256 m_gas = 0;
}; };

8
libevmjit/VM.h

@ -14,12 +14,12 @@ namespace jit
class VM: public VMFace class VM: public VMFace
{ {
public: virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final;
explicit VM(u256 _gas = 0): VMFace(_gas) {}
virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) final;
private: private:
friend VMFace;
explicit VM(u256 _gas = 0): VMFace(_gas) {}
bytes m_output; bytes m_output;
}; };

7
test/vm.cpp

@ -436,7 +436,8 @@ h160 FakeState::createNewAddress(Address _newAddress, Address _sender, u256 _end
m_cache[_newAddress] = AddressState(0, balance(_newAddress) + _endowment, h256(), h256()); m_cache[_newAddress] = AddressState(0, balance(_newAddress) + _endowment, h256(), h256());
// Execute init code. // Execute init code.
VM vm(*_gas); auto vmObj = VMFace::create(VMFace::Interpreter, *_gas);
VMFace& vm = *vmObj;
ExtVM evm(*this, _newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _code, o_ms, _level); ExtVM evm(*this, _newAddress, _sender, _origin, _endowment, _gasPrice, bytesConstRef(), _code, o_ms, _level);
bool revert = false; bool revert = false;
bytesConstRef out; bytesConstRef out;
@ -517,8 +518,8 @@ void doTests(json_spirit::mValue& v, bool _fillin)
auto argv = boost::unit_test::framework::master_test_suite().argv; auto argv = boost::unit_test::framework::master_test_suite().argv;
auto useJit = argc >= 2 && std::string(argv[1]) == "--jit"; auto useJit = argc >= 2 && std::string(argv[1]) == "--jit";
auto vm = useJit ? std::unique_ptr<VMFace>(new jit::VM) : std::unique_ptr<VMFace>(new VM); auto vmKind = useJit ? VMFace::JIT : VMFace::Interpreter;
vm->reset(fev.gas); auto vm = VMFace::create(vmKind, fev.gas);
bytes output; bytes output;
auto outOfGas = false; auto outOfGas = false;
try try

3
windows/Eth.vcxproj

@ -155,6 +155,9 @@
<ProjectReference Include="..\windows\LibEthereum.vcxproj"> <ProjectReference Include="..\windows\LibEthereum.vcxproj">
<Project>{826e68cb-d3ee-4a8a-b540-59a8c3f38d4f}</Project> <Project>{826e68cb-d3ee-4a8a-b540-59a8c3f38d4f}</Project>
</ProjectReference> </ProjectReference>
<ProjectReference Include="LibEvmJit.vcxproj">
<Project>{9c816740-5c11-4377-a3a7-46be12f35fa0}</Project>
</ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\eth\main.cpp" /> <ClCompile Include="..\eth\main.cpp" />

Loading…
Cancel
Save