Skip to content

Commit 9238ecb

Browse files
luke-jrjtimon
authored andcommitted
Policy: MOVEONLY: 3 functions to policy.o:
- [script/standard.o] IsStandard - [main.o] IsStandardTx - [main.o] AreInputsStandard Also, don't use namespace std in policy.cpp
1 parent 627b9de commit 9238ecb

File tree

9 files changed

+198
-184
lines changed

9 files changed

+198
-184
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ libbitcoin_server_a_SOURCES = \
177177
net.cpp \
178178
noui.cpp \
179179
policy/fees.cpp \
180+
policy/policy.cpp \
180181
pow.cpp \
181182
rest.cpp \
182183
rpcblockchain.cpp \

src/main.cpp

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -606,76 +606,6 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
606606
return nEvicted;
607607
}
608608

609-
610-
611-
612-
613-
614-
615-
bool IsStandardTx(const CTransaction& tx, string& reason)
616-
{
617-
if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) {
618-
reason = "version";
619-
return false;
620-
}
621-
622-
// Extremely large transactions with lots of inputs can cost the network
623-
// almost as much to process as they cost the sender in fees, because
624-
// computing signature hashes is O(ninputs*txsize). Limiting transactions
625-
// to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
626-
unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
627-
if (sz >= MAX_STANDARD_TX_SIZE) {
628-
reason = "tx-size";
629-
return false;
630-
}
631-
632-
BOOST_FOREACH(const CTxIn& txin, tx.vin)
633-
{
634-
// Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
635-
// keys. (remember the 520 byte limit on redeemScript size) That works
636-
// out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
637-
// bytes of scriptSig, which we round off to 1650 bytes for some minor
638-
// future-proofing. That's also enough to spend a 20-of-20
639-
// CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
640-
// considered standard)
641-
if (txin.scriptSig.size() > 1650) {
642-
reason = "scriptsig-size";
643-
return false;
644-
}
645-
if (!txin.scriptSig.IsPushOnly()) {
646-
reason = "scriptsig-not-pushonly";
647-
return false;
648-
}
649-
}
650-
651-
unsigned int nDataOut = 0;
652-
txnouttype whichType;
653-
BOOST_FOREACH(const CTxOut& txout, tx.vout) {
654-
if (!::IsStandard(txout.scriptPubKey, whichType)) {
655-
reason = "scriptpubkey";
656-
return false;
657-
}
658-
659-
if (whichType == TX_NULL_DATA)
660-
nDataOut++;
661-
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
662-
reason = "bare-multisig";
663-
return false;
664-
} else if (txout.IsDust(::minRelayTxFee)) {
665-
reason = "dust";
666-
return false;
667-
}
668-
}
669-
670-
// only one OP_RETURN txout is permitted
671-
if (nDataOut > 1) {
672-
reason = "multi-op-return";
673-
return false;
674-
}
675-
676-
return true;
677-
}
678-
679609
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
680610
{
681611
if (tx.nLockTime == 0)
@@ -694,74 +624,6 @@ bool CheckFinalTx(const CTransaction &tx)
694624
return IsFinalTx(tx, chainActive.Height() + 1, GetAdjustedTime());
695625
}
696626

