Bitcoin Core  22.99.0
P2P Digital Currency
script_flags.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2020 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 <pubkey.h>
6 #include <script/interpreter.h>
7 #include <streams.h>
8 #include <test/util/script.h>
9 #include <version.h>
10 
11 #include <test/fuzz/fuzz.h>
12 
14 {
15  static const ECCVerifyHandle verify_handle;
16 }
17 
19 {
21  try {
22  int nVersion;
23  ds >> nVersion;
24  ds.SetVersion(nVersion);
25  } catch (const std::ios_base::failure&) {
26  return;
27  }
28 
29  try {
30  const CTransaction tx(deserialize, ds);
31 
32  unsigned int verify_flags;
33  ds >> verify_flags;
34 
36 
37  unsigned int fuzzed_flags;
38  ds >> fuzzed_flags;
39 
40  std::vector<CTxOut> spent_outputs;
41  for (unsigned i = 0; i < tx.vin.size(); ++i) {
42  CTxOut prevout;
43  ds >> prevout;
44  if (!MoneyRange(prevout.nValue)) {
45  // prevouts should be consensus-valid
46  prevout.nValue = 1;
47  }
48  spent_outputs.push_back(prevout);
49  }
51  txdata.Init(tx, std::move(spent_outputs));
52 
53  for (unsigned i = 0; i < tx.vin.size(); ++i) {
54  const CTxOut& prevout = txdata.m_spent_outputs.at(i);
55  const TransactionSignatureChecker checker{&tx, i, prevout.nValue, txdata, MissingDataBehavior::ASSERT_FAIL};
56 
57  ScriptError serror;
58  const bool ret = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror);
59  assert(ret == (serror == SCRIPT_ERR_OK));
60 
61  // Verify that removing flags from a passing test or adding flags to a failing test does not change the result
62  if (ret) {
63  verify_flags &= ~fuzzed_flags;
64  } else {
65  verify_flags |= fuzzed_flags;
66  }
68 
69  ScriptError serror_fuzzed;
70  const bool ret_fuzzed = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror_fuzzed);
71  assert(ret_fuzzed == (serror_fuzzed == SCRIPT_ERR_OK));
72 
73  assert(ret_fuzzed == ret);
74  }
75  } catch (const std::ios_base::failure&) {
76  return;
77  }
78 }
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:13
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:277
interpreter.h
MoneyRange
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:26
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(script_flags, initialize_script_flags)
Definition: script_flags.cpp:18
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:362
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:315
fuzz.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
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