Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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/solver.h>
14 : :
15 : : #include <cstdint>
16 : : #include <string>
17 : :
18 : : class CCoinsViewCache;
19 : : class CFeeRate;
20 : : class CScript;
21 : :
22 : : /** Default for -blockmaxweight, which controls the range of block weights the mining code will create **/
23 : : static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT{MAX_BLOCK_WEIGHT};
24 : : /** Default for -blockreservedweight **/
25 : : static constexpr unsigned int DEFAULT_BLOCK_RESERVED_WEIGHT{8000};
26 : : /** This accounts for the block header, var_int encoding of the transaction count and a minimally viable
27 : : * coinbase transaction. It adds an additional safety margin, because even with a thorough understanding
28 : : * of block serialization, it's easy to make a costly mistake when trying to squeeze every last byte.
29 : : * Setting a lower value is prevented at startup. */
30 : : static constexpr unsigned int MINIMUM_BLOCK_RESERVED_WEIGHT{2000};
31 : : /** Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by mining code **/
32 : : static constexpr unsigned int DEFAULT_BLOCK_MIN_TX_FEE{1000};
33 : : /** The maximum weight for transactions we're willing to relay/mine */
34 : : static constexpr int32_t MAX_STANDARD_TX_WEIGHT{400000};
35 : : /** The minimum non-witness size for transactions we're willing to relay/mine: one larger than 64 */
36 : : static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE{65};
37 : : /** Maximum number of signature check operations in an IsStandard() P2SH script */
38 : : static constexpr unsigned int MAX_P2SH_SIGOPS{15};
39 : : /** The maximum number of sigops we're willing to relay/mine in a single tx */
40 : : static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST{MAX_BLOCK_SIGOPS_COST/5};
41 : : /** The maximum number of potentially executed legacy signature operations in a single standard tx */
42 : : static constexpr unsigned int MAX_TX_LEGACY_SIGOPS{2'500};
43 : : /** Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or replacement **/
44 : : static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{1000};
45 : : /** Default for -bytespersigop */
46 : : static constexpr unsigned int DEFAULT_BYTES_PER_SIGOP{20};
47 : : /** Default for -permitbaremultisig */
48 : : static constexpr bool DEFAULT_PERMIT_BAREMULTISIG{true};
49 : : /** The maximum number of witness stack items in a standard P2WSH script */
50 : : static constexpr unsigned int MAX_STANDARD_P2WSH_STACK_ITEMS{100};
51 : : /** The maximum size in bytes of each witness stack item in a standard P2WSH script */
52 : : static constexpr unsigned int MAX_STANDARD_P2WSH_STACK_ITEM_SIZE{80};
53 : : /** The maximum size in bytes of each witness stack item in a standard BIP 342 script (Taproot, leaf version 0xc0) */
54 : : static constexpr unsigned int MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE{80};
55 : : /** The maximum size in bytes of a standard witnessScript */
56 : : static constexpr unsigned int MAX_STANDARD_P2WSH_SCRIPT_SIZE{3600};
57 : : /** The maximum size of a standard ScriptSig */
58 : : static constexpr unsigned int MAX_STANDARD_SCRIPTSIG_SIZE{1650};
59 : : /** Min feerate for defining dust.
60 : : * Changing the dust limit changes which transactions are
61 : : * standard and should be done with care and ideally rarely. It makes sense to
62 : : * only increase the dust limit after prior releases were already not creating
63 : : * outputs below the new threshold */
64 : : static constexpr unsigned int DUST_RELAY_TX_FEE{3000};
65 : : /** Default for -minrelaytxfee, minimum relay fee for transactions */
66 : : static constexpr unsigned int DEFAULT_MIN_RELAY_TX_FEE{1000};
67 : : /** Default for -limitancestorcount, max number of in-mempool ancestors */
68 : : static constexpr unsigned int DEFAULT_ANCESTOR_LIMIT{25};
69 : : /** Default for -limitancestorsize, maximum kilobytes of tx + all in-mempool ancestors */
70 : : static constexpr unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT_KVB{101};
71 : : /** Default for -limitdescendantcount, max number of in-mempool descendants */
72 : : static constexpr unsigned int DEFAULT_DESCENDANT_LIMIT{25};
73 : : /** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */
74 : : static constexpr unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT_KVB{101};
75 : : /** Default for -datacarrier */
76 : : static const bool DEFAULT_ACCEPT_DATACARRIER = true;
77 : : /**
78 : : * Default setting for -datacarriersize in vbytes.
79 : : */
80 : : static const unsigned int MAX_OP_RETURN_RELAY = MAX_STANDARD_TX_WEIGHT / WITNESS_SCALE_FACTOR;
81 : : /**
82 : : * An extra transaction can be added to a package, as long as it only has one
83 : : * ancestor and is no larger than this. Not really any reason to make this
84 : : * configurable as it doesn't materially change DoS parameters.
85 : : */
86 : : static constexpr unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT{10000};
87 : :
88 : : /**
89 : : * Maximum number of ephemeral dust outputs allowed.
90 : : */
91 : : static constexpr unsigned int MAX_DUST_OUTPUTS_PER_TX{1};
92 : :
93 : : /**
94 : : * Mandatory script verification flags that all new transactions must comply with for
95 : : * them to be valid. Failing one of these tests may trigger a DoS ban;
96 : : * see CheckInputScripts() for details.
97 : : *
98 : : * Note that this does not affect consensus validity; see GetBlockScriptFlags()
99 : : * for that.
100 : : */
101 : : static constexpr unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS{SCRIPT_VERIFY_P2SH |
102 : : SCRIPT_VERIFY_DERSIG |
103 : : SCRIPT_VERIFY_NULLDUMMY |
104 : : SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY |
105 : : SCRIPT_VERIFY_CHECKSEQUENCEVERIFY |
106 : : SCRIPT_VERIFY_WITNESS |
107 : : SCRIPT_VERIFY_TAPROOT};
108 : :
109 : : /**
110 : : * Standard script verification flags that standard transactions will comply
111 : : * with. However we do not ban/disconnect nodes that forward txs violating
112 : : * the additional (non-mandatory) rules here, to improve forwards and
113 : : * backwards compatibility.
114 : : */
115 : : static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS{MANDATORY_SCRIPT_VERIFY_FLAGS |
116 : : SCRIPT_VERIFY_STRICTENC |
117 : : SCRIPT_VERIFY_MINIMALDATA |
118 : : SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
119 : : SCRIPT_VERIFY_CLEANSTACK |
120 : : SCRIPT_VERIFY_MINIMALIF |
121 : : SCRIPT_VERIFY_NULLFAIL |
122 : : SCRIPT_VERIFY_LOW_S |
123 : : SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM |
124 : : SCRIPT_VERIFY_WITNESS_PUBKEYTYPE |
125 : : SCRIPT_VERIFY_CONST_SCRIPTCODE |
126 : : SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION |
127 : : SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS |
128 : : SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE};
129 : :
130 : : /** For convenience, standard but not mandatory verify flags. */
131 : : static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS{STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS};
132 : :
133 : : /** Used as the flags parameter to sequence and nLocktime checks in non-consensus code. */
134 : : static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS{LOCKTIME_VERIFY_SEQUENCE};
135 : :
136 : : CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee);
137 : :
138 : : bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee);
139 : :
140 : : bool IsStandard(const CScript& scriptPubKey, TxoutType& whichType);
141 : :
142 : : /** Get the vout index numbers of all dust outputs */
143 : : std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate);
144 : :
145 : : // Changing the default transaction version requires a two step process: first
146 : : // adapting relay policy by bumping TX_MAX_STANDARD_VERSION, and then later
147 : : // allowing the new transaction version in the wallet/RPC.
148 : : static constexpr decltype(CTransaction::version) TX_MAX_STANDARD_VERSION{3};
149 : :
150 : : /**
151 : : * Check for standard transaction types
152 : : * @return True if all outputs (scriptPubKeys) use only standard transaction forms
153 : : */
154 : : bool IsStandardTx(const CTransaction& tx, const std::optional<unsigned>& max_datacarrier_bytes, bool permit_bare_multisig, const CFeeRate& dust_relay_fee, std::string& reason);
155 : : /**
156 : : * Check for standard transaction types
157 : : * @param[in] mapInputs Map of previous transactions that have outputs we're spending
158 : : * @return True if all inputs (scriptSigs) use only standard transaction forms
159 : : */
160 : : bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs);
161 : : /**
162 : : * Check if the transaction is over standard P2WSH resources limit:
163 : : * 3600bytes witnessScript size, 80bytes per witness stack element, 100 witness stack elements
164 : : * These limits are adequate for multisignatures up to n-of-100 using OP_CHECKSIG, OP_ADD, and OP_EQUAL.
165 : : *
166 : : * Also enforce a maximum stack item size limit and no annexes for tapscript spends.
167 : : */
168 : : bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs);
169 : :
170 : : /** Compute the virtual transaction size (weight reinterpreted as bytes). */
171 : : int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop);
172 : : int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop);
173 : : int64_t GetVirtualTransactionInputSize(const CTxIn& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop);
174 : :
175 : 47472 : static inline int64_t GetVirtualTransactionSize(const CTransaction& tx)
176 : : {
177 [ + - + - : 47466 : return GetVirtualTransactionSize(tx, 0, 0);
- - ][ + -
# # # # #
# ][ + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ][ + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- ]
[ # # # # ]
178 : : }
179 : :
180 : 2 : static inline int64_t GetVirtualTransactionInputSize(const CTxIn& tx)
181 : : {
182 [ + - ]: 2 : return GetVirtualTransactionInputSize(tx, 0, 0);
183 : : }
184 : :
185 : : #endif // BITCOIN_POLICY_POLICY_H
|