Transaction.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 TransactionBase.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. namespace dev
  23. {
  24. namespace eth
  25. {
  26. struct EVMSchedule;
  27. /// Named-boolean type to encode whether a signature be included in the serialisation process.
  28. enum IncludeSignature
  29. {
  30. WithoutSignature = 0, ///< Do not include a signature.
  31. WithSignature = 1, ///< Do include a signature.
  32. };
  33. enum class CheckTransaction
  34. {
  35. None,
  36. Cheap,
  37. Everything
  38. };
  39. /// Encodes a transaction, ready to be exported to or freshly imported from RLP.
  40. class TransactionBase
  41. {
  42. public:
  43. /// Constructs a null transaction.
  44. TransactionBase() {}
  45. /// Constructs a transaction from a transaction skeleton & optional secret.
  46. TransactionBase(TransactionSkeleton const& _ts, Secret const& _s = Secret());
  47. /// Constructs a signed message-call transaction.
  48. TransactionBase(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, Address const& _dest, bytes const& _data, u256 const& _nonce, Secret const& _secret): m_type(MessageCall), m_nonce(_nonce), m_value(_value), m_receiveAddress(_dest), m_gasPrice(_gasPrice), m_gas(_gas), m_data(_data) { sign(_secret); }
  49. /// Constructs a signed contract-creation transaction.
  50. TransactionBase(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce, Secret const& _secret): m_type(ContractCreation), m_nonce(_nonce), m_value(_value), m_gasPrice(_gasPrice), m_gas(_gas), m_data(_data) { sign(_secret); }
  51. /// Constructs an unsigned message-call transaction.
  52. TransactionBase(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, Address const& _dest, bytes const& _data, u256 const& _nonce = 0): m_type(MessageCall), m_nonce(_nonce), m_value(_value), m_receiveAddress(_dest), m_gasPrice(_gasPrice), m_gas(_gas), m_data(_data) {}
  53. /// Constructs an unsigned contract-creation transaction.
  54. TransactionBase(u256 const& _value, u256 const& _gasPrice, u256 const& _gas, bytes const& _data, u256 const& _nonce = 0): m_type(ContractCreation), m_nonce(_nonce), m_value(_value), m_gasPrice(_gasPrice), m_gas(_gas), m_data(_data) {}
  55. /// Constructs a transaction from the given RLP.
  56. explicit TransactionBase(bytesConstRef _rlp, CheckTransaction _checkSig);
  57. /// Constructs a transaction from the given RLP.
  58. explicit TransactionBase(bytes const& _rlp, CheckTransaction _checkSig): TransactionBase(&_rlp, _checkSig) {}
  59. /// Checks equality of transactions.
  60. bool operator==(TransactionBase const& _c) const { return m_type == _c.m_type && (m_type == ContractCreation || m_receiveAddress == _c.m_receiveAddress) && m_value == _c.m_value && m_data == _c.m_data; }
  61. /// Checks inequality of transactions.
  62. bool operator!=(TransactionBase const& _c) const { return !operator==(_c); }
  63. /// @returns sender of the transaction from the signature (and hash).
  64. Address const& sender() const;
  65. /// Like sender() but will never throw. @returns a null Address if the signature is invalid.
  66. Address const& safeSender() const noexcept;
  67. /// Force the sender to a particular value. This will result in an invalid transaction RLP.
  68. void forceSender(Address const& _a) { m_sender = _a; }
  69. /// @throws InvalidSValue if the signature has an invalid S value.
  70. void checkLowS() const;
  71. /// @throws InvalidSValue if the chain id is neither -4 nor equal to @a chainId
  72. /// Note that "-4" is the chain ID of the pre-155 rules, which should also be considered valid
  73. /// after EIP155
  74. void checkChainId(int chainId = -4) const;
  75. /// @returns true if transaction is non-null.
  76. explicit operator bool() const { return m_type != NullTransaction; }
  77. /// @returns true if transaction is contract-creation.
  78. bool isCreation() const { return m_type == ContractCreation; }
  79. /// @returns true if transaction is message-call.
  80. bool isMessageCall() const { return m_type == MessageCall; }
  81. /// Serialises this transaction to an RLPStream.
  82. void streamRLP(RLPStream& _s, IncludeSignature _sig = WithSignature, bool _forEip155hash = false) const;
  83. /// @returns the RLP serialisation of this transaction.
  84. bytes rlp(IncludeSignature _sig = WithSignature) const { RLPStream s; streamRLP(s, _sig); return s.out(); }
  85. /// @returns the SHA3 hash of the RLP serialisation of this transaction.
  86. h256 sha3(IncludeSignature _sig = WithSignature) const;
  87. /// @returns the amount of ETH to be transferred by this (message-call) transaction, in Wei. Synonym for endowment().
  88. u256 value() const { return m_value; }
  89. /// @returns the amount of ETH to be endowed by this (contract-creation) transaction, in Wei. Synonym for value().
  90. u256 endowment() const { return m_value; }
  91. /// @returns the base fee and thus the implied exchange rate of ETH to GAS.
  92. u256 gasPrice() const { return m_gasPrice; }
  93. /// @returns the total gas to convert, paid for from sender's account. Any unused gas gets refunded once the contract is ended.
  94. u256 gas() const { return m_gas; }
  95. /// @returns the receiving address of the message-call transaction (undefined for contract-creation transactions).
  96. Address receiveAddress() const { return m_receiveAddress; }
  97. /// Synonym for receiveAddress().
  98. Address to() const { return m_receiveAddress; }
  99. /// Synonym for safeSender().
  100. Address from() const { return safeSender(); }
  101. /// @returns the data associated with this (message-call) transaction. Synonym for initCode().
  102. bytes const& data() const { return m_data; }
  103. /// @returns the initialisation code associated with this (contract-creation) transaction. Synonym for data().
  104. bytes const& initCode() const { return m_data; }
  105. /// @returns the transaction-count of the sender.
  106. u256 nonce() const { return m_nonce; }
  107. /// Sets the nonce to the given value. Clears any signature.
  108. void setNonce(u256 const& _n) { clearSignature(); m_nonce = _n; }
  109. /// Clears the signature.
  110. void clearSignature() { m_vrs = SignatureStruct(); }
  111. /// @returns the signature of the transaction. Encodes the sender.
  112. SignatureStruct const& signature() const { return m_vrs; }
  113. void sign(Secret const& _priv); ///< Sign the transaction.
  114. /// @returns true if the transaction contains enough gas for the basic payment.
  115. bigint gasRequired(EVMSchedule const& _es, u256 const& _gas = 0) const { return gasRequired(m_type == TransactionBase::ContractCreation, &m_data, _es, _gas); }
  116. /// Get the fee associated for a transaction with the given data.
  117. static bigint gasRequired(bool _contractCreation, bytesConstRef _data, EVMSchedule const& _es, u256 const& _gas = 0);
  118. protected:
  119. /// Type of transaction.
  120. enum Type
  121. {
  122. NullTransaction, ///< Null transaction.
  123. ContractCreation, ///< Transaction to create contracts - receiveAddress() is ignored.
  124. MessageCall ///< Transaction to invoke a message call - receiveAddress() is used.
  125. };
  126. Type m_type = NullTransaction; ///< Is this a contract-creation transaction or a message-call transaction?
  127. u256 m_nonce; ///< The transaction-count of the sender.
  128. u256 m_value; ///< The amount of ETH to be transferred by this transaction. Called 'endowment' for contract-creation transactions.
  129. Address m_receiveAddress; ///< The receiving address of the transaction.
  130. u256 m_gasPrice; ///< The base fee and thus the implied exchange rate of ETH to GAS.
  131. u256 m_gas; ///< The total gas to convert, paid for from sender's account. Any unused gas gets refunded once the contract is ended.
  132. bytes m_data; ///< The data associated with the transaction, or the initialiser if it's a creation transaction.
  133. SignatureStruct m_vrs; ///< The signature of the transaction. Encodes the sender.
  134. int m_chainId = -4; ///< EIP155 value for calculating transaction hash https://github.com/ethereum/EIPs/issues/155
  135. mutable h256 m_hashWith; ///< Cached hash of transaction with signature.
  136. mutable Address m_sender; ///< Cached sender, determined from signature.
  137. mutable bigint m_gasRequired = 0; ///< Memoised amount required for the transaction to run.
  138. };
  139. /// Nice name for vector of Transaction.
  140. using TransactionBases = std::vector<TransactionBase>;
  141. /// Simple human-readable stream-shift operator.
  142. inline std::ostream& operator<<(std::ostream& _out, TransactionBase const& _t)
  143. {
  144. _out << _t.sha3().abridged() << "{";
  145. if (_t.receiveAddress())
  146. _out << _t.receiveAddress().abridged();
  147. else
  148. _out << "[CREATE]";
  149. _out << "/" << _t.data().size() << "$" << _t.value() << "+" << _t.gas() << "@" << _t.gasPrice();
  150. _out << "<-" << _t.safeSender().abridged() << " #" << _t.nonce() << "}";
  151. return _out;
  152. }
  153. }
  154. }