Bitcoin Core  24.1.0
P2P Digital Currency
script_flags.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <consensus/amount.h>
6 #include <pubkey.h>
7 #include <script/interpreter.h>
8 #include <streams.h>
9 #include <test/util/script.h>
10 #include <version.h>
11 
12 #include <test/fuzz/fuzz.h>
13 
15 {
16  static const ECCVerifyHandle verify_handle;
17 }
18 
20 {
22  try {
23  int nVersion;
24  ds >> nVersion;
25  ds.SetVersion(nVersion);
26  } catch (const std::ios_base::failure&) {
27  return;
28  }
29 
30  try {
31  const CTransaction tx(deserialize, ds);
32 
33  unsigned int verify_flags;
34  ds >> verify_flags;
35 
37 
38  unsigned int fuzzed_flags;
39  ds >> fuzzed_flags;
40 
41  std::vector<CTxOut> spent_outputs;
42  for (unsigned i = 0; i < tx.vin.size(); ++i) {
43  CTxOut prevout;
44  ds >> prevout;
45  if (!MoneyRange(prevout.nValue)) {
46  // prevouts should be consensus-valid
47  prevout.nValue = 1;
48  }
49  spent_outputs.push_back(prevout);
50  }
52  txdata.Init(tx, std::move(spent_outputs));
53 
54  for (unsigned i = 0; i < tx.vin.size(); ++i) {
55  const CTxOut& prevout = txdata.m_spent_outputs.at(i);
56  const TransactionSignatureChecker checker{&tx, i, prevout.nValue, txdata, MissingDataBehavior::ASSERT_FAIL};
57 
58  ScriptError serror;
59  const bool ret = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror);
60  assert(ret == (serror == SCRIPT_ERR_OK));
61 
62  // Verify that removing flags from a passing test or adding flags to a failing test does not change the result
63  if (ret) {
64  verify_flags &= ~fuzzed_flags;
65  } else {
66  verify_flags |= fuzzed_flags;
67  }
69 
70  ScriptError serror_fuzzed;
71  const bool ret_fuzzed = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror_fuzzed);
72  assert(ret_fuzzed == (serror_fuzzed == SCRIPT_ERR_OK));
73 
74  assert(ret_fuzzed == ret);
75  }
76  } catch (const std::ios_base::failure&) {
77  return;
78  }
79 }
CAmount nValue
Definition: transaction.h:159
int ret
assert(!tx.IsCoinBase())
enum ScriptError_t ScriptError
CScript scriptPubKey
Definition: transaction.h:160
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
constexpr deserialize_type deserialize
Definition: serialize.h:48
std::vector< CTxOut > m_spent_outputs
Definition: interpreter.h:168
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:185
void initialize_script_flags()
const std::vector< CTxIn > vin
Definition: transaction.h:298
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:335
Abort execution through assertion failure (for consensus code)
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
An output of a transaction.
Definition: transaction.h:156
bool IsValidFlagCombination(unsigned flags)
Flags that are not forbidden by an assert in script validation.
Definition: script.cpp:8
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:287
void SetVersion(int n)
Definition: streams.h:277
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.
FUZZ_TARGET_INIT(script_flags, initialize_script_flags)