Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 // NOTE: This file is intended to be customised by the end user, and includes only local node policy logic
8 
9 #include "policy/policy.h"
10 
11 #include "consensus/validation.h"
12 #include "validation.h"
13 #include "coins.h"
14 #include "tinyformat.h"
15 #include "util.h"
16 #include "utilstrencodings.h"
17 
18 
19 CAmount GetDustThreshold(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
20 {
21  // "Dust" is defined in terms of dustRelayFee,
22  // which has units satoshis-per-kilobyte.
23  // If you'd pay more in fees than the value of the output
24  // to spend something, then we consider it dust.
25  // A typical spendable non-segwit txout is 34 bytes big, and will
26  // need a CTxIn of at least 148 bytes to spend:
27  // so dust is a spendable txout less than
28  // 182*dustRelayFee/1000 (in satoshis).
29  // 546 satoshis at the default rate of 3000 sat/kB.
30  // A typical spendable segwit txout is 31 bytes big, and will
31  // need a CTxIn of at least 67 bytes to spend:
32  // so dust is a spendable txout less than
33  // 98*dustRelayFee/1000 (in satoshis).
34  // 294 satoshis at the default rate of 3000 sat/kB.
35  if (txout.scriptPubKey.IsUnspendable())
36  return 0;
37 
38  size_t nSize = GetSerializeSize(txout, SER_DISK, 0);
39  int witnessversion = 0;
40  std::vector<unsigned char> witnessprogram;
41 
42  if (txout.scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
43  // sum the sizes of the parts of a transaction input
44  // with 75% segwit discount applied to the script size.
45  nSize += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
46  } else {
47  nSize += (32 + 4 + 1 + 107 + 4); // the 148 mentioned above
48  }
49 
50  return dustRelayFeeIn.GetFee(nSize);
51 }
52 
53 bool IsDust(const CTxOut& txout, const CFeeRate& dustRelayFeeIn)
54 {
55 
56  if (txout.scriptPubKey.IsAssetScript())
57  return false;
58  else
59  return (txout.nValue < GetDustThreshold(txout, dustRelayFeeIn));
60 }
61 
62 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType, const bool witnessEnabled) {
63  std::vector<std::vector<unsigned char> > vSolutions;
64  if (!Solver(scriptPubKey, whichType, vSolutions))
65  return false;
66 
67 
68  if (whichType == TX_MULTISIG) {
69  unsigned char m = vSolutions.front()[0];
70  unsigned char n = vSolutions.back()[0];
71  // Support up to x-of-3 multisig txns as standard
72  if (n < 1 || n > 3)
73  return false;
74  if (m < 1 || m > n)
75  return false;
76  } else if (whichType == TX_NULL_DATA &&
77  (!fAcceptDatacarrier || scriptPubKey.size() > nMaxDatacarrierBytes))
78  return false;
79  else if (whichType == TX_RESTRICTED_ASSET_DATA && scriptPubKey.size() > MAX_OP_RETURN_RELAY)
80  return false;
81  else if (!witnessEnabled && (whichType == TX_WITNESS_V0_KEYHASH || whichType == TX_WITNESS_V0_SCRIPTHASH))
82  return false;
83 
84  return whichType != TX_NONSTANDARD ;
85 }
86 
87 bool IsStandardTx(const CTransaction& tx, std::string& reason, const bool witnessEnabled)
88 {
90  reason = "version";
91  return false;
92  }
93 
94  // Extremely large transactions with lots of inputs can cost the network
95  // almost as much to process as they cost the sender in fees, because
96  // computing signature hashes is O(ninputs*txsize). Limiting transactions
97  // to MAX_STANDARD_TX_WEIGHT mitigates CPU exhaustion attacks.
98  unsigned int sz = GetTransactionWeight(tx);
99  if (sz >= MAX_STANDARD_TX_WEIGHT) {
100  reason = "tx-size";
101  return false;
102  }
103 
104  for (const CTxIn& txin : tx.vin)
105  {
106  // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
107  // keys (remember the 520 byte limit on redeemScript size). That works
108  // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
109  // bytes of scriptSig, which we round off to 1650 bytes for some minor
110  // future-proofing. That's also enough to spend a 20-of-20
111  // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
112  // considered standard.
113  if (txin.scriptSig.size() > 1650) {
114  reason = "scriptsig-size";
115  return false;
116  }
117  if (!txin.scriptSig.IsPushOnly()) {
118  reason = "scriptsig-not-pushonly";
119  return false;
120  }
121  }
122 
123  unsigned int nDataOut = 0;
124  unsigned int nAssetDataOut = 0;
125  txnouttype whichType;
126  for (const CTxOut& txout : tx.vout) {
127  if (!::IsStandard(txout.scriptPubKey, whichType, witnessEnabled)) {
128  reason = "scriptpubkey";
129  return false;
130  }
131 
132  if (whichType == TX_NULL_DATA)
133  nDataOut++;
134  else if (whichType == TX_RESTRICTED_ASSET_DATA)
135  nAssetDataOut++;
136  else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
137  reason = "bare-multisig";
138  return false;
139  } else if (IsDust(txout, ::dustRelayFee)) {
140  reason = "dust";
141  return false;
142  }
143  }
144 
145  // only one OP_RETURN txout is permitted
146  if (nDataOut > 1) {
147  reason = "multi-op-return";
148  return false;
149  }
150 
151  // only ten OP_RAVEN_ASSET txout is permitted
152  if (nAssetDataOut > 10) {
153  reason = "tomany-op-rvn-asset";
154  return false;
155  }
156 
157  return true;
158 }
159 
176 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
177 {
178  if (tx.IsCoinBase())
179  return true; // Coinbases don't use vin normally
180 
181  for (unsigned int i = 0; i < tx.vin.size(); i++)
182  {
183  const CTxOut& prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
184 
185  std::vector<std::vector<unsigned char> > vSolutions;
186  txnouttype whichType;
187  // get the scriptPubKey corresponding to this input:
188  const CScript& prevScript = prev.scriptPubKey;
189  if (!Solver(prevScript, whichType, vSolutions))
190  return false;
191 
192  if (whichType == TX_SCRIPTHASH)
193  {
194  std::vector<std::vector<unsigned char> > stack;
195  // convert the scriptSig into a stack, so we can inspect the redeemScript
196  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
197  return false;
198  if (stack.empty())
199  return false;
200  CScript subscript(stack.back().begin(), stack.back().end());
201  if (subscript.GetSigOpCount(true) > MAX_P2SH_SIGOPS) {
202  return false;
203  }
204  }
205  }
206 
207  return true;
208 }
209 
210 bool IsWitnessStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
211 {
212  if (tx.IsCoinBase())
213  return true; // Coinbases are skipped
214 
215  for (unsigned int i = 0; i < tx.vin.size(); i++)
216  {
217  // We don't care if witness for this input is empty, since it must not be bloated.
218  // If the script is invalid without witness, it would be caught sooner or later during validation.
219  if (tx.vin[i].scriptWitness.IsNull())
220  continue;
221 
222  const CTxOut &prev = mapInputs.AccessCoin(tx.vin[i].prevout).out;
223 
224  // get the scriptPubKey corresponding to this input:
225  CScript prevScript = prev.scriptPubKey;
226 
227  if (prevScript.IsPayToScriptHash()) {
228  std::vector <std::vector<unsigned char> > stack;
229  // If the scriptPubKey is P2SH, we try to extract the redeemScript casually by converting the scriptSig
230  // into a stack. We do not check IsPushOnly nor compare the hash as these will be done later anyway.
231  // If the check fails at this stage, we know that this txid must be a bad one.
232  if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker(), SIGVERSION_BASE))
233  return false;
234  if (stack.empty())
235  return false;
236  prevScript = CScript(stack.back().begin(), stack.back().end());
237  }
238 
239  int witnessversion = 0;
240  std::vector<unsigned char> witnessprogram;
241 
242  // Non-witness program must not be associated with any witness
243  if (!prevScript.IsWitnessProgram(witnessversion, witnessprogram))
244  return false;
245 
246  // Check P2WSH standard limits
247  if (witnessversion == 0 && witnessprogram.size() == 32) {
248  if (tx.vin[i].scriptWitness.stack.back().size() > MAX_STANDARD_P2WSH_SCRIPT_SIZE)
249  return false;
250  size_t sizeWitnessStack = tx.vin[i].scriptWitness.stack.size() - 1;
251  if (sizeWitnessStack > MAX_STANDARD_P2WSH_STACK_ITEMS)
252  return false;
253  for (unsigned int j = 0; j < sizeWitnessStack; j++) {
254  if (tx.vin[i].scriptWitness.stack[j].size() > MAX_STANDARD_P2WSH_STACK_ITEM_SIZE)
255  return false;
256  }
257  }
258  }
259  return true;
260 }
261 
262 CFeeRate incrementalRelayFee = CFeeRate(DEFAULT_INCREMENTAL_RELAY_FEE);
263 CFeeRate dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
264 unsigned int nBytesPerSigOp = DEFAULT_BYTES_PER_SIGOP;
265 
266 int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
267 {
268  return (std::max(nWeight, nSigOpCost * nBytesPerSigOp) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR;
269 }
270 
271 int64_t GetVirtualTransactionSize(const CTransaction& tx, int64_t nSigOpCost)
272 {
273  return GetVirtualTransactionSize(GetTransactionWeight(tx), nSigOpCost);
274 }
CAmount nValue
Definition: transaction.h:140
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost)
Compute the virtual transaction size (weight reinterpreted as bytes).
Definition: policy.cpp:266
static const int32_t MAX_STANDARD_VERSION
Definition: transaction.h:280
unspendable OP_RETURN script that carries data
Definition: standard.h:65
CScript scriptPubKey
Definition: transaction.h:141
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or a pruned one if not found.
Definition: coins.cpp:390
bool IsPayToScriptHash() const
Definition: script.cpp:221
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
FeeReason reason
Definition: fees.h:129
CTxOut out
unspent transaction output
Definition: coins.h:36
bool fAcceptDatacarrier
A data carrying output is an unspendable output containing data.
Definition: standard.cpp:19
bool IsWitnessProgram(int &version, std::vector< unsigned char > &program) const
Definition: script.cpp:368
unsigned int nBytesPerSigOp
Definition: policy.cpp:264
bool IsCoinBase() const
Definition: transaction.h:360
bool fIsBareMultisigStd
Definition: validation.cpp:90
const std::vector< CTxIn > vin
Definition: transaction.h:287
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
Definition: policy.cpp:210
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack...
Definition: script.cpp:449
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:176
An input of a transaction.
Definition: transaction.h:67
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
Definition: script.cpp:400
const std::vector< CTxOut > vout
Definition: transaction.h:288
An output of a transaction.
Definition: transaction.h:137
bool IsStandardTx(const CTransaction &tx, std::string &reason, const bool witnessEnabled)
Check for standard transaction types.
Definition: policy.cpp:87
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
CAmount GetDustThreshold(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:19
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:47
CScript scriptSig
Definition: transaction.h:71
txnouttype
Definition: standard.h:57
const int32_t nVersion
Definition: transaction.h:289
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:20
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:53
size_type size() const
Definition: prevector.h:283
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType, const bool witnessEnabled)
Definition: policy.cpp:62
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:208
CFeeRate incrementalRelayFee
Definition: policy.cpp:262
unsigned nMaxDatacarrierBytes
Maximum size of TX_NULL_DATA scripts that this node considers standard.
Definition: standard.cpp:20
unspendable OP_RAVEN_ASSET script that carries data
Definition: standard.h:72
bool IsAssetScript(int &nType, bool &fIsOwner, int &nStartingIndex) const
Definition: script.cpp:245
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:24
CFeeRate dustRelayFee
Definition: policy.cpp:263