Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ BITCOIN_TESTS += wallet/test/db_tests.cpp
endif

FUZZ_WALLET_SRC = \
wallet/test/fuzz/coincontrol.cpp \
wallet/test/fuzz/coinselection.cpp

if USE_SQLITE
Expand Down
89 changes: 89 additions & 0 deletions src/wallet/test/fuzz/coincontrol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/util/setup_common.h>
#include <wallet/coincontrol.h>
#include <wallet/test/util.h>

namespace wallet {
namespace {

const TestingSetup* g_setup;

void initialize_coincontrol()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
g_setup = testing_setup.get();
}

FUZZ_TARGET(coincontrol, .init = initialize_coincontrol)
{
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const auto& node = g_setup->m_node;
ArgsManager& args = *node.args;

// for GetBoolArg to return true sometimes
args.ForceSetArg("-avoidpartialspends", fuzzed_data_provider.ConsumeBool()?"1":"0");

CCoinControl coin_control;
COutPoint out_point;

LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
{
CallOneOf(
fuzzed_data_provider,
[&] {
std::optional<COutPoint> optional_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
if (!optional_out_point) {
return;
}
out_point = *optional_out_point;
},
[&] {
(void)coin_control.HasSelected();
},
[&] {
(void)coin_control.IsSelected(out_point);
},
[&] {
(void)coin_control.IsExternalSelected(out_point);
},
[&] {
CTxOut txout;
(void)coin_control.GetExternalOutput(out_point, txout);
},
[&] {
(void)coin_control.Select(out_point);
},
[&] {
const CTxOut tx_out{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)};
(void)coin_control.SelectExternal(out_point, tx_out);
},
[&] {
(void)coin_control.UnSelect(out_point);
},
[&] {
(void)coin_control.UnSelectAll();
},
[&] {
std::vector<COutPoint> selected;
coin_control.ListSelected(selected);
},
[&] {
int64_t weight{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
(void)coin_control.SetInputWeight(out_point, weight);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

27902: should skip this line IMO and next one, because it's segwit related. I will prepare fix to remove GetInputWeight and HasInputWeight from coin_control

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? What harm does keeping it do? it seems it just keeps our code closer to upstream w/o downside

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because we use everywhere a size of transaction instead of "weight".

Having leftover helpers with HasInputWeight and GetInpuptWeight can cause an error in future, if some new (especially backported) code will use it. If these helpers won't exist, freshly backported code will get compilation error -> a developer got a signal to replace "weight" to "size". Otherwise it maybe a silent bug

},
[&] {
// Condition to avoid the assertion in GetInputWeight
if (coin_control.HasInputWeight(out_point)) {
(void)coin_control.GetInputWeight(out_point);
}
});
}
}
} // namespace
} // namespace wallet