Ethereum  PoC-8
The C++ Implementation of Ethereum
Executive.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3  cpp-ethereum is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7  cpp-ethereum is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11  You should have received a copy of the GNU General Public License
12  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
13 */
14 
15 #pragma once
16 
17 #include "Transaction.h"
18 
19 #include <libdevcore/Log.h>
20 #include <libethcore/Common.h>
21 #include <libevm/VMFace.h>
22 
23 #include <json/json.h>
24 #include <functional>
25 
26 namespace Json
27 {
28  class Value;
29 }
30 
31 namespace dev
32 {
33 
34 class OverlayDB;
35 
36 namespace eth
37 {
38 
39 class State;
40 class Block;
41 class BlockChain;
42 class ExtVM;
43 class SealEngineFace;
44 struct Manifest;
45 
47 {
48 public:
49  struct DebugOptions
50  {
51  bool disableStorage = false;
52  bool disableMemory = false;
53  bool disableStack = false;
54  bool fullStorage = false;
55  };
56 
57  StandardTrace();
58  void operator()(uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
59  bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _extVM);
60 
61  void setShowMnemonics() { m_showMnemonics = true; }
62  void setOptions(DebugOptions _options) { m_options = _options; }
63 
64  Json::Value jsonValue() const { return m_trace; }
65  std::string styledJson() const;
66  std::string multilineTrace() const;
67 
69  {
70  return [=](uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
71  bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _extVM) {
72  (*this)(_steps, _PC, _inst, _newMemSize, _gasCost, _gas, _vm, _extVM);
73  };
74  }
75 
76 private:
77  bool m_showMnemonics = false;
78  std::vector<Instruction> m_lastInst;
79  Json::Value m_trace;
80  DebugOptions m_options;
81 };
82 
104 {
105 public:
107  Executive(State& _s, EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, unsigned _level = 0): m_s(_s), m_envInfo(_envInfo), m_depth(_level), m_sealEngine(_sealEngine) {}
108 
113  Executive(Block& _s, BlockChain const& _bc, unsigned _level = 0);
114 
119  Executive(Block& _s, LastBlockHashesFace const& _lh, unsigned _level = 0);
120 
126  Executive(State& io_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level = 0);
127 
128  Executive(Executive const&) = delete;
129  void operator=(Executive) = delete;
130 
132  void initialize(bytesConstRef _transaction) { initialize(Transaction(_transaction, CheckTransaction::None)); }
133  void initialize(Transaction const& _transaction);
137  bool finalize();
140  bool execute();
143  Transaction const& t() const { return m_t; }
146  LogEntries const& logs() const { return m_logs; }
149  u256 gasUsed() const;
150 
151  owning_bytes_ref takeOutput() { return std::move(m_output); }
152 
155  bool create(Address const& _txSender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress);
157  bool createOpcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress);
159  bool create2Opcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress, u256 const& _salt);
162  bool call(Address const& _receiveAddress, Address const& _txSender, u256 const& _txValue, u256 const& _gasPrice, bytesConstRef _txData, u256 const& _gas);
163  bool call(CallParameters const& _cp, u256 const& _gasPrice, Address const& _origin);
165  void accrueSubState(SubState& _parentContext);
166 
169  bool go(OnOpFunc const& _onOp = OnOpFunc());
170 
173 
175  u256 gas() const { return m_gas; }
176 
178  Address newAddress() const { return m_newAddress; }
179 
181  TransactionException getException() const noexcept { return m_excepted; }
182 
184  void setResultRecipient(ExecutionResult& _res) { m_res = &_res; }
185 
187  void revert();
188 
189 private:
191  bool executeCreate(Address const& _txSender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress);
192 
193  State& m_s;
194  // TODO: consider changign to EnvInfo const& to avoid LastHashes copy at every CALL/CREATE
195  EnvInfo m_envInfo;
196  std::shared_ptr<ExtVM> m_ext;
197  owning_bytes_ref m_output;
198  ExecutionResult* m_res = nullptr;
199 
200  unsigned m_depth = 0;
202  int64_t m_baseGasRequired;
203  u256 m_gas = 0;
204 
205  Transaction m_t;
206  LogEntries m_logs;
207 
208  u256 m_gasCost;
209  SealEngineFace const& m_sealEngine;
210 
211  bool m_isCreation = false;
212  Address m_newAddress;
213  size_t m_savepoint = 0;
214 
215  Logger m_execLogger{createLogger(VerbosityDebug, "exec")};
216  Logger m_detailsLogger{createLogger(VerbosityTrace, "exec")};
217  Logger m_vmTraceLogger{createLogger(VerbosityTrace, "vmtrace")};
218 };
219 
220 }
221 }
dev::eth::SubState
Definition: ExtVMFace.h:90
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::Executive::go
bool go(OnOpFunc const &_onOp=OnOpFunc())
Definition: Executive.cpp:441
dev::eth::StandardTrace::setOptions
void setOptions(DebugOptions _options)
Definition: Executive.h:62
dev::eth::StandardTrace::DebugOptions
Definition: Executive.h:50
dev::eth::Transaction
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:86
dev::vector_ref< byte const >
dev::eth::SealEngineFace
Definition: SealEngine.h:46
VMFace.h
dev::eth::CheckTransaction::None
@ None
dev::eth::StandardTrace::DebugOptions::disableStack
bool disableStack
Definition: Executive.h:53
dev::eth::StandardTrace::jsonValue
Json::Value jsonValue() const
Definition: Executive.h:64
dev::eth::Executive::getException
TransactionException getException() const noexcept
Definition: Executive.h:181
dev::eth::Executive::create
bool create(Address const &_txSender, u256 const &_endowment, u256 const &_gasPrice, u256 const &_gas, bytesConstRef _code, Address const &_originAddress)
Definition: Executive.cpp:357
dev::eth::StandardTrace::setShowMnemonics
void setShowMnemonics()
Definition: Executive.h:61
dev::eth::owning_bytes_ref
Definition: ExtVMFace.h:58
dev::eth::Executive::gasUsed
u256 gasUsed() const
Definition: Executive.cpp:206
dev::eth::VMFace
EVM Virtual Machine interface.
Definition: VMFace.h:64
dev::eth::ExtVMFace
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:204
dev::eth::StandardTrace::DebugOptions::disableMemory
bool disableMemory
Definition: Executive.h:52
dev::FixedHash< 20 >
dev::eth::Executive::call
bool call(Address const &_receiveAddress, Address const &_txSender, u256 const &_txValue, u256 const &_gasPrice, bytesConstRef _txData, u256 const &_gas)
Definition: Executive.cpp:285
dev::eth::Executive::initialize
void initialize(bytesConstRef _transaction)
Initializes the executive for evaluating a transaction. You must call finalize() at some point follow...
Definition: Executive.h:132
dev::eth::Executive::simpleTrace
OnOpFunc simpleTrace()
Operation function for providing a simple trace of the VM execution.
Definition: Executive.cpp:420
dev::eth::Executive::setResultRecipient
void setResultRecipient(ExecutionResult &_res)
Collect execution results in the result storage provided.
Definition: Executive.h:184
dev::eth::Executive::revert
void revert()
Revert all changes made to the state by this execution.
Definition: Executive.cpp:575
Common.h
dev::eth::Executive::operator=
void operator=(Executive)=delete
dev::eth::StandardTrace::DebugOptions::fullStorage
bool fullStorage
Definition: Executive.h:54
dev::eth::Executive::logs
LogEntries const & logs() const
Definition: Executive.h:146
dev::eth::StandardTrace::multilineTrace
std::string multilineTrace() const
Definition: Executive.cpp:171
dev::eth::Executive::gas
u256 gas() const
Definition: Executive.h:175
dev::eth::Executive::Executive
Executive(Executive const &)=delete
dev::Logger
boost::log::sources::severity_channel_logger<> Logger
Definition: Log.h:124
dev::eth::TransactionException::None
@ None
dev::eth::StandardTrace::styledJson
std::string styledJson() const
Definition: Executive.cpp:166
dev::eth::LogEntries
std::vector< LogEntry > LogEntries
Definition: LogEntry.h:51
dev::createLogger
Logger createLogger(int _severity, std::string const &_channel)
Definition: Log.h:125
Transaction.h
dev::eth::Executive
Message-call/contract-creation executor; useful for executing transactions.
Definition: Executive.h:104
dev::eth::BlockChain
Implements the blockchain database. All data this gives is disk-backed. @threadsafe.
Definition: BlockChain.h:105
dev::eth::StandardTrace
Definition: Executive.h:47
dev::eth::TransactionException
TransactionException
Definition: Transaction.h:36
dev::eth::Executive::newAddress
Address newAddress() const
Definition: Executive.h:178
dev::VerbosityDebug
@ VerbosityDebug
Definition: Log.h:71
dev::eth::Instruction
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:29
dev::bigint
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<> > bigint
Definition: Common.h:118
Json
Definition: Executive.h:27
dev::eth::Executive::finalize
bool finalize()
Definition: Executive.cpp:535
dev::eth::CallParameters
Definition: ExtVMFace.h:118
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::Executive::create2Opcode
bool create2Opcode(Address const &_sender, u256 const &_endowment, u256 const &_gasPrice, u256 const &_gas, bytesConstRef _code, Address const &_originAddress, u256 const &_salt)
Definition: Executive.cpp:370
dev
Definition: Address.cpp:21
dev::eth::ExecutionResult
Description of the result of executing a transaction.
Definition: Transaction.h:71
dev::eth::Executive::t
Transaction const & t() const
Definition: Executive.h:143
dev::eth::StandardTrace::onOp
OnOpFunc onOp()
Definition: Executive.h:68
dev::eth::Executive::accrueSubState
void accrueSubState(SubState &_parentContext)
Finalise an operation through accruing the substate into the parent context.
Definition: Executive.cpp:211
dev::eth::Executive::createOpcode
bool createOpcode(Address const &_sender, u256 const &_endowment, u256 const &_gasPrice, u256 const &_gas, bytesConstRef _code, Address const &_originAddress)
Definition: Executive.cpp:363
dev::eth::EnvInfo
Definition: ExtVMFace.h:143
dev::eth::StandardTrace::StandardTrace
StandardTrace()
Definition: Executive.cpp:63
dev::eth::LastBlockHashesFace
Interface for getting a list of recent block hashes @threadsafe.
Definition: LastBlockHashesFace.h:37
dev::VerbosityTrace
@ VerbosityTrace
Definition: Log.h:72
Log.h
dev::eth::Block
Active model of a block within the block chain. Keeps track of all transactions, receipts and state f...
Definition: Block.h:69
dev::eth::Executive::execute
bool execute()
Definition: Executive.cpp:269
dev::eth::StandardTrace::DebugOptions::disableStorage
bool disableStorage
Definition: Executive.h:51
dev::eth::Executive::takeOutput
owning_bytes_ref takeOutput()
Definition: Executive.h:151
dev::eth::Executive::Executive
Executive(State &_s, EnvInfo const &_envInfo, SealEngineFace const &_sealEngine, unsigned _level=0)
Simple constructor; executive will operate on given state, with the given environment info.
Definition: Executive.h:107
dev::eth::StandardTrace::operator()
void operator()(uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize, bigint _gasCost, bigint _gas, VMFace const *_vm, ExtVMFace const *_extVM)
Definition: Executive.cpp:88