2014-10-12 00:41:05 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2016-11-06 18:12:50 +01:00
|
|
|
// Copyright (c) 2009-2016 The Bitcoin Core developers
|
2014-10-12 00:41:05 +02:00
|
|
|
// 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"
|
|
|
|
|
2017-06-17 02:18:42 +02:00
|
|
|
#include "consensus/validation.h"
|
2016-12-02 01:06:41 +01:00
|
|
|
#include "validation.h"
|
2017-04-07 04:04:19 +02:00
|
|
|
#include "coins.h"
|
2014-10-12 00:41:05 +02:00
|
|
|
#include "tinyformat.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "utilstrencodings.h"
|
|
|
|
|
|
|
|
|
2017-05-19 10:40:05 +02:00
|
|
|
CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
|
2016-10-14 17:49:05 +02:00
|
|
|
{
|
|
|
|
// "Dust" is defined in terms of dustRelayFee,
|
|
|
|
// which has units satoshis-per-kilobyte.
|
2017-07-17 12:52:09 +02:00
|
|
|
// If you'd pay more in fees than the value of the output
|
2016-10-14 17:49:05 +02:00
|
|
|
// to spend something, then we consider it dust.
|
|
|
|
// A typical spendable non-segwit txout is 34 bytes big, and will
|
|
|
|
// need a CTxIn of at least 148 bytes to spend:
|
|
|
|
// so dust is a spendable txout less than
|
2017-07-17 12:52:09 +02:00
|
|
|
// 182*dustRelayFee/1000 (in satoshis).
|
|
|
|
// 546 satoshis at the default rate of 3000 sat/kB.
|
2016-10-14 17:49:05 +02:00
|
|
|
// A typical spendable segwit txout is 31 bytes big, and will
|
|
|
|
// need a CTxIn of at least 67 bytes to spend:
|
|
|
|
// so dust is a spendable txout less than
|
2017-07-17 12:52:09 +02:00
|
|
|
// 98*dustRelayFee/1000 (in satoshis).
|
|
|
|
// 294 satoshis at the default rate of 3000 sat/kB.
|
2016-10-14 17:49:05 +02:00
|
|
|
if (txout.scriptPubKey.IsUnspendable())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
|
|
|
|
int witnessversion = 0;
|
|
|
|
std::vector<unsigned char> witnessprogram;
|
|
|
|
|
|
|
|
if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
|
|
|
|
// sum the sizes of the parts of a transaction input
|
|
|
|
// with 75% segwit discount applied to the script size.
|
|
|
|
nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
|
|
|
|
} else {
|
|
|
|
nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:52:09 +02:00
|
|
|
return dustRelayFeeIn.GetFee(nSize);
|
2016-10-14 17:49:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 10:40:05 +02:00
|
|
|
bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
|
2016-10-14 17:49:05 +02:00
|
|
|
{
|
2017-05-19 10:40:05 +02:00
|
|
|
return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
|
2016-10-14 17:49:05 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 00:41:05 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2016-07-20 12:31:45 +02:00
|
|
|
bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled)
|
2014-10-12 00:41:05 +02:00
|
|
|
{
|
|
|
|
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;
|
2014-10-13 16:18:05 +02:00
|
|
|
} else if (whichType == TX_NULL_DATA &&
|
2015-06-27 21:21:41 +02:00
|
|
|
(!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
|
2014-10-13 16:18:05 +02:00
|
|
|
return false;
|
2014-10-12 00:41:05 +02:00
|
|
|
|
2016-07-20 12:31:45 +02:00
|
|
|
else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
|
|
|
|
return false;
|
2014-10-12 00:41:05 +02:00
|
|
|
|
|
|
|
return whichType != TX_NONSTANDARD;
|
|
|
|
}
|
|
|
|
|
2016-07-20 12:31:45 +02:00
|
|
|
bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
|
2014-10-12 00:41:05 +02:00
|
|
|
{
|
2016-02-19 20:52:31 +01:00
|
|
|
if (tx.nVersion > CTransaction::MAX_STANDARD_VERSION || tx.nVersion < 1) {
|
2014-10-12 00:41:05 +02:00
|
|
|
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
|
2016-11-14 07:38:07 +01:00
|
|
|
// to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
|
2016-07-18 19:28:26 +02:00
|
|
|
unsigned int sz = GetTransactionWeight(tx);
|
|
|
|
if (sz >= MAX_STANDARD_TX_WEIGHT) {
|
2014-10-12 00:41:05 +02:00
|
|
|
reason = "tx-size";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CTxIn& txin : tx.vin)
|
2014-10-12 00:41:05 +02:00
|
|
|
{
|
|
|
|
// Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
|
2016-04-01 23:44:26 +02:00
|
|
|
// keys (remember the 520 byte limit on redeemScript size). That works
|
2014-10-12 00:41:05 +02:00
|
|
|
// 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
|
2016-04-01 23:44:26 +02:00
|
|
|
// considered standard.
|
2014-10-12 00:41:05 +02:00
|
|
|
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;
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CTxOut& txout : tx.vout) {
|
2016-07-20 12:31:45 +02:00
|
|
|
if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
|
2014-10-12 00:41:05 +02:00
|
|
|
reason = "scriptpubkey";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (whichType == TX_NULL_DATA)
|
|
|
|
nDataOut++;
|
|
|
|
else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
|
|
|
|
reason = "bare-multisig";
|
|
|
|
return false;
|
2016-10-14 17:49:05 +02:00
|
|
|
} else if (IsDust(txout, ::dustRelayFee)) {
|
2014-10-12 00:41:05 +02:00
|
|
|
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++)
|
|
|
|
{
|
2017-04-27 01:09:27 +02:00
|
|
|
const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
|
2014-10-12 00:41:05 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (whichType == TX_SCRIPTHASH)
|
|
|
|
{
|
2016-01-21 13:15:19 +01:00
|
|
|
std::vector<std::vector<unsigned char> > stack;
|
|
|
|
// convert the scriptSig into a stack, so we can inspect the redeemScript
|
2015-12-27 19:49:08 +01:00
|
|
|
if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
|
2016-01-21 13:15:19 +01:00
|
|
|
return false;
|
2014-10-12 00:41:05 +02:00
|
|
|
if (stack.empty())
|
|
|
|
return false;
|
|
|
|
CScript subscript(stack.back().begin(), stack.back().end());
|
2016-01-21 13:15:19 +01:00
|
|
|
if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
|
|
|
|
return false;
|
2014-10-12 00:41:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-03 18:54:50 +01:00
|
|
|
|
2016-10-16 17:53:16 +02:00
|
|
|
bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
|
|
|
|
{
|
|
|
|
if (tx.IsCoinBase())
|
|
|
|
return true; // Coinbases are skipped
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < tx.vin.size(); i++)
|
|
|
|
{
|
|
|
|
// We don't care if witness for this input is empty, since it must not be bloated.
|
|
|
|
// If the script is invalid without witness, it would be caught sooner or later during validation.
|
2016-08-04 02:49:16 +02:00
|
|
|
if (tx.vin[i].scriptWitness.IsNull())
|
2016-10-16 17:53:16 +02:00
|
|
|
continue;
|
|
|
|
|
2017-04-27 01:09:27 +02:00
|
|
|
const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
|
2016-10-16 17:53:16 +02:00
|
|
|
|
|
|
|
// get the scriptPubKey corresponding to this input:
|
|
|
|
CScript prevScript = prev.scriptPubKey;
|
|
|
|
|
|
|
|
if (prevScript.IsPayToScriptHash()) {
|
|
|
|
std::vector <std::vector<unsigned char> > stack;
|
|
|
|
// If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
|
|
|
|
// into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
|
|
|
|
// If the check fails at this stage, we know that this txid must be a bad one.
|
|
|
|
if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
|
|
|
|
return false;
|
|
|
|
if (stack.empty())
|
|
|
|
return false;
|
|
|
|
prevScript = CScript(stack.back().begin(), stack.back().end());
|
|
|
|
}
|
|
|
|
|
|
|
|
int witnessversion = 0;
|
|
|
|
std::vector<unsigned char> witnessprogram;
|
|
|
|
|
|
|
|
// Non-witness program must not be associated with any witness
|
|
|
|
if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check P2WSH standard limits
|
|
|
|
if (witnessversion == 0 && witnessprogram.size() == 32) {
|
2016-08-04 02:49:16 +02:00
|
|
|
if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
|
2016-10-16 17:53:16 +02:00
|
|
|
return false;
|
2016-08-04 02:49:16 +02:00
|
|
|
size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
|
2016-10-16 17:53:16 +02:00
|
|
|
if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
|
|
|
|
return false;
|
|
|
|
for (unsigned int j = 0; j < sizeWitnessStack; j++) {
|
2016-08-04 02:49:16 +02:00
|
|
|
if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
|
2016-10-16 17:53:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:43:35 +01:00
|
|
|
CFeeRate incrementalRelayFee = CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE);
|
2016-12-13 22:19:17 +01:00
|
|
|
CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
|
2016-07-18 20:57:20 +02:00
|
|
|
unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
|
|
|
|
|
|
|
|
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
|
2016-01-03 18:54:50 +01:00
|
|
|
{
|
2016-07-18 20:57:20 +02:00
|
|
|
return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
|
2016-01-03 18:54:50 +01:00
|
|
|
}
|
|
|
|
|
2016-07-18 20:57:20 +02:00
|
|
|
int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
|
2016-01-03 18:54:50 +01:00
|
|
|
{
|
2016-07-18 20:57:20 +02:00
|
|
|
return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);
|
2016-01-03 18:54:50 +01:00
|
|
|
}
|