fuzz coverage

Coverage Report

Created: 2025-10-29 15:27

/Users/eugenesiegel/btc/bitcoin/src/wallet/walletdb.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#ifndef BITCOIN_WALLET_WALLETDB_H
7
#define BITCOIN_WALLET_WALLETDB_H
8
9
#include <key.h>
10
#include <primitives/transaction_identifier.h>
11
#include <script/sign.h>
12
#include <wallet/db.h>
13
#include <wallet/walletutil.h>
14
15
#include <cstdint>
16
#include <string>
17
#include <vector>
18
19
class CScript;
20
class uint160;
21
class uint256;
22
struct CBlockLocator;
23
24
namespace wallet {
25
class CMasterKey;
26
class CWallet;
27
class CWalletTx;
28
struct WalletContext;
29
30
// Logs information about the database, including available engines, features, and other capabilities
31
void LogDBInfo();
32
33
/**
34
 * Overview of wallet database classes:
35
 *
36
 * - WalletBatch is an abstract modifier object for the wallet database, and encapsulates a database
37
 *   batch update as well as methods to act on the database. It should be agnostic to the database implementation.
38
 */
39
40
/** Error statuses for the wallet database.
41
 * Values are in order of severity. When multiple errors occur, the most severe (highest value) will be returned.
42
 */
43
enum class DBErrors : int
44
{
45
    LOAD_OK = 0,
46
    NEED_RESCAN = 1,
47
    NEED_REWRITE = 2,
48
    EXTERNAL_SIGNER_SUPPORT_REQUIRED = 3,
49
    NONCRITICAL_ERROR = 4,
50
    TOO_NEW = 5,
51
    UNKNOWN_DESCRIPTOR = 6,
52
    LOAD_FAIL = 7,
53
    UNEXPECTED_LEGACY_ENTRY = 8,
54
    LEGACY_WALLET = 9,
55
    CORRUPT = 10,
56
};
57
58
namespace DBKeys {
59
extern const std::string ACENTRY;
60
extern const std::string ACTIVEEXTERNALSPK;
61
extern const std::string ACTIVEINTERNALSPK;
62
extern const std::string BESTBLOCK;
63
extern const std::string BESTBLOCK_NOMERKLE;
64
extern const std::string CRYPTED_KEY;
65
extern const std::string CSCRIPT;
66
extern const std::string DEFAULTKEY;
67
extern const std::string DESTDATA;
68
extern const std::string FLAGS;
69
extern const std::string HDCHAIN;
70
extern const std::string KEY;
71
extern const std::string KEYMETA;
72
extern const std::string LOCKED_UTXO;
73
extern const std::string MASTER_KEY;
74
extern const std::string MINVERSION;
75
extern const std::string NAME;
76
extern const std::string OLD_KEY;
77
extern const std::string ORDERPOSNEXT;
78
extern const std::string POOL;
79
extern const std::string PURPOSE;
80
extern const std::string SETTINGS;
81
extern const std::string TX;
82
extern const std::string VERSION;
83
extern const std::string WALLETDESCRIPTOR;
84
extern const std::string WALLETDESCRIPTORCKEY;
85
extern const std::string WALLETDESCRIPTORKEY;
86
extern const std::string WATCHMETA;
87
extern const std::string WATCHS;
88
89
// Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors.
90
extern const std::unordered_set<std::string> LEGACY_TYPES;
91
} // namespace DBKeys
92
93
/* simple HD chain data model */
94
class CHDChain
95
{
96
public:
97
    uint32_t nExternalChainCounter;
98
    uint32_t nInternalChainCounter;
99
    CKeyID seed_id; //!< seed hash160
100
    int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
101
    int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
102
103
    static const int VERSION_HD_BASE        = 1;
104
    static const int VERSION_HD_CHAIN_SPLIT = 2;
105
    static const int CURRENT_VERSION        = VERSION_HD_CHAIN_SPLIT;
106
    int nVersion;
107
108
0
    CHDChain() { SetNull(); }
109
110
    SERIALIZE_METHODS(CHDChain, obj)
111
0
    {
112
0
        READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
113
0
        if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
114
0
            READWRITE(obj.nInternalChainCounter);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
115
0
        }
116
0
    }
117
118
    void SetNull()
119
0
    {
120
0
        nVersion = CHDChain::CURRENT_VERSION;
121
0
        nExternalChainCounter = 0;
122
0
        nInternalChainCounter = 0;
123
0
        seed_id.SetNull();
124
0
    }
125
126
    bool operator==(const CHDChain& chain) const
127
0
    {
128
0
        return seed_id == chain.seed_id;
129
0
    }
130
};
131
132
class CKeyMetadata
133
{
134
public:
135
    static const int VERSION_BASIC=1;
136
    static const int VERSION_WITH_HDDATA=10;
137
    static const int VERSION_WITH_KEY_ORIGIN = 12;
138
    static const int CURRENT_VERSION=VERSION_WITH_KEY_ORIGIN;
139
    int nVersion;
140
    int64_t nCreateTime; // 0 means unknown
141
    std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
142
    CKeyID hd_seed_id; //id of the HD seed used to derive this key
143
    KeyOriginInfo key_origin; // Key origin info with path and fingerprint
144
    bool has_key_origin = false; //!< Whether the key_origin is useful
145
146
    CKeyMetadata()
147
0
    {
148
0
        SetNull();
149
0
    }
150
    explicit CKeyMetadata(int64_t nCreateTime_)
151
0
    {
152
0
        SetNull();
153
0
        nCreateTime = nCreateTime_;
154
0
    }
155
156
    SERIALIZE_METHODS(CKeyMetadata, obj)
157
0
    {
158
0
        READWRITE(obj.nVersion, obj.nCreateTime);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
        READWRITE(obj.nVersion, obj.nCreateTime);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
159
0
        if (obj.nVersion >= VERSION_WITH_HDDATA) {
160
0
            READWRITE(obj.hdKeypath, obj.hd_seed_id);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
            READWRITE(obj.hdKeypath, obj.hd_seed_id);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
161
0
        }
162
0
        if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
163
0
        {
164
0
            READWRITE(obj.key_origin);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
            READWRITE(obj.key_origin);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
165
0
            READWRITE(obj.has_key_origin);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
            READWRITE(obj.has_key_origin);
Line
Count
Source
145
0
#define READWRITE(...) (ser_action.SerReadWriteMany(s, __VA_ARGS__))
166
0
        }
167
0
    }
Unexecuted instantiation: _ZN6wallet12CKeyMetadata16SerializationOpsI10DataStreamS0_17ActionUnserializeEEvRT0_RT_T1_
Unexecuted instantiation: _ZN6wallet12CKeyMetadata16SerializationOpsI10DataStreamKS0_15ActionSerializeEEvRT0_RT_T1_
168
169
    void SetNull()
170
0
    {
171
0
        nVersion = CKeyMetadata::CURRENT_VERSION;
172
0
        nCreateTime = 0;
173
0
        hdKeypath.clear();
174
0
        hd_seed_id.SetNull();
175
0
        key_origin.clear();
176
0
        has_key_origin = false;
177
0
    }
178
};
179
180
struct DbTxnListener
181
{
182
    std::function<void()> on_commit, on_abort;
183
};
184
185
/** Access to the wallet database.
186
 * Opens the database and provides read and write access to it. Each read and write is its own transaction.
187
 * Multiple operation transactions can be started using TxnBegin() and committed using TxnCommit()
188
 * Otherwise the transaction will be committed when the object goes out of scope.
189
 * Optionally (on by default) it will flush to disk on close.
190
 * Every 1000 writes will automatically trigger a flush to disk.
191
 */
