Transaction.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Transaction.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <libdevcore/RLP.h>
  20. #include <libdevcore/SHA3.h>
  21. #include <libethcore/Common.h>
  22. #include <libethcore/Transaction.h>
  23. #include <libethcore/ChainOperationParams.h>
  24. namespace dev
  25. {
  26. namespace eth
  27. {
  28. enum class TransactionException
  29. {
  30. None = 0,
  31. Unknown,
  32. BadRLP,
  33. InvalidFormat,
  34. OutOfGasIntrinsic, ///< Too little gas to pay for the base transaction cost.
  35. InvalidSignature,
  36. InvalidNonce,
  37. NotEnoughCash,
  38. OutOfGasBase, ///< Too little gas to pay for the base transaction cost.
  39. BlockGasLimitReached,
  40. BadInstruction,
  41. BadJumpDestination,
  42. OutOfGas, ///< Ran out of gas executing code of the transaction.
  43. OutOfStack, ///< Ran out of stack executing code of the transaction.
  44. StackUnderflow
  45. };
  46. enum class CodeDeposit
  47. {
  48. None = 0,
  49. Failed,
  50. Success
  51. };
  52. struct VMException;
  53. TransactionException toTransactionException(Exception const& _e);
  54. std::ostream& operator<<(std::ostream& _out, TransactionException const& _er);
  55. /// Description of the result of executing a transaction.
  56. struct ExecutionResult
  57. {
  58. u256 gasUsed = 0;
  59. TransactionException excepted = TransactionException::Unknown;
  60. Address newAddress;
  61. bytes output;
  62. CodeDeposit codeDeposit = CodeDeposit::None; ///< Failed if an attempted deposit failed due to lack of gas.
  63. u256 gasRefunded = 0;
  64. unsigned depositSize = 0; ///< Amount of code of the creation's attempted deposit.
  65. u256 gasForDeposit; ///< Amount of gas remaining for the code deposit phase.
  66. };
  67. std::ostream& operator<<(std::ostream& _out, ExecutionResult const& _er);
  68. /// Encodes a transaction, ready to be exported to or freshly imported from RLP.
  69. class Transaction: public TransactionBase
  70. {
  71. public:
  72. /// Constructs a null transaction.
  73. Transaction() {}
  74. /// Constructs from a transaction skeleton & optional secret.
  75. Transaction(TransactionSkeleton const& _ts, Secret const& _s = Secret()): TransactionBase(_ts, _s) {}
  76. /// Constructs a signed message-call transaction.
  77. Transaction(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, Address const& _dest, bytes const& _data, u256 const& _nonce, Secret const& _secret):
  78. TransactionBase(_value, _gasPrice, _gas, _dest, _data, _nonce, _secret)
  79. {}
  80. /// Constructs a signed contract-creation transaction.
  81. Transaction(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce, Secret const& _secret):
  82. TransactionBase(_value, _gasPrice, _gas, _data, _nonce, _secret)
  83. {}
  84. /// Constructs an unsigned message-call transaction.
  85. Transaction(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, Address const& _dest, bytes const& _data, u256 const& _nonce = Invalid256):
  86. TransactionBase(_value, _gasPrice, _gas, _dest, _data, _nonce)
  87. {}
  88. /// Constructs an unsigned contract-creation transaction.
  89. Transaction(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce = Invalid256):
  90. TransactionBase(_value, _gasPrice, _gas, _data, _nonce)
  91. {}
  92. /// Constructs a transaction from the given RLP.
  93. explicit Transaction(bytesConstRef _rlp, CheckTransaction _checkSig);
  94. /// Constructs a transaction from the given RLP.
  95. explicit Transaction(bytes const& _rlp, CheckTransaction _checkSig): Transaction(&_rlp, _checkSig) {}
  96. };
  97. /// Nice name for vector of Transaction.
  98. using Transactions = std::vector<Transaction>;
  99. class LocalisedTransaction: public Transaction
  100. {
  101. public:
  102. LocalisedTransaction(
  103. Transaction const& _t,
  104. h256 const& _blockHash,
  105. unsigned _transactionIndex,
  106. BlockNumber _blockNumber = 0
  107. ):
  108. Transaction(_t),
  109. m_blockHash(_blockHash),
  110. m_transactionIndex(_transactionIndex),
  111. m_blockNumber(_blockNumber)
  112. {}
  113. h256 const& blockHash() const { return m_blockHash; }
  114. unsigned transactionIndex() const { return m_transactionIndex; }
  115. BlockNumber blockNumber() const { return m_blockNumber; }
  116. private:
  117. h256 m_blockHash;
  118. unsigned m_transactionIndex;
  119. BlockNumber m_blockNumber;
  120. };
  121. }
  122. }