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 "validation.h"
11 #include "tinyformat.h"
13 #include "utilstrencodings.h"
15 #include <boost/foreach.hpp>
18 * Check transaction inputs to mitigate two
19 * potential denial-of-service attacks:
21 * 1. scriptSigs with extra data stuffed into them,
22 * not consumed by scriptPubKey (or P2SH script)
23 * 2. P2SH scripts with a crazy number of expensive
24 * CHECKSIG/CHECKMULTISIG operations
26 * Why bother? To avoid denial-of-service attacks; an attacker
27 * can submit a standard HASH... OP_EQUAL transaction,
28 * which will get accepted into blocks. The redemption
29 * script can be anything; an attacker could use a very
30 * expensive-to-check-upon-redemption script like:
31 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
34 bool IsStandard(const CScript
& scriptPubKey
, txnouttype
& whichType
, const bool witnessEnabled
)
36 std::vector
<std::vector
<unsigned char> > vSolutions
;
37 if (!Solver(scriptPubKey
, whichType
, vSolutions
))
40 if (whichType
== TX_MULTISIG
)
42 unsigned char m
= vSolutions
.front()[0];
43 unsigned char n
= vSolutions
.back()[0];
44 // Support up to x-of-3 multisig txns as standard
49 } else if (whichType
== TX_NULL_DATA
&&
50 (!fAcceptDatacarrier
|| scriptPubKey
.size() > nMaxDatacarrierBytes
))
53 else if (!witnessEnabled
&& (whichType
== TX_WITNESS_V0_KEYHASH
|| whichType
== TX_WITNESS_V0_SCRIPTHASH
))
56 return whichType
!= TX_NONSTANDARD
;
59 bool IsStandardTx(const CTransaction
& tx
, std::string
& reason
, const bool witnessEnabled
)
61 if (tx
.nVersion
> CTransaction::MAX_STANDARD_VERSION
|| tx
.nVersion
< 1) {
66 // Extremely large transactions with lots of inputs can cost the network
67 // almost as much to process as they cost the sender in fees, because
68 // computing signature hashes is O(ninputs*txsize). Limiting transactions
69 // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
70 unsigned int sz
= GetTransactionWeight(tx
);
71 if (sz
>= MAX_STANDARD_TX_WEIGHT
) {
76 BOOST_FOREACH(const CTxIn
& txin
, tx
.vin
)
78 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
79 // keys (remember the 520 byte limit on redeemScript size). That works
80 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
81 // bytes of scriptSig, which we round off to 1650 bytes for some minor
82 // future-proofing. That's also enough to spend a 20-of-20
83 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
84 // considered standard.
85 if (txin
.scriptSig
.size() > 1650) {
86 reason
= "scriptsig-size";
89 if (!txin
.scriptSig
.IsPushOnly()) {
90 reason
= "scriptsig-not-pushonly";
95 unsigned int nDataOut
= 0;
97 BOOST_FOREACH(const CTxOut
& txout
, tx
.vout
) {
98 if (!::IsStandard(txout
.scriptPubKey
, whichType
, witnessEnabled
)) {
99 reason
= "scriptpubkey";
103 if (whichType
== TX_NULL_DATA
)
105 else if ((whichType
== TX_MULTISIG
) && (!fIsBareMultisigStd
)) {
106 reason
= "bare-multisig";
108 } else if (txout
.IsDust(::minRelayTxFee
)) {
114 // only one OP_RETURN txout is permitted
116 reason
= "multi-op-return";
123 bool AreInputsStandard(const CTransaction
& tx
, const CCoinsViewCache
& mapInputs
)
126 return true; // Coinbases don't use vin normally
128 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
130 const CTxOut
& prev
= mapInputs
.GetOutputFor(tx
.vin
[i
]);
132 std::vector
<std::vector
<unsigned char> > vSolutions
;
133 txnouttype whichType
;
134 // get the scriptPubKey corresponding to this input:
135 const CScript
& prevScript
= prev
.scriptPubKey
;
136 if (!Solver(prevScript
, whichType
, vSolutions
))
139 if (whichType
== TX_SCRIPTHASH
)
141 std::vector
<std::vector
<unsigned char> > stack
;
142 // convert the scriptSig into a stack, so we can inspect the redeemScript
143 if (!EvalScript(stack
, tx
.vin
[i
].scriptSig
, SCRIPT_VERIFY_NONE
, BaseSignatureChecker(), SIGVERSION_BASE
))
147 CScript
subscript(stack
.back().begin(), stack
.back().end());
148 if (subscript
.GetSigOpCount(true) > MAX_P2SH_SIGOPS
) {
157 bool IsWitnessStandard(const CTransaction
& tx
, const CCoinsViewCache
& mapInputs
)
160 return true; // Coinbases are skipped
162 for (unsigned int i
= 0; i
< tx
.vin
.size(); i
++)
164 // We don't care if witness for this input is empty, since it must not be bloated.
165 // If the script is invalid without witness, it would be caught sooner or later during validation.
166 if (tx
.vin
[i
].scriptWitness
.IsNull())
169 const CTxOut
&prev
= mapInputs
.GetOutputFor(tx
.vin
[i
]);
171 // get the scriptPubKey corresponding to this input:
172 CScript prevScript
= prev
.scriptPubKey
;
174 if (prevScript
.IsPayToScriptHash()) {
175 std::vector
<std::vector
<unsigned char> > stack
;
176 // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
177 // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
178 // If the check fails at this stage, we know that this txid must be a bad one.
179 if (!EvalScript(stack
, tx
.vin
[i
].scriptSig
, SCRIPT_VERIFY_NONE
, BaseSignatureChecker(), SIGVERSION_BASE
))
183 prevScript
= CScript(stack
.back().begin(), stack
.back().end());
186 int witnessversion
= 0;
187 std::vector
<unsigned char> witnessprogram
;
189 // Non-witness program must not be associated with any witness
190 if (!prevScript
.IsWitnessProgram(witnessversion
, witnessprogram
))
193 // Check P2WSH standard limits
194 if (witnessversion
== 0 && witnessprogram
.size() == 32) {
195 if (tx
.vin
[i
].scriptWitness
.stack
.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE
)
197 size_t sizeWitnessStack
= tx
.vin
[i
].scriptWitness
.stack
.size() - 1;
198 if (sizeWitnessStack
> MAX_STANDARD_P2WSH_STACK_ITEMS
)
200 for (unsigned int j
= 0; j
< sizeWitnessStack
; j
++) {
201 if (tx
.vin
[i
].scriptWitness
.stack
[j
].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE
)
209 unsigned int nBytesPerSigOp
= DEFAULT_BYTES_PER_SIGOP
;
211 int64_t GetVirtualTransactionSize(int64_t nWeight
, int64_t nSigOpCost
)
213 return (std::max(nWeight
, nSigOpCost
* nBytesPerSigOp
) + WITNESS_SCALE_FACTOR
- 1) / WITNESS_SCALE_FACTOR
;
216 int64_t GetVirtualTransactionSize(const CTransaction
& tx
, int64_t nSigOpCost
)
218 return GetVirtualTransactionSize(GetTransactionWeight(tx
), nSigOpCost
);