Coverage Report

Created: 2025-02-21 14:37

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