Bitcoin Core 29.99.0
P2P Digital Currency
wallet.h
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#ifndef BITCOIN_TEST_FUZZ_UTIL_WALLET_H
6#define BITCOIN_TEST_FUZZ_UTIL_WALLET_H
7
9#include <test/fuzz/fuzz.h>
10#include <test/fuzz/util.h>
11#include <policy/policy.h>
12#include <wallet/coincontrol.h>
13#include <wallet/fees.h>
14#include <wallet/spend.h>
15#include <wallet/test/util.h>
16#include <wallet/wallet.h>
17
18namespace wallet {
19
24 std::shared_ptr<CWallet> wallet;
25 FuzzedWallet(interfaces::Chain& chain, const std::string& name, const std::string& seed_insecure)
26 {
27 wallet = std::make_shared<CWallet>(&chain, name, CreateMockableWalletDatabase());
28 {
29 LOCK(wallet->cs_wallet);
30 wallet->SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
31 auto height{*Assert(chain.getHeight())};
32 wallet->SetLastBlockProcessed(height, chain.getBlockHash(height));
33 }
34 wallet->m_keypool_size = 1; // Avoid timeout in TopUp()
35 assert(wallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
36 ImportDescriptors(seed_insecure);
37 }
38 void ImportDescriptors(const std::string& seed_insecure)
39 {
40 const std::vector<std::string> DESCS{
41 "pkh(%s/%s/*)",
42 "sh(wpkh(%s/%s/*))",
43 "tr(%s/%s/*)",
44 "wpkh(%s/%s/*)",
45 };
46
47 for (const std::string& desc_fmt : DESCS) {
48 for (bool internal : {true, false}) {
49 const auto descriptor{strprintf(tfm::RuntimeFormat{desc_fmt}, "[5aa9973a/66h/4h/2h]" + seed_insecure, int{internal})};
50
52 std::string error;
53 auto parsed_desc = std::move(Parse(descriptor, keys, error, /*require_checksum=*/false).at(0));
54 assert(parsed_desc);
55 assert(error.empty());
56 assert(parsed_desc->IsRange());
57 assert(parsed_desc->IsSingleType());
58 assert(!keys.keys.empty());
59 WalletDescriptor w_desc{std::move(parsed_desc), /*creation_time=*/0, /*range_start=*/0, /*range_end=*/1, /*next_index=*/0};
60 assert(!wallet->GetDescriptorScriptPubKeyMan(w_desc));
61 LOCK(wallet->cs_wallet);
62 auto& spk_manager = Assert(wallet->AddWalletDescriptor(w_desc, keys, /*label=*/"", internal))->get();
63 wallet->AddActiveScriptPubKeyMan(spk_manager.GetID(), *Assert(w_desc.descriptor->GetOutputType()), internal);
64 }
65 }
66 }
68 {
69 auto type{fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES)};
70 if (fuzzed_data_provider.ConsumeBool()) {
71 return *Assert(wallet->GetNewDestination(type, ""));
72 } else {
73 return *Assert(wallet->GetNewChangeDestination(type));
74 }
75 }
76 CScript GetScriptPubKey(FuzzedDataProvider& fuzzed_data_provider) { return GetScriptForDestination(GetDestination(fuzzed_data_provider)); }
77};
78}
79
80#endif // BITCOIN_TEST_FUZZ_UTIL_WALLET_H
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.
Definition: addresstype.h:143
#define Assert(val)
Identity function.
Definition: check.h:106
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
T PickValueInArray(const T(&array)[size])
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:130
virtual std::optional< int > getHeight()=0
Get current chain height, not including genesis block (returns 0 if chain only contains genesis block...
virtual uint256 getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
Descriptor with some wallet metadata.
Definition: walletutil.h:85
static UniValue Parse(std::string_view raw)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:317
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition: util.cpp:186
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:74
static constexpr auto OUTPUT_TYPES
Definition: outputtype.h:25
const char * name
Definition: rest.cpp:49
std::map< CKeyID, CKey > keys
Wraps a descriptor wallet for fuzzing.
Definition: wallet.h:23
FuzzedWallet(interfaces::Chain &chain, const std::string &name, const std::string &seed_insecure)
Definition: wallet.h:25
void ImportDescriptors(const std::string &seed_insecure)
Definition: wallet.h:38
CScript GetScriptPubKey(FuzzedDataProvider &fuzzed_data_provider)
Definition: wallet.h:76
CTxDestination GetDestination(FuzzedDataProvider &fuzzed_data_provider)
Definition: wallet.h:67
std::shared_ptr< CWallet > wallet
Definition: wallet.h:24
#define LOCK(cs)
Definition: sync.h:265
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
assert(!tx.IsCoinBase())