Ethereum  PoC-8
The C++ Implementation of Ethereum
ExtVM.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 #include "Executive.h"
21 #include "State.h"
22 
23 #include <libethcore/Common.h>
24 #include <libethcore/SealEngine.h>
25 #include <libevm/ExtVMFace.h>
26 
27 #include <functional>
28 #include <map>
29 
30 namespace dev
31 {
32 namespace eth
33 {
34 
35 class SealEngineFace;
36 
38 class ExtVM : public ExtVMFace
39 {
40 public:
42  ExtVM(State& _s, EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, Address _myAddress,
43  Address _caller, Address _origin, u256 _value, u256 _gasPrice, bytesConstRef _data,
44  bytesConstRef _code, h256 const& _codeHash, unsigned _depth, bool _isCreate,
45  bool _staticCall)
46  : ExtVMFace(_envInfo, _myAddress, _caller, _origin, _value, _gasPrice, _data, _code.toBytes(),
47  _codeHash, _depth, _isCreate, _staticCall),
48  m_s(_s),
49  m_sealEngine(_sealEngine)
50  {
51  // Contract: processing account must exist. In case of CALL, the ExtVM
52  // is created only if an account has code (so exist). In case of CREATE
53  // the account must be created first.
54  assert(m_s.addressInUse(_myAddress));
55  }
56 
58  u256 store(u256 _n) final { return m_s.storage(myAddress, _n); }
59 
61  void setStore(u256 _n, u256 _v) final;
62 
64  u256 originalStorageValue(u256 const& _key) final
65  {
66  return m_s.originalStorageValue(myAddress, _key);
67  }
68 
70  bytes const& codeAt(Address _a) final { return m_s.code(_a); }
71 
73  size_t codeSizeAt(Address _a) final;
74 
76  h256 codeHashAt(Address _a) final;
77 
79  CreateResult create(u256 _endowment, u256& io_gas, bytesConstRef _code, Instruction _op, u256 _salt, OnOpFunc const& _onOp = {}) final;
80 
82  CallResult call(CallParameters& _params) final;
83 
85  u256 balance(Address _a) final { return m_s.balance(_a); }
86 
88  bool exists(Address _a) final
89  {
90  if (evmSchedule().emptinessIsNonexistence())
91  return m_s.accountNonemptyAndExisting(_a);
92  else
93  return m_s.addressInUse(_a);
94  }
95 
97  void suicide(Address _a) final;
98 
100  EVMSchedule const& evmSchedule() const final
101  {
102  return m_sealEngine.evmSchedule(envInfo().number());
103  }
104 
105  State const& state() const { return m_s; }
106 
108  h256 blockHash(u256 _number) final;
109 
110 private:
111  State& m_s;
112  SealEngineFace const& m_sealEngine;
113 };
114 
115 }
116 }
117 
dev::eth::OnOpFunc
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VMFace const *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:115
dev::eth::State::addressInUse
bool addressInUse(Address const &_address) const
Check if the address is in use.
Definition: State.cpp:291
dev::vector_ref< byte const >
dev::eth::ExtVM::setStore
void setStore(u256 _n, u256 _v) final
Write a value in storage.
Definition: ExtVM.cpp:149
dev::eth::SealEngineFace
Definition: SealEngine.h:46
dev::eth::State::code
bytes const & code(Address const &_addr) const
Definition: State.cpp:503
dev::eth::ExtVM::suicide
void suicide(Address _a) final
Suicide the associated contract to the given address.
Definition: ExtVM.cpp:175
dev::eth::ExtVMFace
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:204
dev::eth::ExtVM::evmSchedule
EVMSchedule const & evmSchedule() const final
Return the EVM gas-price schedule for this execution context.
Definition: ExtVM.h:100
dev::FixedHash< 20 >
dev::eth::State::accountNonemptyAndExisting
bool accountNonemptyAndExisting(Address const &_address) const
Definition: State.cpp:296
dev::eth::ExtVM::balance
u256 balance(Address _a) final
Read address's balance.
Definition: ExtVM.h:85
Common.h
dev::eth::ExtVMFace::myAddress
Address myAddress
Address associated with executing code (a contract, or contract-to-be).
Definition: ExtVMFace.h:266
ExtVMFace.h
dev::eth::ExtVM::exists
bool exists(Address _a) final
Does the account exist?
Definition: ExtVM.h:88
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
SealEngine.h
Executive.h
dev::eth::ExtVM
Externality interface for the Virtual Machine providing access to world state.
Definition: ExtVM.h:39
dev::eth::ExtVM::ExtVM
ExtVM(State &_s, EnvInfo const &_envInfo, SealEngineFace const &_sealEngine, Address _myAddress, Address _caller, Address _origin, u256 _value, u256 _gasPrice, bytesConstRef _data, bytesConstRef _code, h256 const &_codeHash, unsigned _depth, bool _isCreate, bool _staticCall)
Full constructor.
Definition: ExtVM.h:42
dev::eth::ExtVM::codeSizeAt
size_t codeSizeAt(Address _a) final
Definition: ExtVM.cpp:139
dev::eth::Instruction
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:29
dev::eth::ExtVM::call
CallResult call(CallParameters &_params) final
Create a new message call.
Definition: ExtVM.cpp:126
dev::eth::ExtVM::codeAt
bytes const & codeAt(Address _a) final
Read address's code.
Definition: ExtVM.h:70
dev::eth::State
Definition: State.h:160
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::eth::ExtVM::codeHashAt
h256 codeHashAt(Address _a) final
Definition: ExtVM.cpp:144
dev::eth::State::originalStorageValue
u256 originalStorageValue(Address const &_contract, u256 const &_key) const
Definition: State.cpp:436
dev::eth::ExtVM::originalStorageValue
u256 originalStorageValue(u256 const &_key) final
Read original storage value (before modifications in the current transaction).
Definition: ExtVM.h:64
dev::eth::CreateResult
Definition: ExtVMFace.h:190
dev::eth::ExtVM::blockHash
h256 blockHash(u256 _number) final
Hash of a block if within the last 256 blocks, or h256() otherwise.
Definition: ExtVM.cpp:186
dev::eth::ExtVM::create
CreateResult create(u256 _endowment, u256 &io_gas, bytesConstRef _code, Instruction _op, u256 _salt, OnOpFunc const &_onOp={}) final
Create a new contract.
Definition: ExtVM.cpp:154
dev
Definition: Address.cpp:21
dev::eth::ExtVM::state
State const & state() const
Definition: ExtVM.h:105
dev::eth::State::balance
u256 balance(Address const &_id) const
Definition: State.cpp:312
dev::eth::ExtVM::store
u256 store(u256 _n) final
Read storage location.
Definition: ExtVM.h:58
dev::eth::ExtVMFace::envInfo
EnvInfo const & envInfo() const
Get the execution environment information.
Definition: ExtVMFace.h:256
dev::eth::SealEngineFace::evmSchedule
virtual EVMSchedule const & evmSchedule(u256 const &_blockNumber) const =0
dev::eth::State::storage
u256 storage(Address const &_contract, u256 const &_memory) const
Definition: State.cpp:422
dev::eth::EVMSchedule
Definition: EVMSchedule.h:29
dev::eth::EnvInfo
Definition: ExtVMFace.h:143
State.h