Bitcoin Core 28.99.0
P2P Digital Currency
spend.cpp
Go to the documentation of this file.
1// Copyright (c) 2024-present 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
6#include <test/fuzz/fuzz.h>
7#include <test/fuzz/util.h>
9#include <test/util/random.h>
11#include <wallet/coincontrol.h>
12#include <wallet/context.h>
13#include <wallet/spend.h>
14#include <wallet/test/util.h>
15#include <wallet/wallet.h>
16#include <validation.h>
17#include <addresstype.h>
18
19using util::ToString;
20
21namespace wallet {
22namespace {
23const TestingSetup* g_setup;
24
25void initialize_setup()
26{
27 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
28 g_setup = testing_setup.get();
29}
30
31FUZZ_TARGET(wallet_create_transaction, .init = initialize_setup)
32{
34 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
35 const auto& node = g_setup->m_node;
36 Chainstate& chainstate{node.chainman->ActiveChainstate()};
37 ArgsManager& args = *node.args;
38 args.ForceSetArg("-dustrelayfee", ToString(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, MAX_MONEY)));
39 FuzzedWallet fuzzed_wallet{
40 *g_setup->m_node.chain,
41 "fuzzed_wallet_a",
42 "tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",
43 };
44
45 CCoinControl coin_control;
46 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_version = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
47 coin_control.m_avoid_partial_spends = fuzzed_data_provider.ConsumeBool();
48 coin_control.m_include_unsafe_inputs = fuzzed_data_provider.ConsumeBool();
49 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
50 coin_control.destChange = fuzzed_data_provider.ConsumeBool() ? fuzzed_wallet.GetDestination(fuzzed_data_provider) : ConsumeTxDestination(fuzzed_data_provider);
51 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_change_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
52 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_feerate = CFeeRate(ConsumeMoney(fuzzed_data_provider, /*max=*/COIN));
53 coin_control.m_allow_other_inputs = fuzzed_data_provider.ConsumeBool();
54 coin_control.m_locktime = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
55 coin_control.fOverrideFeeRate = fuzzed_data_provider.ConsumeBool();
56
57 int next_locktime{0};
58 CAmount all_values{0};
59 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
60 {
62 tx.nLockTime = next_locktime++;
63 tx.vout.resize(1);
64 CAmount n_value{ConsumeMoney(fuzzed_data_provider)};
65 all_values += n_value;
66 if (all_values > MAX_MONEY) return;
67 tx.vout[0].nValue = n_value;
68 tx.vout[0].scriptPubKey = GetScriptForDestination(fuzzed_wallet.GetDestination(fuzzed_data_provider));
69 LOCK(fuzzed_wallet.wallet->cs_wallet);
70 auto txid{tx.GetHash()};
71 auto 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}))};
72 assert(ret.second);
73 }
74
75 std::vector<CRecipient> recipients;
76 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
77 CTxDestination destination;
79 fuzzed_data_provider,
80 [&] {
81 destination = fuzzed_wallet.GetDestination(fuzzed_data_provider);
82 },
83 [&] {
86 destination = CNoDestination{script};
87 },
88 [&] {
89 destination = ConsumeTxDestination(fuzzed_data_provider);
90 }
91 );
92 recipients.push_back({destination,
93 /*nAmount=*/ConsumeMoney(fuzzed_data_provider),
94 /*fSubtractFeeFromAmount=*/fuzzed_data_provider.ConsumeBool()});
95 }
96
97 std::optional<unsigned int> change_pos;
98 if (fuzzed_data_provider.ConsumeBool()) change_pos = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
99 (void)CreateTransaction(*fuzzed_wallet.wallet, recipients, change_pos, coin_control);
100}
101} // namespace
102} // namespace wallet
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition: addresstype.h:140
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
int ret
ArgsManager & args
Definition: bitcoind.cpp:277
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:546
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:505
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
Definition: messages.h:20
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:233
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, std::optional< unsigned int > change_pos, const CCoinControl &coin_control, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition: spend.cpp:1370
FUZZ_TARGET(coin_grinder)
static constexpr auto OUTPUT_TYPES
Definition: outputtype.h:25
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
@ OP_RETURN
Definition: script.h:111
node::NodeContext m_node
Definition: setup_common.h:65
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Definition: transaction.cpp:69
Testing setup that configures a complete environment.
Definition: setup_common.h:120
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:76
#define LOCK(cs)
Definition: sync.h:257
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:184
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition: util.cpp:29
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
assert(!tx.IsCoinBase())