text: // Copyright (c) 2017-2018 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 <consensus/tx_check.h>
  
  #include <primitives/transaction.h>
  #include <consensus/validation.h>
  #include <statsd_client.h>
  #include <boost/thread.hpp>
  
  statsd::StatsdClient txstatsClient2;
  
  bool CheckTransaction(const CTransaction& tx, CValidationState& state)
  {
      boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
      // Basic checks that don't depend on any context
      if (tx.vin.empty())
          return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-vin-empty");
      if (tx.vout.empty())
          return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-vout-empty");
      // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
      if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
          return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-oversize");
  
      // Check for negative or overflow output values (see CVE-2010-5139)
      CAmount nValueOut = 0;
      for (const auto& txout : tx.vout)
      {
          if (txout.nValue < 0)
              return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-vout-negative");
          if (txout.nValue > MAX_MONEY)
              return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-vout-toolarge");
          nValueOut += txout.nValue;
          if (!MoneyRange(nValueOut))
              return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-txouttotal-toolarge");
      }
  
      // Check for duplicate inputs (see CVE-2018-17144)
      // While Consensus::CheckTxInputs does check if all inputs of a tx are available, and UpdateCoins marks all inputs
      // of a tx as spent, it does not check if the tx has duplicate inputs.
      // Failure to run this check will result in either a crash or an inflation bug, depending on the implementation of
      // the underlying coins database.
      std::set<COutPoint> vInOutPoints;
      for (const auto& txin : tx.vin) {
          if (!vInOutPoints.insert(txin.prevout).second)
              return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-inputs-duplicate");
      }
  
      if (tx.IsCoinBase())
      {
          if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
              return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-cb-length");
      }
      else
      {
          for (const auto& txin : tx.vin)
              if (txin.prevout.IsNull())
                  return state.Invalid(ValidationInvalidReason::CONSENSUS, false, "bad-txns-prevout-null");
      }
  
      boost::posix_time::ptime finish = boost::posix_time::microsec_clock::local_time();
      boost::posix_time::time_duration diff = finish - start;
      txstatsClient2.timing("CheckTransaction_us", diff.total_microseconds(), 1.0f);
  
      return true;
  }
  