PIVX Core  5.6.99
P2P Digital Currency
policy.cpp
Go to the documentation of this file.
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 "consensus/tx_verify.h" // for IsFinal()
11 #include "tinyformat.h"
12 #include "util/system.h"
13 #include "utilstrencodings.h"
14 #include "validation.h"
15 
16 
17 bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
18 
19 CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
20 
21 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
22 {
23  // "Dust" is defined in terms of dustRelayFee,
24  // which has units satoshis-per-kilobyte.
25  // If you'd pay more in fees than the value of the output
26  // to spend something, then we consider it dust.
27  // A typical spendable txout is 34 bytes big, and will
28  // need a CTxIn of at least 148 bytes to spend:
29  // so dust is a spendable txout less than
30  // 546*dustRelayFee/1000 (in satoshis).
31  if (txout.scriptPubKey.IsUnspendable())
32  return 0;
33 
34  size_t nSize = GetSerializeSize(txout, 0);
35  nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
36  return dustRelayFeeIn.GetFee(nSize);
37 }
38 
39 CAmount GetDustThreshold(const CFeeRate& dustRelayFeeIn)
40 {
41  // return the dust threshold for a typical 34 bytes output
42  return dustRelayFeeIn.GetFee(182);
43 }
44 
45 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
46 {
47  return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
48 }
49 
51 {
52  unsigned int K = DEFAULT_SHIELDEDTXFEE_K; // Fixed (100) for now
53  return K * dustRelayFeeIn.GetFee(SPENDDESCRIPTION_SIZE +
56 }
57 
78 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
79 {
80  std::vector<valtype> vSolutions;
81  if (!Solver(scriptPubKey, whichType, vSolutions))
82  return false;
83 
84  if (whichType == TX_MULTISIG)
85  {
86  unsigned char m = vSolutions.front()[0];
87  unsigned char n = vSolutions.back()[0];
88  // Support up to x-of-3 multisig txns as standard
89  if (n < 1 || n > 3)
90  return false;
91  if (m < 1 || m > n)
92  return false;
93  } else if (whichType == TX_NULL_DATA &&
94  (!gArgs.GetBoolArg("-datacarrier", DEFAULT_ACCEPT_DATACARRIER) || scriptPubKey.size() > nMaxDatacarrierBytes))
95  return false;
96 
97  return whichType != TX_NONSTANDARD;
98 }
99 
100 bool IsStandardTx(const CTransactionRef& tx, int nBlockHeight, std::string& reason)
101 {
103  if (!Params().GetConsensus().NetworkUpgradeActive(nBlockHeight, Consensus::UPGRADE_V5_0)) {
104  // Before v5, all txes with version other than STANDARD_VERSION (1) are considered non-standard
105  if (tx->nVersion != CTransaction::TxVersion::LEGACY) {
106  reason = "version";
107  return false;
108  }
109  }
110  // After v5, all txes with a version number accepted by consensus are considered standard.
111 
112  // Treat non-final transactions as non-standard to prevent a specific type
113  // of double-spend attack, as well as DoS attacks. (if the transaction
114  // can't be mined, the attacker isn't expending resources broadcasting it)
115  // Basically we don't want to propagate transactions that can't be included in
116  // the next block.
117  //
118  // However, IsFinalTx() is confusing... Without arguments, it uses
119  // chainActive.Height() to evaluate nLockTime; when a block is accepted, chainActive.Height()
120  // is set to the value of nHeight in the block. However, when IsFinalTx()
121  // is called within CBlock::AcceptBlock(), the height of the block *being*
122  // evaluated is what is used. Thus if we want to know if a transaction can
123  // be part of the *next* block, we need to call IsFinalTx() with one more
124  // than chainActive.Height().
125  //
126  // Timestamps on the other hand don't get any special treatment, because we
127  // can't know what timestamp the next block will have, and there aren't
128  // timestamp applications where it matters.
129  if (!IsFinalTx(tx, nBlockHeight)) {
130  reason = "non-final";
131  return false;
132  }
133 
134  // Extremely large transactions with lots of inputs can cost the network
135  // almost as much to process as they cost the sender in fees, because
136  // computing signature hashes is O(ninputs*txsize). Limiting transactions
137  // to MAX_STANDARD_TX_SIZE mitigates CPU exhaustion attacks.
138  unsigned int sz = tx->GetTotalSize();
139  unsigned int nMaxSize = tx->IsShieldedTx() ? MAX_TX_SIZE_AFTER_SAPLING : MAX_STANDARD_TX_SIZE;
140  if (sz >= nMaxSize) {
141  reason = "tx-size";
142  return false;
143  }
144 
145  for (const CTxIn& txin : tx->vin) {
146  // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
147  // keys. (remember the 520 byte limit on redeemScript size) That works
148  // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
149  // bytes of scriptSig, which we round off to 1650 bytes for some minor
150  // future-proofing. That's also enough to spend a 20-of-20
151  // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
152  // considered standard)
153  if (txin.scriptSig.size() > 1650) {
154  reason = "scriptsig-size";
155  return false;
156  }
157  if (!txin.scriptSig.IsPushOnly()) {
158  reason = "scriptsig-not-pushonly";
159  return false;
160  }
161  }
162 
163  unsigned int nDataOut = 0;
164  txnouttype whichType;
165  for (const CTxOut& txout : tx->vout) {
166  if (!::IsStandard(txout.scriptPubKey, whichType)) {
167  reason = "scriptpubkey";
168  return false;
169  }
170 
171  if (whichType == TX_NULL_DATA)
172  nDataOut++;
173  else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
174  reason = "bare-multisig";
175  return false;
176  } else if (IsDust(txout, dustRelayFee)) {
177  reason = "dust";
178  return false;
179  }
180  }
181 
182  // only one OP_RETURN txout is permitted
183  if (nDataOut > 1) {
184  reason = "multi-op-return";
185  return false;
186  }
187 
188  return true;
189 }
190 
191 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
192 {
193  if (tx.IsCoinBase()) {
194  return true; // coinbases don't use vin normally
195  }
196 
197  for (unsigned int i = 0; i < tx.vin.size(); i++) {
198  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
199 
200  std::vector<std::vector<unsigned char> > vSolutions;
201  txnouttype whichType;
202  // get the scriptPubKey corresponding to this input:
203  const CScript& prevScript = prev.scriptPubKey;
204  if (!Solver(prevScript, whichType, vSolutions))
205  return false;
206 
207  if (whichType == TX_SCRIPTHASH)
208  {
209  std::vector<std::vector<unsigned char> > stack;
210  // convert the scriptSig into a stack, so we can inspect the redeemScript
211  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), tx.GetRequiredSigVersion()))
212  return false;
213  if (stack.empty())
214  return false;
215  CScript subscript(stack.back().begin(), stack.back().end());
216  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
217  return false;
218  }
219  }
220  }
221 
222  return true;
223 }
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
const CChainParams & Params()
Return the currently selected parameters.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:465
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:283
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to a Coin in the cache, or a pruned one if not found.
Definition: coins.cpp:151
Fee rate in PIV per kilobyte: CAmount / kB.
Definition: feerate.h:20
CAmount GetFee(size_t size) const
Definition: feerate.cpp:21
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:285
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
Definition: script.h:654
unsigned int GetSigOpCount(bool fAccurate) const
Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs as 20 sigops.
Definition: script.cpp:164
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
std::vector< CTxIn > vin
Definition: transaction.h:270
SigVersion GetRequiredSigVersion() const
Definition: transaction.h:347
bool IsCoinBase() const
Definition: transaction.h:376
An input of a transaction.
Definition: transaction.h:94
CScript scriptSig
Definition: transaction.h:97
An output of a transaction.
Definition: transaction.h:137
CScript scriptPubKey
Definition: transaction.h:140
CAmount nValue
Definition: transaction.h:139
CTxOut out
unspent transaction output
Definition: coins.h:41
size_type size() const
Definition: prevector.h:277
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
@ SCRIPT_VERIFY_NONE
Definition: interpreter.h:33
@ UPGRADE_V5_0
Definition: params.h:36
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:80
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check for standard transaction types.
Definition: policy.cpp:191
CAmount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:21
bool IsStandardTx(const CTransactionRef &tx, int nBlockHeight, std::string &reason)
Check for standard transaction types.
Definition: policy.cpp:100
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:45
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:78
CAmount GetShieldedDustThreshold(const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:50
bool fIsBareMultisigStd
Definition: policy.cpp:17
CFeeRate dustRelayFee
Definition: policy.cpp:19
#define BINDINGSIG_SIZE
#define SPENDDESCRIPTION_SIZE
#define CTXOUT_REGULAR_SIZE
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
Definition: serialize.h:847
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
Definition: standard.cpp:90
unsigned nMaxDatacarrierBytes
Definition: standard.cpp:14
txnouttype
Definition: standard.h:46
@ TX_NULL_DATA
Definition: standard.h:53
@ TX_SCRIPTHASH
Definition: standard.h:51
@ TX_NONSTANDARD
Definition: standard.h:47
@ TX_MULTISIG
Definition: standard.h:52
#define AssertLockHeld(cs)
Definition: sync.h:75
ArgsManager gArgs
Definition: system.cpp:89
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456
bool IsFinalTx(const CTransactionRef &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time.
Definition: tx_verify.cpp:12
bool NetworkUpgradeActive(int nHeight, const Consensus::Params &params, Consensus::UpgradeIndex idx)
Returns true if the given network upgrade is active as of the given block height.
Definition: upgrades.cpp:107