Branch data Line data Source code
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 : : #ifndef BITCOIN_TEST_FUZZ_UTIL_WALLET_H
6 : : #define BITCOIN_TEST_FUZZ_UTIL_WALLET_H
7 : :
8 : : #include <outputtype.h>
9 : : #include <policy/policy.h>
10 : : #include <test/fuzz/FuzzedDataProvider.h>
11 : : #include <test/fuzz/fuzz.h>
12 : : #include <test/fuzz/util.h>
13 : : #include <util/check.h>
14 : : #include <wallet/coincontrol.h>
15 : : #include <wallet/fees.h>
16 : : #include <wallet/scriptpubkeyman.h>
17 : : #include <wallet/spend.h>
18 : : #include <wallet/test/util.h>
19 : : #include <wallet/wallet.h>
20 : :
21 : : #include <array>
22 : : #include <cstddef>
23 : : #include <cstdint>
24 : : #include <memory>
25 : : #include <string>
26 : : #include <vector>
27 : :
28 : : namespace wallet {
29 : :
30 : : /**
31 : : * Wraps a descriptor wallet for fuzzing.
32 : : */
33 : : struct FuzzedWallet {
34 : : static constexpr size_t DESTINATION_POOL_SIZE{16};
35 : : static constexpr uint64_t REBUILD_INTERVAL{1000};
36 : :
37 : : std::shared_ptr<CWallet> wallet;
38 : :
39 : 1 : FuzzedWallet(interfaces::Chain& chain, const std::string& name, const std::string& seed_insecure)
40 [ - + - + : 3 : : m_chain{chain}, m_name{name}, m_seed_insecure{seed_insecure}
+ - ]
41 : : {
42 [ + - ]: 1 : Build();
43 [ - - ]: 1 : }
44 : :
45 : 1973 : void Reset()
46 : : {
47 [ + + ]: 1973 : if (++m_iterations >= REBUILD_INTERVAL) {
48 : 1 : Build();
49 : 1 : return;
50 : : }
51 : 1972 : m_cursors.fill(0);
52 : 1972 : m_change_cursors.fill(0);
53 : 1972 : {
54 : 1972 : LOCK(wallet->cs_wallet);
55 [ + - ]: 1972 : wallet->ClearInMemoryTxStateForTest();
56 [ - + ]: 1972 : assert(wallet->mapWallet.empty());
57 [ - + ]: 1972 : assert(wallet->GetTXOs().empty());
58 : 1972 : }
59 : : }
60 : :
61 : 424453 : CTxDestination GetDestination(FuzzedDataProvider& fuzzed_data_provider)
62 : : {
63 : 424453 : const auto type{fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES)};
64 : 424453 : const bool external{fuzzed_data_provider.ConsumeBool()};
65 : 424453 : const size_t i{TypeIndex(type)};
66 [ + + ]: 424453 : auto& pool{external ? m_dests : m_change_dests};
67 [ - + - + ]: 424453 : auto& cursor{external ? m_cursors.at(i) : m_change_cursors.at(i)};
68 : 424453 : const CTxDestination& dest{pool.at(i).at(cursor % DESTINATION_POOL_SIZE)};
69 : 424453 : ++cursor;
70 : 424453 : return dest;
71 : : }
72 : :
73 : : CScript GetScriptPubKey(FuzzedDataProvider& fuzzed_data_provider) { return GetScriptForDestination(GetDestination(fuzzed_data_provider)); }
74 : :
75 : 3946 : int64_t RangeTotal() const
76 : : {
77 : 3946 : int64_t total{0};
78 [ + + ]: 35514 : for (const DescriptorScriptPubKeyMan* spkm : m_spkms) total += spkm->GetEndRange();
79 : 3946 : return total;
80 : : }
81 : :
82 : : private:
83 : : using DestinationPool = std::array<std::vector<CTxDestination>, OUTPUT_TYPES.size()>;
84 : :
85 : : interfaces::Chain& m_chain;
86 : : const std::string m_name;
87 : : const std::string m_seed_insecure;
88 : :
89 : : using PoolCursors = std::array<size_t, OUTPUT_TYPES.size()>;
90 : :
91 : : DestinationPool m_dests;
92 : : DestinationPool m_change_dests;
93 : : PoolCursors m_cursors{};
94 : : PoolCursors m_change_cursors{};
95 : :
96 : 424453 : static size_t TypeIndex(OutputType type)
97 : : {
98 [ + - ]: 1337215 : for (size_t i = 0; i < OUTPUT_TYPES.size(); ++i) {
99 [ + + ]: 1337215 : if (OUTPUT_TYPES.at(i) == type) return i;
100 : : }
101 : 0 : assert(false);
102 : : }
103 : :
104 : : std::vector<DescriptorScriptPubKeyMan*> m_spkms;
105 : : uint64_t m_iterations{0};
106 : :
107 : 2 : void Build()
108 : : {
109 [ + + ]: 2 : m_spkms.clear();
110 [ + + ]: 10 : for (auto& pool : m_dests) pool.clear();
111 [ + + ]: 10 : for (auto& pool : m_change_dests) pool.clear();
112 : 2 : m_cursors.fill(0);
113 : 2 : m_change_cursors.fill(0);
114 : :
115 [ + - - + ]: 2 : wallet = std::make_shared<CWallet>(&m_chain, m_name, CreateMockableWalletDatabase());
116 : 2 : {
117 : 2 : LOCK(wallet->cs_wallet);
118 [ + - ]: 2 : wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
119 [ + - + - : 2 : auto height{*Assert(m_chain.getHeight())};
+ - ]
120 [ + - + - ]: 2 : wallet->SetLastBlockProcessed(height, m_chain.getBlockHash(height));
121 : 2 : }
122 : 2 : wallet->m_keypool_size = 1; // Avoid timeout in TopUp()
123 [ - + ]: 2 : assert(wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
124 : 2 : ImportDescriptors(m_seed_insecure);
125 : 2 : PrecomputeDestinations();
126 : :
127 : 2 : m_iterations = 0;
128 : 2 : }
129 : :
130 : 2 : void ImportDescriptors(const std::string& seed_insecure)
131 : : {
132 : 2 : const std::vector<std::string> DESCS{
133 : : "pkh(%s/%s/*)",
134 : : "sh(wpkh(%s/%s/*))",
135 : : "tr(%s/%s/*)",
136 : : "wpkh(%s/%s/*)",
137 : 18 : };
[ + - + +
- + + + -
- - - ]
138 : :
139 [ + + ]: 10 : for (const std::string& desc_fmt : DESCS) {
140 [ + + ]: 24 : for (bool internal : {true, false}) {
141 [ + - + - ]: 16 : const auto descriptor{strprintf(tfm::RuntimeFormat{desc_fmt}, "[5aa9973a/66h/4h/2h]" + seed_insecure, int{internal})};
142 : :
143 : 16 : FlatSigningProvider keys;
144 [ - + ]: 16 : std::string error;
145 [ - + + - ]: 32 : auto parsed_desc = std::move(Parse(descriptor, keys, error, /*require_checksum=*/false).at(0));
146 [ - + ]: 16 : assert(parsed_desc);
147 [ - + ]: 16 : assert(error.empty());
148 [ + - - + ]: 16 : assert(parsed_desc->IsRange());
149 [ + - - + ]: 16 : assert(parsed_desc->IsSingleType());
150 [ - + ]: 16 : assert(!keys.keys.empty());
151 [ + - + - ]: 16 : WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/0};
152 [ + - - + ]: 16 : assert(!wallet->GetDescriptorScriptPubKeyMan(w_desc));
153 [ + - ]: 16 : LOCK(wallet->cs_wallet);
154 [ + - + - : 16 : auto& spk_manager = Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", internal))->get();
+ - ]
155 [ + - + - : 16 : wallet->AddActiveScriptPubKeyMan(spk_manager.GetID(), *Assert(w_desc.descriptor->GetOutputType()), internal);
+ - + - ]
156 [ + - + - ]: 16 : m_spkms.push_back(&spk_manager);
157 : 16 : }
158 : : }
159 : 2 : }
160 : :
161 : 2 : void PrecomputeDestinations()
162 : : {
163 [ + + ]: 10 : for (size_t i = 0; i < OUTPUT_TYPES.size(); ++i) {
164 : 8 : const OutputType type{OUTPUT_TYPES.at(i)};
165 [ + + ]: 136 : for (size_t j = 0; j < DESTINATION_POOL_SIZE; ++j) {
166 [ + - + - : 256 : m_dests.at(i).push_back(*Assert(wallet->GetNewDestination(type, "")));
+ - ]
167 [ + - + - ]: 256 : m_change_dests.at(i).push_back(*Assert(wallet->GetNewChangeDestination(type)));
168 : : }
169 : : }
170 : 2 : }
171 : : };
172 : : } // namespace wallet
173 : :
174 : : #endif // BITCOIN_TEST_FUZZ_UTIL_WALLET_H
|