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) 2024-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 <addresstype.h>6#include <test/fuzz/FuzzedDataProvider.h>7#include <test/fuzz/fuzz.h>8#include <test/fuzz/util.h>9#include <test/fuzz/util/wallet.h>10#include <test/util/random.h>11#include <test/util/setup_common.h>12#include <test/util/time.h>13#include <util/time.h>14#include <validation.h>15#include <wallet/coincontrol.h>16#include <wallet/context.h>17#include <wallet/spend.h>18#include <wallet/test/util.h>19#include <wallet/wallet.h>21using util::ToString;23namespace wallet {24namespace {25const TestingSetup* g_setup;27void initialize_setup()28{29static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();30g_setup = testing_setup.get();31}33FUZZ_TARGET(wallet_create_transaction, .init = initialize_setup)34{35SeedRandomStateForTest(SeedRand::ZEROS);36FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};37FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};38const auto& node = g_setup->m_node;39Chainstate& chainstate{node.chainman->ActiveChainstate()};40ArgsManager& args = *node.args;41args.ForceSetArg("-dustrelayfee", ToString(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, MAX_MONEY)));42FuzzedWallet fuzzed_wallet{43*g_setup->m_node.chain,44"fuzzed_wallet_a",45"tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",46};48CCoinControl coin_control;49if (fuzzed_data_provider.ConsumeBool()) coin_control.m_version = fuzzed_data_provider.ConsumeIntegral<unsigned int>();50coin_control.m_avoid_partial_spends = fuzzed_data_provider.ConsumeBool();51coin_control.m_include_unsafe_inputs = fuzzed_data_provider.ConsumeBool();52if (fuzzed_data_provider.ConsumeBool()) coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);53coin_control.destChange = fuzzed_data_provider.ConsumeBool() ? fuzzed_wallet.GetDestination(fuzzed_data_provider) : ConsumeTxDestination(fuzzed_data_provider);54if (fuzzed_data_provider.ConsumeBool()) coin_control.m_change_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);55if (fuzzed_data_provider.ConsumeBool()) coin_control.m_feerate = CFeeRate(ConsumeMoney(fuzzed_data_provider, /*max=*/COIN));56coin_control.m_allow_other_inputs = fuzzed_data_provider.ConsumeBool();57coin_control.m_locktime = fuzzed_data_provider.ConsumeIntegral<unsigned int>();58coin_control.fOverrideFeeRate = fuzzed_data_provider.ConsumeBool();60int next_locktime{0};61CAmount all_values{0};62LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {63CMutableTransaction tx;64tx.nLockTime = next_locktime++;65tx.vout.resize(1);66CAmount n_value{ConsumeMoney(fuzzed_data_provider)};67all_values += n_value;68if (all_values > MAX_MONEY) return;69tx.vout[0].nValue = n_value;70tx.vout[0].scriptPubKey = GetScriptForDestination(fuzzed_wallet.GetDestination(fuzzed_data_provider));71LOCK(fuzzed_wallet.wallet->cs_wallet);72auto txid{tx.GetHash()};73auto ret{fuzzed_wallet.wallet->mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(txid), std::forward_as_tuple(MakeTransactionRef(std::move(tx)), TxStateConfirmed{chainstate.m_chain.Tip()->GetBlockHash(), chainstate.m_chain.Height(), /*index=*/0}))};74assert(ret.second);75fuzzed_wallet.wallet->RefreshTXOsFromTx(ret.first->second);76}78std::vector<CRecipient> recipients;79LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {80CTxDestination destination;81CallOneOf(82fuzzed_data_provider,83[&] {84destination = fuzzed_wallet.GetDestination(fuzzed_data_provider);85},86[&] {87CScript script;88script << OP_RETURN;89destination = CNoDestination{script};90},91[&] {92destination = ConsumeTxDestination(fuzzed_data_provider);93}94);95recipients.push_back({destination,96/*nAmount=*/ConsumeMoney(fuzzed_data_provider),97/*fSubtractFeeFromAmount=*/fuzzed_data_provider.ConsumeBool()});98}100std::optional<unsigned int> change_pos;101if (fuzzed_data_provider.ConsumeBool()) change_pos = fuzzed_data_provider.ConsumeIntegral<unsigned int>();102(void)CreateTransaction(*fuzzed_wallet.wallet, recipients, change_pos, coin_control);103}104} // namespace105} // namespace wallet
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.