Bitcoin ABC  0.24.7
P2P Digital Currency
policy.cpp
Go to the documentation of this file.
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.
5 
6 // NOTE: This file is intended to be customised by the end user, and includes
7 // only local node policy logic
8 
9 #include <coins.h>
10 #include <policy/policy.h>
11 #include <script/interpreter.h>
12 #include <util/system.h>
13 
14 Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn) {
22  if (txout.scriptPubKey.IsUnspendable()) {
23  return Amount::zero();
24  }
25 
26  size_t nSize = GetSerializeSize(txout);
27 
28  // the 148 mentioned above
29  nSize += (32 + 4 + 1 + 107 + 4);
30 
31  return 3 * dustRelayFeeIn.GetFee(nSize);
32 }
33 
34 bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn) {
35  return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
36 }
37 
38 bool IsStandard(const CScript &scriptPubKey, TxoutType &whichType) {
39  std::vector<std::vector<uint8_t>> vSolutions;
40  whichType = Solver(scriptPubKey, vSolutions);
41 
42  if (whichType == TxoutType::NONSTANDARD) {
43  return false;
44  } else if (whichType == TxoutType::MULTISIG) {
45  uint8_t m = vSolutions.front()[0];
46  uint8_t 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  }
51  if (m < 1 || m > n) {
52  return false;
53  }
54  } else if (whichType == TxoutType::NULL_DATA) {
55  if (!fAcceptDatacarrier) {
56  return false;
57  }
58 
59  unsigned nMaxDatacarrierBytes =
60  gArgs.GetArg("-datacarriersize", MAX_OP_RETURN_RELAY);
61  if (scriptPubKey.size() > nMaxDatacarrierBytes) {
62  return false;
63  }
64  }
65 
66  return true;
67 }
68 
69 bool IsStandardTx(const CTransaction &tx, bool permit_bare_multisig,
70  const CFeeRate &dust_relay_fee, std::string &reason) {
72  reason = "version";
73  return false;
74  }
75 
76  // Extremely large transactions with lots of inputs can cost the network
77  // almost as much to process as they cost the sender in fees, because
78  // computing signature hashes is O(ninputs*txsize). Limiting transactions
79  // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
80  uint32_t sz = tx.GetTotalSize();
81  if (sz > MAX_STANDARD_TX_SIZE) {
82  reason = "tx-size";
83  return false;
84  }
85 
86  for (const CTxIn &txin : tx.vin) {
88  reason = "scriptsig-size";
89  return false;
90  }
91  if (!txin.scriptSig.IsPushOnly()) {
92  reason = "scriptsig-not-pushonly";
93  return false;
94  }
95  }
96 
97  unsigned int nDataOut = 0;
98  TxoutType whichType;
99  for (const CTxOut &txout : tx.vout) {
100  if (!::IsStandard(txout.scriptPubKey, whichType)) {
101  reason = "scriptpubkey";
102  return false;
103  }
104 
105  if (whichType == TxoutType::NULL_DATA) {
106  nDataOut++;
107  } else if ((whichType == TxoutType::MULTISIG) &&
108  (!permit_bare_multisig)) {
109  reason = "bare-multisig";
110  return false;
111  } else if (IsDust(txout, dust_relay_fee)) {
112  reason = "dust";
113  return false;
114  }
115  }
116 
117  // only one OP_RETURN txout is permitted
118  if (nDataOut > 1) {
119  reason = "multi-op-return";
120  return false;
121  }
122 
123  return true;
124 }
125 
142 bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs,
143  uint32_t flags) {
144  if (tx.IsCoinBase()) {
145  // Coinbases don't use vin normally.
146  return true;
147  }
148 
149  for (const CTxIn &in : tx.vin) {
150  const CTxOut &prev = mapInputs.AccessCoin(in.prevout).GetTxOut();
151 
152  std::vector<std::vector<uint8_t>> vSolutions;
153  TxoutType whichType = Solver(prev.scriptPubKey, vSolutions);
154  if (whichType == TxoutType::NONSTANDARD) {
155  return false;
156  }
157  }
158 
159  return true;
160 }
161 
162 int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOpCount,
163  unsigned int bytes_per_sigop) {
164  return std::max(nSize, nSigOpCount * bytes_per_sigop);
165 }
166 
168  unsigned int bytes_per_sigop) {
170  nSigOpCount, bytes_per_sigop);
171 }
172 
173 int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCount,
174  unsigned int bytes_per_sigop) {
176  nSigOpCount, bytes_per_sigop);
177 }
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1182
CTxIn
An input of a transaction.
Definition: transaction.h:61
policy.h
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:210
CTxOut::nValue
Amount nValue
Definition: transaction.h:132
IsStandard
bool IsStandard(const CScript &scriptPubKey, TxoutType &whichType)
Definition: policy.cpp:38
TxoutType
TxoutType
Definition: standard.h:85
flags
int flags
Definition: bitcoin-tx.cpp:532
TxoutType::NONSTANDARD
@ NONSTANDARD
GetDustThreshold
Amount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:14
GetVirtualTransactionInputSize
int64_t GetVirtualTransactionInputSize(const CTxIn &txin, int64_t nSigOpCount, unsigned int bytes_per_sigop)
Definition: policy.cpp:173
AreInputsStandard
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs, uint32_t flags)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:142
IsStandardTx
bool IsStandardTx(const CTransaction &tx, bool permit_bare_multisig, const CFeeRate &dust_relay_fee, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:69
interpreter.h
CCoinsViewCache::AccessCoin
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or coinEmpty if not found.
Definition: coins.cpp:180
CFeeRate
Fee rate in satoshis per kilobyte: Amount / kB.
Definition: feerate.h:21
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
Amount::zero
static constexpr Amount zero()
Definition: amount.h:42
CScript::IsPushOnly
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:425
CTransaction::IsCoinBase
bool IsCoinBase() const
Definition: transaction.h:256
nSigOpCount
unsigned int nSigOpCount
Definition: mempool_eviction.cpp:15
CTxOut
An output of a transaction.
Definition: transaction.h:130
CTransaction::vout
const std::vector< CTxOut > vout
Definition: transaction.h:211
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:133
Solver
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:111
CFeeRate::GetFee
Amount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:51
MAX_TX_IN_SCRIPT_SIG_SIZE
static const unsigned int MAX_TX_IN_SCRIPT_SIG_SIZE
Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed keys (remember the 520 byte limit...
Definition: policy.h:43
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:498
CTransaction::GetTotalSize
unsigned int GetTotalSize() const
Get the total transaction size in bytes.
Definition: transaction.cpp:90
Amount
Definition: amount.h:19
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
CTransaction::MAX_STANDARD_VERSION
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:203
coins.h
TxoutType::NULL_DATA
@ NULL_DATA
system.h
MAX_OP_RETURN_RELAY
static const unsigned int MAX_OP_RETURN_RELAY
Default setting for nMaxDatacarrierBytes.
Definition: standard.h:77
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
gArgs
ArgsManager gArgs
Definition: system.cpp:75
CTxIn::prevout
COutPoint prevout
Definition: transaction.h:63
prevector::size
size_type size() const
Definition: prevector.h:386
MAX_STANDARD_TX_SIZE
static const unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we're willing to relay/mine.
Definition: policy.h:33
CTxIn::scriptSig
CScript scriptSig
Definition: transaction.h:64
TxoutType::MULTISIG
@ MULTISIG
IsDust
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:34
GetVirtualTransactionSize
int64_t GetVirtualTransactionSize(int64_t nSize, int64_t nSigOpCount, unsigned int bytes_per_sigop)
Compute the virtual transaction size (size, or more if sigops are too dense).
Definition: policy.cpp:162
CTransaction::nVersion
const int32_t nVersion
Definition: transaction.h:212
CScript::IsUnspendable
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:548
fAcceptDatacarrier
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:15
Coin::GetTxOut
CTxOut & GetTxOut()
Definition: coins.h:48
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11