Browse Source
0.139238ecb
Policy: MOVEONLY: 3 functions to policy.o: (Luke Dashjr)627b9de
Policy: MOVEONLY: Create policy/policy.h with some constants (Jorge Timón)
Wladimir J. van der Laan
9 years ago
16 changed files with 250 additions and 214 deletions
@ -0,0 +1,178 @@
@@ -0,0 +1,178 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
// NOTE: This file is intended to be customised by the end user, and includes only local node policy logic
|
||||
|
||||
#include "policy/policy.h" |
||||
|
||||
#include "main.h" |
||||
#include "tinyformat.h" |
||||
#include "util.h" |
||||
#include "utilstrencodings.h" |
||||
|
||||
#include <boost/foreach.hpp> |
||||
|
||||
/**
|
||||
* Check transaction inputs to mitigate two |
||||
* potential denial-of-service attacks: |
||||
* |
||||
* 1. scriptSigs with extra data stuffed into them, |
||||
* not consumed by scriptPubKey (or P2SH script) |
||||
* 2. P2SH scripts with a crazy number of expensive |
||||
* CHECKSIG/CHECKMULTISIG operations |
||||
* |
||||
* Check transaction inputs, and make sure any |
||||
* pay-to-script-hash transactions are evaluating IsStandard scripts |
||||
* |
||||
* Why bother? To avoid denial-of-service attacks; an attacker |
||||
* can submit a standard HASH... OP_EQUAL transaction, |
||||
* which will get accepted into blocks. The redemption |
||||
* script can be anything; an attacker could use a very |
||||
* expensive-to-check-upon-redemption script like: |
||||
* DUP CHECKSIG DROP ... repeated 100 times... OP_1 |
||||
*/ |
||||
|
||||
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType) |
||||
{ |
||||
std::vector<std::vector<unsigned char> > vSolutions; |
||||
if (!Solver(scriptPubKey, whichType, vSolutions)) |
||||
return false; |
||||
|
||||
if (whichType == TX_MULTISIG) |
||||
{ |
||||
unsigned char m = vSolutions.front()[0]; |
||||
unsigned char n = vSolutions.back()[0]; |
||||
// Support up to x-of-3 multisig txns as standard
|
||||
if (n < 1 || n > 3) |
||||
return false; |
||||
if (m < 1 || m > n) |
||||
return false; |
||||
} |
||||
|
||||
return whichType != TX_NONSTANDARD; |
||||
} |
||||
|
||||
bool IsStandardTx(const CTransaction& tx, std::string& reason) |
||||
{ |
||||
if (tx.nVersion > CTransaction::CURRENT_VERSION || tx.nVersion < 1) { |
||||
reason = "version"; |
||||
return false; |
||||
} |
||||
|
||||
// Extremely large transactions with lots of inputs can cost the network
|
||||
// almost as much to process as they cost the sender in fees, because
|
||||
// computing signature hashes is O(ninputs*txsize). Limiting transactions
|
||||
// to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
|
||||
unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION); |
||||
if (sz >= MAX_STANDARD_TX_SIZE) { |
||||
reason = "tx-size"; |
||||
return false; |
||||
} |
||||
|
||||
BOOST_FOREACH(const CTxIn& txin, tx.vin) |
||||
{ |
||||
// Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
|
||||
// keys. (remember the 520 byte limit on redeemScript size) That works
|
||||
// out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
|
||||
// bytes of scriptSig, which we round off to 1650 bytes for some minor
|
||||
// future-proofing. That's also enough to spend a 20-of-20
|
||||
// CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
|
||||
// considered standard)
|
||||
if (txin.scriptSig.size() > 1650) { |
||||
reason = "scriptsig-size"; |
||||
return false; |
||||
} |
||||
if (!txin.scriptSig.IsPushOnly()) { |
||||
reason = "scriptsig-not-pushonly"; |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
unsigned int nDataOut = 0; |
||||
txnouttype whichType; |
||||
BOOST_FOREACH(const CTxOut& txout, tx.vout) { |
||||
if (!::IsStandard(txout.scriptPubKey, whichType)) { |
||||
reason = "scriptpubkey"; |
||||
return false; |
||||
} |
||||
|
||||
if (whichType == TX_NULL_DATA) |
||||
nDataOut++; |
||||
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) { |
||||
reason = "bare-multisig"; |
||||
return false; |
||||
} else if (txout.IsDust(::minRelayTxFee)) { |
||||
reason = "dust"; |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
// only one OP_RETURN txout is permitted
|
||||
if (nDataOut > 1) { |
||||
reason = "multi-op-return"; |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs) |
||||
{ |
||||
if (tx.IsCoinBase()) |
||||
return true; // Coinbases don't use vin normally
|
||||
|
||||
for (unsigned int i = 0; i < tx.vin.size(); i++) |
||||
{ |
||||
const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]); |
||||
|
||||
std::vector<std::vector<unsigned char> > vSolutions; |
||||
txnouttype whichType; |
||||
// get the scriptPubKey corresponding to this input:
|
||||
const CScript& prevScript = prev.scriptPubKey; |
||||
if (!Solver(prevScript, whichType, vSolutions)) |
||||
return false; |
||||
int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions); |
||||
if (nArgsExpected < 0) |
||||
return false; |
||||
|
||||
// Transactions with extra stuff in their scriptSigs are
|
||||
// non-standard. Note that this EvalScript() call will
|
||||
// be quick, because if there are any operations
|
||||
// beside "push data" in the scriptSig
|
||||
// IsStandardTx() will have already returned false
|
||||
// and this method isn't called.
|
||||
std::vector<std::vector<unsigned char> > stack; |
||||
if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker())) |
||||
return false; |
||||
|
||||
if (whichType == TX_SCRIPTHASH) |
||||
{ |
||||
if (stack.empty()) |
||||
return false; |
||||
CScript subscript(stack.back().begin(), stack.back().end()); |
||||
std::vector<std::vector<unsigned char> > vSolutions2; |
||||
txnouttype whichType2; |
||||
if (Solver(subscript, whichType2, vSolutions2)) |
||||
{ |
||||
int tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2); |
||||
if (tmpExpected < 0) |
||||
return false; |
||||
nArgsExpected += tmpExpected; |
||||
} |
||||
else |
||||
{ |
||||
// Any other Script with less than 15 sigops OK:
|
||||
unsigned int sigops = subscript.GetSigOpCount(true); |
||||
// ... extra data left on the stack after execution is OK, too:
|
||||
return (sigops <= MAX_P2SH_SIGOPS); |
||||
} |
||||
} |
||||
|
||||
if (stack.size() != (unsigned int)nArgsExpected) |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
} |
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
// Copyright (c) 2009-2014 The Bitcoin developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_POLICY_H |
||||
#define BITCOIN_POLICY_H |
||||
|
||||
#include "consensus/consensus.h" |
||||
#include "script/interpreter.h" |
||||
#include "script/standard.h" |
||||
|
||||
#include <string> |
||||
|
||||
class CCoinsViewCache; |
||||
|
||||
/** Default for -blockmaxsize and -blockminsize, which control the range of sizes the mining code will create **/ |
||||
static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 750000; |
||||
static const unsigned int DEFAULT_BLOCK_MIN_SIZE = 0; |
||||
/** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/ |
||||
static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 50000; |
||||
/** The maximum size for transactions we're willing to relay/mine */ |
||||
static const unsigned int MAX_STANDARD_TX_SIZE = 100000; |
||||
/** Maximum number of signature check operations in an IsStandard() P2SH script */ |
||||
static const unsigned int MAX_P2SH_SIGOPS = 15; |
||||
/** The maximum number of sigops we're willing to relay/mine in a single tx */ |
||||
static const unsigned int MAX_STANDARD_TX_SIGOPS = MAX_BLOCK_SIGOPS/5; |
||||
/**
|
||||
* Standard script verification flags that standard transactions will comply |
||||
* with. However scripts violating these flags may still be present in valid |
||||
* blocks and we must accept those blocks. |
||||
*/ |
||||
static const unsigned int STANDARD_SCRIPT_VERIFY_FLAGS = MANDATORY_SCRIPT_VERIFY_FLAGS | |
||||
SCRIPT_VERIFY_DERSIG | |
||||
SCRIPT_VERIFY_STRICTENC | |
||||
SCRIPT_VERIFY_MINIMALDATA | |
||||
SCRIPT_VERIFY_NULLDUMMY | |
||||
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS | |
||||
SCRIPT_VERIFY_CLEANSTACK | |
||||
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY; |
||||
|
||||
/** For convenience, standard but not mandatory verify flags. */ |
||||
static const unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS = STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS; |
||||
|
||||
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType); |
||||
/**
|
||||
* Check for standard transaction types |
||||
* @return True if all outputs (scriptPubKeys) use only standard transaction forms |
||||
*/ |
||||
bool IsStandardTx(const CTransaction& tx, std::string& reason); |
||||
/**
|
||||
* Check for standard transaction types |
||||
* @param[in] mapInputs Map of previous transactions that have outputs we're spending |
||||
* @return True if all inputs (scriptSigs) use only standard transaction forms |
||||
*/ |
||||
bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs); |
||||
|
||||
#endif // BITCOIN_POLICY_H
|
Loading…
Reference in new issue