Coverage Report

Created: 2024-11-15 12:18

/root/bitcoin/src/wallet/test/fuzz/spend.cpp
Line
Count
Source (jump to first uncovered line)
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 <test/fuzz/FuzzedDataProvider.h>
6
#include <test/fuzz/fuzz.h>
7
#include <test/fuzz/util.h>
8
#include <test/fuzz/util/wallet.h>
9
#include <test/util/random.h>
10
#include <test/util/setup_common.h>
11
#include <wallet/coincontrol.h>
12
#include <wallet/context.h>
13
#include <wallet/spend.h>
14
#include <wallet/test/util.h>
15
#include <wallet/wallet.h>
16
#include <validation.h>
17
#include <addresstype.h>
18
19
using util::ToString;
20
21
namespace wallet {
22
namespace {
23
const TestingSetup* g_setup;
24
25
void initialize_setup()
26
0
{
27
0
    static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
28
0
    g_setup = testing_setup.get();
29
0
}
30
31
FUZZ_TARGET(wallet_create_transaction, .init = initialize_setup)
32
0
{
33
0
    SeedRandomStateForTest(SeedRand::ZEROS);
34
0
    FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
35
0
    const auto& node = g_setup->m_node;
36
0
    Chainstate& chainstate{node.chainman->ActiveChainstate()};
37
0
    ArgsManager& args = *node.args;
38
0
    args.ForceSetArg("-dustrelayfee", ToString(fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, MAX_MONEY)));
39
0
    FuzzedWallet fuzzed_wallet{
40
0
        *g_setup->m_node.chain,
41
0
        "fuzzed_wallet_a",
42
0
        "tprv8ZgxMBicQKsPd1QwsGgzfu2pcPYbBosZhJknqreRHgsWx32nNEhMjGQX2cgFL8n6wz9xdDYwLcs78N4nsCo32cxEX8RBtwGsEGgybLiQJfk",
43
0
    };
44
45
0
    CCoinControl coin_control;
46
0
    if (fuzzed_data_provider.ConsumeBool()) coin_control.m_version = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
47
0
    coin_control.m_avoid_partial_spends = fuzzed_data_provider.ConsumeBool();
48
0
    coin_control.m_include_unsafe_inputs = fuzzed_data_provider.ConsumeBool();
49
0
    if (fuzzed_data_provider.ConsumeBool()) coin_control.m_confirm_target = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 999'000);
50
0
    coin_control.destChange = fuzzed_data_provider.ConsumeBool() ? fuzzed_wallet.GetDestination(fuzzed_data_provider) : ConsumeTxDestination(fuzzed_data_provider);
51
0
    if (fuzzed_data_provider.ConsumeBool()) coin_control.m_change_type = fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES);
52
0
    if (fuzzed_data_provider.ConsumeBool()) coin_control.m_feerate = CFeeRate(ConsumeMoney(fuzzed_data_provider, /*max=*/COIN));
53
0
    coin_control.m_allow_other_inputs = fuzzed_data_provider.ConsumeBool();
54
0
    coin_control.m_locktime = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
55
0
    coin_control.fOverrideFeeRate = fuzzed_data_provider.ConsumeBool();
56
57
0
    int next_locktime{0};
58
0
    CAmount all_values{0};
59
0
    LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
60
0
    {
61
0
        CMutableTransaction tx;
62
0
        tx.nLockTime = next_locktime++;
63
0
        tx.vout.resize(1);
64
0
        CAmount n_value{ConsumeMoney(fuzzed_data_provider)};
65
0
        all_values += n_value;
66
0
        if (all_values > MAX_MONEY) return;
67
0
        tx.vout[0].nValue = n_value;
68
0
        tx.vout[0].scriptPubKey = GetScriptForDestination(fuzzed_wallet.GetDestination(fuzzed_data_provider));
69
0
        LOCK(fuzzed_wallet.wallet->cs_wallet);
70
0
        auto txid{tx.GetHash()};
71
0
        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}))};
72
0
        assert(ret.second);
73
0
    }
74
75
0
    std::vector<CRecipient> recipients;
76
0
    LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100) {
77
0
        CTxDestination destination;
78
0
        CallOneOf(
79
0
            fuzzed_data_provider,
80
0
            [&] {
81
0
                destination = fuzzed_wallet.GetDestination(fuzzed_data_provider);
82
0
            },
83
0
            [&] {
84
0
                CScript script;
85
0
                script << OP_RETURN;
86
0
                destination = CNoDestination{script};
87
0
            },
88
0
            [&] {
89
0
                destination = ConsumeTxDestination(fuzzed_data_provider);
90
0
            }
91
0
        );
92
0
        recipients.push_back({destination,
93
0
                              /*nAmount=*/ConsumeMoney(fuzzed_data_provider),
94
0
                              /*fSubtractFeeFromAmount=*/fuzzed_data_provider.ConsumeBool()});
95
0
    }
96
97
0
    std::optional<unsigned int> change_pos;
98
0
    if (fuzzed_data_provider.ConsumeBool()) change_pos = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
99
0
    (void)CreateTransaction(*fuzzed_wallet.wallet, recipients, change_pos, coin_control);
100
0
}
101
} // namespace
102
} // namespace wallet