Browse Source

Merge pull request #406 from imapp-pl/pr-changes

ExtVMFace pylimorphic changes
cl-refactor
Gav Wood 10 years ago
parent
commit
485eb111c5
  1. 20
      libethereum/ExtVM.h
  2. 35
      libevm/ExtVMFace.h
  3. 8
      test/vm.cpp
  4. 22
      test/vm.h

20
libethereum/ExtVM.h

@ -46,16 +46,16 @@ public:
} }
/// Read storage location. /// Read storage location.
u256 store(u256 _n) { return m_s.storage(myAddress, _n); } virtual u256 store(u256 _n) override final { return m_s.storage(myAddress, _n); }
/// Write a value in storage. /// Write a value in storage.
void setStore(u256 _n, u256 _v) { m_s.setStorage(myAddress, _n, _v); if (m_ms) m_ms->altered.push_back(_n); } virtual void setStore(u256 _n, u256 _v) override final { m_s.setStorage(myAddress, _n, _v); if (m_ms) m_ms->altered.push_back(_n); }
/// Read address's code. /// Read address's code.
bytes const& codeAt(Address _a) { return m_s.code(_a); } virtual bytes const& codeAt(Address _a) override final { return m_s.code(_a); }
/// Create a new contract. /// Create a new contract.
h160 create(u256 _endowment, u256* _gas, bytesConstRef _code, OnOpFunc const& _onOp = OnOpFunc()) virtual h160 create(u256 _endowment, u256* _gas, bytesConstRef _code, OnOpFunc const& _onOp = OnOpFunc()) override final
{ {
// Increment associated nonce for sender. // Increment associated nonce for sender.
m_s.noteSending(myAddress); m_s.noteSending(myAddress);
@ -68,7 +68,7 @@ public:
} }
/// Create a new message call. Leave _myAddressOverride as the default to use the present address as caller. /// Create a new message call. Leave _myAddressOverride as the default to use the present address as caller.
bool call(Address _receiveAddress, u256 _txValue, bytesConstRef _txData, u256* _gas, bytesRef _out, OnOpFunc const& _onOp = OnOpFunc(), Address _myAddressOverride = Address(), Address _codeAddressOverride = Address()) virtual bool call(Address _receiveAddress, u256 _txValue, bytesConstRef _txData, u256* _gas, bytesRef _out, OnOpFunc const& _onOp = {}, Address _myAddressOverride = {}, Address _codeAddressOverride = {}) override final
{ {
if (m_ms) if (m_ms)
m_ms->internal.resize(m_ms->internal.size() + 1); m_ms->internal.resize(m_ms->internal.size() + 1);
@ -79,16 +79,16 @@ public:
} }
/// Read address's balance. /// Read address's balance.
u256 balance(Address _a) { return m_s.balance(_a); } virtual u256 balance(Address _a) override final { return m_s.balance(_a); }
/// Subtract amount from account's balance. /// Subtract amount from account's balance.
void subBalance(u256 _a) { m_s.subBalance(myAddress, _a); } virtual void subBalance(u256 _a) override final { m_s.subBalance(myAddress, _a); }
/// Determine account's TX count. /// Determine account's TX count.
u256 txCount(Address _a) { return m_s.transactionsFrom(_a); } virtual u256 txCount(Address _a) override final { return m_s.transactionsFrom(_a); }
/// Suicide the associated contract to the given address. /// Suicide the associated contract to the given address.
void suicide(Address _a) virtual void suicide(Address _a) override final
{ {
m_s.addBalance(_a, m_s.balance(myAddress)); m_s.addBalance(_a, m_s.balance(myAddress));
ExtVMFace::suicide(_a); ExtVMFace::suicide(_a);
@ -96,7 +96,7 @@ public:
/// Revert any changes made (by any of the other calls). /// Revert any changes made (by any of the other calls).
/// @TODO check call site for the parent manifest being discarded. /// @TODO check call site for the parent manifest being discarded.
void revert() { if (m_ms) *m_ms = Manifest(); m_s.m_cache = m_origCache; } virtual void revert() override final { if (m_ms) *m_ms = Manifest(); m_s.m_cache = m_origCache; }
State& state() const { return m_s; } State& state() const { return m_s; }

35
libevm/ExtVMFace.h

@ -21,7 +21,7 @@
#pragma once #pragma once
#include <list> #include <set>
#include <functional> #include <functional>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libevmface/Instruction.h> #include <libevmface/Instruction.h>
@ -45,49 +45,54 @@ struct Post
using OnOpFunc = std::function<void(uint64_t /*steps*/, Instruction /*instr*/, bigint /*newMemSize*/, bigint /*gasCost*/, void/*VM*/*, void/*ExtVM*/ const*)>; using OnOpFunc = std::function<void(uint64_t /*steps*/, Instruction /*instr*/, bigint /*newMemSize*/, bigint /*gasCost*/, void/*VM*/*, void/*ExtVM*/ const*)>;
/** /**
* @brief A null implementation of the class for specifying VM externalities. * @brief Interface and null implementation of the class for specifying VM externalities.
*/ */
class ExtVMFace class ExtVMFace
{ {
public: public:
/// Null constructor. /// Null constructor.
ExtVMFace() {} ExtVMFace() = default;
/// Full constructor. /// Full constructor.
ExtVMFace(Address _myAddress, Address _caller, Address _origin, u256 _value, u256 _gasPrice, bytesConstRef _data, bytesConstRef _code, BlockInfo const& _previousBlock, BlockInfo const& _currentBlock, unsigned _depth); ExtVMFace(Address _myAddress, Address _caller, Address _origin, u256 _value, u256 _gasPrice, bytesConstRef _data, bytesConstRef _code, BlockInfo const& _previousBlock, BlockInfo const& _currentBlock, unsigned _depth);
/// Get the code at the given location in code ROM. virtual ~ExtVMFace() = default;
byte getCode(u256 _n) const { return _n < code.size() ? code[(unsigned)_n] : 0; }
ExtVMFace(ExtVMFace const&) = delete;
void operator=(ExtVMFace) = delete;
/// Read storage location. /// Read storage location.
u256 store(u256) { return 0; } virtual u256 store(u256) { return 0; }
/// Write a value in storage. /// Write a value in storage.
void setStore(u256, u256) {} virtual void setStore(u256, u256) {}
/// Read address's balance. /// Read address's balance.
u256 balance(Address) { return 0; } virtual u256 balance(Address) { return 0; }
/// Read address's code. /// Read address's code.
bytes const& codeAt(Address) { return NullBytes; } virtual bytes const& codeAt(Address) { return NullBytes; }
/// Subtract amount from account's balance. /// Subtract amount from account's balance.
void subBalance(u256) {} virtual void subBalance(u256) {}
/// Determine account's TX count. /// Determine account's TX count.
u256 txCount(Address) { return 0; } virtual u256 txCount(Address) { return 0; }
/// Suicide the associated contract and give proceeds to the given address. /// Suicide the associated contract and give proceeds to the given address.
void suicide(Address) { suicides.insert(myAddress); } virtual void suicide(Address) { suicides.insert(myAddress); }
/// Create a new (contract) account. /// Create a new (contract) account.
h160 create(u256, u256*, bytesConstRef, bytesConstRef) { return h160(); } virtual h160 create(u256, u256*, bytesConstRef, OnOpFunc const&) { return h160(); }
/// Make a new message call. /// Make a new message call.
bool call(Address, u256, bytesConstRef, u256*, bytesRef, OnOpFunc const&, Address, Address) { return false; } virtual bool call(Address, u256, bytesConstRef, u256*, bytesRef, OnOpFunc const&, Address, Address) { return false; }
/// Revert any changes made (by any of the other calls). /// Revert any changes made (by any of the other calls).
void revert() {} virtual void revert() {}
/// Get the code at the given location in code ROM.
byte getCode(u256 _n) const { return _n < code.size() ? code[(size_t)_n] : 0; }
Address myAddress; ///< Address associated with executing code (a contract, or contract-to-be). Address myAddress; ///< Address associated with executing code (a contract, or contract-to-be).
Address caller; ///< Address which sent the message (either equal to origin or a contract). Address caller; ///< Address which sent the message (either equal to origin or a contract).

8
test/vm.cpp

@ -35,7 +35,7 @@ using namespace dev::test;
FakeExtVM::FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock, unsigned _depth): /// TODO: XXX: remove the default argument & fix. FakeExtVM::FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock, unsigned _depth): /// TODO: XXX: remove the default argument & fix.
ExtVMFace(Address(), Address(), Address(), 0, 1, bytesConstRef(), bytesConstRef(), _previousBlock, _currentBlock, _depth) {} ExtVMFace(Address(), Address(), Address(), 0, 1, bytesConstRef(), bytesConstRef(), _previousBlock, _currentBlock, _depth) {}
h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFunc) h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFunc const&)
{ {
Transaction t; Transaction t;
t.value = _endowment; t.value = _endowment;
@ -45,7 +45,7 @@ h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFun
m_s.noteSending(myAddress); m_s.noteSending(myAddress);
m_ms.internal.resize(m_ms.internal.size() + 1); m_ms.internal.resize(m_ms.internal.size() + 1);
auto ret = m_s.create(myAddress, _endowment, gasPrice, _gas, _init, origin, &suicides, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1); auto ret = m_s.create(myAddress, _endowment, gasPrice, _gas, _init, origin, &suicides, &m_ms ? &(m_ms.internal.back()) : nullptr, {}, 1);
if (!m_ms.internal.back().from) if (!m_ms.internal.back().from)
m_ms.internal.pop_back(); m_ms.internal.pop_back();
@ -61,7 +61,7 @@ h160 FakeExtVM::create(u256 _endowment, u256* _gas, bytesConstRef _init, OnOpFun
return ret; return ret;
} }
bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, OnOpFunc, Address _myAddressOverride = Address(), Address _codeAddressOverride = Address()) bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, OnOpFunc const&, Address _myAddressOverride, Address _codeAddressOverride)
{ {
u256 contractgas = 0xffff; u256 contractgas = 0xffff;
@ -91,7 +91,7 @@ bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data,
if (!m_s.addresses().count(myAddress)) if (!m_s.addresses().count(myAddress))
{ {
m_ms.internal.resize(m_ms.internal.size() + 1); m_ms.internal.resize(m_ms.internal.size() + 1);
auto na = m_s.createNewAddress(myAddress, myAddress, balance(myAddress), gasPrice, &contractgas, init, origin, &suicides, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1); auto na = m_s.createNewAddress(myAddress, myAddress, balance(myAddress), gasPrice, &contractgas, init, origin, &suicides, &m_ms ? &(m_ms.internal.back()) : nullptr, {}, 1);
if (!m_ms.internal.back().from) if (!m_ms.internal.back().from)
m_ms.internal.pop_back(); m_ms.internal.pop_back();
if (na != myAddress) if (na != myAddress)

22
test/vm.h

@ -44,24 +44,24 @@ class FakeState: public eth::State
{ {
public: public:
/// Execute a contract-creation transaction. /// Execute a contract-creation transaction.
h160 createNewAddress(Address _newAddress, Address _txSender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _originAddress = Address(), std::set<Address>* o_suicides = nullptr, eth::Manifest* o_ms = nullptr, eth::OnOpFunc const& _onOp = eth::OnOpFunc(), unsigned _level = 0); h160 createNewAddress(Address _newAddress, Address _txSender, u256 _endowment, u256 _gasPrice, u256* _gas, bytesConstRef _code, Address _originAddress = {}, std::set<Address>* o_suicides = nullptr, eth::Manifest* o_ms = nullptr, eth::OnOpFunc const& _onOp = {}, unsigned _level = 0);
}; };
class FakeExtVM: public eth::ExtVMFace class FakeExtVM: public eth::ExtVMFace
{ {
public: public:
FakeExtVM() {} FakeExtVM() = default;
FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock, unsigned _depth = 0); FakeExtVM(eth::BlockInfo const& _previousBlock, eth::BlockInfo const& _currentBlock, unsigned _depth = 0);
u256 store(u256 _n) { return std::get<2>(addresses[myAddress])[_n]; } virtual u256 store(u256 _n) override { return std::get<2>(addresses[myAddress])[_n]; }
void setStore(u256 _n, u256 _v) { std::get<2>(addresses[myAddress])[_n] = _v; } virtual void setStore(u256 _n, u256 _v) override { std::get<2>(addresses[myAddress])[_n] = _v; }
u256 balance(Address _a) { return std::get<0>(addresses[_a]); } virtual u256 balance(Address _a) override { return std::get<0>(addresses[_a]); }
void subBalance(u256 _a) { std::get<0>(addresses[myAddress]) -= _a; } virtual void subBalance(u256 _a) override { std::get<0>(addresses[myAddress]) -= _a; }
u256 txCount(Address _a) { return std::get<1>(addresses[_a]); } virtual u256 txCount(Address _a) override { return std::get<1>(addresses[_a]); }
void suicide(Address _a) { std::get<0>(addresses[_a]) += std::get<0>(addresses[myAddress]); addresses.erase(myAddress); } virtual void suicide(Address _a) override { std::get<0>(addresses[_a]) += std::get<0>(addresses[myAddress]); addresses.erase(myAddress); }
bytes const& codeAt(Address _a) { return std::get<3>(addresses[_a]); } virtual bytes const& codeAt(Address _a) override { return std::get<3>(addresses[_a]); }
h160 create(u256 _endowment, u256* _gas, bytesConstRef _init, eth::OnOpFunc); virtual h160 create(u256 _endowment, u256* _gas, bytesConstRef _init, eth::OnOpFunc const&) override;
bool call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, eth::OnOpFunc, Address, Address); virtual bool call(Address _receiveAddress, u256 _value, bytesConstRef _data, u256* _gas, bytesRef _out, eth::OnOpFunc const&, Address, Address) override;
void setTransaction(Address _caller, u256 _value, u256 _gasPrice, bytes const& _data); void setTransaction(Address _caller, u256 _value, u256 _gasPrice, bytes const& _data);
void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage, bytes const& _code); void setContract(Address _myAddress, u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage, bytes const& _code);
void set(Address _a, u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage, bytes const& _code); void set(Address _a, u256 _myBalance, u256 _myNonce, std::map<u256, u256> const& _storage, bytes const& _code);

Loading…
Cancel
Save