text: // Copyright (c) 2009-2010 Satoshi Nakamoto
  // Copyright (c) 2009-2015 The Bitcoin Core developers
  // Distributed under the MIT software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include "primitives/transaction.h"
  #include "script/interpreter.h"
  #include "hash.h"
  #include "tinyformat.h"
  #include "utilstrencodings.h"
  
  /** Fees smaller than this (in ztoshi) are considered zero fee (for transaction creation) */
  int64_t CTransaction::nMinTxFee = 1000000; // 0.01 verticalcoin
  /** Fees smaller than this (in ztoshi) are considered zero fee (for relaying) */
  int64_t CTransaction::nMinRelayTxFee = 1000000; // 0.01 verticalcoin
  /** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/
  static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 50000; // 50KB
  
  std::string COutPoint::ToString() const
  {
      return strprintf("COutPoint(%s, %u)", hash.ToString(), n);
  }
  
  std::string COutPoint::ToStringShort() const
  {
      return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,64), n);
  }
  
  CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, uint32_t nSequenceIn)
  {
      prevout = prevoutIn;
      scriptSig = scriptSigIn;
      nSequence = nSequenceIn;
  }
  
  CTxIn::CTxIn(uint256 hashPrevTx, uint32_t nOut, CScript scriptSigIn, uint32_t nSequenceIn)
  {
      prevout = COutPoint(hashPrevTx, nOut);
      scriptSig = scriptSigIn;
      nSequence = nSequenceIn;
  }
  
  std::string CTxIn::ToString() const
  {
      std::string str;
      str += "CTxIn(";
      str += prevout.ToString();
      if (prevout.IsNull())
          str += strprintf(", coinbase %s", HexStr(scriptSig).substr(0, 24));
      else
          str += strprintf(", scriptSig=%s", HexStr(scriptSig).substr(0, 24));
      if (nSequence != SEQUENCE_FINAL)
          str += strprintf(", nSequence=%u", nSequence);
      str += ")";
      return str;
  }
  
  CTxOut::CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn)
  {
      nValue = nValueIn;
      scriptPubKey = scriptPubKeyIn;
  }
  
  uint256 CTxOut::GetHash() const
  {
      return SerializeHash(*this);
  }
  
  std::string CTxOut::ToString() const
  {
      return strprintf("CTxOut(nValue=%d.%08d, scriptPubKey=%s)", nValue / COIN, nValue % COIN, HexStr(scriptPubKey).substr(0, 30));
  }
  
  CMutableTransaction::CMutableTransaction() : nVersion(CTransaction::CURRENT_VERSION), nLockTime(0) {}
  CMutableTransaction::CMutableTransaction(const CTransaction& tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), wit(tx.wit), nLockTime(tx.nLockTime) {}
  
  uint256 CMutableTransaction::GetHash() const
  {
      return SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
  }
  
  std::string CMutableTransaction::ToString() const
  {
      std::string str;
      str += strprintf("CMutableTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
                       GetHash().ToString().substr(0,10),
                       nVersion,
                       vin.size(),
                       vout.size(),
                       nLockTime);
      for (unsigned int i = 0; i < vin.size(); i++)
          str += "    " + vin[i].ToString() + "\n";
      for (unsigned int i = 0; i < vout.size(); i++)
          str += "    " + vout[i].ToString() + "\n";
      return str;
  }
  
  void CTransaction::UpdateHash() const
  {
      *const_cast<uint256*>(&hash) = SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
  }
  
  
  int64_t CTransaction::GetMinFee(unsigned int nBlockSize, bool fAllowFree, enum GetMinFee_mode mode) const
  {
      // Base fee is either nMinTxFee or nMinRelayTxFee
      int64_t nBaseFee = nMinTxFee;
  
      unsigned int nBytes = ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
      unsigned int nNewBlockSize = nBlockSize + nBytes;
      int64_t nMinFee = (1 + (int64_t) nBytes / 1000) * nBaseFee;
      if (fAllowFree)
      {
          // There is a free transaction area in blocks created by most miners,
          // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
          //   to be considered to fall into this category. We don't want to encourage sending
          //   multiple transactions instead of one big transaction to avoid fees.
          // * If we are creating a transaction we allow transactions up to 5,000 bytes
          //   to be considered safe and assume they can likely make it into this section.
          if (nBytes < (mode == GMF_SEND ? 5000 : (DEFAULT_BLOCK_PRIORITY_SIZE - 1000)))
              nMinFee = 0;
      }
  
      // ZCoin
      // To limit dust spam, add nBaseFee for each output less than DUST_SOFT_LIMIT
      for (unsigned int i = 0; i < vout.size(); i++)
          if (vout[i].nValue < DUST_SOFT_LIMIT) {
              nMinFee += nBaseFee;
          }
      // Raise the price as the block approaches full
      if (nBlockSize != 1 && nNewBlockSize >= MEDIAN_BLOCK_SIZE_GEN / 2)
      {
          if (nNewBlockSize >= MEDIAN_BLOCK_SIZE_GEN)
              return MAX_MONEY;
          nMinFee *= MEDIAN_BLOCK_SIZE_GEN / (MEDIAN_BLOCK_SIZE_GEN - nNewBlockSize);
      }
  
      if (!MoneyRange(nMinFee))
          nMinFee = MAX_MONEY;
      return nMinFee;
  }
  
  uint256 CTransaction::GetWitnessHash() const
  {
      return SerializeHash(*this, SER_GETHASH, 0);
  }
  
  uint256 CTransaction::GetNormalizedHash() const
  {
      return SignatureHash(CScript(), *this, 0, SIGHASH_ALL, 0, SIGVERSION_BASE);
  }
  
  CTransaction::CTransaction() : nVersion(CTransaction::CURRENT_VERSION), vin(), vout(), nLockTime(0) { }
  
  CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion), vin(tx.vin), vout(tx.vout), wit(tx.wit), nLockTime(tx.nLockTime) {
      UpdateHash();
  }
  
  CTransaction& CTransaction::operator=(const CTransaction &tx) {
      *const_cast<int*>(&nVersion) = tx.nVersion;
      *const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
      *const_cast<std::vector<CTxOut>*>(&vout) = tx.vout;
      *const_cast<CTxWitness*>(&wit) = tx.wit;
      *const_cast<unsigned int*>(&nLockTime) = tx.nLockTime;
      *const_cast<uint256*>(&hash) = tx.hash;
      return *this;
  }
  
  CAmount CTransaction::GetValueOut() const
  {
      CAmount nValueOut = 0;
      for (std::vector<CTxOut>::const_iterator it(vout.begin()); it != vout.end(); ++it)
      {
          nValueOut += it->nValue;
          if (!MoneyRange(it->nValue) || !MoneyRange(nValueOut))
              throw std::runtime_error(std::string(__func__) + ": value out of range");
      }
      return nValueOut;
  }
  
  double CTransaction::ComputePriority(double dPriorityInputs, unsigned int nTxSize) const
  {
      nTxSize = CalculateModifiedSize(nTxSize);
      if (nTxSize == 0) return 0.0;
  
      return dPriorityInputs / nTxSize;
  }
  
  bool CTransaction::IsCoinBase() const
  {
      return (vin.size() == 1 && vin[0].prevout.IsNull() && (vin[0].scriptSig[0] != OP_ZEROCOINSPEND) );
  }
  
  bool CTransaction::IsZerocoinSpend() const
  {
      return (vin.size() == 1 && vin[0].prevout.IsNull() && (vin[0].scriptSig[0] == OP_ZEROCOINSPEND) && (vout.size() == 1) );
  }
  
  bool CTransaction::IsZerocoinMint(const CTransaction& tx) const
  {
      for (std::vector<CTxOut>::const_iterator it(tx.vout.begin()); it != tx.vout.end(); ++it)
      {
          if (it -> scriptPubKey.IsZerocoinMint())
              return true;
      }
      return false;
  }
  
  unsigned int CTransaction::CalculateModifiedSize(unsigned int nTxSize) const
  {
      // In order to avoid disincentivizing cleaning up the UTXO set we don't count
      // the constant overhead for each txin and up to 110 bytes of scriptSig (which
      // is enough to cover a compressed pubkey p2sh redemption) for priority.
      // Providing any more cleanup incentive than making additional inputs free would
      // risk encouraging people to create junk outputs to redeem later.
      if (nTxSize == 0)
          nTxSize = (GetTransactionWeight(*this) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
      for (std::vector<CTxIn>::const_iterator it(vin.begin()); it != vin.end(); ++it)
      {
          unsigned int offset = 41U + std::min(110U, (unsigned int)it->scriptSig.size());
          if (nTxSize > offset)
              nTxSize -= offset;
      }
      return nTxSize;
  }
  
  std::string CTransaction::ToString() const
  {
      std::string str;
      str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%u, vout.size=%u, nLockTime=%u)\n",
          GetHash().ToString(),
          nVersion,
          vin.size(),
          vout.size(),
          nLockTime);
      for (unsigned int i = 0; i < vin.size(); i++)
          str += "    " + vin[i].ToString() + "\n";
      for (unsigned int i = 0; i < wit.vtxinwit.size(); i++)
          str += "    " + wit.vtxinwit[i].scriptWitness.ToString() + "\n";
      for (unsigned int i = 0; i < vout.size(); i++)
          str += "    " + vout[i].ToString() + "\n";
      return str;
  }
  
  
  int64_t GetTransactionWeight(const CTransaction& tx)
  {
      return ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR -1) + ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
  }
  