Bitcoin Core  24.99.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 
14 FUZZ_TARGET(script_flags)
15 {
17  try {
18  int nVersion;
19  ds >> nVersion;
20  ds.SetVersion(nVersion);
21  } catch (const std::ios_base::failure&) {
22  return;
23  }
24 
25  try {
26  const CTransaction tx(deserialize, ds);
27 
28  unsigned int verify_flags;
29  ds >> verify_flags;
30 
32 
33  unsigned int fuzzed_flags;
34  ds >> fuzzed_flags;
35 
36  std::vector<CTxOut> spent_outputs;
37  for (unsigned i = 0; i < tx.vin.size(); ++i) {
38  CTxOut prevout;
39  ds >> prevout;
40  if (!MoneyRange(prevout.nValue)) {
41  // prevouts should be consensus-valid
42  prevout.nValue = 1;
43  }
44  spent_outputs.push_back(prevout);
45  }
47  txdata.Init(tx, std::move(spent_outputs));
48 
49  for (unsigned i = 0; i < tx.vin.size(); ++i) {
50  const CTxOut& prevout = txdata.m_spent_outputs.at(i);
51  const TransactionSignatureChecker checker{&tx, i, prevout.nValue, txdata, MissingDataBehavior::ASSERT_FAIL};
52 
53  ScriptError serror;
54  const bool ret = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror);
55  assert(ret == (serror == SCRIPT_ERR_OK));
56 
57  // Verify that removing flags from a passing test or adding flags to a failing test does not change the result
58  if (ret) {
59  verify_flags &= ~fuzzed_flags;
60  } else {
61  verify_flags |= fuzzed_flags;
62  }
64 
65  ScriptError serror_fuzzed;
66  const bool ret_fuzzed = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror_fuzzed);
67  assert(ret_fuzzed == (serror_fuzzed == SCRIPT_ERR_OK));
68 
69  assert(ret_fuzzed == ret);
70  }
71  } catch (const std::ios_base::failure&) {
72  return;
73  }
74 }
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
int ret
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.
void SetVersion(int n)
Definition: streams.h:362
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:295
const std::vector< CTxIn > vin
Definition: transaction.h:305
An output of a transaction.
Definition: transaction.h:158
CScript scriptPubKey
Definition: transaction.h:161
CAmount nValue
Definition: transaction.h:160
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
enum ScriptError_t ScriptError
@ SCRIPT_ERR_OK
Definition: script_error.h:13
FUZZ_TARGET(script_flags)
@ SER_NETWORK
Definition: serialize.h:131
constexpr deserialize_type deserialize
Definition: serialize.h:48
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
std::vector< CTxOut > m_spent_outputs
Definition: interpreter.h:168
bool IsValidFlagCombination(unsigned flags)
Flags that are not forbidden by an assert in script validation.
Definition: script.cpp:8
assert(!tx.IsCoinBase())
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15