Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <consensus/amount.h>6#include <primitives/transaction.h>7#include <script/interpreter.h>8#include <serialize.h>9#include <streams.h>10#include <test/fuzz/fuzz.h>11#include <test/util/script.h>13#include <cassert>14#include <ios>15#include <utility>16#include <vector>18static SpanReader& operator>>(SpanReader& ds, script_verify_flags& f)19{20script_verify_flags::value_type n{0};21ds >> n;22f = script_verify_flags::from_int(n);23assert(n == f.as_int());24return ds;25}27FUZZ_TARGET(script_flags)28{29if (buffer.size() > 100'000) return;30SpanReader ds{buffer};31try {32const CTransaction tx(deserialize, TX_WITH_WITNESS, ds);34script_verify_flags verify_flags;35ds >> verify_flags;37assert(verify_flags == script_verify_flags::from_int(verify_flags.as_int()));39if (!IsValidFlagCombination(verify_flags)) return;41script_verify_flags fuzzed_flags;42ds >> fuzzed_flags;44std::vector<CTxOut> spent_outputs;45for (unsigned i = 0; i < tx.vin.size(); ++i) {46CTxOut prevout;47ds >> prevout;48if (!MoneyRange(prevout.nValue)) {49// prevouts should be consensus-valid50prevout.nValue = 1;51}52spent_outputs.push_back(prevout);53}54PrecomputedTransactionData txdata;55txdata.Init(tx, std::move(spent_outputs));57for (unsigned i = 0; i < tx.vin.size(); ++i) {58const CTxOut& prevout = txdata.m_spent_outputs.at(i);59const TransactionSignatureChecker checker{&tx, i, prevout.nValue, txdata, MissingDataBehavior::ASSERT_FAIL};61ScriptError serror;62const bool ret = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror);63assert(ret == (serror == SCRIPT_ERR_OK));65// Verify that removing flags from a passing test or adding flags to a failing test does not change the result66if (ret) {67verify_flags &= ~fuzzed_flags;68} else {69verify_flags |= fuzzed_flags;70}71if (!IsValidFlagCombination(verify_flags)) return;73ScriptError serror_fuzzed;74const bool ret_fuzzed = VerifyScript(tx.vin.at(i).scriptSig, prevout.scriptPubKey, &tx.vin.at(i).scriptWitness, verify_flags, checker, &serror_fuzzed);75assert(ret_fuzzed == (serror_fuzzed == SCRIPT_ERR_OK));77assert(ret_fuzzed == ret);78}79} catch (const std::ios_base::failure&) {80return;81}82}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.