tx_verify.h raw

   1  // Copyright (c) 2017-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #ifndef BITCOIN_CONSENSUS_TX_VERIFY_H
   6  #define BITCOIN_CONSENSUS_TX_VERIFY_H
   7  
   8  #include <consensus/amount.h>
   9  #include <script/verify_flags.h>
  10  
  11  #include <cstdint>
  12  #include <vector>
  13  
  14  class CBlockIndex;
  15  class CCoinsViewCache;
  16  class CTransaction;
  17  class TxValidationState;
  18  
  19  /** Transaction validation functions */
  20  
  21  namespace Consensus {
  22  /**
  23   * Check whether all inputs of this transaction are valid (no double spends and amounts)
  24   * This does not modify the UTXO set. This does not check scripts and sigs.
  25   * @param[out] txfee Set to the transaction fee if successful.
  26   * Preconditions: tx.IsCoinBase() is false.
  27   */
  28  [[nodiscard]] bool CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee);
  29  } // namespace Consensus
  30  
  31  /** Auxiliary functions for transaction validation (ideally should not be exposed) */
  32  
  33  /**
  34   * Count ECDSA signature operations the old-fashioned (pre-0.6) way
  35   * @return number of sigops this transaction's outputs will produce when spent
  36   * @see CTransaction::FetchInputs
  37   */
  38  unsigned int GetLegacySigOpCount(const CTransaction& tx);
  39  
  40  /**
  41   * Count ECDSA signature operations in pay-to-script-hash inputs.
  42   *
  43   * @param[in] mapInputs Map of previous transactions that have outputs we're spending
  44   * @return maximum number of sigops required to validate this transaction's inputs
  45   * @see CTransaction::FetchInputs
  46   */
  47  unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& mapInputs);
  48  
  49  /**
  50   * Compute total signature operation cost of a transaction.
  51   * @param[in] tx     Transaction for which we are computing the cost
  52   * @param[in] inputs Map of previous transactions that have outputs we're spending
  53   * @param[in] flags Script verification flags
  54   * @return Total signature operation cost of tx
  55   */
  56  int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, script_verify_flags flags);
  57  
  58  /**
  59   * Check if transaction is final and can be included in a block with the
  60   * specified height and time. Consensus critical.
  61   */
  62  bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime);
  63  
  64  /**
  65   * Calculates the block height and previous block's median time past at
  66   * which the transaction will be considered final in the context of BIP 68.
  67   * For each input that is not sequence locked, the corresponding entries in
  68   * prevHeights are set to 0 as they do not affect the calculation.
  69   */
  70  std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block);
  71  
  72  bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair);
  73  /**
  74   * Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
  75   * Consensus critical. Takes as input a list of heights at which tx's inputs (in order) confirmed.
  76   */
  77  bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block);
  78  
  79  #endif // BITCOIN_CONSENSUS_TX_VERIFY_H
  80