Branch data Line data Source code
# 1 : : // Copyright (c) 2017-2020 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 : : #include <consensus/tx_verify.h>
# 6 : :
# 7 : : #include <consensus/consensus.h>
# 8 : : #include <primitives/transaction.h>
# 9 : : #include <script/interpreter.h>
# 10 : : #include <consensus/validation.h>
# 11 : :
# 12 : : // TODO remove the following dependencies
# 13 : : #include <chain.h>
# 14 : : #include <coins.h>
# 15 : : #include <util/moneystr.h>
# 16 : :
# 17 : : bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
# 18 : 2636199 : {
# 19 [ + + ]: 2636199 : if (tx.nLockTime == 0)
# 20 : 1448735 : return true;
# 21 [ + + ][ + + ]: 1187464 : if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
# 22 : 1187355 : return true;
# 23 [ + + ]: 109 : for (const auto& txin : tx.vin) {
# 24 [ + + ]: 109 : if (!(txin.nSequence == CTxIn::SEQUENCE_FINAL))
# 25 : 104 : return false;
# 26 : 109 : }
# 27 : 109 : return true;
# 28 : 109 : }
# 29 : :
# 30 : : std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block)
# 31 : 96021 : {
# 32 : 96021 : assert(prevHeights.size() == tx.vin.size());
# 33 : :
# 34 : : // Will be set to the equivalent height- and time-based nLockTime
# 35 : : // values that would be necessary to satisfy all relative lock-
# 36 : : // time constraints given our view of block chain history.
# 37 : : // The semantics of nLockTime are the last invalid height/time, so
# 38 : : // use -1 to have the effect of any height or time being valid.
# 39 : 96021 : int nMinHeight = -1;
# 40 : 96021 : int64_t nMinTime = -1;
# 41 : :
# 42 : : // tx.nVersion is signed integer so requires cast to unsigned otherwise
# 43 : : // we would be doing a signed comparison and half the range of nVersion
# 44 : : // wouldn't support BIP 68.
# 45 [ + + ]: 96021 : bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
# 46 [ + + ]: 96021 : && flags & LOCKTIME_VERIFY_SEQUENCE;
# 47 : :
# 48 : : // Do not enforce sequence numbers as a relative lock time
# 49 : : // unless we have been instructed to
# 50 [ + + ]: 96021 : if (!fEnforceBIP68) {
# 51 : 81479 : return std::make_pair(nMinHeight, nMinTime);
# 52 : 81479 : }
# 53 : :
# 54 [ + + ]: 61025 : for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
# 55 : 46483 : const CTxIn& txin = tx.vin[txinIndex];
# 56 : :
# 57 : : // Sequence numbers with the most significant bit set are not
# 58 : : // treated as relative lock-times, nor are they given any
# 59 : : // consensus-enforced meaning at this point.
# 60 [ + + ]: 46483 : if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
# 61 : : // The height of this input is not relevant for sequence locks
# 62 : 45266 : prevHeights[txinIndex] = 0;
# 63 : 45266 : continue;
# 64 : 45266 : }
# 65 : :
# 66 : 1217 : int nCoinHeight = prevHeights[txinIndex];
# 67 : :
# 68 [ + + ]: 1217 : if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
# 69 : 439 : int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
# 70 : : // NOTE: Subtract 1 to maintain nLockTime semantics
# 71 : : // BIP 68 relative lock times have the semantics of calculating
# 72 : : // the first block or time at which the transaction would be
# 73 : : // valid. When calculating the effective block time or height
# 74 : : // for the entire transaction, we switch to using the
# 75 : : // semantics of nLockTime which is the last invalid block
# 76 : : // time or height. Thus we subtract 1 from the calculated
# 77 : : // time or height.
# 78 : :
# 79 : : // Time-based relative lock-times are measured from the
# 80 : : // smallest allowed timestamp of the block containing the
# 81 : : // txout being spent, which is the median time past of the
# 82 : : // block prior.
# 83 : 439 : nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
# 84 : 778 : } else {
# 85 : 778 : nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
# 86 : 778 : }
# 87 : 1217 : }
# 88 : :
# 89 : 14542 : return std::make_pair(nMinHeight, nMinTime);
# 90 : 14542 : }
# 91 : :
# 92 : : bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
# 93 : 96716 : {
# 94 : 96716 : assert(block.pprev);
# 95 : 96716 : int64_t nBlockTime = block.pprev->GetMedianTimePast();
# 96 [ + + ][ + + ]: 96716 : if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
# 97 : 383 : return false;
# 98 : :
# 99 : 96333 : return true;
# 100 : 96333 : }
# 101 : :
# 102 : : bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block)
# 103 : 68961 : {
# 104 : 68961 : return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
# 105 : 68961 : }
# 106 : :
# 107 : : unsigned int GetLegacySigOpCount(const CTransaction& tx)
# 108 : 451091 : {
# 109 : 451091 : unsigned int nSigOps = 0;
# 110 [ + + ]: 451091 : for (const auto& txin : tx.vin)
# 111 : 602360 : {
# 112 : 602360 : nSigOps += txin.scriptSig.GetSigOpCount(false);
# 113 : 602360 : }
# 114 [ + + ]: 451091 : for (const auto& txout : tx.vout)
# 115 : 1428056 : {
# 116 : 1428056 : nSigOps += txout.scriptPubKey.GetSigOpCount(false);
# 117 : 1428056 : }
# 118 : 451091 : return nSigOps;
# 119 : 451091 : }
# 120 : :
# 121 : : unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
# 122 : 95284 : {
# 123 [ - + ]: 95284 : if (tx.IsCoinBase())
# 124 : 0 : return 0;
# 125 : :
# 126 : 95284 : unsigned int nSigOps = 0;
# 127 [ + + ]: 275603 : for (unsigned int i = 0; i < tx.vin.size(); i++)
# 128 : 180319 : {
# 129 : 180319 : const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
# 130 : 180319 : assert(!coin.IsSpent());
# 131 : 180319 : const CTxOut &prevout = coin.out;
# 132 [ + + ]: 180319 : if (prevout.scriptPubKey.IsPayToScriptHash())
# 133 : 57371 : nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
# 134 : 180319 : }
# 135 : 95284 : return nSigOps;
# 136 : 95284 : }
# 137 : :
# 138 : : int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
# 139 : 195979 : {
# 140 : 195979 : int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;
# 141 : :
# 142 [ + + ]: 195979 : if (tx.IsCoinBase())
# 143 : 100701 : return nSigOps;
# 144 : :
# 145 [ + - ]: 95278 : if (flags & SCRIPT_VERIFY_P2SH) {
# 146 : 95278 : nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
# 147 : 95278 : }
# 148 : :
# 149 [ + + ]: 275583 : for (unsigned int i = 0; i < tx.vin.size(); i++)
# 150 : 180305 : {
# 151 : 180305 : const Coin& coin = inputs.AccessCoin(tx.vin[i].prevout);
# 152 : 180305 : assert(!coin.IsSpent());
# 153 : 180305 : const CTxOut &prevout = coin.out;
# 154 : 180305 : nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, &tx.vin[i].scriptWitness, flags);
# 155 : 180305 : }
# 156 : 95278 : return nSigOps;
# 157 : 95278 : }
# 158 : :
# 159 : : bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee)
# 160 : 5321797 : {
# 161 : : // are the actual inputs available?
# 162 [ + + ]: 5321797 : if (!inputs.HaveInputs(tx)) {
# 163 : 25 : return state.Invalid(TxValidationResult::TX_MISSING_INPUTS, "bad-txns-inputs-missingorspent",
# 164 : 25 : strprintf("%s: inputs missing/spent", __func__));
# 165 : 25 : }
# 166 : :
# 167 : 5321772 : CAmount nValueIn = 0;
# 168 [ + + ]: 13046750 : for (unsigned int i = 0; i < tx.vin.size(); ++i) {
# 169 : 7724985 : const COutPoint &prevout = tx.vin[i].prevout;
# 170 : 7724985 : const Coin& coin = inputs.AccessCoin(prevout);
# 171 : 7724985 : assert(!coin.IsSpent());
# 172 : :
# 173 : : // If prev is coinbase, check that it's matured
# 174 [ + + ][ + + ]: 7724985 : if (coin.IsCoinBase() && nSpendHeight - coin.nHeight < COINBASE_MATURITY) {
# 175 : 7 : return state.Invalid(TxValidationResult::TX_PREMATURE_SPEND, "bad-txns-premature-spend-of-coinbase",
# 176 : 7 : strprintf("tried to spend coinbase at depth %d", nSpendHeight - coin.nHeight));
# 177 : 7 : }
# 178 : :
# 179 : : // Check for negative or overflow input values
# 180 : 7724978 : nValueIn += coin.out.nValue;
# 181 [ - + ][ - + ]: 7724978 : if (!MoneyRange(coin.out.nValue) || !MoneyRange(nValueIn)) {
# 182 : 0 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputvalues-outofrange");
# 183 : 0 : }
# 184 : 7724978 : }
# 185 : :
# 186 : 5321772 : const CAmount value_out = tx.GetValueOut();
# 187 [ + + ]: 5321765 : if (nValueIn < value_out) {
# 188 : 4 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-in-belowout",
# 189 : 4 : strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(value_out)));
# 190 : 4 : }
# 191 : :
# 192 : : // Tally transaction fees
# 193 : 5321761 : const CAmount txfee_aux = nValueIn - value_out;
# 194 [ - + ]: 5321761 : if (!MoneyRange(txfee_aux)) {
# 195 : 0 : return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-fee-outofrange");
# 196 : 0 : }
# 197 : :
# 198 : 5321761 : txfee = txfee_aux;
# 199 : 5321761 : return true;
# 200 : 5321761 : }
|