Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 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 <coins.h> 11 : #include <consensus/amount.h> 12 : #include <consensus/consensus.h> 13 : #include <consensus/validation.h> 14 : #include <policy/feerate.h> 15 : #include <primitives/transaction.h> 16 : #include <script/interpreter.h> 17 : #include <script/script.h> 18 : #include <script/standard.h> 19 : #include <serialize.h> 20 : #include <span.h> 21 : 22 : #include <algorithm> 23 : #include <cstddef> 24 : #include <vector> 25 : 26 9914 : CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn) 27 : { 28 : // "Dust" is defined in terms of dustRelayFee, 29 : // which has units satoshis-per-kilobyte. 30 : // If you'd pay more in fees than the value of the output 31 : // to spend something, then we consider it dust. 32 : // A typical spendable txout is 34 bytes big, and will 33 : // need a CTxIn of at least 148 bytes to spend: 34 : // so dust is a spendable txout less than 35 : // 182*dustRelayFee/1000 (in satoshis). 36 : // 546 satoshis at the default rate of 3000 sat/kB. 37 9914 : if (txout.scriptPubKey.IsUnspendable()) 38 0 : return 0; 39 : 40 9914 : size_t nSize = GetSerializeSize(txout)+148u; 41 9914 : return dustRelayFeeIn.GetFee(nSize); 42 9914 : } 43 : 44 9914 : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn) 45 : { 46 9914 : return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn)); 47 : } 48 : 49 225 : bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType) 50 : { 51 225 : std::vector<std::vector<unsigned char> > vSolutions; 52 225 : whichType = Solver(scriptPubKey, vSolutions); 53 : 54 225 : if (whichType == TxoutType::NONSTANDARD) { 55 8 : return false; 56 217 : } else if (whichType == TxoutType::MULTISIG) { 57 9 : unsigned char m = vSolutions.front()[0]; 58 9 : unsigned char n = vSolutions.back()[0]; 59 : // Support up to x-of-3 multisig txns as standard 60 9 : if (n < 1 || n > 3) 61 1 : return false; 62 8 : if (m < 1 || m > n) 63 0 : return false; 64 230 : } else if (whichType == TxoutType::NULL_DATA && 65 14 : (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes)) { 66 1 : return false; 67 : } 68 : 69 215 : return true; 70 225 : } 71 : 72 203 : bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason) 73 : { 74 203 : if (tx.nVersion > TX_MAX_STANDARD_VERSION || tx.nVersion < 1) { 75 3 : reason = "version"; 76 3 : return false; 77 : } 78 : 79 : // Extremely large transactions with lots of inputs can cost the network 80 : // almost as much to process as they cost the sender in fees, because 81 : // computing signature hashes is O(ninputs*txsize). Limiting transactions 82 : // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks. 83 200 : unsigned int sz = GetSerializeSize(tx, CTransaction::CURRENT_VERSION); 84 200 : if (sz >= MAX_STANDARD_TX_SIZE) { 85 1 : reason = "tx-size"; 86 1 : return false; 87 : } 88 : 89 328 : for (const CTxIn& txin : tx.vin) 90 : { 91 : // Biggest 'standard' txin involving only keys is a 15-of-15 P2SH 92 : // multisig with compressed keys (remember the 520 byte limit on 93 : // redeemScript size). That works out to a (15*(33+1))+3=513 byte 94 : // redeemScript, 513+1+15*(73+1)+3=1627 bytes of scriptSig, which 95 : // we round off to 1650(MAX_STANDARD_SCRIPTSIG_SIZE) bytes for 96 : // some minor future-proofing. That's also enough to spend a 97 : // 20-of-20 CHECKMULTISIG scriptPubKey, though such a scriptPubKey 98 : // is not considered standard. 99 195 : if (txin.scriptSig.size() > MAX_STANDARD_SCRIPTSIG_SIZE) { 100 1 : reason = "scriptsig-size"; 101 1 : return false; 102 : } 103 194 : if (!txin.scriptSig.IsPushOnly()) { 104 65 : reason = "scriptsig-not-pushonly"; 105 65 : return false; 106 : } 107 : } 108 : 109 133 : unsigned int nDataOut = 0; 110 : TxoutType whichType; 111 342 : for (const CTxOut& txout : tx.vout) { 112 215 : if (!::IsStandard(txout.scriptPubKey, whichType)) { 113 3 : reason = "scriptpubkey"; 114 3 : return false; 115 : } 116 : 117 212 : if (whichType == TxoutType::NULL_DATA) 118 13 : nDataOut++; 119 199 : else if ((whichType == TxoutType::MULTISIG) && (!permit_bare_multisig)) { 120 1 : reason = "bare-multisig"; 121 1 : return false; 122 198 : } else if (IsDust(txout, dust_relay_fee)) { 123 2 : reason = "dust"; 124 2 : return false; 125 : } 126 : } 127 : 128 : // only one OP_RETURN txout is permitted 129 127 : if (nDataOut > 1) { 130 3 : reason = "multi-op-return"; 131 3 : return false; 132 : } 133 : 134 124 : return true; 135 203 : } 136 : 137 : /** 138 : * Check transaction inputs to mitigate two 139 : * potential denial-of-service attacks: 140 : * 141 : * 1. scriptSigs with extra data stuffed into them, 142 : * not consumed by scriptPubKey (or P2SH script) 143 : * 2. P2SH scripts with a crazy number of expensive 144 : * CHECKSIG/CHECKMULTISIG operations 145 : * 146 : * Why bother? To avoid denial-of-service attacks; an attacker 147 : * can submit a standard HASH... OP_EQUAL transaction, 148 : * which will get accepted into blocks. The redemption 149 : * script can be anything; an attacker could use a very 150 : * expensive-to-check-upon-redemption script like: 151 : * DUP CHECKSIG DROP ... repeated 100 times... OP_1 152 : */ 153 96 : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) 154 : { 155 96 : if (tx.IsCoinBase()) 156 0 : return true; // Coinbases don't use vin normally 157 : 158 196 : for (unsigned int i = 0; i < tx.vin.size(); i++) 159 : { 160 102 : const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out; 161 : 162 102 : std::vector<std::vector<unsigned char> > vSolutions; 163 102 : TxoutType whichType = Solver(prev.scriptPubKey, vSolutions); 164 102 : if (whichType == TxoutType::NONSTANDARD) { 165 0 : return false; 166 102 : } else if (whichType == TxoutType::SCRIPTHASH) { 167 71 : std::vector<std::vector<unsigned char> > stack; 168 : // convert the scriptSig into a stack, so we can inspect the redeemScript 169 71 : if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SigVersion::BASE)) 170 0 : return false; 171 71 : if (stack.empty()) 172 0 : return false; 173 71 : CScript subscript(stack.back().begin(), stack.back().end()); 174 71 : if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) { 175 2 : return false; 176 : } 177 71 : } 178 102 : } 179 : 180 94 : return true; 181 96 : } 182 : 183 70976167 : int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOp, unsigned int bytes_per_sigop) 184 : { 185 70976167 : return std::max(nSize, nSigOp * bytes_per_sigop); 186 : } 187 : 188 115 : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOp, unsigned int bytes_per_sigop) 189 : { 190 115 : return GetVirtualTransactionSize(tx.GetTotalSize(), nSigOp, bytes_per_sigop); 191 : }