spend.cpp raw
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
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>
20
21 using util::ToString;
22
23 namespace wallet {
24 namespace {
25 const TestingSetup* g_setup;
26
27 void initialize_setup()
28 {
29 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
30 g_setup = testing_setup.get();
31 }
32
33 FUZZ_TARGET(wallet_create_transaction, .init = initialize_setup)
34 {
35 SeedRandomStateForTest(SeedRand::ZEROS);
36 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
37 FakeNodeClock clock{ConsumeTime(fuzzed_data_provider)};
38 const auto& node = g_setup->m_node;
39 Chainstate& chainstate{node.chainman->ActiveChainstate()};
40 ArgsManager& args = *node.args;
41 args.ForceSetArg("-dustrelayfee", ToString(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, MAX_MONEY)));
42 FuzzedWallet fuzzed_wallet{
43 *g_setup->m_node.chain,
44 "fuzzed_wallet_a",
45 "tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",
46 };
47
48 CCoinControl coin_control;
49 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_version = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
50 coin_control.m_avoid_partial_spends = fuzzed_data_provider.ConsumeBool();
51 coin_control.m_include_unsafe_inputs = fuzzed_data_provider.ConsumeBool();
52 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
53 coin_control.destChange = fuzzed_data_provider.ConsumeBool() ? fuzzed_wallet.GetDestination(fuzzed_data_provider) : ConsumeTxDestination(fuzzed_data_provider);
54 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_change_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
55 if (fuzzed_data_provider.ConsumeBool()) coin_control.m_feerate = CFeeRate(ConsumeMoney(fuzzed_data_provider, /*max=*/COIN));
56 coin_control.m_allow_other_inputs = fuzzed_data_provider.ConsumeBool();
57 coin_control.m_locktime = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
58 coin_control.fOverrideFeeRate = fuzzed_data_provider.ConsumeBool();
59
60 int next_locktime{0};
61 CAmount all_values{0};
62 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
63 {
64 CMutableTransaction tx;
65 tx.nLockTime = next_locktime++;
66 tx.vout.resize(1);
67 CAmount n_value{ConsumeMoney(fuzzed_data_provider)};
68 all_values += n_value;
69 if (all_values > MAX_MONEY) return;
70 tx.vout[0].nValue = n_value;
71 tx.vout[0].scriptPubKey = GetScriptForDestination(fuzzed_wallet.GetDestination(fuzzed_data_provider));
72 LOCK(fuzzed_wallet.wallet->cs_wallet);
73 auto txid{tx.GetHash()};
74 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}))};
75 assert(ret.second);
76 }
77
78 std::vector<CRecipient> recipients;
79 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
80 CTxDestination destination;
81 CallOneOf(
82 fuzzed_data_provider,
83 [&] {
84 destination = fuzzed_wallet.GetDestination(fuzzed_data_provider);
85 },
86 [&] {
87 CScript script;
88 script << OP_RETURN;
89 destination = CNoDestination{script};
90 },
91 [&] {
92 destination = ConsumeTxDestination(fuzzed_data_provider);
93 }
94 );
95 recipients.push_back({destination,
96 /*nAmount=*/ConsumeMoney(fuzzed_data_provider),
97 /*fSubtractFeeFromAmount=*/fuzzed_data_provider.ConsumeBool()});
98 }
99
100 std::optional<unsigned int> change_pos;
101 if (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 } // namespace
105 } // namespace wallet
106