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