1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka 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 LIMENKA_POLICY_POLICY_H
7 #define LIMENKA_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 #include <unordered_set>
18 #include <utility>
19 20 class CCoinsViewCache;
21 class CFeeRate;
22 class CScript;
23 namespace kernel {
24 struct MemPoolOptions;
25 };
26 27 /** Default for -blockmaxsize, which controls the maximum size of block the mining code will create **/
28 static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 300000;
29 /** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/
30 static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 100000;
31 /** Minimum priority for transactions to be accepted into the priority area **/
32 static const double MINIMUM_TX_PRIORITY = COIN * 144 / 250;
33 /** Default for -blockmaxweight, which controls the range of block weights the mining code will create **/
34 static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT{DEFAULT_BLOCK_MAX_SIZE * WITNESS_SCALE_FACTOR};
35 /** Default for BlockCreateOptions.block_reserved_size **/
36 static constexpr unsigned int DEFAULT_BLOCK_RESERVED_SIZE{1000};
37 /** Default for -blockreservedweight **/
38 static constexpr unsigned int DEFAULT_BLOCK_RESERVED_WEIGHT{8000};
39 /** This accounts for the block header, var_int encoding of the transaction count and a minimally viable
40 * coinbase transaction. It adds an additional safety margin, because even with a thorough understanding
41 * of block serialization, it's easy to make a costly mistake when trying to squeeze every last byte.
42 * Setting a lower value is prevented at startup. */
43 static constexpr unsigned int MINIMUM_BLOCK_RESERVED_WEIGHT{2000};
44 /** Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by mining code **/
45 static constexpr unsigned int DEFAULT_BLOCK_MIN_TX_FEE{1000};
46 /** The maximum weight for transactions we're willing to relay/mine */
47 static constexpr int32_t MAX_STANDARD_TX_WEIGHT{400000};
48 /** The minimum non-witness size for transactions we're willing to relay/mine: one larger than 64 */
49 static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE{65};
50 /** Maximum number of signature check operations in an IsStandard() P2SH script */
51 static constexpr unsigned int MAX_P2SH_SIGOPS{15};
52 /** The maximum number of sigops we're willing to relay/mine in a single tx */
53 static constexpr unsigned int MAX_STANDARD_TX_SIGOPS_COST{MAX_BLOCK_SIGOPS_COST/5};
54 /** The maximum number of potentially executed legacy signature operations in a single standard tx */
55 static constexpr unsigned int MAX_TX_LEGACY_SIGOPS{2'500};
56 /** Default for -incrementalrelayfee, which sets the minimum feerate increase for mempool limiting or replacement **/
57 static constexpr unsigned int DEFAULT_INCREMENTAL_RELAY_FEE{1000};
58 static constexpr CAmount CORE_INCREMENTAL_RELAY_FEE{100};
59 /** Default for -maxscriptsize */
60 static constexpr unsigned int DEFAULT_SCRIPT_SIZE_POLICY_LIMIT{1650};
61 /** Default for -bytespersigop */
62 static constexpr unsigned int DEFAULT_BYTES_PER_SIGOP{20};
63 /** Default for -bytespersigopstrict */
64 static constexpr unsigned int DEFAULT_BYTES_PER_SIGOP_STRICT{20};
65 /** Default for -datacarriercost (multiplied by WITNESS_SCALE_FACTOR) */
66 static constexpr unsigned int DEFAULT_WEIGHT_PER_DATA_BYTE{4};
67 /** Default for -rejecttokens */
68 static constexpr bool DEFAULT_REJECT_TOKENS{false};
69 /** Default for -subdustfeepenalty */
70 static constexpr bool DEFAULT_SUBDUSTFEEPENALTY{true};
71 72 // NOTE: Changes to these three require manually adjusting doc in init.cpp
73 /** Default for -permitephemeral=send */
74 static constexpr bool DEFAULT_PERMITEPHEMERAL_SEND{false};
75 /** Default for -permitephemeral=dust */
76 static constexpr bool DEFAULT_PERMITEPHEMERAL_DUST{false};
77 /** Default for -permitephemeral=anchor */
78 static constexpr bool DEFAULT_PERMITEPHEMERAL_ANCHOR{true};
79 80 /** Default for -permitbareanchor */
81 static constexpr bool DEFAULT_PERMITBAREANCHOR{true};
82 /** Default for -permitbarepubkey */
83 static constexpr bool DEFAULT_PERMIT_BAREPUBKEY{false};
84 /** Default for -permitbaremultisig */
85 static constexpr bool DEFAULT_PERMIT_BAREMULTISIG{false};
86 /** Default for -rejectparasites */
87 static constexpr bool DEFAULT_REJECT_PARASITES{true};
88 /** The maximum number of witness stack items in a standard P2WSH script */
89 static constexpr unsigned int MAX_STANDARD_P2WSH_STACK_ITEMS{100};
90 /** The maximum size in bytes of each witness stack item in a standard P2WSH script */
91 static constexpr unsigned int MAX_STANDARD_P2WSH_STACK_ITEM_SIZE{80};
92 /** The maximum size in bytes of each witness stack item in a standard BIP 342 script (Taproot, leaf version 0xc0) */
93 static constexpr unsigned int MAX_STANDARD_TAPSCRIPT_STACK_ITEM_SIZE{80};
94 /** The maximum size in bytes of a standard witnessScript */
95 static constexpr unsigned int MAX_STANDARD_P2WSH_SCRIPT_SIZE{3600};
96 /** The maximum size of a standard ScriptSig */
97 static constexpr unsigned int MAX_STANDARD_SCRIPTSIG_SIZE{1650};
98 /** Min feerate for defining dust.
99 * Changing the dust limit changes which transactions are
100 * standard and should be done with care and ideally rarely. It makes sense to
101 * only increase the dust limit after prior releases were already not creating
102 * outputs below the new threshold */
103 static constexpr unsigned int DUST_RELAY_TX_FEE{3000};
104 static const std::string DEFAULT_DUST_DYNAMIC{"off"};
105 static const int DEFAULT_DUST_RELAY_MULTIPLIER{3'000};
106 static const std::string DEFAULT_SPKREUSE{"allow"};
107 /** Default for -minrelaytxfee, minimum relay fee for transactions */
108 static constexpr unsigned int DEFAULT_MIN_RELAY_TX_FEE{1000};
109 /** Default for -limitancestorcount, max number of in-mempool ancestors */
110 static constexpr unsigned int DEFAULT_ANCESTOR_LIMIT{25};
111 /** Default for -limitancestorsize, maximum kilobytes of tx + all in-mempool ancestors */
112 static constexpr unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT_KVB{101};
113 /** Default for -limitdescendantcount, max number of in-mempool descendants */
114 static constexpr unsigned int DEFAULT_DESCENDANT_LIMIT{25};
115 /** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */
116 static constexpr unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT_KVB{101};
117 /** Default for -datacarrier */
118 static const bool DEFAULT_ACCEPT_DATACARRIER = true;
119 /**
120 * Default setting for -datacarriersize. 80 bytes of data, +1 for OP_RETURN,
121 * +2 for the pushdata opcodes.
122 */
123 static const unsigned int MAX_OP_RETURN_RELAY = 83;
124 /** Default for -permitbaredatacarrier */
125 static const bool DEFAULT_PERMITBAREDATACARRIER{false};
126 /** Default for -datacarrierfullcount */
127 static constexpr bool DEFAULT_DATACARRIER_FULLCOUNT{true};
128 /** Default for -datacarriersize: maximum bytes per OP_RETURN output */
129 static constexpr unsigned int DEFAULT_DATA_CARRIER_SIZE{80};
130 /** Default for -maxopreturnrelay: maximum OP_RETURN outputs per transaction */
131 static constexpr unsigned int DEFAULT_MAX_OP_RETURN_OUTPUTS{1};
132 /** Default for -taprootwitnesslimit: maximum witness bytes per taproot input (0 = unlimited) */
133 static constexpr unsigned int DEFAULT_TAPROOT_WITNESS_LIMIT{98};
134 /** Flag day MTP for rejecting taproot transactions in mempool. Jan 1 2027 00:00:00 UTC. */
135 static constexpr int64_t TAPROOT_REJECT_MTP{1792328400}; // fork activation: 2026-10-18 09:00 EDT
136 /**
137 * An extra transaction can be added to a package, as long as it only has one
138 * ancestor and is no larger than this. Not really any reason to make this
139 * configurable as it doesn't materially change DoS parameters.
140 */
141 static constexpr unsigned int EXTRA_DESCENDANT_TX_SIZE_LIMIT{10000};
142 143 /**
144 * Maximum number of ephemeral dust outputs allowed.
145 */
146 static constexpr unsigned int MAX_DUST_OUTPUTS_PER_TX{1};
147 148 /**
149 * Mandatory script verification flags that all new transactions must comply with for
150 * them to be valid. Failing one of these tests may trigger a DoS ban;
151 * see CheckInputScripts() for details.
152 *
153 * Note that this does not affect consensus validity; see GetBlockScriptFlags()
154 * for that.
155 */
156 static constexpr unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS{SCRIPT_VERIFY_P2SH |
157 SCRIPT_VERIFY_DERSIG |
158 SCRIPT_VERIFY_NULLDUMMY |
159 SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY |
160 SCRIPT_VERIFY_CHECKSEQUENCEVERIFY |
161 SCRIPT_VERIFY_WITNESS |
162 SCRIPT_VERIFY_TAPROOT |
163 SCRIPT_VERIFY_P2SPKH};
164 165 /**
166 * Standard script verification flags that standard transactions will comply
167 * with. However we do not ban/disconnect nodes that forward txs violating
168 * the additional (non-mandatory) rules here, to improve forwards and
169 * backwards compatibility.
170 */
171 static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS{MANDATORY_SCRIPT_VERIFY_FLAGS |
172 SCRIPT_VERIFY_STRICTENC |
173 SCRIPT_VERIFY_MINIMALDATA |
174 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS |
175 SCRIPT_VERIFY_CLEANSTACK |
176 SCRIPT_VERIFY_MINIMALIF |
177 SCRIPT_VERIFY_NULLFAIL |
178 SCRIPT_VERIFY_LOW_S |
179 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM |
180 SCRIPT_VERIFY_WITNESS_PUBKEYTYPE |
181 SCRIPT_VERIFY_CONST_SCRIPTCODE |
182 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION |
183 SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS |
184 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE |
185 REDUCED_DATA_MANDATORY_VERIFY_FLAGS};
186 187 /** For convenience, standard but not mandatory verify flags. */
188 static constexpr unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS{STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS};
189 190 /** Used as the flags parameter to sequence and nLocktime checks in non-consensus code. */
191 static constexpr unsigned int STANDARD_LOCKTIME_VERIFY_FLAGS{LOCKTIME_VERIFY_SEQUENCE};
192 193 typedef std::unordered_set<std::string> ignore_rejects_type;
194 static const ignore_rejects_type empty_ignore_rejects{};
195 196 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFee);
197 198 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFee);
199 200 bool IsStandard(const CScript& scriptPubKey, const std::optional<unsigned>& max_datacarrier_bytes, TxoutType& whichType);
201 202 /** Get the vout index numbers of all dust outputs */
203 std::vector<uint32_t> GetDust(const CTransaction& tx, CFeeRate dust_relay_rate);
204 205 // Changing the default transaction version requires a two step process: first
206 // adapting relay policy by bumping TX_MAX_STANDARD_VERSION, and then later
207 // allowing the new transaction version in the wallet/RPC.
208 static constexpr decltype(CTransaction::version) TX_MAX_STANDARD_VERSION{3};
209 210 /**
211 * Check for standard transaction types
212 * @return True if all outputs (scriptPubKeys) use only standard transaction forms
213 */
214 bool IsStandardTx(const CTransaction& tx, const kernel::MemPoolOptions& opts, std::string& out_reason, const ignore_rejects_type& ignore_rejects=empty_ignore_rejects);
215 /**
216 * Check for standard transaction types
217 * @param[in] mapInputs Map of previous transactions that have outputs we're spending
218 * @return True if all inputs (scriptSigs) use only standard transaction forms
219 */
220 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs, const kernel::MemPoolOptions& opts, const std::string& reason_prefix, std::string& out_reason, const ignore_rejects_type& ignore_rejects=empty_ignore_rejects);
221 222 /**
223 * Check if the transaction is over standard P2WSH resources limit:
224 * 3600bytes witnessScript size, 80bytes per witness stack element, 100 witness stack elements
225 * These limits are adequate for multisignatures up to n-of-100 using OP_CHECKSIG, OP_ADD, and OP_EQUAL.
226 *
227 * Also enforce a maximum stack item size limit and no annexes for tapscript spends.
228 */
229 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs, const std::string& reason_prefix, std::string& out_reason, const ignore_rejects_type& ignore_rejects=empty_ignore_rejects, bool reject_p2sh_taproot=false);
230 /**
231 * Check whether this transaction spends any witness program but P2A, including not-yet-defined ones.
232 * May return `false` early for consensus-invalid transactions.
233 */
234 bool SpendsNonAnchorWitnessProg(const CTransaction& tx, const CCoinsViewCache& prevouts);
235 236 /** Compute the virtual transaction size (weight reinterpreted as bytes). */
237 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop);
238 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop);
239 int64_t GetVirtualTransactionInputSize(const CTxIn& tx, int64_t nSigOpCost, unsigned int bytes_per_sigop);
240 241 static inline int64_t GetVirtualTransactionSize(const CTransaction& tx)
242 {
243 return GetVirtualTransactionSize(tx, 0, 0);
244 }
245 246 static inline int64_t GetVirtualTransactionInputSize(const CTxIn& tx)
247 {
248 return GetVirtualTransactionInputSize(tx, 0, 0);
249 }
250 251 std::pair<CScript, unsigned int> GetScriptForTransactionInput(CScript prevScript, const CTxIn&);
252 253 std::pair<size_t, size_t> DatacarrierBytes(const CTransaction& tx, const CCoinsViewCache& view);
254 255 int32_t CalculateExtraTxWeight(const CTransaction& tx, const CCoinsViewCache& view, const unsigned int weight_per_data_byte);
256 257 #endif // LIMENKA_POLICY_POLICY_H
258