Bitcoin Core 32.99.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 {
26
28{
29 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
30 g_setup = testing_setup.get();
31}
32
34{
36 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
38 const auto& node = g_setup->m_node;
39 Chainstate& chainstate{node.chainman->ActiveChainstate()};
40 ArgsManager& args = *node.args;
42 FuzzedWallet fuzzed_wallet{
44 "fuzzed_wallet_a",
45 "tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",
46 };
47
48 CCoinControl coin_control;
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);
56 coin_control.m_allow_other_inputs = fuzzed_data_provider.ConsumeBool();
57 coin_control.m_locktime = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
59
60 int next_locktime{0};
65 tx.vout.resize(1);
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;
80 CTxDestination destination;
83 [&] {
84 destination = fuzzed_wallet.GetDestination(fuzzed_data_provider);
85 },
86 [&] {
89 destination = CNoDestination{script};
90 },
91 [&] {
93 }
94 );
95 recipients.push_back({destination,
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
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:611
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition feerate.h:32
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:550
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
#define FUZZ_TARGET(...)
Definition fuzz.h:35
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition fuzz.h:22
Definition basic.cpp:11
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:1448
constexpr auto OUTPUT_TYPES
Definition outputtype.h:26
static CTransactionRef MakeTransactionRef(Tx &&txIn)
@ OP_RETURN
Definition script.h:112
node::NodeContext m_node
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Testing setup that configures a complete environment.
std::unique_ptr< interfaces::Chain > chain
Definition context.h:78
#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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46
FakeNodeClock clock