Executive.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /** @file Executive.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <functional>
  20. /* jsoncpp/json/json.h???????? */
  21. #include <json/json.h>
  22. #include <libdevcore/Log.h>
  23. #include <libevmcore/Instruction.h>
  24. #include <libethcore/Common.h>
  25. #include <libevm/VMFace.h>
  26. #include "Transaction.h"
  27. namespace Json
  28. {
  29. class Value;
  30. }
  31. namespace dev
  32. {
  33. class OverlayDB;
  34. namespace eth
  35. {
  36. class State;
  37. class Block;
  38. class BlockChain;
  39. class ExtVM;
  40. class SealEngineFace;
  41. struct Manifest;
  42. struct VMTraceChannel: public LogChannel { static const char* name(); static const int verbosity = 11; };
  43. struct ExecutiveWarnChannel: public LogChannel { static const char* name(); static const int verbosity = 6; };
  44. class StandardTrace
  45. {
  46. public:
  47. struct DebugOptions
  48. {
  49. bool disableStorage = false;
  50. bool disableMemory = false;
  51. bool disableStack = false;
  52. bool fullStorage = false;
  53. };
  54. StandardTrace();
  55. void operator()(uint64_t _steps, uint64_t _PC, Instruction _inst, bigint _newMemSize, bigint _gasCost, bigint _gas, VM* _vm, ExtVMFace const* _extVM);
  56. void setShowMnemonics() { m_showMnemonics = true; }
  57. void setOptions(DebugOptions _options) { m_options = _options; }
  58. std::string json(bool _styled = false) const;
  59. 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); }; }
  60. private:
  61. bool m_showMnemonics = false;
  62. std::vector<Instruction> m_lastInst;
  63. bytes m_lastCallData;
  64. Json::Value m_trace;
  65. DebugOptions m_options;
  66. };
  67. /**
  68. * @brief Message-call/contract-creation executor; useful for executing transactions.
  69. *
  70. * Two ways of using this class - either as a transaction executive or a CALL/CREATE executive.
  71. *
  72. * In the first use, after construction, begin with initialize(), then execute() and end with finalize(). Call go()
  73. * after execute() only if it returns false.
  74. *
  75. * In the second use, after construction, begin with call() or create() and end with
  76. * accrueSubState(). Call go() after call()/create() only if it returns false.
  77. *
  78. * Example:
  79. * @code
  80. * Executive e(state, blockchain, 0);
  81. * e.initialize(transaction);
  82. * if (!e.execute())
  83. * e.go();
  84. * e.finalize();
  85. * @endcode
  86. */
  87. class Executive
  88. {
  89. public:
  90. /// Simple constructor; executive will operate on given state, with the given environment info.
  91. Executive(State& _s, EnvInfo const& _envInfo, SealEngineFace const& _sealEngine, unsigned _level = 0): m_s(_s), m_envInfo(_envInfo), m_depth(_level), m_sealEngine(_sealEngine) {}
  92. /** Easiest constructor.
  93. * Creates executive to operate on the state of end of the given block, populating environment
  94. * info from given Block and the LastHashes portion from the BlockChain.
  95. */
  96. Executive(Block& _s, BlockChain const& _bc, unsigned _level = 0);
  97. /** LastHashes-split constructor.
  98. * Creates executive to operate on the state of end of the given block, populating environment
  99. * info accordingly, with last hashes given explicitly.
  100. */
  101. Executive(Block& _s, LastHashes const& _lh = LastHashes(), unsigned _level = 0);
  102. /** Previous-state constructor.
  103. * Creates executive to operate on the state of a particular transaction in the given block,
  104. * populating environment info from the given Block and the LastHashes portion from the BlockChain.
  105. * State is assigned the resultant value, but otherwise unused.
  106. */
  107. Executive(State& _s, Block const& _block, unsigned _txIndex, BlockChain const& _bc, unsigned _level = 0);
  108. Executive(Executive const&) = delete;
  109. void operator=(Executive) = delete;
  110. /// Initializes the executive for evaluating a transaction. You must call finalize() at some point following this.
  111. void initialize(bytesConstRef _transaction) { initialize(Transaction(_transaction, CheckTransaction::None)); }
  112. void initialize(Transaction const& _transaction);
  113. /// Finalise a transaction previously set up with initialize().
  114. /// @warning Only valid after initialize() and execute(), and possibly go().
  115. void finalize();
  116. /// Begins execution of a transaction. You must call finalize() following this.
  117. /// @returns true if the transaction is done, false if go() must be called.
  118. bool execute();
  119. /// @returns the transaction from initialize().
  120. /// @warning Only valid after initialize().
  121. Transaction const& t() const { return m_t; }
  122. /// @returns the log entries created by this operation.
  123. /// @warning Only valid after finalise().
  124. LogEntries const& logs() const { return m_logs; }
  125. /// @returns total gas used in the transaction/operation.
  126. /// @warning Only valid after finalise().
  127. u256 gasUsed() const;
  128. /// @returns total gas used in the transaction/operation, excluding anything refunded.
  129. /// @warning Only valid after finalise().
  130. u256 gasUsedNoRefunds() const;
  131. /// Set up the executive for evaluating a bare CREATE (contract-creation) operation.
  132. /// @returns false iff go() must be called (and thus a VM execution in required).
  133. bool create(Address _txSender, u256 _endowment, u256 _gasPrice, u256 _gas, bytesConstRef _code, Address _originAddress);
  134. /// Set up the executive for evaluating a bare CALL (message call) operation.
  135. /// @returns false iff go() must be called (and thus a VM execution in required).
  136. bool call(Address _receiveAddress, Address _txSender, u256 _txValue, u256 _gasPrice, bytesConstRef _txData, u256 _gas);
  137. bool call(CallParameters const& _cp, u256 const& _gasPrice, Address const& _origin);
  138. /// Finalise an operation through accruing the substate into the parent context.
  139. void accrueSubState(SubState& _parentContext);
  140. /// Executes (or continues execution of) the VM.
  141. /// @returns false iff go() must be called again to finish the transaction.
  142. bool go(OnOpFunc const& _onOp = OnOpFunc());
  143. /// Operation function for providing a simple trace of the VM execution.
  144. static OnOpFunc simpleTrace();
  145. /// Operation function for providing a simple trace of the VM execution.
  146. static OnOpFunc standardTrace(std::ostream& o_output);
  147. /// @returns gas remaining after the transaction/operation. Valid after the transaction has been executed.
  148. u256 gas() const { return m_gas; }
  149. /// @returns the new address for the created contract in the CREATE operation.
  150. h160 newAddress() const { return m_newAddress; }
  151. /// @returns true iff the operation ended with a VM exception.
  152. bool excepted() const { return m_excepted != TransactionException::None; }
  153. /// Collect execution results in the result storage provided.
  154. void setResultRecipient(ExecutionResult& _res) { m_res = &_res; }
  155. private:
  156. State& m_s; ///< The state to which this operation/transaction is applied.
  157. // TODO: consider changign to EnvInfo const& to avoid LastHashes copy at every CALL/CREATE
  158. EnvInfo m_envInfo; ///< Information on the runtime environment.
  159. 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.
  160. bytesRef m_outRef; ///< Reference to "expected output" buffer.
  161. ExecutionResult* m_res = nullptr; ///< Optional storage for execution results.
  162. Address m_newAddress; ///< The address of the created contract in the case of create() being called.
  163. unsigned m_depth = 0; ///< The context's call-depth.
  164. bool m_isCreation = false; ///< True if the transaction creates a contract, or if create() is called.
  165. TransactionException m_excepted = TransactionException::None; ///< Details if the VM's execution resulted in an exception.
  166. bigint m_baseGasRequired; ///< The base amount of gas requried for executing this transactions.
  167. u256 m_gas = 0; ///< The gas for EVM code execution. Initial amount before go() execution, final amount after go() execution.
  168. u256 m_refunded = 0; ///< The amount of gas refunded.
  169. Transaction m_t; ///< The original transaction. Set by setup().
  170. LogEntries m_logs; ///< The log entries created by this transaction. Set by finalize().
  171. bigint m_gasCost;
  172. SealEngineFace const& m_sealEngine;
  173. };
  174. }
  175. }