Bitcoin Core  22.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 
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 }
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:270
assert
assert(!tx.IsCoinBase())
streams.h
SCRIPT_ERR_OK
@ SCRIPT_ERR_OK
Definition: script_error.h:13
PrecomputedTransactionData::m_spent_outputs
std::vector< CTxOut > m_spent_outputs
Definition: interpreter.h:167
IsValidFlagCombination
bool IsValidFlagCombination(unsigned flags)
Flags that are not forbidden by an assert in script validation.
Definition: script.cpp:8
initialize_script_flags
void initialize_script_flags()
Definition: script_flags.cpp:14
verify_flags
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.
Definition: bitcoinconsensus.cpp:71
GenericTransactionSignatureChecker
Definition: interpreter.h:278
interpreter.h
MoneyRange
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:27
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(script_flags, initialize_script_flags)
Definition: script_flags.cpp:19
pubkey.h
version.h
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition: interpreter.cpp:1969
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
ScriptError
enum ScriptError_t ScriptError
CTxOut::nValue
CAmount nValue
Definition: transaction.h:131
script.h
CTxOut
An output of a transaction.
Definition: transaction.h:128
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
PrecomputedTransactionData::Init
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
Definition: interpreter.cpp:1423
PrecomputedTransactionData
Definition: interpreter.h:150
MissingDataBehavior::ASSERT_FAIL
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:342
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:332
fuzz.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
amount.h
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:48
INIT_PROTO_VERSION
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15