Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

transaction.cpp 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #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", scriptSig.ToString().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, scriptPubKey.ToString().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. size_t CTransaction::DynamicMemoryUsage() const
  63. {
  64. return memusage::RecursiveDynamicUsage(vin) + memusage::RecursiveDynamicUsage(vout);
  65. }
  66. CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }
  67. CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {
  68. UpdateHash();
  69. }
  70. CTransaction& CTransaction::operator=(const CTransaction &tx) {
  71. *const_cast<int*>(&nVersion) = tx.nVersion;
  72. *const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
  73. *const_cast<std::vector<CTxOut>*>(&vout) = tx.vout;
  74. *const_cast<unsigned int*>(&nLockTime) = tx.nLockTime;
  75. *const_cast<uint256*>(&hash) = tx.hash;
  76. return *this;
  77. }
  78. CAmount CTransaction::GetValueOut() const
  79. {
  80. CAmount nValueOut = 0;
  81. for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it)
  82. {
  83. nValueOut += it->nValue;
  84. if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut))
  85. throw std::runtime_error("CTransaction::GetValueOut(): value out of range");
  86. }
  87. return nValueOut;
  88. }
  89. double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const
  90. {
  91. nTxSize = CalculateModifiedSize(nTxSize);
  92. if (nTxSize == 0) return 0.0;
  93. return dPriorityInputs / nTxSize;
  94. }
  95. unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const
  96. {
  97. // In order to avoid disincentivizing cleaning up the UTXO set we don't count
  98. // the constant overhead for each txin and up to 110 bytes of scriptSig (which
  99. // is enough to cover a compressed pubkey p2sh redemption) for priority.
  100. // Providing any more cleanup incentive than making additional inputs free would
  101. // risk encouraging people to create junk outputs to redeem later.
  102. if (nTxSize == 0)
  103. nTxSize = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
  104. for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it)
  105. {
  106. unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
  107. if (nTxSize > offset)
  108. nTxSize -= offset;
  109. }
  110. return nTxSize;
  111. }
  112. std::string CTransaction::ToString() const
  113. {
  114. std::string str;
  115. str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
  116. GetHash().ToString().substr(0,10),
  117. nVersion,
  118. vin.size(),
  119. vout.size(),
  120. nLockTime);
  121. for (unsigned int i = 0; i < vin.size(); i++)
  122. str += " " + vin[i].ToString() + "\n";
  123. for (unsigned int i = 0; i < vout.size(); i++)
  124. str += " " + vout[i].ToString() + "\n";
  125. return str;
  126. }