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) 2019-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 <chainparams.h>6#include <compressor.h>7#include <core_io.h>8#include <core_memusage.h>9#include <key_io.h>10#include <policy/policy.h>11#include <pubkey.h>12#include <rpc/util.h>13#include <script/descriptor.h>14#include <script/interpreter.h>15#include <script/script.h>16#include <script/script_error.h>17#include <script/sign.h>18#include <script/signingprovider.h>19#include <script/solver.h>20#include <streams.h>21#include <test/fuzz/FuzzedDataProvider.h>22#include <test/fuzz/fuzz.h>23#include <test/fuzz/util.h>24#include <univalue.h>25#include <util/chaintype.h>27#include <algorithm>28#include <cassert>29#include <cstdint>30#include <optional>31#include <string>32#include <vector>34void initialize_script()35{36SelectParams(ChainType::REGTEST);37}39FUZZ_TARGET(script, .init = initialize_script)40{41FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());42const CScript script{ConsumeScript(fuzzed_data_provider)};44CompressedScript compressed;45if (CompressScript(script, compressed)) {46const unsigned int size = compressed[0];47compressed.erase(compressed.begin());48assert(size <= 5);49CScript decompressed_script;50const bool ok = DecompressScript(decompressed_script, size, compressed);51assert(ok);52assert(script == decompressed_script);53}55TxoutType which_type;56bool is_standard_ret = IsStandard(script, which_type);57if (!is_standard_ret) {58assert(which_type == TxoutType::NONSTANDARD ||59which_type == TxoutType::NULL_DATA ||60which_type == TxoutType::MULTISIG);61}62if (which_type == TxoutType::NONSTANDARD) {63assert(!is_standard_ret);64}65if (which_type == TxoutType::NULL_DATA) {66assert(script.IsUnspendable());67}68if (script.IsUnspendable()) {69assert(which_type == TxoutType::NULL_DATA ||70which_type == TxoutType::NONSTANDARD);71}73CTxDestination address;74bool extract_destination_ret = ExtractDestination(script, address);75if (!extract_destination_ret) {76assert(which_type == TxoutType::PUBKEY ||77which_type == TxoutType::NONSTANDARD ||78which_type == TxoutType::NULL_DATA ||79which_type == TxoutType::MULTISIG);80}81if (which_type == TxoutType::NONSTANDARD ||82which_type == TxoutType::NULL_DATA ||83which_type == TxoutType::MULTISIG) {84assert(!extract_destination_ret);85}87const FlatSigningProvider signing_provider;88(void)InferDescriptor(script, signing_provider);89(void)IsSegWitOutput(signing_provider, script);91(void)RecursiveDynamicUsage(script);93std::vector<std::vector<unsigned char>> solutions;94(void)Solver(script, solutions);96{97const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);98CompressedScript compressed_script;99compressed_script.assign(bytes.begin(), bytes.end());100// DecompressScript(..., ..., bytes) is not guaranteed to be defined if the bytes vector is too short101if (compressed_script.size() >= 32) {102CScript decompressed_script;103DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), compressed_script);104}105}107const std::optional<CScript> other_script = ConsumeDeserializable<CScript>(fuzzed_data_provider);108if (other_script) {109{110CScript script_mut{script};111(void)FindAndDelete(script_mut, *other_script);112}113const std::vector<std::string> random_string_vector = ConsumeRandomLengthStringVector(fuzzed_data_provider);114const auto flags_rand{fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>()};115const auto flags = script_verify_flags::from_int(flags_rand) | SCRIPT_VERIFY_P2SH;116{117CScriptWitness wit;118for (const auto& s : random_string_vector) {119wit.stack.emplace_back(s.begin(), s.end());120}121(void)CountWitnessSigOps(script, *other_script, wit, flags);122wit.SetNull();123}124}126(void)GetOpName(ConsumeOpcodeType(fuzzed_data_provider));127(void)ScriptErrorString(static_cast<ScriptError>(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, SCRIPT_ERR_ERROR_COUNT)));129{130const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider);131CScript append_script{bytes.begin(), bytes.end()};132append_script << fuzzed_data_provider.ConsumeIntegral<int64_t>();133append_script << ConsumeOpcodeType(fuzzed_data_provider);134append_script << CScriptNum{fuzzed_data_provider.ConsumeIntegral<int64_t>()};135append_script << ConsumeRandomLengthByteVector(fuzzed_data_provider);136}138{139const CTxDestination tx_destination_1{140fuzzed_data_provider.ConsumeBool() ?141DecodeDestination(fuzzed_data_provider.ConsumeRandomLengthString()) :142ConsumeTxDestination(fuzzed_data_provider)};143const CTxDestination tx_destination_2{ConsumeTxDestination(fuzzed_data_provider)};144const std::string encoded_dest{EncodeDestination(tx_destination_1)};145const UniValue json_dest{DescribeAddress(tx_destination_1)};146(void)GetKeyForDestination(/*store=*/{}, tx_destination_1);147const CScript dest{GetScriptForDestination(tx_destination_1)};148const bool valid{IsValidDestination(tx_destination_1)};150if (!std::get_if<PubKeyDestination>(&tx_destination_1)) {151// Only try to round trip non-pubkey destinations since PubKeyDestination has no encoding152Assert(dest.empty() != valid);153Assert(tx_destination_1 == DecodeDestination(encoded_dest));154Assert(valid == IsValidDestinationString(encoded_dest));155}157(void)(tx_destination_1 < tx_destination_2);158if (tx_destination_1 == tx_destination_2) {159Assert(encoded_dest == EncodeDestination(tx_destination_2));160Assert(json_dest.write() == DescribeAddress(tx_destination_2).write());161Assert(dest == GetScriptForDestination(tx_destination_2));162}163}164}
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.