Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
coincontrol.cpp
Go to the documentation of this file.
1// Copyright (c) 2022-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>
10#include <wallet/test/util.h>
11
12namespace wallet {
13namespace {
14
15const TestingSetup* g_setup;
16
17void initialize_coincontrol()
18{
19 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
20 g_setup = testing_setup.get();
21}
22
23FUZZ_TARGET(coincontrol, .init = initialize_coincontrol)
24{
25 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
26 const auto& node = g_setup->m_node;
27 ArgsManager& args = *node.args;
28
29 // for GetBoolArg to return true sometimes
30 args.ForceSetArg("-avoidpartialspends", fuzzed_data_provider.ConsumeBool()?"1":"0");
31
32 CCoinControl coin_control;
33 COutPoint out_point;
34
35 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
36 {
38 fuzzed_data_provider,
39 [&] {
40 std::optional<COutPoint> optional_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
41 if (!optional_out_point) {
42 return;
43 }
44 out_point = *optional_out_point;
45 },
46 [&] {
47 (void)coin_control.HasSelected();
48 },
49 [&] {
50 (void)coin_control.IsSelected(out_point);
51 },
52 [&] {
53 (void)coin_control.IsExternalSelected(out_point);
54 },
55 [&] {
56 (void)coin_control.GetExternalOutput(out_point);
57 },
58 [&] {
59 (void)coin_control.Select(out_point);
60 },
61 [&] {
62 const CTxOut tx_out{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)};
63 (void)coin_control.Select(out_point).SetTxOut(tx_out);
64 },
65 [&] {
66 (void)coin_control.UnSelect(out_point);
67 },
68 [&] {
69 (void)coin_control.UnSelectAll();
70 },
71 [&] {
72 (void)coin_control.ListSelected();
73 },
74 [&] {
75 int64_t weight{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
76 (void)coin_control.SetInputWeight(out_point, weight);
77 },
78 [&] {
79 (void)coin_control.GetInputWeight(out_point);
80 });
81 }
82}
83} // namespace
84} // namespace wallet
ArgsManager & args
Definition bitcoind.cpp:277
const TestingSetup * g_setup
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition args.cpp:571
bool IsSelected(const COutPoint &outpoint) const
Returns true if the given output is pre-selected.
std::optional< CTxOut > GetExternalOutput(const COutPoint &outpoint) const
Returns the external output for the given outpoint if it exists.
std::optional< int64_t > GetInputWeight(const COutPoint &outpoint) const
Returns the input weight.
void UnSelectAll()
Unselects all outputs.
void UnSelect(const COutPoint &outpoint)
Unselects the given output.
PreselectedInput & Select(const COutPoint &outpoint)
Lock-in the given output for spending.
bool HasSelected() const
Returns true if there are pre-selected inputs.
bool IsExternalSelected(const COutPoint &outpoint) const
Returns true if the given output is selected as an external input.
void SetInputWeight(const COutPoint &outpoint, int64_t weight)
Set an input's weight.
std::vector< COutPoint > ListSelected() const
List the selected inputs.
Coin Control Features.
Definition coincontrol.h:84
void SetTxOut(const CTxOut &txout)
Set the previous output for this input.
#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
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
CScript ConsumeScript(FuzzedDataProvider &fuzzed_data_provider, const bool maybe_p2wsh) noexcept
Definition util.cpp:98
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:35
std::optional< T > ConsumeDeserializable(FuzzedDataProvider &fuzzed_data_provider, const P &params, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition util.h:100