192
class WalletBatch
193
{
194
private:
195
    template <typename K, typename T>
196
    bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
197
0
    {
198
0
        if (!m_batch->Write(key, value, fOverwrite)) {
199
0
            return false;
200
0
        }
201
0
        return true;
202
0
    }
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEES9_EES9_EEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE22transaction_identifierILb0EEEENS_9CWalletTxEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CPubKeyEENS_12CKeyMetadataEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CPubKeyEENS3_INS2_6vectorIh16secure_allocatorIhEEE7uint256EEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CPubKeyEENS3_INS2_6vectorIhNS7_IhEEEE7uint256EEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEjEENS_10CMasterKeyEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CScriptEENS_12CKeyMetadataEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CScriptEEhEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE13CBlockLocatorEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEExEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEhEE7uint256EEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_I7uint2567CPubKeyEEEENS3_INS2_6vectorIh16secure_allocatorIhEEESA_EEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_I7uint2567CPubKeyEEEENS2_6vectorIhNS7_IhEEEEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7uint256EENS_16WalletDescriptorEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS3_INS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7uint256EENS3_IjjEEEENS2_6vectorIhNS7_IhEEEEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS3_INS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7uint256EEjEENS2_6vectorIhNS7_IhEEEEEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_I22transaction_identifierILb0EEjEEEEhEEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_IS9_S9_EEEES9_EEbRKT_RKT0_b
Unexecuted instantiation: _ZN6wallet11WalletBatch7WriteICINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEyEEbRKT_RKT0_b
203
204
    template <typename K>
205
    bool EraseIC(const K& key)
206
0
    {
207
0
        if (!m_batch->Erase(key)) {
208
0
            return false;
209
0
        }
210
0
        return true;
211
0
    }
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEES9_EEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7uint256EEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CPubKeyEEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEjEEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE7CScriptEEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEhEEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_I7uint2567CPubKeyEEEEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_I22transaction_identifierILb0EEjEEEEEEbRKT_
Unexecuted instantiation: _ZN6wallet11WalletBatch7EraseICINSt3__14pairINS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEENS3_IS9_S9_EEEEEEbRKT_
212
213
public:
214
    explicit WalletBatch(WalletDatabase &database) :
215
0
        m_batch(database.MakeBatch())
216
0
    {
217
0
    }
218
    WalletBatch(const WalletBatch&) = delete;
219
    WalletBatch& operator=(const WalletBatch&) = delete;
220
221
    bool WriteName(const std::string& strAddress, const std::string& strName);
222
    bool EraseName(const std::string& strAddress);
223
224
    bool WritePurpose(const std::string& strAddress, const std::string& purpose);
225
    bool ErasePurpose(const std::string& strAddress);
226
227
    bool WriteTx(const CWalletTx& wtx);
228
    bool EraseTx(Txid hash);
229
230
    bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
231
    bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
232
    bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
233
    bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
234
    bool EraseMasterKey(unsigned int id);
235
236
    bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
237
    bool EraseWatchOnly(const CScript &script);
238
239
    bool WriteBestBlock(const CBlockLocator& locator);
240
    bool ReadBestBlock(CBlockLocator& locator);
241
242
    // Returns true if wallet stores encryption keys
243
    bool IsEncrypted();
244
245
    bool WriteOrderPosNext(int64_t nOrderPosNext);
246
247
    bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
248
    bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
249
    bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
250
    bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
251
    bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
252
    bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
253
    bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
254
255
    bool WriteLockedUTXO(const COutPoint& output);
256
    bool EraseLockedUTXO(const COutPoint& output);
257
258
    bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent);
