You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transaction.h 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef BITCOIN_PRIMITIVES_TRANSACTION_H
  6. #define BITCOIN_PRIMITIVES_TRANSACTION_H
  7. #include "amount.h"
  8. #include "memusage.h"
  9. #include "script/script.h"
  10. #include "serialize.h"
  11. #include "uint256.h"
  12. /** An outpoint - a combination of a transaction hash and an index n into its vout */
  13. class COutPoint
  14. {
  15. public:
  16. uint256 hash;
  17. uint32_t n;
  18. COutPoint() { SetNull(); }
  19. COutPoint(uint256 hashIn, uint32_t nIn) { hash = hashIn; n = nIn; }
  20. ADD_SERIALIZE_METHODS;
  21. template <typename Stream, typename Operation>
  22. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  23. READWRITE(hash);
  24. READWRITE(n);
  25. }
  26. void SetNull() { hash.SetNull(); n = (uint32_t) -1; }
  27. bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
  28. friend bool operator<(const COutPoint& a, const COutPoint& b)
  29. {
  30. return (a.hash < b.hash || (a.hash == b.hash && a.n < b.n));
  31. }
  32. friend bool operator==(const COutPoint& a, const COutPoint& b)
  33. {
  34. return (a.hash == b.hash && a.n == b.n);
  35. }
  36. friend bool operator!=(const COutPoint& a, const COutPoint& b)
  37. {
  38. return !(a == b);
  39. }
  40. std::string ToString() const;
  41. size_t DynamicMemoryUsage() const { return 0; }
  42. };
  43. /** An input of a transaction. It contains the location of the previous
  44. * transaction's output that it claims and a signature that matches the
  45. * output's public key.
  46. */
  47. class CTxIn
  48. {
  49. public:
  50. COutPoint prevout;
  51. CScript scriptSig;
  52. uint32_t nSequence;
  53. CTxIn()
  54. {
  55. nSequence = std::numeric_limits<unsigned int>::max();
  56. }
  57. explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max());
  58. CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=std::numeric_limits<uint32_t>::max());
  59. ADD_SERIALIZE_METHODS;
  60. template <typename Stream, typename Operation>
  61. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  62. READWRITE(prevout);
  63. READWRITE(scriptSig);
  64. READWRITE(nSequence);
  65. }
  66. bool IsFinal() const
  67. {
  68. return (nSequence == std::numeric_limits<uint32_t>::max());
  69. }
  70. friend bool operator==(const CTxIn& a, const CTxIn& b)
  71. {
  72. return (a.prevout == b.prevout &&
  73. a.scriptSig == b.scriptSig &&
  74. a.nSequence == b.nSequence);
  75. }
  76. friend bool operator!=(const CTxIn& a, const CTxIn& b)
  77. {
  78. return !(a == b);
  79. }
  80. std::string ToString() const;
  81. size_t DynamicMemoryUsage() const { return scriptSig.DynamicMemoryUsage(); }
  82. };
  83. /** An output of a transaction. It contains the public key that the next input
  84. * must be able to sign with to claim it.
  85. */
  86. class CTxOut
  87. {
  88. public:
  89. CAmount nValue;
  90. CScript scriptPubKey;
  91. CTxOut()
  92. {
  93. SetNull();
  94. }
  95. CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);
  96. ADD_SERIALIZE_METHODS;
  97. template <typename Stream, typename Operation>
  98. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  99. READWRITE(nValue);
  100. READWRITE(scriptPubKey);
  101. }
  102. void SetNull()
  103. {
  104. nValue = -1;
  105. scriptPubKey.clear();
  106. }
  107. bool IsNull() const
  108. {
  109. return (nValue == -1);
  110. }
  111. uint256 GetHash() const;
  112. CAmount GetDustThreshold(const CFeeRate &minRelayTxFee) const
  113. {
  114. // "Dust" is defined in terms of CTransaction::minRelayTxFee,
  115. // which has units satoshis-per-kilobyte.
  116. // If you'd pay more than 1/3 in fees
  117. // to spend something, then we consider it dust.
  118. // A typical txout is 34 bytes big, and will
  119. // need a CTxIn of at least 148 bytes to spend:
  120. // so dust is a txout less than 546 satoshis
  121. // with default minRelayTxFee.
  122. size_t nSize = GetSerializeSize(SER_DISK,0)+148u;
  123. return 3*minRelayTxFee.GetFee(nSize);
  124. }
  125. bool IsDust(const CFeeRate &minRelayTxFee) const
  126. {
  127. return (nValue < GetDustThreshold(minRelayTxFee));
  128. }
  129. friend bool operator==(const CTxOut& a, const CTxOut& b)
  130. {
  131. return (a.nValue == b.nValue &&
  132. a.scriptPubKey == b.scriptPubKey);
  133. }
  134. friend bool operator!=(const CTxOut& a, const CTxOut& b)
  135. {
  136. return !(a == b);
  137. }
  138. std::string ToString() const;
  139. size_t DynamicMemoryUsage() const { return scriptPubKey.DynamicMemoryUsage(); }
  140. };
  141. struct CMutableTransaction;
  142. /** The basic transaction that is broadcasted on the network and contained in
  143. * blocks. A transaction can contain multiple inputs and outputs.
  144. */
  145. class CTransaction
  146. {
  147. private:
  148. /** Memory only. */
  149. const uint256 hash;
  150. void UpdateHash() const;
  151. public:
  152. static const int32_t CURRENT_VERSION=1;
  153. // The local variables are made const to prevent unintended modification
  154. // without updating the cached hash value. However, CTransaction is not
  155. // actually immutable; deserialization and assignment are implemented,
  156. // and bypass the constness. This is safe, as they update the entire
  157. // structure, including the hash.
  158. const int32_t nVersion;
  159. const std::vector<CTxIn> vin;
  160. const std::vector<CTxOut> vout;
  161. const uint32_t nLockTime;
  162. /** Construct a CTransaction that qualifies as IsNull() */
  163. CTransaction();
  164. /** Convert a CMutableTransaction into a CTransaction. */
  165. CTransaction(const CMutableTransaction &tx);
  166. CTransaction& operator=(const CTransaction& tx);
  167. ADD_SERIALIZE_METHODS;
  168. template <typename Stream, typename Operation>
  169. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  170. READWRITE(*const_cast<int32_t*>(&this->nVersion));
  171. nVersion = this->nVersion;
  172. READWRITE(*const_cast<std::vector<CTxIn>*>(&vin));
  173. READWRITE(*const_cast<std::vector<CTxOut>*>(&vout));
  174. READWRITE(*const_cast<uint32_t*>(&nLockTime));
  175. if (ser_action.ForRead())
  176. UpdateHash();
  177. }
  178. bool IsNull() const {
  179. return vin.empty() && vout.empty();
  180. }
  181. const uint256& GetHash() const {
  182. return hash;
  183. }
  184. // Return sum of txouts.
  185. CAmount GetValueOut() const;
  186. // GetValueIn() is a method on CCoinsViewCache, because
  187. // inputs must be known to compute value in.
  188. // Compute priority, given priority of inputs and (optionally) tx size
  189. double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const;
  190. // Compute modified tx size for priority calculation (optionally given tx size)
  191. unsigned int CalculateModifiedSize(unsigned int nTxSize=0) const;
  192. bool IsCoinBase() const
  193. {
  194. return (vin.size() == 1 && vin[0].prevout.IsNull());
  195. }
  196. friend bool operator==(const CTransaction& a, const CTransaction& b)
  197. {
  198. return a.hash == b.hash;
  199. }
  200. friend bool operator!=(const CTransaction& a, const CTransaction& b)
  201. {
  202. return a.hash != b.hash;
  203. }
  204. std::string ToString() const;
  205. size_t DynamicMemoryUsage() const;
  206. };
  207. /** A mutable version of CTransaction. */
  208. struct CMutableTransaction
  209. {
  210. int32_t nVersion;
  211. std::vector<CTxIn> vin;
  212. std::vector<CTxOut> vout;
  213. uint32_t nLockTime;
  214. CMutableTransaction();
  215. CMutableTransaction(const CTransaction& tx);
  216. ADD_SERIALIZE_METHODS;
  217. template <typename Stream, typename Operation>
  218. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  219. READWRITE(this->nVersion);
  220. nVersion = this->nVersion;
  221. READWRITE(vin);
  222. READWRITE(vout);
  223. READWRITE(nLockTime);
  224. }
  225. /** Compute the hash of this CMutableTransaction. This is computed on the
  226. * fly, as opposed to GetHash() in CTransaction, which uses a cached result.
  227. */
  228. uint256 GetHash() const;
  229. };
  230. #endif // BITCOIN_PRIMITIVES_TRANSACTION_H