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