697-
/**
698-
* Check transaction inputs to mitigate two
699-
* potential denial-of-service attacks:
700-
*
701-
* 1. scriptSigs with extra data stuffed into them,
702-
* not consumed by scriptPubKey (or P2SH script)
703-
* 2. P2SH scripts with a crazy number of expensive
704-
* CHECKSIG/CHECKMULTISIG operations
705-
*/
706-
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
707-
{
708-
if (tx.IsCoinBase())
709-
return true; // Coinbases don't use vin normally
710-
711-
for (unsigned int i = 0; i < tx.vin.size(); i++)
712-
{
713-
const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
714-
715-
vector<vector<unsigned char> > vSolutions;
716-
txnouttype whichType;
717-
// get the scriptPubKey corresponding to this input:
718-
const CScript& prevScript = prev.scriptPubKey;
719-
if (!Solver(prevScript, whichType, vSolutions))
720-
return false;
721-
int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions);
722-
if (nArgsExpected < 0)
723-
return false;
724-
725-
// Transactions with extra stuff in their scriptSigs are
726-
// non-standard. Note that this EvalScript() call will
727-
// be quick, because if there are any operations
728-
// beside "push data" in the scriptSig
729-
// IsStandardTx() will have already returned false
730-
// and this method isn't called.
731-
vector<vector<unsigned char> > stack;
732-
if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker()))
733-
return false;
734-
735-
if (whichType == TX_SCRIPTHASH)
736-
{
737-
if (stack.empty())
738-
return false;
739-
CScript subscript(stack.back().begin(), stack.back().end());
740-
vector<vector<unsigned char> > vSolutions2;
741-
txnouttype whichType2;
742-
if (Solver(subscript, whichType2, vSolutions2))
743-
{
744-
int tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2);
745-
if (tmpExpected < 0)
746-
return false;
747-
nArgsExpected += tmpExpected;
748-
}
749-
else
750-
{
751-
// Any other Script with less than 15 sigops OK:
752-
unsigned int sigops = subscript.GetSigOpCount(true);
753-
// ... extra data left on the stack after execution is OK, too:
754-
return (sigops <= MAX_P2SH_SIGOPS);
755-
}
756-
}
757-
758-
if (stack.size() != (unsigned int)nArgsExpected)
759-
return false;
760-
}
761-
762-
return true;
763-
}
764-
765627
unsigned int GetLegacySigOpCount(const CTransaction& tx)
766628
{
767629
unsigned int nSigOps = 0;

src/main.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,25 +261,6 @@ struct CDiskTxPos : public CDiskBlockPos
261261

262262
CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree);
263263

264-
/**
265-
* Check transaction inputs, and make sure any
266-
* pay-to-script-hash transactions are evaluating IsStandard scripts
267-
*
268-
* Why bother? To avoid denial-of-service attacks; an attacker
269-
* can submit a standard HASH... OP_EQUAL transaction,
270-
* which will get accepted into blocks. The redemption
271-
* script can be anything; an attacker could use a very
272-
* expensive-to-check-upon-redemption script like:
273-
* DUP CHECKSIG DROP ... repeated 100 times... OP_1
274-
*/
275-
276-
/**
277-
* Check for standard transaction types
278-
* @param[in] mapInputs Map of previous transactions that have outputs we're spending
279-
* @return True if all inputs (scriptSigs) use only standard transaction forms
280-
*/
281-
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs);
282-
283264
/**
284265
* Count ECDSA signature operations the old-fashioned (pre-0.6) way
285266
* @return number of sigops this transaction's outputs will produce when spent
@@ -311,11 +292,6 @@ void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCach
311292
/** Context-independent validity checks */
312293
bool CheckTransaction(const CTransaction& tx, CValidationState& state);
313294

