LCOV - code coverage report
Current view: top level - src/policy - policy.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 119 126 94.4 %
Date: 2020-09-26 01:30:44 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2019 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : // NOTE: This file is intended to be customised by the end user, and includes only local node policy logic
       7             : 
       8             : #include <policy/policy.h>
       9             : 
      10             : #include <consensus/validation.h>
      11             : #include <coins.h>
      12             : 
      13             : 
      14       98709 : CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
      15             : {
      16             :     // "Dust" is defined in terms of dustRelayFee,
      17             :     // which has units satoshis-per-kilobyte.
      18             :     // If you'd pay more in fees than the value of the output
      19             :     // to spend something, then we consider it dust.
      20             :     // A typical spendable non-segwit txout is 34 bytes big, and will
      21             :     // need a CTxIn of at least 148 bytes to spend:
      22             :     // so dust is a spendable txout less than
      23             :     // 182*dustRelayFee/1000 (in satoshis).
      24             :     // 546 satoshis at the default rate of 3000 sat/kB.
      25             :     // A typical spendable segwit txout is 31 bytes big, and will
      26             :     // need a CTxIn of at least 67 bytes to spend:
      27             :     // so dust is a spendable txout less than
      28             :     // 98*dustRelayFee/1000 (in satoshis).
      29             :     // 294 satoshis at the default rate of 3000 sat/kB.
      30       98709 :     if (txout.scriptPubKey.IsUnspendable())
      31          12 :         return 0;
      32             : 
      33       98697 :     size_t nSize = GetSerializeSize(txout);
      34       98697 :     int witnessversion = 0;
      35       98697 :     std::vector<unsigned char> witnessprogram;
      36             : 
      37       98697 :     if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
      38             :         // sum the sizes of the parts of a transaction input
      39             :         // with 75% segwit discount applied to the script size.
      40       96204 :         nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
      41       96204 :     } else {
      42        2493 :         nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
      43             :     }
      44             : 
      45       98697 :     return dustRelayFeeIn.GetFee(nSize);
      46       98709 : }
      47             : 
      48       98688 : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
      49             : {
      50       98688 :     return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
      51             : }
      52             : 
      53       25781 : bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType)
      54             : {
      55       25781 :     std::vector<std::vector<unsigned char> > vSolutions;
      56       25781 :     whichType = Solver(scriptPubKey, vSolutions);
      57             : 
      58       25781 :     if (whichType == TxoutType::NONSTANDARD) {
      59          96 :         return false;
      60       25685 :     } else if (whichType == TxoutType::MULTISIG) {
      61          10 :         unsigned char m = vSolutions.front()[0];
      62          10 :         unsigned char n = vSolutions.back()[0];
      63             :         // Support up to x-of-3 multisig txns as standard
      64          10 :         if (n < 1 || n > 3)
      65           1 :             return false;
      66           9 :         if (m < 1 || m > n)
      67           0 :             return false;
      68       25702 :     } else if (whichType == TxoutType::NULL_DATA &&
      69          18 :                (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes)) {
      70           1 :           return false;
      71             :     }
      72             : 
      73       25683 :     return true;
      74       25781 : }
      75             : 
      76        3324 : bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason)
      77             : {
      78        3324 :     if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
      79           4 :         reason = "version";
      80           4 :         return false;
      81             :     }
      82             : 
      83             :     // Extremely large transactions with lots of inputs can cost the network
      84             :     // almost as much to process as they cost the sender in fees, because
      85             :     // computing signature hashes is O(ninputs*txsize). Limiting transactions
      86             :     // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
      87        3320 :     unsigned int sz = GetTransactionWeight(tx);
      88        3320 :     if (sz > MAX_STANDARD_TX_WEIGHT) {
      89           3 :         reason = "tx-size";
      90           3 :         return false;
      91             :     }
      92             : 
      93       19990 :     for (const CTxIn& txin : tx.vin)
      94             :     {
      95             :         // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
      96             :         // keys (remember the 520 byte limit on redeemScript size). That works
      97             :         // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
      98             :         // bytes of scriptSig, which we round off to 1650 bytes for some minor
      99             :         // future-proofing. That's also enough to spend a 20-of-20
     100             :         // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
     101             :         // considered standard.
     102       16673 :         if (txin.scriptSig.size() > 1650) {
     103           2 :             reason = "scriptsig-size";
     104       31827 :             return false;
     105             :         }
     106       16671 :         if (!txin.scriptSig.IsPushOnly()) {
     107         238 :             reason = "scriptsig-not-pushonly";
     108         238 :             return false;
     109             :         }
     110       16433 :     }
     111             : 
     112             :     unsigned int nDataOut = 0;
     113        3077 :     TxoutType whichType;
     114       28848 :     for (const CTxOut& txout : tx.vout) {
     115       25771 :         if (!::IsStandard(txout.scriptPubKey, whichType)) {
     116          91 :             reason = "scriptpubkey";
     117          91 :             return false;
     118             :         }
     119             : 
     120       25680 :         if (whichType == TxoutType::NULL_DATA)
     121          17 :             nDataOut++;
     122       25663 :         else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) {
     123           2 :             reason = "bare-multisig";
     124           2 :             return false;
     125       25661 :         } else if (IsDust(txout, dust_relay_fee)) {
     126           7 :             reason = "dust";
     127           7 :             return false;
     128             :         }
     129       25671 :     }
     130             : 
     131             :     // only one OP_RETURN txout is permitted
     132        2977 :     if (nDataOut > 1) {
     133           4 :         reason = "multi-op-return";
     134           4 :         return false;
     135             :     }
     136             : 
     137        2973 :     return true;
     138        6401 : }
     139             : 
     140             : /**
     141             :  * Check transaction inputs to mitigate two
     142             :  * potential denial-of-service attacks:
     143             :  *
     144             :  * 1. scriptSigs with extra data stuffed into them,
     145             :  *    not consumed by scriptPubKey (or P2SH script)
     146             :  * 2. P2SH scripts with a crazy number of expensive
     147             :  *    CHECKSIG/CHECKMULTISIG operations
     148             :  *
     149             :  * Why bother? To avoid denial-of-service attacks; an attacker
     150             :  * can submit a standard HASH... OP_EQUAL transaction,
     151             :  * which will get accepted into blocks. The redemption
     152             :  * script can be anything; an attacker could use a very
     153             :  * expensive-to-check-upon-redemption script like:
     154             :  *   DUP CHECKSIG DROP ... repeated 100 times... OP_1
     155             :  *
     156             :  * Note that only the non-witness portion of the transaction is checked here.
     157             :  */
     158        2821 : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
     159             : {
     160        2821 :     if (tx.IsCoinBase())
     161           0 :         return true; // Coinbases don't use vin normally
     162             : 
     163       15978 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     164             :     {
     165       13177 :         const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
     166             : 
     167       13177 :         std::vector<std::vector<unsigned char> > vSolutions;
     168       13177 :         TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
     169       13177 :         if (whichType == TxoutType::NONSTANDARD || whichType == TxoutType::WITNESS_UNKNOWN) {
     170             :             // WITNESS_UNKNOWN failures are typically also caught with a policy
     171             :             // flag in the script interpreter, but it can be helpful to catch
     172             :             // this type of NONSTANDARD transaction earlier in transaction
     173             :             // validation.
     174          18 :             return false;
     175       13159 :         } else if (whichType == TxoutType::SCRIPTHASH) {
     176         243 :             std::vector<std::vector<unsigned char> > stack;
     177             :             // convert the scriptSig into a stack, so we can inspect the redeemScript
     178         243 :             if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
     179           0 :                 return false;
     180         243 :             if (stack.empty())
     181           0 :                 return false;
     182         243 :             CScript subscript(stack.back().begin(), stack.back().end());
     183         243 :             if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
     184           2 :                 return false;
     185             :             }
     186         243 :         }
     187       13177 :     }
     188             : 
     189        2801 :     return true;
     190        2821 : }
     191             : 
     192        2053 : bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
     193             : {
     194        2053 :     if (tx.IsCoinBase())
     195           0 :         return true; // Coinbases are skipped
     196             : 
     197       13529 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     198             :     {
     199             :         // We don't care if witness for this input is empty, since it must not be bloated.
     200             :         // If the script is invalid without witness, it would be caught sooner or later during validation.
     201       11483 :         if (tx.vin[i].scriptWitness.IsNull())
     202             :             continue;
     203             : 
     204       10545 :         const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
     205             : 
     206             :         // get the scriptPubKey corresponding to this input:
     207       10545 :         CScript prevScript = prev.scriptPubKey;
     208             : 
     209       10545 :         if (prevScript.IsPayToScriptHash()) {
     210         193 :             std::vector <std::vector<unsigned char> > stack;
     211             :             // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
     212             :             // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
     213             :             // If the check fails at this stage, we know that this txid must be a bad one.
     214         193 :             if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE))
     215           0 :                 return false;
     216         193 :             if (stack.empty())
     217           0 :                 return false;
     218         193 :             prevScript = CScript(stack.back().begin(), stack.back().end());
     219         193 :         }
     220             : 
     221       10545 :         int witnessversion = 0;
     222       10545 :         std::vector<unsigned char> witnessprogram;
     223             : 
     224             :         // Non-witness program must not be associated with any witness
     225       10545 :         if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
     226           1 :             return false;
     227             : 
     228             :         // Check P2WSH standard limits
     229       10544 :         if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
     230         378 :             if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
     231           2 :                 return false;
     232         376 :             size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
     233         376 :             if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
     234           2 :                 return false;
     235        1036 :             for (unsigned int j = 0; j < sizeWitnessStack; j++) {
     236         664 :                 if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
     237           2 :                     return false;
     238             :             }
     239         372 :         }
     240       10545 :     }
     241        2046 :     return true;
     242        2053 : }
     243             : 
     244   208382813 : int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
     245             : {
     246   208382813 :     return (std::max(nWeight, nSigOpCost * bytes_per_sigop) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
     247             : }
     248             : 
     249       11461 : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop)
     250             : {
     251       11461 :     return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost, bytes_per_sigop);
     252             : }
     253             : 
     254      228428 : int64_t GetVirtualTransactionInputSize(const CTxIn& txin, int64_t nSigOpCost, unsigned int bytes_per_sigop)
     255             : {
     256      228428 :     return GetVirtualTransactionSize(GetTransactionInputWeight(txin), nSigOpCost, bytes_per_sigop);
     257             : }

Generated by: LCOV version 1.15