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