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.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "primitives/transaction.h"
  6. #include "hash.h"
  7. #include "tinyformat.h"
  8. #include "utilstrencodings.h"
  9. std::string COutPoint::ToString() const
  10. {
  11. return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
  12. }
  13. CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
  14. {
  15. prevout = prevoutIn;
  16. scriptSig = scriptSigIn;
  17. nSequence = nSequenceIn;
  18. }
  19. CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
  20. {
  21. prevout = COutPoint(hashPrevTx, nOut);
  22. scriptSig = scriptSigIn;
  23. nSequence = nSequenceIn;
  24. }
  25. std::string CTxIn::ToString() const
  26. {
  27. std::string str;
  28. str += "CTxIn(";
  29. str += prevout.ToString();
  30. if (prevout.IsNull())
  31. str += strprintf(", coinbase %s", HexStr(scriptSig));
  32. else
  33. str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
  34. if (nSequence != std::numeric_limits<unsigned int>::max())
  35. str += strprintf(", nSequence=%u", nSequence);
  36. str += ")";
  37. return str;
  38. }
  39. CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
  40. {
  41. nValue = nValueIn;
  42. scriptPubKey = scriptPubKeyIn;
  43. }
  44. uint256 CTxOut::GetHash() const
  45. {
  46. return SerializeHash(*this);
  47. }
  48. std::string CTxOut::ToString() const
  49. {
  50. return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
  51. }
  52. CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
  53. CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {}
  54. uint256 CMutableTransaction::GetHash() const
  55. {
  56. return SerializeHash(*this);
  57. }
  58. void CTransaction::UpdateHash() const
  59. {
  60. *const_cast<uint256*>(&hash) = SerializeHash(*this);
  61. }
  62. CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }
  63. CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {
  64. UpdateHash();
  65. }
  66. CTransaction& CTransaction::operator=(const CTransaction &tx) {
  67. *const_cast<int*>(&nVersion) = tx.nVersion;
  68. *const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
  69. *const_cast<std::vector<CTxOut>*>(&vout) = tx.vout;
  70. *const_cast<unsigned int*>(&nLockTime) = tx.nLockTime;
  71. *const_cast<uint256*>(&hash) = tx.hash;
  72. return *this;
  73. }
  74. CAmount CTransaction::GetValueOut() const
  75. {
  76. CAmount nValueOut = 0;
  77. for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it)
  78. {
  79. nValueOut += it->nValue;
  80. if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut))
  81. throw std::runtime_error("CTransaction::GetValueOut(): value out of range");
  82. }
  83. return nValueOut;
  84. }
  85. double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const
  86. {
  87. nTxSize = CalculateModifiedSize(nTxSize);
  88. if (nTxSize == 0) return 0.0;
  89. return dPriorityInputs / nTxSize;
  90. }
  91. unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const
  92. {
  93. // In order to avoid disincentivizing cleaning up the UTXO set we don't count
  94. // the constant overhead for each txin and up to 110 bytes of scriptSig (which
  95. // is enough to cover a compressed pubkey p2sh redemption) for priority.
  96. // Providing any more cleanup incentive than making additional inputs free would
  97. // risk encouraging people to create junk outputs to redeem later.
  98. if (nTxSize == 0)
  99. nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
  100. for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it)
  101. {
  102. unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
  103. if (nTxSize > offset)
  104. nTxSize -= offset;
  105. }
  106. return nTxSize;
  107. }
  108. std::string CTransaction::ToString() const
  109. {
  110. std::string str;
  111. str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
  112. GetHash().ToString().substr(0,10),
  113. nVersion,
  114. vin.size(),
  115. vout.size(),
  116. nLockTime);
  117. for (unsigned int i = 0; i < vin.size(); i++)
  118. str += " " + vin[i].ToString() + "\n";
  119. for (unsigned int i = 0; i < vout.size(); i++)
  120. str += " " + vout[i].ToString() + "\n";
  121. return str;
  122. }