Merge #8006: Qt: Add option to disable the system tray icon
[bitcoinplatinum.git] / src / policy / policy.cpp
blobd1a15451dc1f647bd6d2b22874243bd26cfe8f8c
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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.
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 "main.h"
11 #include "tinyformat.h"
12 #include "util.h"
13 #include "utilstrencodings.h"
15 #include <boost/foreach.hpp>
17 /**
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)
36 std::vector<std::vector<unsigned char> > vSolutions;
37 if (!Solver(scriptPubKey, whichType, vSolutions))
38 return false;
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
45 if (n < 1 || n > 3)
46 return false;
47 if (m < 1 || m > n)
48 return false;
49 } else if (whichType == TX_NULL_DATA &&
50 (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
51 return false;
53 return whichType != TX_NONSTANDARD;
56 bool IsStandardTx(const CTransaction& tx, std::string& reason)
58 if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
59 reason = "version";
60 return false;
63 // Extremely large transactions with lots of inputs can cost the network
64 // almost as much to process as they cost the sender in fees, because
65 // computing signature hashes is O(ninputs*txsize). Limiting transactions
66 // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
67 unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
68 if (sz >= MAX_STANDARD_TX_SIZE) {
69 reason = "tx-size";
70 return false;
73 BOOST_FOREACH(const CTxIn& txin, tx.vin)
75 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
76 // keys (remember the 520 byte limit on redeemScript size). That works
77 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
78 // bytes of scriptSig, which we round off to 1650 bytes for some minor
79 // future-proofing. That's also enough to spend a 20-of-20
80 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
81 // considered standard.
82 if (txin.scriptSig.size() > 1650) {
83 reason = "scriptsig-size";
84 return false;
86 if (!txin.scriptSig.IsPushOnly()) {
87 reason = "scriptsig-not-pushonly";
88 return false;
92 unsigned int nDataOut = 0;
93 txnouttype whichType;
94 BOOST_FOREACH(const CTxOut& txout, tx.vout) {
95 if (!::IsStandard(txout.scriptPubKey, whichType)) {
96 reason = "scriptpubkey";
97 return false;
100 if (whichType == TX_NULL_DATA)
101 nDataOut++;
102 else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
103 reason = "bare-multisig";
104 return false;
105 } else if (txout.IsDust(::minRelayTxFee)) {
106 reason = "dust";
107 return false;
111 // only one OP_RETURN txout is permitted
112 if (nDataOut > 1) {
113 reason = "multi-op-return";
114 return false;
117 return true;
120 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
122 if (tx.IsCoinBase())
123 return true; // Coinbases don't use vin normally
125 for (unsigned int i = 0; i < tx.vin.size(); i++)
127 const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
129 std::vector<std::vector<unsigned char> > vSolutions;
130 txnouttype whichType;
131 // get the scriptPubKey corresponding to this input:
132 const CScript& prevScript = prev.scriptPubKey;
133 if (!Solver(prevScript, whichType, vSolutions))
134 return false;
136 if (whichType == TX_SCRIPTHASH)
138 std::vector<std::vector<unsigned char> > stack;
139 // convert the scriptSig into a stack, so we can inspect the redeemScript
140 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), 0))
141 return false;
142 if (stack.empty())
143 return false;
144 CScript subscript(stack.back().begin(), stack.back().end());
145 if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
146 return false;
151 return true;