Bitcoin Core  22.99.0
P2P Digital Currency
script_descriptor_cache.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <pubkey.h>
6 #include <script/descriptor.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 
11 #include <cstdint>
12 #include <optional>
13 #include <string>
14 #include <vector>
15 
16 FUZZ_TARGET(script_descriptor_cache)
17 {
18  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19  DescriptorCache descriptor_cache;
20  while (fuzzed_data_provider.ConsumeBool()) {
21  const std::vector<uint8_t> code = fuzzed_data_provider.ConsumeBytes<uint8_t>(BIP32_EXTKEY_SIZE);
22  if (code.size() == BIP32_EXTKEY_SIZE) {
23  CExtPubKey xpub;
24  xpub.Decode(code.data());
25  const uint32_t key_exp_pos = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
26  CExtPubKey xpub_fetched;
27  if (fuzzed_data_provider.ConsumeBool()) {
28  (void)descriptor_cache.GetCachedParentExtPubKey(key_exp_pos, xpub_fetched);
29  descriptor_cache.CacheParentExtPubKey(key_exp_pos, xpub);
30  assert(descriptor_cache.GetCachedParentExtPubKey(key_exp_pos, xpub_fetched));
31  } else {
32  const uint32_t der_index = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
33  (void)descriptor_cache.GetCachedDerivedExtPubKey(key_exp_pos, der_index, xpub_fetched);
34  descriptor_cache.CacheDerivedExtPubKey(key_exp_pos, der_index, xpub);
35  assert(descriptor_cache.GetCachedDerivedExtPubKey(key_exp_pos, der_index, xpub_fetched));
36  }
37  assert(xpub == xpub_fetched);
38  }
39  (void)descriptor_cache.GetCachedParentExtPubKeys();
40  (void)descriptor_cache.GetCachedDerivedExtPubKeys();
41  }
42 }
DescriptorCache::CacheParentExtPubKey
void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub)
Cache a parent xpub.
Definition: descriptor.cpp:1417
BIP32_EXTKEY_SIZE
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
assert
assert(!tx.IsCoinBase())
DescriptorCache::GetCachedParentExtPubKey
bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const
Retrieve a cached parent xpub.
Definition: descriptor.cpp:1433
DescriptorCache::GetCachedParentExtPubKeys
const ExtPubKeyMap GetCachedParentExtPubKeys() const
Retrieve all cached parent xpubs.
Definition: descriptor.cpp:1500
util.h
pubkey.h
CExtPubKey::Decode
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:347
FUZZ_TARGET
FUZZ_TARGET(script_descriptor_cache)
Definition: script_descriptor_cache.cpp:16
FuzzedDataProvider.h
DescriptorCache
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
DescriptorCache::CacheDerivedExtPubKey
void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey &xpub)
Cache an xpub derived at an index.
Definition: descriptor.cpp:1422
fuzz.h
DescriptorCache::GetCachedDerivedExtPubKey
bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey &xpub) const
Retrieve a cached xpub derived at an index.
Definition: descriptor.cpp:1441
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
DescriptorCache::GetCachedDerivedExtPubKeys
const std::unordered_map< uint32_t, ExtPubKeyMap > GetCachedDerivedExtPubKeys() const
Retrieve all cached derived xpubs.
Definition: descriptor.cpp:1505
CExtPubKey
Definition: pubkey.h:287
descriptor.h
FuzzedDataProvider::ConsumeBytes
std::vector< T > ConsumeBytes(size_t num_bytes)
Definition: FuzzedDataProvider.h:108