This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathExecutive.h
More file actions
221 lines (183 loc) · 9.44 KB
/
Executive.h
File metadata and controls
221 lines (183 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Transaction.h"
#include <libdevcore/Log.h>
#include <libethcore/Common.h>
#include <libevm/VMFace.h>
#include <json/json.h>
#include <functional>
namespace Json
{
class Value;
}
namespace dev
{
class OverlayDB;
namespace eth
{
class State;
class Block;
class BlockChain;
class ExtVM;
class SealEngineFace;
struct Manifest;
class StandardTrace
{
public:
struct DebugOptions
{
bool disableStorage = false;
bool disableMemory = false;
bool disableStack = false;
bool fullStorage = false;
};
StandardTrace();
void operator()(uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _extVM);
void setShowMnemonics() { m_showMnemonics = true; }
void setOptions(DebugOptions _options) { m_options = _options; }
Json::Value jsonValue() const { return m_trace; }
std::string styledJson() const;
std::string multilineTrace() const;
OnOpFunc onOp()
{
return [=](uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize,
bigint _gasCost, bigint _gas, VMFace const* _vm, ExtVMFace const* _extVM) {
(*this)(_steps, _PC, _inst, _newMemSize, _gasCost, _gas, _vm, _extVM);
};
}
private:
bool m_showMnemonics = false;
std::vector<Instruction> m_lastInst;
Json::Value m_trace;
DebugOptions m_options;
};
/**
* @brief Message-call/contract-creation executor; useful for executing transactions.
*
* Two ways of using this class - either as a transaction executive or a CALL/CREATE executive.
*
* In the first use, after construction, begin with initialize(), then execute() and end with finalize(). Call go()
* after execute() only if it returns false.
*
* In the second use, after construction, begin with call() or create() and end with
* accrueSubState(). Call go() after call()/create() only if it returns false.
*
* Example:
* @code
* Executive e(state, blockchain, 0);
* e.initialize(transaction);
* if (!e.execute())
* e.go();
* e.finalize();
* @endcode
*/
class Executive
{
public:
/// Simple constructor; executive will operate on given state, with the given environment info.
Executive(State& _s, EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, unsigned _level = 0): m_s(_s), m_envInfo(_envInfo), m_depth(_level), m_sealEngine(_sealEngine) {}
/** Easiest constructor.
* Creates executive to operate on the state of end of the given block, populating environment
* info from given Block and the LastHashes portion from the BlockChain.
*/
Executive(Block& _s, BlockChain const& _bc, unsigned _level = 0);
/** LastHashes-split constructor.
* Creates executive to operate on the state of end of the given block, populating environment
* info accordingly, with last hashes given explicitly.
*/
Executive(Block& _s, LastBlockHashesFace const& _lh, unsigned _level = 0);
/** Previous-state constructor.
* Creates executive to operate on the state of a particular transaction in the given block,
* populating environment info from the given Block and the LastHashes portion from the BlockChain.
* State is assigned the resultant value, but otherwise unused.
*/
Executive(State& io_s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level = 0);
Executive(Executive const&) = delete;
void operator=(Executive) = delete;
/// Initializes the executive for evaluating a transaction. You must call finalize() at some point following this.
void initialize(bytesConstRef _transaction) { initialize(Transaction(_transaction, CheckTransaction::None)); }
void initialize(Transaction const& _transaction);
/// Finalise a transaction previously set up with initialize().
/// @warning Only valid after initialize() and execute(), and possibly go().
/// @returns true if the outermost execution halted normally, false if exceptionally halted.
bool finalize();
/// Begins execution of a transaction. You must call finalize() following this.
/// @returns true if the transaction is done, false if go() must be called.
bool execute();
/// @returns the transaction from initialize().
/// @warning Only valid after initialize().
Transaction const& t() const { return m_t; }
/// @returns the log entries created by this operation.
/// @warning Only valid after finalise().
LogEntries const& logs() const { return m_logs; }
/// @returns total gas used in the transaction/operation.
/// @warning Only valid after finalise().
u256 gasUsed() const;
owning_bytes_ref takeOutput() { return std::move(m_output); }
/// Set up the executive for evaluating a bare CREATE (contract-creation) operation.
/// @returns false iff go() must be called (and thus a VM execution in required).
bool create(Address const& _txSender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress);
/// @returns false iff go() must be called (and thus a VM execution in required).
bool createOpcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress);
/// @returns false iff go() must be called (and thus a VM execution in required).
bool create2Opcode(Address const& _sender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress, u256 const& _salt);
/// Set up the executive for evaluating a bare CALL (message call) operation.
/// @returns false iff go() must be called (and thus a VM execution in required).
bool call(Address const& _receiveAddress, Address const& _txSender, u256 const& _txValue, u256 const& _gasPrice, bytesConstRef _txData, u256 const& _gas);
bool call(CallParameters const& _cp, u256 const& _gasPrice, Address const& _origin);
/// Finalise an operation through accruing the substate into the parent context.
void accrueSubState(SubState& _parentContext);
/// Executes (or continues execution of) the VM.
/// @returns false iff go() must be called again to finish the transaction.
bool go(OnOpFunc const& _onOp = OnOpFunc());
/// Operation function for providing a simple trace of the VM execution.
OnOpFunc simpleTrace();
/// @returns gas remaining after the transaction/operation. Valid after the transaction has been executed.
u256 gas() const { return m_gas; }
/// @returns the new address for the created contract in the CREATE operation.
Address newAddress() const { return m_newAddress; }
/// @returns The exception that has happened during the execution if any.
TransactionException getException() const noexcept { return m_excepted; }
/// Collect execution results in the result storage provided.
void setResultRecipient(ExecutionResult& _res) { m_res = &_res; }
/// Revert all changes made to the state by this execution.
void revert();
private:
/// @returns false iff go() must be called (and thus a VM execution in required).
bool executeCreate(Address const& _txSender, u256 const& _endowment, u256 const& _gasPrice, u256 const& _gas, bytesConstRef _code, Address const& _originAddress);
State& m_s; ///< The state to which this operation/transaction is applied.
// TODO: consider changign to EnvInfo const& to avoid LastHashes copy at every CALL/CREATE
EnvInfo m_envInfo; ///< Information on the runtime environment.
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 allow ExtVM forward reference. This field does *NOT* survive this object.
owning_bytes_ref m_output; ///< Execution output.
ExecutionResult* m_res = nullptr; ///< Optional storage for execution results.
unsigned m_depth = 0; ///< The context's call-depth.
TransactionException m_excepted = TransactionException::None; ///< Details if the VM's execution resulted in an exception.
int64_t m_baseGasRequired; ///< The base amount of gas requried for executing this transaction.
u256 m_gas = 0; ///< The gas for EVM code execution. Initial amount before go() execution, final amount after go() execution.
Transaction m_t; ///< The original transaction. Set by setup().
LogEntries m_logs; ///< The log entries created by this transaction. Set by finalize().
u256 m_gasCost;
SealEngineFace const& m_sealEngine;
bool m_isCreation = false;
Address m_newAddress;
size_t m_savepoint = 0;
Logger m_execLogger{createLogger(VerbosityDebug, "exec")};
Logger m_detailsLogger{createLogger(VerbosityTrace, "exec")};
Logger m_vmTraceLogger{createLogger(VerbosityTrace, "vmtrace")};
};
}
}