Ethereum  PoC-8
The C++ Implementation of Ethereum
TransactionBase.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 #include <libethcore/Common.h>
21 #include <libdevcrypto/Common.h>
22 #include <libdevcore/RLP.h>
23 #include <libdevcore/SHA3.h>
24 
25 #include <boost/optional.hpp>
26 
27 namespace dev
28 {
29 namespace eth
30 {
31 
32 struct EVMSchedule;
33 
36 {
39 };
40 
41 enum class CheckTransaction
42 {
43  None,
44  Cheap,
46 };
47 
50 {
51 public:
54 
56  TransactionBase(TransactionSkeleton const& _ts, Secret const& _s = Secret());
57 
59  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); }
60 
62  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); }
63 
65  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) {}
66 
68  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) {}
69 
71  explicit TransactionBase(bytesConstRef _rlp, CheckTransaction _checkSig);
72 
74  explicit TransactionBase(bytes const& _rlp, CheckTransaction _checkSig): TransactionBase(&_rlp, _checkSig) {}
75 
77  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; }
79  bool operator!=(TransactionBase const& _c) const { return !operator==(_c); }
80 
83  Address const& sender() const;
85  Address const& safeSender() const noexcept;
87  void forceSender(Address const& _a) { m_sender = _a; }
88 
91  void checkLowS() const;
92 
96  void checkChainId(int chainId = -4) const;
97 
99  explicit operator bool() const { return m_type != NullTransaction; }
100 
102  bool isCreation() const { return m_type == ContractCreation; }
103 
106  void streamRLP(RLPStream& _s, IncludeSignature _sig = WithSignature, bool _forEip155hash = false) const;
107 
109  bytes rlp(IncludeSignature _sig = WithSignature) const { RLPStream s; streamRLP(s, _sig); return s.out(); }
110 
112  h256 sha3(IncludeSignature _sig = WithSignature) const;
113 
115  u256 value() const { return m_value; }
116 
118  u256 gasPrice() const { return m_gasPrice; }
119 
121  u256 gas() const { return m_gas; }
122 
125 
127  Address to() const { return m_receiveAddress; }
128 
130  Address from() const { return safeSender(); }
131 
133  bytes const& data() const { return m_data; }
134 
136  u256 nonce() const { return m_nonce; }
137 
139  void setNonce(u256 const& _n) { clearSignature(); m_nonce = _n; }
140 
142  bool hasSignature() const { return m_vrs.is_initialized(); }
143 
145  bool hasZeroSignature() const { return m_vrs && isZeroSignature(m_vrs->r, m_vrs->s); }
146 
148  bool isReplayProtected() const { return m_chainId != -4; }
149 
152  SignatureStruct const& signature() const;
153 
154  void sign(Secret const& _priv);
155 
157  int64_t baseGasRequired(EVMSchedule const& _es) const { return baseGasRequired(isCreation(), &m_data, _es); }
158 
160  static int64_t baseGasRequired(bool _contractCreation, bytesConstRef _data, EVMSchedule const& _es);
161 
162 protected:
164  enum Type
165  {
169  };
170 
171  static bool isZeroSignature(u256 const& _r, u256 const& _s) { return !_r && !_s; }
172 
175 
183  boost::optional<SignatureStruct> m_vrs;
184  int m_chainId = -4;
185 
186  mutable h256 m_hashWith;
187  mutable Address m_sender;
188 };
189 
191 using TransactionBases = std::vector<TransactionBase>;
192 
194 inline std::ostream& operator<<(std::ostream& _out, TransactionBase const& _t)
195 {
196  _out << _t.sha3().abridged() << "{";
197  if (_t.receiveAddress())
198  _out << _t.receiveAddress().abridged();
199  else
200  _out << "[CREATE]";
201 
202  _out << "/" << _t.data().size() << "$" << _t.value() << "+" << _t.gas() << "@" << _t.gasPrice();
203  _out << "<-" << _t.safeSender().abridged() << " #" << _t.nonce() << "}";
204  return _out;
205 }
206 
207 }
208 }
Definition: Address.cpp:20
u256 m_gas
The total gas to convert, paid for from sender&#39;s account. Any unused gas gets refunded once the contr...
TransactionBase()
Constructs a null transaction.
u256 m_gasPrice
The base fee and thus the implied exchange rate of ETH to GAS.
Do include a signature.
SignatureStruct const & signature() const
TransactionBase(u256 const &_value, u256 const &_gasPrice, u256 const &_gas, bytes const &_data, u256 const &_nonce=0)
Constructs an unsigned contract-creation transaction.
std::vector< TransactionBase > TransactionBases
Nice name for vector of Transaction.
void setNonce(u256 const &_n)
Sets the nonce to the given value. Clears any signature.
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:217
Address receiveAddress() const
bytes rlp(IncludeSignature _sig=WithSignature) const
TransactionBase(u256 const &_value, u256 const &_gasPrice, u256 const &_gas, bytes const &_data, u256 const &_nonce, Secret const &_secret)
Constructs a signed contract-creation transaction.
SecureFixedHash< 32 > Secret
Definition: Common.h:36
Transaction to create contracts - receiveAddress() is ignored.
std::string abridged() const
Definition: FixedHash.h:123
IncludeSignature
Named-boolean type to encode whether a signature be included in the serialisation process...
h256 m_hashWith
Cached hash of transaction with signature.
TransactionBase(u256 const &_value, u256 const &_gasPrice, u256 const &_gas, Address const &_dest, bytes const &_data, u256 const &_nonce, Secret const &_secret)
Constructs a signed message-call transaction.
static bool isZeroSignature(u256 const &_r, u256 const &_s)
Do not include a signature.
bytes m_data
The data associated with the transaction, or the initialiser if it&#39;s a creation transaction.
Type
Type of transaction.
u256 m_value
The amount of ETH to be transferred by this transaction. Called &#39;endowment&#39; for contract-creation tra...
h256 sha3(IncludeSignature _sig=WithSignature) const
Address const & safeSender() const noexcept
Like sender() but will never throw.
int m_chainId
EIP155 value for calculating transaction hash https://github.com/ethereum/EIPs/issues/155.
u256 m_nonce
The transaction-count of the sender.
std::vector< byte > bytes
Definition: Common.h:72
boost::optional< SignatureStruct > m_vrs
The signature of the transaction. Encodes the sender.
Address m_receiveAddress
The receiving address of the transaction.
void streamRLP(RLPStream &_s, IncludeSignature _sig=WithSignature, bool _forEip155hash=false) const
Address to() const
Synonym for receiveAddress().
bytes const & out() const
Read the byte stream.
Definition: RLP.h:419
bool operator==(TransactionBase const &_c) const
Checks equality of transactions.
Address const & sender() const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
void forceSender(Address const &_a)
Force the sender to a particular value. This will result in an invalid transaction RLP...
void checkChainId(int chainId=-4) const
bool operator!=(TransactionBase const &_c) const
Checks inequality of transactions.
TransactionBase(u256 const &_value, u256 const &_gasPrice, u256 const &_gas, Address const &_dest, bytes const &_data, u256 const &_nonce=0)
Constructs an unsigned message-call transaction.
void clearSignature()
Clears the signature.
Class for writing to an RLP bytestream.
Definition: RLP.h:369
TransactionBase(bytes const &_rlp, CheckTransaction _checkSig)
Constructs a transaction from the given RLP.
bytes const & data() const
int64_t baseGasRequired(EVMSchedule const &_es) const
Address from() const
Synonym for safeSender().
Transaction to invoke a message call - receiveAddress() is used.
void sign(Secret const &_priv)
Sign the transaction.
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Address m_sender
Cached sender, determined from signature.
Type m_type
Is this a contract-creation transaction or a message-call transaction?