fuzz coverage

Coverage Report

Created: 2025-06-01 19:34

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