Bitcoin Core 32.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
5#include <addresstype.h>
7#include <test/fuzz/fuzz.h>
8#include <test/fuzz/util.h>
10#include <test/util/random.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
21using util::ToString;
22
23namespace wallet {
24namespace {
25const TestingSetup* g_setup;
26
27void initialize_setup()
28{
29 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
30 g_setup = testing_setup.get();
31}
32
33FUZZ_TARGET(wallet_create_transaction, .init = initialize_setup)
34{
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{
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 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 fuzzed_wallet.wallet->RefreshTXOsFromTx(ret.first->second);
76 }
77
78 std::vector<CRecipient> recipients;
79 LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 100) {
80 CTxDestination destination;
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
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
constexpr CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition amount.h:26
int64_t CAmount
Amount in satoshis (Can be negative).
Definition amount.h:12
constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition amount.h:15
int ret
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition string.h:250
ArgsManager & args
Definition bitcoind.cpp:280
const TestingSetup * g_setup
void ForceSetArg(const std::string &strArg, const std::string &strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Definition args.cpp:627
uint256 GetBlockHash() const
Definition chain.h:198
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition chain.h:396
int Height() const
Return the maximal height in the chain.
Definition chain.h:425
std::optional< unsigned int > m_confirm_target
Override the default confirmation target if set.
Definition coincontrol.h:99
std::optional< OutputType > m_change_type
Override the default change type if set, ignored if destChange is set.
Definition coincontrol.h:88
bool m_allow_other_inputs
Definition coincontrol.h:93
bool fOverrideFeeRate
Override automatic min/max checks on fee, m_feerate must be set if true.
Definition coincontrol.h:95
std::optional< CFeeRate > m_feerate
Override the wallet's fee rate if set.
Definition coincontrol.h:97
bool m_include_unsafe_inputs
If false, only safe inputs will be used.
Definition coincontrol.h:90
bool m_avoid_partial_spends
Avoid partial use of funds sent to a given address.
uint32_t m_version
Version.
CTxDestination destChange
Custom change destination, if not set an address is generated.
Definition coincontrol.h:86
std::optional< uint32_t > m_locktime
Locktime.
CFeeRate()=default
Fee rate of 0 satoshis per 0 vB.
CChain m_chain
Definition validation.h:630
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
Coin Control Features.
Definition coincontrol.h:83
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition string.h:250
util::Result< CreatedTransactionResult > CreateTransaction(CWallet &wallet, const std::vector< CRecipient > &vecSend, std::optional< unsigned int > change_pos, const CCoinControl &coin_control, bool sign)
Create a new transaction paying the recipients with a set of coins selected by SelectCoins(); Also cr...
Definition spend.cpp:1442
FUZZ_TARGET(coin_grinder)
constexpr auto OUTPUT_TYPES
Definition outputtype.h:26
@ OP_RETURN
Definition script.h:112
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
node::NodeContext m_node
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
std::unique_ptr< interfaces::Chain > chain
Definition context.h:80
Wraps a descriptor wallet for fuzzing.
Definition wallet.h:23
State of transaction confirmed in a block.
Definition transaction.h:34
#define LOCK(cs)
Definition sync.h:268
SeedRandomStateForTest(SeedRand::ZEROS)
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition util.cpp:34
CTxDestination ConsumeTxDestination(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition util.cpp:184
CAmount ConsumeMoney(FuzzedDataProvider &fuzzed_data_provider, const std::optional< CAmount > &max) noexcept
Definition util.cpp:29
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition util.h:37
@ ZEROS
Seed with a compile time constant of zeros.
Definition random.h:19
assert(!tx.IsCoinBase())