Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
util
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
8
#include <
test/fuzz/FuzzedDataProvider.h
>
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
18
namespace
wallet
{
19
23
struct
FuzzedWallet
{
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
51
FlatSigningProvider
keys;
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
}
67
CTxDestination
GetDestination
(
FuzzedDataProvider
& fuzzed_data_provider)
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
FuzzedDataProvider.h
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition
addresstype.cpp:166
CTxDestination
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
Definition
addresstype.h:143
Assert
#define Assert(val)
Identity function.
Definition
check.h:113
CScript
Serialized script, used inside transaction inputs and outputs.
Definition
script.h:405
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition
FuzzedDataProvider.h:289
FuzzedDataProvider::PickValueInArray
T PickValueInArray(const T(&array)[size])
Definition
FuzzedDataProvider.h:304
interfaces::Chain
Definition
chain.h:118
interfaces::Chain::getHeight
virtual std::optional< int > getHeight()=0
interfaces::Chain::getBlockHash
virtual uint256 getBlockHash(int height)=0
Get block hash. Height must be valid or this function will abort.
wallet::WalletDescriptor
Descriptor with some wallet metadata.
Definition
walletutil.h:64
wallet::WalletDescriptor::descriptor
std::shared_ptr< Descriptor > descriptor
Definition
walletutil.h:66
Parse
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition
client.cpp:395
coincontrol.h
fuzz.h
wallet
Definition
wallet_balance.cpp:26
wallet::CreateMockableWalletDatabase
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
Definition
util.cpp:211
wallet::WALLET_FLAG_DESCRIPTORS
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition
walletutil.h:53
OUTPUT_TYPES
static constexpr auto OUTPUT_TYPES
Definition
outputtype.h:26
policy.h
name
const char * name
Definition
rest.cpp:48
spend.h
FlatSigningProvider
Definition
signingprovider.h:224
FlatSigningProvider::keys
std::map< CKeyID, CKey > keys
Definition
signingprovider.h:228
tinyformat::RuntimeFormat
Definition
tinyformat.h:184
wallet::FuzzedWallet::FuzzedWallet
FuzzedWallet(interfaces::Chain &chain, const std::string &name, const std::string &seed_insecure)
Definition
wallet.h:25
wallet::FuzzedWallet::ImportDescriptors
void ImportDescriptors(const std::string &seed_insecure)
Definition
wallet.h:38
wallet::FuzzedWallet::GetScriptPubKey
CScript GetScriptPubKey(FuzzedDataProvider &fuzzed_data_provider)
Definition
wallet.h:76
wallet::FuzzedWallet::GetDestination
CTxDestination GetDestination(FuzzedDataProvider &fuzzed_data_provider)
Definition
wallet.h:67
LOCK
#define LOCK(cs)
Definition
sync.h:258
util.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition
tinyformat.h:1172
assert
assert(!tx.IsCoinBase())
fees.h
util.h
wallet.h
Generated on
for Bitcoin Core by
1.17.0