1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 // NOTE: This file is intended to be customised by the end user, and includes only local node policy logic
8 #include "policy/policy.h"
10 #include "consensus/validation.h"
11 #include "validation.h"
13 #include "tinyformat.h"
15 #include "utilstrencodings.h"
18 CAmount
GetDustThreshold(const CTxOut
& txout
, const CFeeRate
& dustRelayFeeIn
)
20 // "Dust" is defined in terms of dustRelayFee,
21 // which has units satoshis-per-kilobyte.
22 // If you'd pay more in fees than the value of the output
23 // to spend something, then we consider it dust.
24 // A typical spendable non-segwit txout is 34 bytes big, and will
25 // need a CTxIn of at least 148 bytes to spend:
26 // so dust is a spendable txout less than
27 // 182*dustRelayFee/1000 (in satoshis).
28 // 546 satoshis at the default rate of 3000 sat/kB.
29 // A typical spendable segwit txout is 31 bytes big, and will
30 // need a CTxIn of at least 67 bytes to spend:
31 // so dust is a spendable txout less than
32 // 98*dustRelayFee/1000 (in satoshis).
33 // 294 satoshis at the default rate of 3000 sat/kB.
34 if (txout
.scriptPubKey
.IsUnspendable())
37 size_t nSize
= GetSerializeSize(txout
, SER_DISK
, 0);
38 int witnessversion
= 0;
39 std::vector
<unsigned char> witnessprogram
;
41 if (txout
.scriptPubKey
.IsWitnessProgram(witnessversion
, witnessprogram
)) {
42 // sum the sizes of the parts of a transaction input
43 // with 75% segwit discount applied to the script size.
44 nSize
+= (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR
) + 4);
46 nSize
+= (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
49 return dustRelayFeeIn
.GetFee(nSize
);
52 bool IsDust(const CTxOut
& txout
, const CFeeRate
& dustRelayFeeIn
)
54 return (txout
.nValue
< GetDustThreshold(txout
, dustRelayFeeIn
));
58 * Check transaction inputs to mitigate two
59 * potential denial-of-service attacks:
61 * 1. scriptSigs with extra data stuffed into them,
62 * not consumed by scriptPubKey (or P2SH script)
63 * 2. P2SH scripts with a crazy number of expensive
64 * CHECKSIG/CHECKMULTISIG operations
66 * Why bother? To avoid denial-of-service attacks; an attacker
67 * can submit a standard HASH... OP_EQUAL transaction,
68 * which will get accepted into blocks. The redemption
69 * script can be anything; an attacker could use a very
70 * expensive-to-check-upon-redemption script like:
71 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
74 bool IsStandard(const CScript
& scriptPubKey
, txnouttype
& whichType
, const bool witnessEnabled
)
76 std::vector
<std::vector
<unsigned char> > vSolutions
;
77 if (!Solver(scriptPubKey
, whichType
, vSolutions
))
80 if (whichType
== TX_MULTISIG
)
82 unsigned char m
= vSolutions
.front()[0];
83 unsigned char n
= vSolutions
.back()[0];
84 // Support up to x-of-3 multisig txns as standard
89 } else if (whichType
== TX_NULL_DATA
&&
90 (!fAcceptDatacarrier
|| scriptPubKey
.size() > nMaxDatacarrierBytes
))
93 else if (!witnessEnabled
&& (whichType
== TX_WITNESS_V0_KEYHASH
|| whichType
== TX_WITNESS_V0_SCRIPTHASH
))
96 return whichType
!= TX_NONSTANDARD
;
99 bool IsStandardTx(const CTransaction
& tx
, std::string
& reason
, const bool witnessEnabled
)
101 if (tx
.nVersion
> CTransaction::MAX_STANDARD_VERSION
|| tx
.nVersion
< 1) {
106 // Extremely large transactions with lots of inputs can cost the network
107 // almost as much to process as they cost the sender in fees, because
108 // computing signature hashes is O(ninputs*txsize). Limiting transactions
109 // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
110 unsigned int sz
= GetTransactionWeight(tx
);
111 if (sz
>= MAX_STANDARD_TX_WEIGHT
) {
116 for (const CTxIn
& txin
: tx
.vin
)
118 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
119 // keys (remember the 520 byte limit on redeemScript size). That works
120 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
121 // bytes of scriptSig, which we round off to 1650 bytes for some minor
122 // future-proofing. That's also enough to spend a 20-of-20
123 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
124 // considered standard.
125 if (txin
.scriptSig
.size() > 1650) {
126 reason
= "scriptsig-size";
129 if (!txin
.scriptSig
.IsPushOnly()) {
130 reason
= "scriptsig-not-pushonly";
135 unsigned int nDataOut
= 0;
136 txnouttype whichType
;
137 for (const CTxOut
& txout
: tx
.vout
) {
138 if (!::IsStandard(txout
.scriptPubKey
, whichType
, witnessEnabled
)) {
139 reason
= "scriptpubkey";
143 if (whichType
== TX_NULL_DATA
)
145 else if ((whichType
== TX_MULTISIG
) && (!fIsBareMultisigStd
)) {
146 reason
= "bare-multisig";
148 } else if (IsDust(txout
, ::dustRelayFee
)) {
154 // only one OP_RETURN txout is permitted
156 reason
= "multi-op-return";
163 bool AreInputsStandard(const CTransaction
& tx
, const CCoinsViewCache
& mapInputs
)
166 return true; // Coinbases don't use vin normally
168 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
170 const CTxOut
& prev
= mapInputs
.AccessCoin(tx
.vin
[i
].prevout
).out
;
172 std::vector
<std::vector
<unsigned char> > vSolutions
;
173 txnouttype whichType
;
174 // get the scriptPubKey corresponding to this input:
175 const CScript
& prevScript
= prev
.scriptPubKey
;
176 if (!Solver(prevScript
, whichType
, vSolutions
))
179 if (whichType
== TX_SCRIPTHASH
)
181 std::vector
<std::vector
<unsigned char> > stack
;
182 // convert the scriptSig into a stack, so we can inspect the redeemScript
183 if (!EvalScript(stack
, tx
.vin
[i
].scriptSig
, SCRIPT_VERIFY_NONE
, BaseSignatureChecker(), SIGVERSION_BASE
))
187 CScript
subscript(stack
.back().begin(), stack
.back().end());
188 if (subscript
.GetSigOpCount(true) > MAX_P2SH_SIGOPS
) {
197 bool IsWitnessStandard(const CTransaction
& tx
, const CCoinsViewCache
& mapInputs
)
200 return true; // Coinbases are skipped
202 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
204 // We don't care if witness for this input is empty, since it must not be bloated.
205 // If the script is invalid without witness, it would be caught sooner or later during validation.
206 if (tx
.vin
[i
].scriptWitness
.IsNull())
209 const CTxOut
&prev
= mapInputs
.AccessCoin(tx
.vin
[i
].prevout
).out
;
211 // get the scriptPubKey corresponding to this input:
212 CScript prevScript
= prev
.scriptPubKey
;
214 if (prevScript
.IsPayToScriptHash()) {
215 std::vector
<std::vector
<unsigned char> > stack
;
216 // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
217 // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
218 // If the check fails at this stage, we know that this txid must be a bad one.
219 if (!EvalScript(stack
, tx
.vin
[i
].scriptSig
, SCRIPT_VERIFY_NONE
, BaseSignatureChecker(), SIGVERSION_BASE
))
223 prevScript
= CScript(stack
.back().begin(), stack
.back().end());
226 int witnessversion
= 0;
227 std::vector
<unsigned char> witnessprogram
;
229 // Non-witness program must not be associated with any witness
230 if (!prevScript
.IsWitnessProgram(witnessversion
, witnessprogram
))
233 // Check P2WSH standard limits
234 if (witnessversion
== 0 && witnessprogram
.size() == 32) {
235 if (tx
.vin
[i
].scriptWitness
.stack
.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE
)
237 size_t sizeWitnessStack
= tx
.vin
[i
].scriptWitness
.stack
.size() - 1;
238 if (sizeWitnessStack
> MAX_STANDARD_P2WSH_STACK_ITEMS
)
240 for (unsigned int j
= 0; j
< sizeWitnessStack
; j
++) {
241 if (tx
.vin
[i
].scriptWitness
.stack
[j
].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE
)
249 CFeeRate incrementalRelayFee
= CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE
);
250 CFeeRate dustRelayFee
= CFeeRate(DUST_RELAY_TX_FEE
);
251 unsigned int nBytesPerSigOp
= DEFAULT_BYTES_PER_SIGOP
;
253 int64_t GetVirtualTransactionSize(int64_t nWeight
, int64_t nSigOpCost
)
255 return (std::max(nWeight
, nSigOpCost
* nBytesPerSigOp
) + WITNESS_SCALE_FACTOR
- 1) / WITNESS_SCALE_FACTOR
;
258 int64_t GetVirtualTransactionSize(const CTransaction
& tx
, int64_t nSigOpCost
)
260 return GetVirtualTransactionSize(GetTransactionWeight(tx
), nSigOpCost
);