Fabcoin Core  0.16.2
P2P Digital Currency
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 */
19 #pragma once
20 
21 #include <functional>
22 #ifndef FASC_BUILD
23 #include <json/json.h>
24 #endif
25 #include <libdevcore/Log.h>
26 #include <libevmcore/Instruction.h>
27 #include <libethcore/Common.h>
28 #include <libevm/VMFace.h>
29 #include "Transaction.h"
30 
31 namespace Json
32 {
33  class Value;
34 }
35 
36 namespace dev
37 {
38 
39 class OverlayDB;
40 
41 namespace eth
42 {
43 
44 class State;
45 class Block;
46 class BlockChain;
47 class ExtVM;
48 class SealEngineFace;
49 struct Manifest;
50 
51 struct VMTraceChannel: public LogChannel { static const char* name(); static const int verbosity = 11; };
52 struct ExecutiveWarnChannel: public LogChannel { static const char* name(); static const int verbosity = 1; };
53 
55 {
56 public:
57  struct DebugOptions
58  {
59  bool disableStorage = false;
60  bool disableMemory = false;
61  bool disableStack = false;
62  bool fullStorage = false;
63  };
64 
65  StandardTrace();
66  void operator()(uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize, bigint _gasCost, bigint _gas, VM* _vm, ExtVMFace const* _extVM);
67 
68  void setShowMnemonics() { m_showMnemonics = true; }
69  void setOptions(DebugOptions _options) { m_options = _options; }
70 
71  std::string json(bool _styled = false) const;
72 
73  OnOpFunc onOp() { return [=](uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize, bigint _gasCost, bigint _gas, VM* _vm, ExtVMFace const* _extVM) { (*this)(_steps, _PC, _inst, _newMemSize, _gasCost, _gas, _vm, _extVM); }; }
74 
75 private:
76  bool m_showMnemonics = false;
77  std::vector<Instruction> m_lastInst;
79 #ifndef FASC_BUILD
81 #endif
83 };
84 
85 
107 {
108 public:
110  Executive(State& _s, EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, unsigned _level = 0): m_s(_s), m_envInfo(_envInfo), m_depth(_level), m_sealEngine(_sealEngine) {}
111 
116  Executive(Block& _s, BlockChain const& _bc, unsigned _level = 0);
117 
122  Executive(Block& _s, LastHashes const& _lh = LastHashes(), unsigned _level = 0);
123 
129  Executive(State& _s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level = 0);
130 
131  Executive(Executive const&) = delete;
132  void operator=(Executive) = delete;
133 
135  void initialize(bytesConstRef _transaction) { initialize(Transaction(_transaction, CheckTransaction::None)); }
136  void initialize(Transaction const& _transaction);
139  void finalize();
142  bool execute();
145  Transaction const& t() const { return m_t; }
148  LogEntries const& logs() const { return m_logs; }
151  u256 gasUsed() const;
152 
153  owning_bytes_ref takeOutput() { return std::move(m_output); }
154 
157  bool create(Address _txSender, u256 _endowment, u256 _gasPrice, u256 _gas, bytesConstRef _code, Address _originAddress);
160  bool call(Address _receiveAddress, Address _txSender, u256 _txValue, u256 _gasPrice, bytesConstRef _txData, u256 _gas);
161  bool call(CallParameters const& _cp, u256 const& _gasPrice, Address const& _origin);
163  void accrueSubState(SubState& _parentContext);
164 
167  bool go(OnOpFunc const& _onOp = OnOpFunc());
168 
170  static OnOpFunc simpleTrace();
171 
173  static OnOpFunc standardTrace(std::ostream& o_output);
174 
176  u256 gas() const { return m_gas; }
177 
179  Address newAddress() const { return m_newAddress; }
181  bool excepted() const { return m_excepted != TransactionException::None; }
182 
184  void setResultRecipient(ExecutionResult& _res) { m_res = &_res; }
185 
187  void revert();
188 
189 private:
191  // TODO: consider changign to EnvInfo const& to avoid LastHashes copy at every CALL/CREATE
193  std::shared_ptr<ExtVM> m_ext;
195  ExecutionResult* m_res = nullptr;
196 
197  unsigned m_depth = 0;
198  TransactionException m_excepted = TransactionException::None;
200  u256 m_gas = 0;
201  u256 m_refunded = 0;
202 
205 
208 
209  bool m_isCreation = false;
211  size_t m_savepoint = 0;
212 };
213 
214 }
215 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::function< void(uint64_t, uint64_t, dev::eth::Instruction, dev::bigint, dev::bigint, dev::bigint, dev::eth::VM *, dev::eth::ExtVMFace const *)> OnOpFunc
Definition: fascstate.h:15
Implements the blockchain database.
Definition: BlockChain.h:105
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
owning_bytes_ref m_output
Execution output.
Definition: Executive.h:194
std::shared_ptr< ExtVM > m_ext
The VM externality object for the VM execution or null if no VM is required. shared_ptr used only to ...
Definition: Executive.h:193
Description of the result of executing a transaction.
Definition: Transaction.h:69
std::vector< Instruction > m_lastInst
Definition: Executive.h:77
Model of an Ethereum state, essentially a facade for the trie.
Definition: State.h:161
Address newAddress() const
Definition: Executive.h:179
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:110
int64_t m_baseGasRequired
The base amount of gas requried for executing this transaction.
Definition: Executive.h:199
Transaction const & t() const
Definition: Executive.h:145
Active model of a block within the block chain.
Definition: Block.h:73
LogEntries m_logs
The log entries created by this transaction. Set by finalize().
Definition: Executive.h:204
std::vector< h256 > LastHashes
Definition: ExtVMFace.h:191
u256 gas() const
Definition: Executive.h:176
Config::Value_type Value
DebugOptions m_options
Definition: Executive.h:82
const char * name
Definition: rest.cpp:36
EnvInfo m_envInfo
Information on the runtime environment.
Definition: Executive.h:192
std::vector< byte > bytes
Definition: Common.h:75
Definition: Executive.h:31
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
Message-call/contract-creation executor; useful for executing transactions.
Definition: Executive.h:106
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
LogEntries const & logs() const
Definition: Executive.h:148
Address m_newAddress
Definition: Executive.h:210
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:84
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:39
void initialize(bytesConstRef _transaction)
Initializes the executive for evaluating a transaction. You must call finalize() at some point follow...
Definition: Executive.h:135
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:265
Reference to a slice of buffer that also owns the buffer.
Definition: ExtVMFace.h:56
State & m_s
The state to which this operation/transaction is applied.
Definition: Executive.h:190
bool excepted() const
Definition: Executive.h:181
owning_bytes_ref takeOutput()
Definition: Executive.h:153
The default logging channels.
Definition: Log.h:130
Transaction m_t
The original transaction. Set by setup().
Definition: Executive.h:203
SealEngineFace const & m_sealEngine
Definition: Executive.h:207
Json::Value m_trace
Definition: Executive.h:80
TransactionException
Definition: Transaction.h:35
std::vector< LogEntry > LogEntries
Definition: ExtVMFace.h:110
void setOptions(DebugOptions _options)
Definition: Executive.h:69
void setResultRecipient(ExecutionResult &_res)
Collect execution results in the result storage provided.
Definition: Executive.h:184