Bitcoin Core  29.1.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>
10 #include <test/util/setup_common.h>
11 #include <util/time.h>
12 #include <wallet/coincontrol.h>
13 #include <wallet/context.h>
14 #include <wallet/spend.h>
15 #include <wallet/test/util.h>
16 #include <wallet/wallet.h>
17 #include <validation.h>
18 #include <addresstype.h>
19 
20 using util::ToString;
21 
22 namespace wallet {
23 namespace {
24 const TestingSetup* g_setup;
25 
26 void initialize_setup()
27 {
28  static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
29  g_setup = testing_setup.get();
30 }
31 
32 FUZZ_TARGET(wallet_create_transaction, .init = initialize_setup)
33 {
35  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
36  SetMockTime(ConsumeTime(fuzzed_data_provider));
37  const auto& node = g_setup->m_node;
38  Chainstate& chainstate{node.chainman->ActiveChainstate()};
39  ArgsManager& args = *node.args;
40  args.ForceSetArg("-dustrelayfee", ToString(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, MAX_MONEY)));
41  FuzzedWallet fuzzed_wallet{
42  *g_setup->m_node.chain,
43  "fuzzed_wallet_a",
44  "tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",
45  };
46 
47  CCoinControl coin_control;
48  if (fuzzed_data_provider.ConsumeBool()) coin_control.m_version = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
49  coin_control.m_avoid_partial_spends = fuzzed_data_provider.ConsumeBool();
50  coin_control.m_include_unsafe_inputs = fuzzed_data_provider.ConsumeBool();
51  if (fuzzed_data_provider.ConsumeBool()) coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
52  coin_control.destChange = fuzzed_data_provider.ConsumeBool() ? fuzzed_wallet.GetDestination(fuzzed_data_provider) : ConsumeTxDestination(fuzzed_data_provider);
53  if (fuzzed_data_provider.ConsumeBool()) coin_control.m_change_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
54  if (fuzzed_data_provider.ConsumeBool()) coin_control.m_feerate = CFeeRate(ConsumeMoney(fuzzed_data_provider, /*max=*/COIN));
55  coin_control.m_allow_other_inputs = fuzzed_data_provider.ConsumeBool();
56  coin_control.m_locktime = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
57  coin_control.fOverrideFeeRate = fuzzed_data_provider.ConsumeBool();
58 
59  int next_locktime{0};
60  CAmount all_values{0};
61  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
62  {
63  CMutableTransaction tx;
64  tx.nLockTime = next_locktime++;
65  tx.vout.resize(1);
66  CAmount n_value{ConsumeMoney(fuzzed_data_provider)};
67  all_values += n_value;
68  if (all_values > MAX_MONEY) return;
69  tx.vout[0].nValue = n_value;
70  tx.vout[0].scriptPubKey = GetScriptForDestination(fuzzed_wallet.GetDestination(fuzzed_data_provider));
71  LOCK(fuzzed_wallet.wallet->cs_wallet);
72  auto txid{tx.GetHash()};
73  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}))};
74  assert(ret.second);
75  }
76 
77  std::vector<CRecipient> recipients;
78  LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
79  CTxDestination destination;
80  CallOneOf(
81  fuzzed_data_provider,
82  [&] {
83  destination = fuzzed_wallet.GetDestination(fuzzed_data_provider);
84  },
85  [&] {
86  CScript script;
87  script << OP_RETURN;
88  destination = CNoDestination{script};
89  },
90  [&] {
91  destination = ConsumeTxDestination(fuzzed_data_provider);
92  }
93  );
94  recipients.push_back({destination,
95  /*nAmount=*/ConsumeMoney(fuzzed_data_provider),
96  /*fSubtractFeeFromAmount=*/fuzzed_data_provider.ConsumeBool()});
97  }
98 
99  std::optional<unsigned int> change_pos;
100  if (fuzzed_data_provider.ConsumeBool()) change_pos = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
101  (void)CreateTransaction(*fuzzed_wallet.wallet, recipients, change_pos, coin_control);
102 }
103 } // namespace
104 } // namespace wallet
FUZZ_TARGET(coin_grinder)
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: args.cpp:546
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40
ArgsManager & args
Definition: bitcoind.cpp:277
Chainstate stores and provides an API to update our local knowledge of the current best chain...
Definition: validation.h:504
Definition: messages.h:20
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
static constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
Seed with a compile time constant of zeros.
Testing setup that configures a complete environment.
Definition: setup_common.h:121
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:233