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 7.9KB

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