314-
/** Check for standard transaction types
315-
* @return True if all outputs (scriptPubKeys) use only standard transaction forms
316-
*/
317-
bool IsStandardTx(const CTransaction& tx, std::string& reason);
318-
319295
/**
320296
* Check if transaction is final and can be included in a block with the
321297
* specified height and time. Consensus critical.

src/policy/policy.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2014 The Bitcoin 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+
// NOTE: This file is intended to be customised by the end user, and includes only local node policy logic
7+
8+
#include "policy/policy.h"
9+
10+
#include "main.h"
11+
#include "tinyformat.h"
12+
#include "util.h"
13+
#include "utilstrencodings.h"
14+
15+
#include <boost/foreach.hpp>
16+
17+
/**
18+
* Check transaction inputs to mitigate two
19+
* potential denial-of-service attacks:
20+
*
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
25+
*
26+
* Check transaction inputs, and make sure any
27+
* pay-to-script-hash transactions are evaluating IsStandard scripts
28+
*
29+
* Why bother? To avoid denial-of-service attacks; an attacker
30+
* can submit a standard HASH... OP_EQUAL transaction,
31+
* which will get accepted into blocks. The redemption
32+
* script can be anything; an attacker could use a very
33+
* expensive-to-check-upon-redemption script like:
34+
* DUP CHECKSIG DROP ... repeated 100 times... OP_1
35+
*/
36+
37+
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
38+
{
39+
std::vector<std::vector<unsigned char> > vSolutions;
40+
if (!Solver(scriptPubKey, whichType, vSolutions))
41+
return false;
42+
43+
if (whichType == TX_MULTISIG)
44+
{
45+
unsigned char m = vSolutions.front()[0];
46+
unsigned char n = vSolutions.back()[0];
47+
// Support up to x-of-3 multisig txns as standard
48+
if (n < 1 || n > 3)
49+
return false;
50+
if (m < 1 || m > n)
51+
return false;
52+
}
53+
54+
return whichType != TX_NONSTANDARD;
55+
}
56+
57+
bool IsStandardTx(const CTransaction& tx, std::string& reason)
58+
{
59+
if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) {
60+
reason = "version";
61+
return false;
62+
}
63+
64+
// Extremely large transactions with lots of inputs can cost the network
65+
// almost as much to process as they cost the sender in fees, because
66+
// computing signature hashes is O(ninputs*txsize). Limiting transactions
67+
// to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
68+
unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
69+
if (sz >= MAX_STANDARD_TX_SIZE) {
70+
reason = "tx-size";
71+
return false;
72+
}
73+
74+
BOOST_FOREACH(const CTxIn& txin, tx.vin)
75+
{
76+
// Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
77+
// keys. (remember the 520 byte limit on redeemScript size) That works
78+
// out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
79+
// bytes of scriptSig, which we round off to 1650 bytes for some minor
80+
// future-proofing. That's also enough to spend a 20-of-20
81+
// CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
82+
// considered standard)
83+
if (txin.scriptSig.size() > 1650) {
84+
reason = "scriptsig-size";
85+
return false;
86+
}
87+
if (!txin.scriptSig.IsPushOnly()) {
88+
reason = "scriptsig-not-pushonly";
89+
return false;
90+
}
91+
}
92+
93+
unsigned int nDataOut = 0;
94+
txnouttype whichType;
95+
BOOST_FOREACH(const CTxOut& txout, tx.vout) {
96+
if (!::IsStandard(txout.scriptPubKey, whichType)) {
97+
reason = "scriptpubkey";
98+
return false;
99+
}
100+
101+
if (whichType == TX_NULL_DATA)
102+
nDataOut++;
103+
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
104+
reason = "bare-multisig";
105+
return false;
106+
} else if (txout.IsDust(::minRelayTxFee)) {
107+
reason = "dust";
108+
return false;
109+
}
110+
}
111+
112+
// only one OP_RETURN txout is permitted
113+
if (nDataOut > 1) {
114+
reason = "multi-op-return";
115+
return false;
116+
}
117+
118+
return true;
119+
}
120+
121+
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
122+
{
123+
if (tx.IsCoinBase())
124+
return true; // Coinbases don't use vin normally
125+
126+
for (unsigned int i = 0; i < tx.vin.size(); i++)
127+
{
128+
const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
129+
130+
std::vector<std::vector<unsigned char> > vSolutions;
131+
txnouttype whichType;
132+
// get the scriptPubKey corresponding to this input:
133+
const CScript& prevScript = prev.scriptPubKey;
134+
if (!Solver(prevScript, whichType, vSolutions))
135+
return false;
136+
int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions);
137+
if (nArgsExpected < 0)
138+
return false;
139+
140+
// Transactions with extra stuff in their scriptSigs are
141+
// non-standard. Note that this EvalScript() call will
142+
// be quick, because if there are any operations
143+
// beside "push data" in the scriptSig
144+
// IsStandardTx() will have already returned false
145+
// and this method isn't called.
146+
std::vector<std::vector<unsigned char> > stack;
147+
if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker()))
148+
return false;
149+
150+
if (whichType == TX_SCRIPTHASH)
151+
{
152+
if (stack.empty())
153+
return false;
154+
CScript subscript(stack.back().begin(), stack.back().end());
155+
std::vector<std::vector<unsigned char> > vSolutions2;
156+
txnouttype whichType2;
157+
if (Solver(subscript, whichType2, vSolutions2))
158+
{
159+
int tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2);
160+
if (tmpExpected < 0)
161+
return false;
162+
nArgsExpected += tmpExpected;
163+
}
164+
else
165+
{
166+
// Any other Script with less than 15 sigops OK:
167+
unsigned int sigops = subscript.GetSigOpCount(true);
168+
// ... extra data left on the stack after execution is OK, too:
169+
return (sigops <= MAX_P2SH_SIGOPS);
170+
}
171+
}
172+
173+
if (stack.size() != (unsigned int)nArgsExpected)
174+
return false;
175+
}
176+
177+
return true;
178+
}

0 commit comments

Comments
 (0)