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 : #ifndef BITCOIN_POLICY_POLICY_H 7 : #define BITCOIN_POLICY_POLICY_H 8 : 9 : #include <consensus/amount.h> 10 : #include <consensus/consensus.h> 11 : #include <primitives/transaction.h> 12 : #include <script/interpreter.h> 13 : #include <script/standard.h> 14 : 15 : #include <cstdint> 16 : #include <string> 17 : 18 : class CCoinsViewCache; 19 : class CFeeRate; 20 : class CScript; 21 : 22 : /** Default for -blockmaxsize, which controls the maximum size of block the mining code will create **/ 23 : static constexpr unsigned int DEFAULT_BLOCK_MAX_SIZE{2000000}; 24 : /** Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by mining code **/ 25 : static constexpr unsigned int DEFAULT_BLOCK_MIN_TX_FEE{1000}; 26 : /** The maximum size for transactions we're willing to relay/mine */ 27 : static constexpr unsigned int MAX_STANDARD_TX_SIZE{100000}; 28 : /** The minimum size for transactions we're willing to relay/mine (1 empty scriptSig input + 1 P2SH output = 83 bytes) */ 29 : static constexpr unsigned int MIN_STANDARD_TX_SIZE{83}; 30 : /** Maximum number of signature check operations in an IsStandard() P2SH script */ 31 : static constexpr unsigned int MAX_P2SH_SIGOPS{15}; 32 : /** The maximum number of sigops we're willing to relay/mine in a single tx */ 33 : static constexpr unsigned int MAX_STANDARD_TX_SIGOPS{4000}; 34 : /** Default for -maxmempool, maximum megabytes of mempool memory usage */ 35 : static constexpr unsigned int DEFAULT_MAX_MEMPOOL_SIZE{300}; 36 : /** Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting **/ 37 : static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{1000}; 38 : /** Default for -bytespersigop */ 39 : static constexpr unsigned int DEFAULT_BYTES_PER_SIGOP{20}; 40 : /** Default for -permitbaremultisig */ 41 : static constexpr bool DEFAULT_PERMIT_BAREMULTISIG{true}; 42 : /** The maximum size of a standard ScriptSig */ 43 : static constexpr unsigned int MAX_STANDARD_SCRIPTSIG_SIZE{1650}; 44 : /** Min feerate for defining dust. Historically this has been based on the 45 : * minRelayTxFee, however changing the dust limit changes which transactions are 46 : * standard and should be done with care and ideally rarely. It makes sense to 47 : * only increase the dust limit after prior releases were already not creating 48 : * outputs below the new threshold */ 49 : static constexpr unsigned int DUST_RELAY_TX_FEE{3000}; 50 : /** Default for -minrelaytxfee, minimum relay fee for transactions */ 51 : static constexpr unsigned int DEFAULT_MIN_RELAY_TX_FEE{1000}; 52 : /** Default for -limitancestorcount, max number of in-mempool ancestors */ 53 : static constexpr unsigned int DEFAULT_ANCESTOR_LIMIT{25}; 54 : /** Default for -limitancestorsize, maximum kilobytes of tx + all in-mempool ancestors */ 55 : static constexpr unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT{101}; 56 : /** Default for -limitdescendantcount, max number of in-mempool descendants */ 57 : static constexpr unsigned int DEFAULT_DESCENDANT_LIMIT{25}; 58 : /** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */ 59 : static constexpr unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT{101}; 60 : /** 61 : * An extra transaction can be added to a package, as long as it only has one 62 : * ancestor and is no larger than this. Not really any reason to make this 63 : * configurable as it doesn't materially change DoS parameters. 64 : */ 65 : static constexpr unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT{10000}; 66 : /** 67 : * Standard script verification flags that standard transactions will comply 68 : * with. However scripts violating these flags may still be present in valid 69 : * blocks and we must accept those blocks. 70 : */ 71 : static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS{MANDATORY_SCRIPT_VERIFY_FLAGS | 72 : SCRIPT_VERIFY_DERSIG | 73 : SCRIPT_VERIFY_STRICTENC | 74 : SCRIPT_VERIFY_MINIMALDATA | 75 : SCRIPT_VERIFY_NULLDUMMY | 76 : SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS | 77 : SCRIPT_VERIFY_CLEANSTACK | 78 : SCRIPT_VERIFY_NULLFAIL | 79 : SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY | 80 : SCRIPT_VERIFY_CHECKSEQUENCEVERIFY | 81 : SCRIPT_VERIFY_LOW_S | 82 : SCRIPT_VERIFY_CONST_SCRIPTCODE}; 83 : 84 : /** For convenience, standard but not mandatory verify flags. */ 85 : static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS{STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS}; 86 : 87 : /** Used as the flags parameter to sequence and nLocktime checks in non-consensus code. */ 88 : static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS{LOCKTIME_VERIFY_SEQUENCE}; 89 : 90 : CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee); 91 : 92 : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee); 93 : 94 : bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType); 95 : 96 : 97 : // Changing the default transaction version requires a two step process: first 98 : // adapting relay policy by bumping TX_MAX_STANDARD_VERSION, and then later 99 : // allowing the new transaction version in the wallet/RPC. 100 : static constexpr decltype(CTransaction::nVersion) TX_MAX_STANDARD_VERSION{3}; 101 : 102 : /** 103 : * Check for standard transaction types 104 : * @return True if all outputs (scriptPubKeys) use only standard transaction forms 105 : */ 106 : bool IsStandardTx(const CTransaction& tx, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason); 107 : /** 108 : * Check for standard transaction types 109 : * @param[in] mapInputs Map of previous transactions that have outputs we're spending 110 : * @return True if all inputs (scriptSigs) use only standard transaction forms 111 : */ 112 : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs); 113 : 114 : /** Compute the virtual transaction size (taking sigops into account). */ 115 : int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOp, unsigned int bytes_per_sigop); 116 : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOp, unsigned int bytes_per_sigop); 117 : 118 115 : static inline int64_t GetVirtualTransactionSize(const CTransaction& tx) 119 : { 120 115 : return GetVirtualTransactionSize(tx, 0, 0); 121 : } 122 : 123 : #endif // BITCOIN_POLICY_POLICY_H