259
    bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request);
260
    bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id);
261
    bool EraseAddressData(const CTxDestination& dest);
262
263
    bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
264
    bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
265
266
    DBErrors LoadWallet(CWallet* pwallet);
267
268
    //! Delete records of the given types
269
    bool EraseRecords(const std::unordered_set<std::string>& types);
270
271
    bool WriteWalletFlags(const uint64_t flags);
272
    //! Begin a new transaction
273
    bool TxnBegin();
274
    //! Commit current transaction
275
    bool TxnCommit();
276
    //! Abort current transaction
277
    bool TxnAbort();
278
0
    bool HasActiveTxn() { return m_batch->HasActiveTxn(); }
279
280
    //! Registers db txn callback functions
281
    void RegisterTxnListener(const DbTxnListener& l);
282
283
private:
284
    std::unique_ptr<DatabaseBatch> m_batch;
285
286
    // External functions listening to the current db txn outcome.
287
    // Listeners are cleared at the end of the transaction.
288
    std::vector<DbTxnListener> m_txn_listeners;
289
};
290
291
/**
292
 * Executes the provided function 'func' within a database transaction context.
293
 *
294
 * This function ensures that all db modifications performed within 'func()' are
295
 * atomically committed to the db at the end of the process. And, in case of a
296
 * failure during execution, all performed changes are rolled back.
297
 *
298
 * @param database The db connection instance to perform the transaction on.
299
 * @param process_desc A description of the process being executed, used for logging purposes in the event of a failure.
300
 * @param func The function to be executed within the db txn context. It returns a boolean indicating whether to commit or roll back the txn.
301
 * @return true if the db txn executed successfully, false otherwise.
302
 */
303
bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func);
304
305
bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
306
bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
307
bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr);
308
bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr);
309
310
//! Returns true if there are any DBKeys::LEGACY_TYPES record in the wallet db
311
bool HasLegacyRecords(CWallet& wallet);
312
bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch);
313
} // namespace wallet
314
315
#endif // BITCOIN_WALLET_WALLETDB_H