Fabcoin Core  0.16.2
P2P Digital Currency
Transaction.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 */
22 #pragma once
23 
24 #include <libdevcore/RLP.h>
25 #include <libdevcore/SHA3.h>
26 #include <libethcore/Common.h>
27 
28 namespace dev
29 {
30 namespace eth
31 {
32 
33 struct EVMSchedule;
34 
37 {
40 };
41 
42 enum class CheckTransaction
43 {
44  None,
45  Cheap,
47 };
48 
51 {
52 public:
55 
57  TransactionBase(TransactionSkeleton const& _ts, Secret const& _s = Secret());
58 
60  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); }
61 
63  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); }
64 
66  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) {}
67 
69  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) {}
70 
72  explicit TransactionBase(bytesConstRef _rlp, CheckTransaction _checkSig);
73 
75  explicit TransactionBase(bytes const& _rlp, CheckTransaction _checkSig): TransactionBase(&_rlp, _checkSig) {}
76 
78  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; }
80  bool operator!=(TransactionBase const& _c) const { return !operator==(_c); }
81 
83  Address const& sender() const;
85  Address const& safeSender() const noexcept;
87  void forceSender(Address const& _a) { m_sender = _a; }
88 
90  void checkLowS() const;
91 
95  void checkChainId(int chainId = -4) const;
96 
98  explicit operator bool() const { return m_type != NullTransaction; }
99 
101  bool isCreation() const { return m_type == ContractCreation; }
102 
104  void streamRLP(RLPStream& _s, IncludeSignature _sig = WithSignature, bool _forEip155hash = false) const;
105 
107  bytes rlp(IncludeSignature _sig = WithSignature) const { RLPStream s; streamRLP(s, _sig); return s.out(); }
108 
110  h256 sha3(IncludeSignature _sig = WithSignature) const;
111 
113  u256 value() const { return m_value; }
114 
116  u256 gasPrice() const { return m_gasPrice; }
117 
119  u256 gas() const { return m_gas; }
120 
122  Address receiveAddress() const { return m_receiveAddress; }
123 
125  Address to() const { return m_receiveAddress; }
126 
128  Address from() const { return safeSender(); }
129 
131  bytes const& data() const { return m_data; }
132 
134  u256 nonce() const { return m_nonce; }
135 
137  void setNonce(u256 const& _n) { clearSignature(); m_nonce = _n; }
138 
140  void clearSignature() { m_vrs = SignatureStruct(); }
141 
143  SignatureStruct const& signature() const { return m_vrs; }
144 
145  void sign(Secret const& _priv);
146 
148  int64_t baseGasRequired(EVMSchedule const& _es) const { return baseGasRequired(isCreation(), &m_data, _es); }
149 
151  static int64_t baseGasRequired(bool _contractCreation, bytesConstRef _data, EVMSchedule const& _es);
152 
153 protected:
155  enum Type
156  {
159  MessageCall
160  };
161 
162  Type m_type = NullTransaction;
170  int m_chainId = -4;
171 
172  mutable h256 m_hashWith;
173  mutable Address m_sender;
174 };
175 
177 using TransactionBases = std::vector<TransactionBase>;
178 
180 inline std::ostream& operator<<(std::ostream& _out, TransactionBase const& _t)
181 {
182  _out << _t.sha3().abridged() << "{";
183  if (_t.receiveAddress())
184  _out << _t.receiveAddress().abridged();
185  else
186  _out << "[CREATE]";
187 
188  _out << "/" << _t.data().size() << "$" << _t.value() << "+" << _t.gas() << "@" << _t.gasPrice();
189  _out << "<-" << _t.safeSender().abridged() << " #" << _t.nonce() << "}";
190  return _out;
191 }
192 
193 }
194 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Address from() const
Synonym for safeSender().
Definition: Transaction.h:128
u256 m_gas
The total gas to convert, paid for from sender&#39;s account. Any unused gas gets refunded once the contr...
Definition: Transaction.h:167
TransactionBase()
Constructs a null transaction.
Definition: Transaction.h:54
u256 m_gasPrice
The base fee and thus the implied exchange rate of ETH to GAS.
Definition: Transaction.h:166
Do include a signature.
Definition: Transaction.h:39
TransactionBase(u256 const &_value, u256 const &_gasPrice, u256 const &_gas, bytes const &_data, u256 const &_nonce=0)
Constructs an unsigned contract-creation transaction.
Definition: Transaction.h:69
std::vector< TransactionBase > TransactionBases
Nice name for vector of Transaction.
Definition: Transaction.h:177
void setNonce(u256 const &_n)
Sets the nonce to the given value. Clears any signature.
Definition: Transaction.h:137
std::ostream & operator<<(std::ostream &_out, BlockHeader const &_bi)
Definition: BlockHeader.h:194
bytes const & out() const
Read the byte stream.
Definition: RLP.h:433
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.
Definition: Transaction.h:63
SecureFixedHash< 32 > Secret
Definition: Common.h:35
Transaction to create contracts - receiveAddress() is ignored.
Definition: Transaction.h:158
int64_t baseGasRequired(EVMSchedule const &_es) const
Definition: Transaction.h:148
IncludeSignature
Named-boolean type to encode whether a signature be included in the serialisation process...
Definition: Transaction.h:36
h256 m_hashWith
Cached hash of transaction with signature.
Definition: Transaction.h:172
bool operator==(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:558
SignatureStruct const & signature() const
Definition: Transaction.h:143
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.
Definition: Transaction.h:60
bytes rlp(IncludeSignature _sig=WithSignature) const
Definition: Transaction.h:107
Do not include a signature.
Definition: Transaction.h:38
bool isCreation() const
Definition: Transaction.h:101
bytes m_data
The data associated with the transaction, or the initialiser if it&#39;s a creation transaction.
Definition: Transaction.h:168
Type
Type of transaction.
Definition: Transaction.h:155
u256 m_value
The amount of ETH to be transferred by this transaction. Called &#39;endowment&#39; for contract-creation tra...
Definition: Transaction.h:164
u256 m_nonce
The transaction-count of the sender.
Definition: Transaction.h:163
Address to() const
Synonym for receiveAddress().
Definition: Transaction.h:125
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
CheckTransaction
Definition: Transaction.h:42
Address m_receiveAddress
The receiving address of the transaction.
Definition: Transaction.h:165
bytes const & data() const
Definition: Transaction.h:131
h256 sha3(IncludeSignature _sig=WithSignature) const
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
bool operator==(TransactionBase const &_c) const
Checks equality of transactions.
Definition: Transaction.h:78
Address const & safeSender() const noexcept
Like sender() but will never throw.
Definition: Transaction.cpp:98
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition: Common.cpp:233
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
void forceSender(Address const &_a)
Force the sender to a particular value. This will result in an invalid transaction RLP...
Definition: Transaction.h:87
std::string abridged() const
Definition: FixedHash.h:124
SignatureStruct m_vrs
The signature of the transaction. Encodes the sender.
Definition: Transaction.h:169
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.
Definition: Transaction.h:66
void clearSignature()
Clears the signature.
Definition: Transaction.h:140
Class for writing to an RLP bytestream.
Definition: RLP.h:383
TransactionBase(bytes const &_rlp, CheckTransaction _checkSig)
Constructs a transaction from the given RLP.
Definition: Transaction.h:75
bool operator!=(TransactionBase const &_c) const
Checks inequality of transactions.
Definition: Transaction.h:80
Encodes a transaction, ready to be exported to or freshly imported from RLP.
Definition: Transaction.h:50
Address m_sender
Cached sender, determined from signature.
Definition: Transaction.h:173
Address receiveAddress() const
Definition: Transaction.h:122
Type m_type
Is this a contract-creation transaction or a message-call transaction?
Definition: Transaction.h:162