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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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 != SEQUENCE_FINAL)
  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. std::string CTxOut::ToString() const
  45. {
  46. return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
  47. }
  48. CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
  49. CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime) {}
  50. uint256 CMutableTransaction::GetHash() const
  51. {
  52. return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
  53. }
  54. uint256 CTransaction::ComputeHash() const
  55. {
  56. return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
  57. }
  58. uint256 CTransaction::GetWitnessHash() const
  59. {
  60. if (!HasWitness()) {
  61. return GetHash();
  62. }
  63. return SerializeHash(*this, SER_GETHASH, 0);
  64. }
  65. /* For backward compatibility, the hash is initialized to 0. TODO: remove the need for this default constructor entirely. */
  66. CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0), hash() {}
  67. CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), nLockTime(tx.nLockTime), hash(ComputeHash()) {}
  68. CTransaction::CTransaction(CMutableTransaction &&tx) : nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), nLockTime(tx.nLockTime), hash(ComputeHash()) {}
  69. CAmount CTransaction::GetValueOut() const
  70. {
  71. CAmount nValueOut = 0;
  72. for (const auto& tx_out : vout) {
  73. nValueOut += tx_out.nValue;
  74. if (!MoneyRange(tx_out.nValue) || !MoneyRange(nValueOut))
  75. throw std::runtime_error(std::string(__func__) + ": value out of range");
  76. }
  77. return nValueOut;
  78. }
  79. unsigned int CTransaction::GetTotalSize() const
  80. {
  81. return ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
  82. }
  83. std::string CTransaction::ToString() const
  84. {
  85. std::string str;
  86. str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
  87. GetHash().ToString().substr(0,10),
  88. nVersion,
  89. vin.size(),
  90. vout.size(),
  91. nLockTime);
  92. for (const auto& tx_in : vin)
  93. str += " " + tx_in.ToString() + "\n";
  94. for (const auto& tx_in : vin)
  95. str += " " + tx_in.scriptWitness.ToString() + "\n";
  96. for (const auto& tx_out : vout)
  97. str += " " + tx_out.ToString() + "\n";
  98. return str;
  99. }