Coverage Report

Created: 2025-06-10 13:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/wallet/walletdb.cpp
Line
Count
Source
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
#include <bitcoin-build-config.h> // IWYU pragma: keep
7
8
#include <wallet/walletdb.h>
9
10
#include <common/system.h>
11
#include <key_io.h>
12
#include <protocol.h>
13
#include <script/script.h>
14
#include <serialize.h>
15
#include <sync.h>
16
#include <util/bip32.h>
17
#include <util/check.h>
18
#include <util/fs.h>
19
#include <util/transaction_identifier.h>
20
#include <util/time.h>
21
#include <util/translation.h>
22
#include <wallet/migrate.h>
23
#include <wallet/sqlite.h>
24
#include <wallet/wallet.h>
25
26
#include <atomic>
27
#include <optional>
28
#include <string>
29
30
namespace wallet {
31
namespace DBKeys {
32
const std::string ACENTRY{"acentry"};
33
const std::string ACTIVEEXTERNALSPK{"activeexternalspk"};
34
const std::string ACTIVEINTERNALSPK{"activeinternalspk"};
35
const std::string BESTBLOCK_NOMERKLE{"bestblock_nomerkle"};
36
const std::string BESTBLOCK{"bestblock"};
37
const std::string CRYPTED_KEY{"ckey"};
38
const std::string CSCRIPT{"cscript"};
39
const std::string DEFAULTKEY{"defaultkey"};
40
const std::string DESTDATA{"destdata"};
41
const std::string FLAGS{"flags"};
42
const std::string HDCHAIN{"hdchain"};
43
const std::string KEYMETA{"keymeta"};
44
const std::string KEY{"key"};
45
const std::string LOCKED_UTXO{"lockedutxo"};
46
const std::string MASTER_KEY{"mkey"};
47
const std::string MINVERSION{"minversion"};
48
const std::string NAME{"name"};
49
const std::string OLD_KEY{"wkey"};
50
const std::string ORDERPOSNEXT{"orderposnext"};
51
const std::string POOL{"pool"};
52
const std::string PURPOSE{"purpose"};
53
const std::string SETTINGS{"settings"};
54
const std::string TX{"tx"};
55
const std::string VERSION{"version"};
56
const std::string WALLETDESCRIPTOR{"walletdescriptor"};
57
const std::string WALLETDESCRIPTORCACHE{"walletdescriptorcache"};
58
const std::string WALLETDESCRIPTORLHCACHE{"walletdescriptorlhcache"};
59
const std::string WALLETDESCRIPTORCKEY{"walletdescriptorckey"};
60
const std::string WALLETDESCRIPTORKEY{"walletdescriptorkey"};
61
const std::string WATCHMETA{"watchmeta"};
62
const std::string WATCHS{"watchs"};
63
const std::unordered_set<std::string> LEGACY_TYPES{CRYPTED_KEY, CSCRIPT, DEFAULTKEY, HDCHAIN, KEYMETA, KEY, OLD_KEY, POOL, WATCHMETA, WATCHS};
64
} // namespace DBKeys
65
66
//
67
// WalletBatch
68
//
69
70
bool WalletBatch::WriteName(const std::string& strAddress, const std::string& strName)
71
0
{
72
0
    return WriteIC(std::make_pair(DBKeys::NAME, strAddress), strName);
73
0
}
74
75
bool WalletBatch::EraseName(const std::string& strAddress)
76
0
{
77
    // This should only be used for sending addresses, never for receiving addresses,
78
    // receiving addresses must always have an address book entry if they're not change return.
79
0
    return EraseIC(std::make_pair(DBKeys::NAME, strAddress));
80
0
}
81
82
bool WalletBatch::WritePurpose(const std::string& strAddress, const std::string& strPurpose)
83
0
{
84
0
    return WriteIC(std::make_pair(DBKeys::PURPOSE, strAddress), strPurpose);
85
0
}
86
87
bool WalletBatch::ErasePurpose(const std::string& strAddress)
88
0
{
89
0
    return EraseIC(std::make_pair(DBKeys::PURPOSE, strAddress));
90
0
}
91
92
bool WalletBatch::WriteTx(const CWalletTx& wtx)
93
0
{
94
0
    return WriteIC(std::make_pair(DBKeys::TX, wtx.GetHash()), wtx);
95
0
}
96
97
bool WalletBatch::EraseTx(Txid hash)
98
0
{
99
0
    return EraseIC(std::make_pair(DBKeys::TX, hash.ToUint256()));
100
0
}
101
102
bool WalletBatch::WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite)
103
0
{
104
0
    return WriteIC(std::make_pair(DBKeys::KEYMETA, pubkey), meta, overwrite);
105
0
}
106
107
bool WalletBatch::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
108
0
{
109
0
    if (!WriteKeyMetadata(keyMeta, vchPubKey, false)) {
  Branch (109:9): [True: 0, False: 0]
110
0
        return false;
111
0
    }
112
113
    // hash pubkey/privkey to accelerate wallet load
114
0
    std::vector<unsigned char> vchKey;
115
0
    vchKey.reserve(vchPubKey.size() + vchPrivKey.size());
116
0
    vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
117
0
    vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end());
118
119
0
    return WriteIC(std::make_pair(DBKeys::KEY, vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey)), false);
120
0
}
121
122
bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
123
                                const std::vector<unsigned char>& vchCryptedSecret,
124
                                const CKeyMetadata &keyMeta)
125
0
{
126
0
    if (!WriteKeyMetadata(keyMeta, vchPubKey, true)) {
  Branch (126:9): [True: 0, False: 0]
127
0
        return false;
128
0
    }
129
130
    // Compute a checksum of the encrypted key
131
0
    uint256 checksum = Hash(vchCryptedSecret);
132
133
0
    const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey);
134
0
    if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
  Branch (134:9): [True: 0, False: 0]
135
        // It may already exist, so try writing just the checksum
136
0
        std::vector<unsigned char> val;
137
0
        if (!m_batch->Read(key, val)) {
  Branch (137:13): [True: 0, False: 0]
138
0
            return false;
139
0
        }
140
0
        if (!WriteIC(key, std::make_pair(val, checksum), true)) {
  Branch (140:13): [True: 0, False: 0]
141
0
            return false;
142
0
        }
143
0
    }
144
0
    EraseIC(std::make_pair(DBKeys::KEY, vchPubKey));
145
0
    return true;
146
0
}
147
148
bool WalletBatch::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
149
0
{
150
0
    return WriteIC(std::make_pair(DBKeys::MASTER_KEY, nID), kMasterKey, true);
151
0
}
152
153
bool WalletBatch::EraseMasterKey(unsigned int id)
154
0
{
155
0
    return EraseIC(std::make_pair(DBKeys::MASTER_KEY, id));
156
0
}
157
158
bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
159
0
{
160
0
    if (!WriteIC(std::make_pair(DBKeys::WATCHMETA, dest), keyMeta)) {
  Branch (160:9): [True: 0, False: 0]
161
0
        return false;
162
0
    }
163
0
    return WriteIC(std::make_pair(DBKeys::WATCHS, dest), uint8_t{'1'});
164
0
}
165
166
bool WalletBatch::EraseWatchOnly(const CScript &dest)
167
0
{
168
0
    if (!EraseIC(std::make_pair(DBKeys::WATCHMETA, dest))) {
  Branch (168:9): [True: 0, False: 0]
169
0
        return false;
170
0
    }
171
0
    return EraseIC(std::make_pair(DBKeys::WATCHS, dest));
172
0
}
173
174
bool WalletBatch::WriteBestBlock(const CBlockLocator& locator)
175
26.0k
{
176
26.0k
    WriteIC(DBKeys::BESTBLOCK, CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan
177
26.0k
    return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, locator);
178
26.0k
}
179
180
bool WalletBatch::ReadBestBlock(CBlockLocator& locator)
181
22.1k
{
182
22.1k
    if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
  Branch (182:9): [True: 22.1k, False: 0]
  Branch (182:54): [True: 0, False: 22.1k]
183
22.1k
    return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
184
22.1k
}
185
186
bool WalletBatch::IsEncrypted()
187
0
{
188
0
    DataStream prefix;
189
0
    prefix << DBKeys::MASTER_KEY;
190
0
    if (auto cursor = m_batch->GetNewPrefixCursor(prefix)) {
  Branch (190:14): [True: 0, False: 0]
191
0
        DataStream k, v;
192
0
        if (cursor->Next(k, v) == DatabaseCursor::Status::MORE) return true;
  Branch (192:13): [True: 0, False: 0]
193
0
    }
194
0
    return false;
195
0
}
196
197
bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
198
0
{
199
0
    return WriteIC(DBKeys::ORDERPOSNEXT, nOrderPosNext);
200
0
}
201
202
bool WalletBatch::WriteMinVersion(int nVersion)
203
11.0k
{
204
11.0k
    return WriteIC(DBKeys::MINVERSION, nVersion);
205
11.0k
}
206
207
bool WalletBatch::WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal)
208
88.7k
{
209
88.7k
    std::string key = internal ? DBKeys::ACTIVEINTERNALSPK : DBKeys::ACTIVEEXTERNALSPK;
  Branch (209:23): [True: 44.3k, False: 44.3k]
210
88.7k
    return WriteIC(make_pair(key, type), id);
211
88.7k
}
212
213
bool WalletBatch::EraseActiveScriptPubKeyMan(uint8_t type, bool internal)
214
0
{
215
0
    const std::string key{internal ? DBKeys::ACTIVEINTERNALSPK : DBKeys::ACTIVEEXTERNALSPK};
  Branch (215:27): [True: 0, False: 0]
216
0
    return EraseIC(make_pair(key, type));
217
0
}
218
219
bool WalletBatch::WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey)
220
88.7k
{
221
    // hash pubkey/privkey to accelerate wallet load
222
88.7k
    std::vector<unsigned char> key;
223
88.7k
    key.reserve(pubkey.size() + privkey.size());
224
88.7k
    key.insert(key.end(), pubkey.begin(), pubkey.end());
225
88.7k
    key.insert(key.end(), privkey.begin(), privkey.end());
226
227
88.7k
    return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)), std::make_pair(privkey, Hash(key)), false);
228
88.7k
}
229
230
bool WalletBatch::WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret)
231
0
{
232
0
    if (!WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORCKEY, std::make_pair(desc_id, pubkey)), secret, false)) {
  Branch (232:9): [True: 0, False: 0]
233
0
        return false;
234
0
    }
235
0
    EraseIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)));
236
0
    return true;
237
0
}
238
239
bool WalletBatch::WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor)
240
266k
{
241
266k
    return WriteIC(make_pair(DBKeys::WALLETDESCRIPTOR, desc_id), descriptor);
242
266k
}
243
244
bool WalletBatch::WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index)
245
0
{
246
0
    std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
247
0
    xpub.Encode(ser_xpub.data());
248
0
    return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), std::make_pair(key_exp_index, der_index)), ser_xpub);
249
0
}
250
251
bool WalletBatch::WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index)
252
88.7k
{
253
88.7k
    std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
254
88.7k
    xpub.Encode(ser_xpub.data());
255
88.7k
    return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), key_exp_index), ser_xpub);
256
88.7k
}
257
258
bool WalletBatch::WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index)
259
88.7k
{
260
88.7k
    std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
261
88.7k
    xpub.Encode(ser_xpub.data());
262
88.7k
    return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORLHCACHE, desc_id), key_exp_index), ser_xpub);
263
88.7k
}
264
265
bool WalletBatch::WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache)
266
887k
{
267
887k
    for (const auto& parent_xpub_pair : cache.GetCachedParentExtPubKeys()) {
  Branch (267:39): [True: 88.7k, False: 887k]
268
88.7k
        if (!WriteDescriptorParentCache(parent_xpub_pair.second, desc_id, parent_xpub_pair.first)) {
  Branch (268:13): [True: 0, False: 88.7k]
269
0
            return false;
270
0
        }
271
88.7k
    }
272
887k
    for (const auto& derived_xpub_map_pair : cache.GetCachedDerivedExtPubKeys()) {
  Branch (272:44): [True: 0, False: 887k]
273
0
        for (const auto& derived_xpub_pair : derived_xpub_map_pair.second) {
  Branch (273:44): [True: 0, False: 0]
274
0
            if (!WriteDescriptorDerivedCache(derived_xpub_pair.second, desc_id, derived_xpub_map_pair.first, derived_xpub_pair.first)) {
  Branch (274:17): [True: 0, False: 0]
275
0
                return false;
276
0
            }
277
0
        }
278
0
    }
279
887k
    for (const auto& lh_xpub_pair : cache.GetCachedLastHardenedExtPubKeys()) {
  Branch (279:35): [True: 88.7k, False: 887k]
280
88.7k
        if (!WriteDescriptorLastHardenedCache(lh_xpub_pair.second, desc_id, lh_xpub_pair.first)) {
  Branch (280:13): [True: 0, False: 88.7k]
281
0
            return false;
282
0
        }
283
88.7k
    }
284
887k
    return true;
285
887k
}
286
287
bool WalletBatch::WriteLockedUTXO(const COutPoint& output)
288
0
{
289
0
    return WriteIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)), uint8_t{'1'});
290
0
}
291
292
bool WalletBatch::EraseLockedUTXO(const COutPoint& output)
293
0
{
294
0
    return EraseIC(std::make_pair(DBKeys::LOCKED_UTXO, std::make_pair(output.hash, output.n)));
295
0
}
296
297
bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
298
0
{
299
0
    LOCK(pwallet->cs_wallet);
300
0
    try {
301
0
        CPubKey vchPubKey;
302
0
        ssKey >> vchPubKey;
303
0
        if (!vchPubKey.IsValid())
  Branch (303:13): [True: 0, False: 0]
304
0
        {
305
0
            strErr = "Error reading wallet database: CPubKey corrupt";
306
0
            return false;
307
0
        }
308
0
        CKey key;
309
0
        CPrivKey pkey;
310
0
        uint256 hash;
311
312
0
        ssValue >> pkey;
313
314
        // Old wallets store keys as DBKeys::KEY [pubkey] => [privkey]
315
        // ... which was slow for wallets with lots of keys, because the public key is re-derived from the private key
316
        // using EC operations as a checksum.
317
        // Newer wallets store keys as DBKeys::KEY [pubkey] => [privkey][hash(pubkey,privkey)], which is much faster while
318
        // remaining backwards-compatible.
319
0
        try
320
0
        {
321
0
            ssValue >> hash;
322
0
        }
323
0
        catch (const std::ios_base::failure&) {}
324
325
0
        bool fSkipCheck = false;
326
327
0
        if (!hash.IsNull())
  Branch (327:13): [True: 0, False: 0]
328
0
        {
329
            // hash pubkey/privkey to accelerate wallet load
330
0
            std::vector<unsigned char> vchKey;
331
0
            vchKey.reserve(vchPubKey.size() + pkey.size());
332
0
            vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
333
0
            vchKey.insert(vchKey.end(), pkey.begin(), pkey.end());
334
335
0
            if (Hash(vchKey) != hash)
  Branch (335:17): [True: 0, False: 0]
336
0
            {
337
0
                strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt";
338
0
                return false;
339
0
            }
340
341
0
            fSkipCheck = true;
342
0
        }
343
344
0
        if (!key.Load(pkey, vchPubKey, fSkipCheck))
  Branch (344:13): [True: 0, False: 0]
345
0
        {
346
0
            strErr = "Error reading wallet database: CPrivKey corrupt";
347
0
            return false;
348
0
        }
349
0
        if (!pwallet->GetOrCreateLegacyDataSPKM()->LoadKey(key, vchPubKey))
  Branch (349:13): [True: 0, False: 0]
350
0
        {
351
0
            strErr = "Error reading wallet database: LegacyDataSPKM::LoadKey failed";
352
0
            return false;
353
0
        }
354
0
    } catch (const std::exception& e) {
355
0
        if (strErr.empty()) {
  Branch (355:13): [True: 0, False: 0]
356
0
            strErr = e.what();
357
0
        }
358
0
        return false;
359
0
    }
360
0
    return true;
361
0
}
362
363
bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
364
0
{
365
0
    LOCK(pwallet->cs_wallet);
366
0
    try {
367
0
        CPubKey vchPubKey;
368
0
        ssKey >> vchPubKey;
369
0
        if (!vchPubKey.IsValid())
  Branch (369:13): [True: 0, False: 0]
370
0
        {
371
0
            strErr = "Error reading wallet database: CPubKey corrupt";
372
0
            return false;
373
0
        }
374
0
        std::vector<unsigned char> vchPrivKey;
375
0
        ssValue >> vchPrivKey;
376
377
        // Get the checksum and check it
378
0
        bool checksum_valid = false;
379
0
        if (!ssValue.eof()) {
  Branch (379:13): [True: 0, False: 0]
380
0
            uint256 checksum;
381
0
            ssValue >> checksum;
382
0
            if (!(checksum_valid = Hash(vchPrivKey) == checksum)) {
  Branch (382:17): [True: 0, False: 0]
383
0
                strErr = "Error reading wallet database: Encrypted key corrupt";
384
0
                return false;
385
0
            }
386
0
        }
387
388
0
        if (!pwallet->GetOrCreateLegacyDataSPKM()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
  Branch (388:13): [True: 0, False: 0]
389
0
        {
390
0
            strErr = "Error reading wallet database: LegacyDataSPKM::LoadCryptedKey failed";
391
0
            return false;
392
0
        }
393
0
    } catch (const std::exception& e) {
394
0
        if (strErr.empty()) {
  Branch (394:13): [True: 0, False: 0]
395
0
            strErr = e.what();
396
0
        }
397
0
        return false;
398
0
    }
399
0
    return true;
400
0
}
401
402
bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr)
403
0
{
404
0
    LOCK(pwallet->cs_wallet);
405
0
    try {
406
        // Master encryption key is loaded into only the wallet and not any of the ScriptPubKeyMans.
407
0
        unsigned int nID;
408
0
        ssKey >> nID;
409
0
        CMasterKey kMasterKey;
410
0
        ssValue >> kMasterKey;
411
0
        if(pwallet->mapMasterKeys.count(nID) != 0)
  Branch (411:12): [True: 0, False: 0]
412
0
        {
413
0
            strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
414
0
            return false;
415
0
        }
416
0
        pwallet->mapMasterKeys[nID] = kMasterKey;
417
0
        if (pwallet->nMasterKeyMaxID < nID)
  Branch (417:13): [True: 0, False: 0]
418
0
            pwallet->nMasterKeyMaxID = nID;
419
420
0
    } catch (const std::exception& e) {
421
0
        if (strErr.empty()) {
  Branch (421:13): [True: 0, False: 0]
422
0
            strErr = e.what();
423
0
        }
424
0
        return false;
425
0
    }
426
0
    return true;
427
0
}
428
429
bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr)
430
0
{
431
0
    LOCK(pwallet->cs_wallet);
432
0
    try {
433
0
        CHDChain chain;
434
0
        ssValue >> chain;
435
0
        pwallet->GetOrCreateLegacyDataSPKM()->LoadHDChain(chain);
436
0
    } catch (const std::exception& e) {
437
0
        if (strErr.empty()) {
  Branch (437:13): [True: 0, False: 0]
438
0
            strErr = e.what();
439
0
        }
440
0
        return false;
441
0
    }
442
0
    return true;
443
0
}
444
445
static DBErrors LoadMinVersion(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
446
11.0k
{
447
11.0k
    AssertLockHeld(pwallet->cs_wallet);
448
11.0k
    int nMinVersion = 0;
449
11.0k
    if (batch.Read(DBKeys::MINVERSION, nMinVersion)) {
  Branch (449:9): [True: 0, False: 11.0k]
450
0
        pwallet->WalletLogPrintf("Wallet file version = %d\n", nMinVersion);
451
0
        if (nMinVersion > FEATURE_LATEST)
  Branch (451:13): [True: 0, False: 0]
452
0
            return DBErrors::TOO_NEW;
453
0
        pwallet->LoadMinVersion(nMinVersion);
454
0
    }
455
11.0k
    return DBErrors::LOAD_OK;
456
11.0k
}
457
458
static DBErrors LoadWalletFlags(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
459
11.0k
{
460
11.0k
    AssertLockHeld(pwallet->cs_wallet);
461
11.0k
    uint64_t flags;
462
11.0k
    if (batch.Read(DBKeys::FLAGS, flags)) {
  Branch (462:9): [True: 0, False: 11.0k]
463
0
        if (!pwallet->LoadWalletFlags(flags)) {
  Branch (463:13): [True: 0, False: 0]
464
0
            pwallet->WalletLogPrintf("Error reading wallet database: Unknown non-tolerable wallet flags found\n");
465
0
            return DBErrors::TOO_NEW;
466
0
        }
467
        // All wallets must be descriptor wallets unless opened with a bdb_ro db
468
        // bdb_ro is only used for legacy to descriptor migration.
469
0
        if (pwallet->GetDatabase().Format() != "bdb_ro" && !pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
  Branch (469:13): [True: 0, False: 0]
  Branch (469:13): [True: 0, False: 0]
  Branch (469:60): [True: 0, False: 0]
470
0
            return DBErrors::LEGACY_WALLET;
471
0
        }
472
0
    }
473
11.0k
    return DBErrors::LOAD_OK;
474
11.0k
}
475
476
struct LoadResult
477
{
478
    DBErrors m_result{DBErrors::LOAD_OK};
479
    int m_records{0};
480
};
481
482
using LoadFunc = std::function<DBErrors(CWallet* pwallet, DataStream& key, DataStream& value, std::string& err)>;
483
static LoadResult LoadRecords(CWallet* pwallet, DatabaseBatch& batch, const std::string& key, DataStream& prefix, LoadFunc load_func)
484
210k
{
485
210k
    LoadResult result;
486
210k
    DataStream ssKey;
487
210k
    DataStream ssValue{};
488
489
210k
    Assume(!prefix.empty());
490
210k
    std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
491
210k
    if (!cursor) {
  Branch (491:9): [True: 0, False: 210k]
492
0
        pwallet->WalletLogPrintf("Error getting database cursor for '%s' records\n", key);
493
0
        result.m_result = DBErrors::CORRUPT;
494
0
        return result;
495
0
    }
496
497
210k
    while (true) {
  Branch (497:12): [Folded - Ignored]
498
210k
        DatabaseCursor::Status status = cursor->Next(ssKey, ssValue);
499
210k
        if (status == DatabaseCursor::Status::DONE) {
  Branch (499:13): [True: 210k, False: 0]
500
210k
            break;
501
210k
        } else if (status == DatabaseCursor::Status::FAIL) {
  Branch (501:20): [True: 0, False: 0]
502
0
            pwallet->WalletLogPrintf("Error reading next '%s' record for wallet database\n", key);
503
0
            result.m_result = DBErrors::CORRUPT;
504
0
            return result;
505
0
        }
506
0
        std::string type;
507
0
        ssKey >> type;
508
0
        assert(type == key);
  Branch (508:9): [True: 0, False: 0]
509
0
        std::string error;
510
0
        DBErrors record_res = load_func(pwallet, ssKey, ssValue, error);
511
0
        if (record_res != DBErrors::LOAD_OK) {
  Branch (511:13): [True: 0, False: 0]
512
0
            pwallet->WalletLogPrintf("%s\n", error);
513
0
        }
514
0
        result.m_result = std::max(result.m_result, record_res);
515
0
        ++result.m_records;
516
0
    }
517
210k
    return result;
518
210k
}
519
520
static LoadResult LoadRecords(CWallet* pwallet, DatabaseBatch& batch, const std::string& key, LoadFunc load_func)
521
210k
{
522
210k
    DataStream prefix;
523
210k
    prefix << key;
524
210k
    return LoadRecords(pwallet, batch, key, prefix, load_func);
525
210k
}
526
527
bool HasLegacyRecords(CWallet& wallet)
528
0
{
529
0
    const auto& batch = wallet.GetDatabase().MakeBatch();
530
0
    return HasLegacyRecords(wallet, *batch);
531
0
}
532
533
bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch)
534
0
{
535
0
    for (const auto& type : DBKeys::LEGACY_TYPES) {
  Branch (535:27): [True: 0, False: 0]
536
0
        DataStream key;
537
0
        DataStream value{};
538
0
        DataStream prefix;
539
540
0
        prefix << type;
541
0
        std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix);
542
0
        if (!cursor) {
  Branch (542:13): [True: 0, False: 0]
543
            // Could only happen on a closed db, which means there is an error in the code flow.
544
0
            wallet.WalletLogPrintf("Error getting database cursor for '%s' records", type);
545
0
            throw std::runtime_error(strprintf("Error getting database cursor for '%s' records", type));
546
0
        }
547
548
0
        DatabaseCursor::Status status = cursor->Next(key, value);
549
0
        if (status != DatabaseCursor::Status::DONE) {
  Branch (549:13): [True: 0, False: 0]
550
0
            return true;
551
0
        }
552
0
    }
553
0
    return false;
554
0
}
555
556
static DBErrors LoadLegacyWalletRecords(CWallet* pwallet, DatabaseBatch& batch, int last_client) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
557
11.0k
{
558
11.0k
    AssertLockHeld(pwallet->cs_wallet);
559
11.0k
    DBErrors result = DBErrors::LOAD_OK;
560
561
    // Make sure descriptor wallets don't have any legacy records
562
11.0k
    if (pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS)) {
  Branch (562:9): [True: 0, False: 11.0k]
563
0
        if (HasLegacyRecords(*pwallet, batch)) {
  Branch (563:13): [True: 0, False: 0]
564
0
            pwallet->WalletLogPrintf("Error: Unexpected legacy entry found in descriptor wallet %s. The wallet might have been tampered with or created with malicious intent.\n", pwallet->GetName());
565
0
            return DBErrors::UNEXPECTED_LEGACY_ENTRY;
566
0
        }
567
568
0
        return DBErrors::LOAD_OK;
569
0
    }
570
571
    // Load HD Chain
572
    // Note: There should only be one HDCHAIN record with no data following the type
573
11.0k
    LoadResult hd_chain_res = LoadRecords(pwallet, batch, DBKeys::HDCHAIN,
574
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
575
0
        return LoadHDChain(pwallet, value, err) ? DBErrors:: LOAD_OK : DBErrors::CORRUPT;
  Branch (575:16): [True: 0, False: 0]
576
0
    });
577
11.0k
    result = std::max(result, hd_chain_res.m_result);
578
579
    // Load unencrypted keys
580
11.0k
    LoadResult key_res = LoadRecords(pwallet, batch, DBKeys::KEY,
581
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
582
0
        return LoadKey(pwallet, key, value, err) ? DBErrors::LOAD_OK : DBErrors::CORRUPT;
  Branch (582:16): [True: 0, False: 0]
583
0
    });
584
11.0k
    result = std::max(result, key_res.m_result);
585
586
    // Load encrypted keys
587
11.0k
    LoadResult ckey_res = LoadRecords(pwallet, batch, DBKeys::CRYPTED_KEY,
588
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
589
0
        return LoadCryptedKey(pwallet, key, value, err) ? DBErrors::LOAD_OK : DBErrors::CORRUPT;
  Branch (589:16): [True: 0, False: 0]
590
0
    });
591
11.0k
    result = std::max(result, ckey_res.m_result);
592
593
    // Load scripts
594
11.0k
    LoadResult script_res = LoadRecords(pwallet, batch, DBKeys::CSCRIPT,
595
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
596
0
        uint160 hash;
597
0
        key >> hash;
598
0
        CScript script;
599
0
        value >> script;
600
0
        if (!pwallet->GetOrCreateLegacyDataSPKM()->LoadCScript(script))
  Branch (600:13): [True: 0, False: 0]
601
0
        {
602
0
            strErr = "Error reading wallet database: LegacyDataSPKM::LoadCScript failed";
603
0
            return DBErrors::NONCRITICAL_ERROR;
604
0
        }
605
0
        return DBErrors::LOAD_OK;
606
0
    });
607
11.0k
    result = std::max(result, script_res.m_result);
608
609
    // Check whether rewrite is needed
610
11.0k
    if (ckey_res.m_records > 0) {
  Branch (610:9): [True: 0, False: 11.0k]
611
        // Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
612
0
        if (last_client == 40000 || last_client == 50000) result = std::max(result, DBErrors::NEED_REWRITE);
  Branch (612:13): [True: 0, False: 0]
  Branch (612:37): [True: 0, False: 0]
613
0
    }
614
615
    // Load keymeta
616
11.0k
    std::map<uint160, CHDChain> hd_chains;
617
11.0k
    LoadResult keymeta_res = LoadRecords(pwallet, batch, DBKeys::KEYMETA,
618
11.0k
        [&hd_chains] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
619
0
        CPubKey vchPubKey;
620
0
        key >> vchPubKey;
621
0
        CKeyMetadata keyMeta;
622
0
        value >> keyMeta;
623
0
        pwallet->GetOrCreateLegacyDataSPKM()->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
624
625
        // Extract some CHDChain info from this metadata if it has any
626
0
        if (keyMeta.nVersion >= CKeyMetadata::VERSION_WITH_HDDATA && !keyMeta.hd_seed_id.IsNull() && keyMeta.hdKeypath.size() > 0) {
  Branch (626:13): [True: 0, False: 0]
  Branch (626:70): [True: 0, False: 0]
  Branch (626:102): [True: 0, False: 0]
627
            // Get the path from the key origin or from the path string
628
            // Not applicable when path is "s" or "m" as those indicate a seed
629
            // See https://github.com/bitcoin/bitcoin/pull/12924
630
0
            bool internal = false;
631
0
            uint32_t index = 0;
632
0
            if (keyMeta.hdKeypath != "s" && keyMeta.hdKeypath != "m") {
  Branch (632:17): [True: 0, False: 0]
  Branch (632:45): [True: 0, False: 0]
633
0
                std::vector<uint32_t> path;
634
0
                if (keyMeta.has_key_origin) {
  Branch (634:21): [True: 0, False: 0]
635
                    // We have a key origin, so pull it from its path vector
636
0
                    path = keyMeta.key_origin.path;
637
0
                } else {
638
                    // No key origin, have to parse the string
639
0
                    if (!ParseHDKeypath(keyMeta.hdKeypath, path)) {
  Branch (639:25): [True: 0, False: 0]
640
0
                        strErr = "Error reading wallet database: keymeta with invalid HD keypath";
641
0
                        return DBErrors::NONCRITICAL_ERROR;
642
0
                    }
643
0
                }
644
645
                // Extract the index and internal from the path
646
                // Path string is m/0'/k'/i'
647
                // Path vector is [0', k', i'] (but as ints OR'd with the hardened bit
648
                // k == 0 for external, 1 for internal. i is the index
649
0
                if (path.size() != 3) {
  Branch (649:21): [True: 0, False: 0]
650
0
                    strErr = "Error reading wallet database: keymeta found with unexpected path";
651
0
                    return DBErrors::NONCRITICAL_ERROR;
652
0
                }
653
0
                if (path[0] != 0x80000000) {
  Branch (653:21): [True: 0, False: 0]
654
0
                    strErr = strprintf("Unexpected path index of 0x%08x (expected 0x80000000) for the element at index 0", path[0]);
655
0
                    return DBErrors::NONCRITICAL_ERROR;
656
0
                }
657
0
                if (path[1] != 0x80000000 && path[1] != (1 | 0x80000000)) {
  Branch (657:21): [True: 0, False: 0]
  Branch (657:46): [True: 0, False: 0]
658
0
                    strErr = strprintf("Unexpected path index of 0x%08x (expected 0x80000000 or 0x80000001) for the element at index 1", path[1]);
659
0
                    return DBErrors::NONCRITICAL_ERROR;
660
0
                }
661
0
                if ((path[2] & 0x80000000) == 0) {
  Branch (661:21): [True: 0, False: 0]
662
0
                    strErr = strprintf("Unexpected path index of 0x%08x (expected to be greater than or equal to 0x80000000)", path[2]);
663
0
                    return DBErrors::NONCRITICAL_ERROR;
664
0
                }
665
0
                internal = path[1] == (1 | 0x80000000);
666
0
                index = path[2] & ~0x80000000;
667
0
            }
668
669
            // Insert a new CHDChain, or get the one that already exists
670
0
            auto [ins, inserted] = hd_chains.emplace(keyMeta.hd_seed_id, CHDChain());
671
0
            CHDChain& chain = ins->second;
672
0
            if (inserted) {
  Branch (672:17): [True: 0, False: 0]
673
                // For new chains, we want to default to VERSION_HD_BASE until we see an internal
674
0
                chain.nVersion = CHDChain::VERSION_HD_BASE;
675
0
                chain.seed_id = keyMeta.hd_seed_id;
676
0
            }
677
0
            if (internal) {
  Branch (677:17): [True: 0, False: 0]
678
0
                chain.nVersion = CHDChain::VERSION_HD_CHAIN_SPLIT;
679
0
                chain.nInternalChainCounter = std::max(chain.nInternalChainCounter, index + 1);
680
0
            } else {
681
0
                chain.nExternalChainCounter = std::max(chain.nExternalChainCounter, index + 1);
682
0
            }
683
0
        }
684
0
        return DBErrors::LOAD_OK;
685
0
    });
686
11.0k
    result = std::max(result, keymeta_res.m_result);
687
688
    // Set inactive chains
689
11.0k
    if (!hd_chains.empty()) {
  Branch (689:9): [True: 0, False: 11.0k]
690
0
        LegacyDataSPKM* legacy_spkm = pwallet->GetLegacyDataSPKM();
691
0
        if (legacy_spkm) {
  Branch (691:13): [True: 0, False: 0]
692
0
            for (const auto& [hd_seed_id, chain] : hd_chains) {
  Branch (692:50): [True: 0, False: 0]
693
0
                if (hd_seed_id != legacy_spkm->GetHDChain().seed_id) {
  Branch (693:21): [True: 0, False: 0]
694
0
                    legacy_spkm->AddInactiveHDChain(chain);
695
0
                }
696
0
            }
697
0
        } else {
698
0
            pwallet->WalletLogPrintf("Inactive HD Chains found but no Legacy ScriptPubKeyMan\n");
699
0
            result = DBErrors::CORRUPT;
700
0
        }
701
0
    }
702
703
    // Load watchonly scripts
704
11.0k
    LoadResult watch_script_res = LoadRecords(pwallet, batch, DBKeys::WATCHS,
705
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
706
0
        CScript script;
707
0
        key >> script;
708
0
        uint8_t fYes;
709
0
        value >> fYes;
710
0
        if (fYes == '1') {
  Branch (710:13): [True: 0, False: 0]
711
0
            pwallet->GetOrCreateLegacyDataSPKM()->LoadWatchOnly(script);
712
0
        }
713
0
        return DBErrors::LOAD_OK;
714
0
    });
715
11.0k
    result = std::max(result, watch_script_res.m_result);
716
717
    // Load watchonly meta
718
11.0k
    LoadResult watch_meta_res = LoadRecords(pwallet, batch, DBKeys::WATCHMETA,
719
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
720
0
        CScript script;
721
0
        key >> script;
722
0
        CKeyMetadata keyMeta;
723
0
        value >> keyMeta;
724
0
        pwallet->GetOrCreateLegacyDataSPKM()->LoadScriptMetadata(CScriptID(script), keyMeta);
725
0
        return DBErrors::LOAD_OK;
726
0
    });
727
11.0k
    result = std::max(result, watch_meta_res.m_result);
728
729
    // Deal with old "wkey" and "defaultkey" records.
730
    // These are not actually loaded, but we need to check for them
731
732
    // We don't want or need the default key, but if there is one set,
733
    // we want to make sure that it is valid so that we can detect corruption
734
    // Note: There should only be one DEFAULTKEY with nothing trailing the type
735
11.0k
    LoadResult default_key_res = LoadRecords(pwallet, batch, DBKeys::DEFAULTKEY,
736
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
737
0
        CPubKey default_pubkey;
738
0
        try {
739
0
            value >> default_pubkey;
740
0
        } catch (const std::exception& e) {
741
0
            err = e.what();
742
0
            return DBErrors::CORRUPT;
743
0
        }
744
0
        if (!default_pubkey.IsValid()) {
  Branch (744:13): [True: 0, False: 0]
745
0
            err = "Error reading wallet database: Default Key corrupt";
746
0
            return DBErrors::CORRUPT;
747
0
        }
748
0
        return DBErrors::LOAD_OK;
749
0
    });
750
11.0k
    result = std::max(result, default_key_res.m_result);
751
752
    // "wkey" records are unsupported, if we see any, throw an error
753
11.0k
    LoadResult wkey_res = LoadRecords(pwallet, batch, DBKeys::OLD_KEY,
754
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
755
0
        err = "Found unsupported 'wkey' record, try loading with version 0.18";
756
0
        return DBErrors::LOAD_FAIL;
757
0
    });
758
11.0k
    result = std::max(result, wkey_res.m_result);
759
760
11.0k
    if (result <= DBErrors::NONCRITICAL_ERROR) {
  Branch (760:9): [True: 11.0k, False: 0]
761
        // Only do logging and time first key update if there were no critical errors
762
11.0k
        pwallet->WalletLogPrintf("Legacy Wallet Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total.\n",
763
11.0k
               key_res.m_records, ckey_res.m_records, keymeta_res.m_records, key_res.m_records + ckey_res.m_records);
764
11.0k
    }
765
766
11.0k
    return result;
767
11.0k
}
768
769
template<typename... Args>
770
static DataStream PrefixStream(const Args&... args)
771
0
{
772
0
    DataStream prefix;
773
0
    SerializeMany(prefix, args...);
774
0
    return prefix;
775
0
}
776
777
static DBErrors LoadDescriptorWalletRecords(CWallet* pwallet, DatabaseBatch& batch, int last_client) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
778
11.0k
{
779
11.0k
    AssertLockHeld(pwallet->cs_wallet);
780
781
    // Load descriptor record
782
11.0k
    int num_keys = 0;
783
11.0k
    int num_ckeys= 0;
784
11.0k
    LoadResult desc_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTOR,
785
11.0k
        [&batch, &num_keys, &num_ckeys, &last_client] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
786
0
        DBErrors result = DBErrors::LOAD_OK;
787
788
0
        uint256 id;
789
0
        key >> id;
790
0
        WalletDescriptor desc;
791
0
        try {
792
0
            value >> desc;
793
0
        } catch (const std::ios_base::failure& e) {
794
0
            strErr = strprintf("Error: Unrecognized descriptor found in wallet %s. ", pwallet->GetName());
795
0
            strErr += (last_client > CLIENT_VERSION) ? "The wallet might had been created on a newer version. " :
  Branch (795:23): [True: 0, False: 0]
796
0
                    "The database might be corrupted or the software version is not compatible with one of your wallet descriptors. ";
797
0
            strErr += "Please try running the latest software version";
798
            // Also include error details
799
0
            strErr = strprintf("%s\nDetails: %s", strErr, e.what());
800
0
            return DBErrors::UNKNOWN_DESCRIPTOR;
801
0
        }
802
0
        DescriptorScriptPubKeyMan& spkm = pwallet->LoadDescriptorScriptPubKeyMan(id, desc);
803
804
        // Prior to doing anything with this spkm, verify ID compatibility
805
0
        if (id != spkm.GetID()) {
  Branch (805:13): [True: 0, False: 0]
806
0
            strErr = "The descriptor ID calculated by the wallet differs from the one in DB";
807
0
            return DBErrors::CORRUPT;
808
0
        }
809
810
0
        DescriptorCache cache;
811
812
        // Get key cache for this descriptor
813
0
        DataStream prefix = PrefixStream(DBKeys::WALLETDESCRIPTORCACHE, id);
814
0
        LoadResult key_cache_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORCACHE, prefix,
815
0
            [&id, &cache] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
816
0
            bool parent = true;
817
0
            uint256 desc_id;
818
0
            uint32_t key_exp_index;
819
0
            uint32_t der_index;
820
0
            key >> desc_id;
821
0
            assert(desc_id == id);
  Branch (821:13): [True: 0, False: 0]
822
0
            key >> key_exp_index;
823
824
            // if the der_index exists, it's a derived xpub
825
0
            try
826
0
            {
827
0
                key >> der_index;
828
0
                parent = false;
829
0
            }
830
0
            catch (...) {}
831
832
0
            std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
833
0
            value >> ser_xpub;
834
0
            CExtPubKey xpub;
835
0
            xpub.Decode(ser_xpub.data());
836
0
            if (parent) {
  Branch (836:17): [True: 0, False: 0]
837
0
                cache.CacheParentExtPubKey(key_exp_index, xpub);
838
0
            } else {
839
0
                cache.CacheDerivedExtPubKey(key_exp_index, der_index, xpub);
840
0
            }
841
0
            return DBErrors::LOAD_OK;
842
0
        });
843
0
        result = std::max(result, key_cache_res.m_result);
844
845
        // Get last hardened cache for this descriptor
846
0
        prefix = PrefixStream(DBKeys::WALLETDESCRIPTORLHCACHE, id);
847
0
        LoadResult lh_cache_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORLHCACHE, prefix,
848
0
            [&id, &cache] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
849
0
            uint256 desc_id;
850
0
            uint32_t key_exp_index;
851
0
            key >> desc_id;
852
0
            assert(desc_id == id);
  Branch (852:13): [True: 0, False: 0]
853
0
            key >> key_exp_index;
854
855
0
            std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
856
0
            value >> ser_xpub;
857
0
            CExtPubKey xpub;
858
0
            xpub.Decode(ser_xpub.data());
859
0
            cache.CacheLastHardenedExtPubKey(key_exp_index, xpub);
860
0
            return DBErrors::LOAD_OK;
861
0
        });
862
0
        result = std::max(result, lh_cache_res.m_result);
863
864
        // Set the cache for this descriptor
865
0
        auto spk_man = (DescriptorScriptPubKeyMan*)pwallet->GetScriptPubKeyMan(id);
866
0
        assert(spk_man);
  Branch (866:9): [True: 0, False: 0]
867
0
        spk_man->SetCache(cache);
868
869
        // Get unencrypted keys
870
0
        prefix = PrefixStream(DBKeys::WALLETDESCRIPTORKEY, id);
871
0
        LoadResult key_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORKEY, prefix,
872
0
            [&id, &spk_man] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
873
0
            uint256 desc_id;
874
0
            CPubKey pubkey;
875
0
            key >> desc_id;
876
0
            assert(desc_id == id);
  Branch (876:13): [True: 0, False: 0]
877
0
            key >> pubkey;
878
0
            if (!pubkey.IsValid())
  Branch (878:17): [True: 0, False: 0]
879
0
            {
880
0
                strErr = "Error reading wallet database: descriptor unencrypted key CPubKey corrupt";
881
0
                return DBErrors::CORRUPT;
882
0
            }
883
0
            CKey privkey;
884
0
            CPrivKey pkey;
885
0
            uint256 hash;
886
887
0
            value >> pkey;
888
0
            value >> hash;
889
890
            // hash pubkey/privkey to accelerate wallet load
891
0
            std::vector<unsigned char> to_hash;
892
0
            to_hash.reserve(pubkey.size() + pkey.size());
893
0
            to_hash.insert(to_hash.end(), pubkey.begin(), pubkey.end());
894
0
            to_hash.insert(to_hash.end(), pkey.begin(), pkey.end());
895
896
0
            if (Hash(to_hash) != hash)
  Branch (896:17): [True: 0, False: 0]
897
0
            {
898
0
                strErr = "Error reading wallet database: descriptor unencrypted key CPubKey/CPrivKey corrupt";
899
0
                return DBErrors::CORRUPT;
900
0
            }
901
902
0
            if (!privkey.Load(pkey, pubkey, true))
  Branch (902:17): [True: 0, False: 0]
903
0
            {
904
0
                strErr = "Error reading wallet database: descriptor unencrypted key CPrivKey corrupt";
905
0
                return DBErrors::CORRUPT;
906
0
            }
907
0
            spk_man->AddKey(pubkey.GetID(), privkey);
908
0
            return DBErrors::LOAD_OK;
909
0
        });
910
0
        result = std::max(result, key_res.m_result);
911
0
        num_keys = key_res.m_records;
912
913
        // Get encrypted keys
914
0
        prefix = PrefixStream(DBKeys::WALLETDESCRIPTORCKEY, id);
915
0
        LoadResult ckey_res = LoadRecords(pwallet, batch, DBKeys::WALLETDESCRIPTORCKEY, prefix,
916
0
            [&id, &spk_man] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
917
0
            uint256 desc_id;
918
0
            CPubKey pubkey;
919
0
            key >> desc_id;
920
0
            assert(desc_id == id);
  Branch (920:13): [True: 0, False: 0]
921
0
            key >> pubkey;
922
0
            if (!pubkey.IsValid())
  Branch (922:17): [True: 0, False: 0]
923
0
            {
924
0
                err = "Error reading wallet database: descriptor encrypted key CPubKey corrupt";
925
0
                return DBErrors::CORRUPT;
926
0
            }
927
0
            std::vector<unsigned char> privkey;
928
0
            value >> privkey;
929
930
0
            spk_man->AddCryptedKey(pubkey.GetID(), pubkey, privkey);
931
0
            return DBErrors::LOAD_OK;
932
0
        });
933
0
        result = std::max(result, ckey_res.m_result);
934
0
        num_ckeys = ckey_res.m_records;
935
936
0
        return result;
937
0
    });
938
939
11.0k
    if (desc_res.m_result <= DBErrors::NONCRITICAL_ERROR) {
  Branch (939:9): [True: 11.0k, False: 0]
940
        // Only log if there are no critical errors
941
11.0k
        pwallet->WalletLogPrintf("Descriptors: %u, Descriptor Keys: %u plaintext, %u encrypted, %u total.\n",
942
11.0k
               desc_res.m_records, num_keys, num_ckeys, num_keys + num_ckeys);
943
11.0k
    }
944
945
11.0k
    return desc_res.m_result;
946
11.0k
}
947
948
static DBErrors LoadAddressBookRecords(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
949
11.0k
{
950
11.0k
    AssertLockHeld(pwallet->cs_wallet);
951
11.0k
    DBErrors result = DBErrors::LOAD_OK;
952
953
    // Load name record
954
11.0k
    LoadResult name_res = LoadRecords(pwallet, batch, DBKeys::NAME,
955
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
956
0
        std::string strAddress;
957
0
        key >> strAddress;
958
0
        std::string label;
959
0
        value >> label;
960
0
        pwallet->m_address_book[DecodeDestination(strAddress)].SetLabel(label);
961
0
        return DBErrors::LOAD_OK;
962
0
    });
963
11.0k
    result = std::max(result, name_res.m_result);
964
965
    // Load purpose record
966
11.0k
    LoadResult purpose_res = LoadRecords(pwallet, batch, DBKeys::PURPOSE,
967
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
968
0
        std::string strAddress;
969
0
        key >> strAddress;
970
0
        std::string purpose_str;
971
0
        value >> purpose_str;
972
0
        std::optional<AddressPurpose> purpose{PurposeFromString(purpose_str)};
973
0
        if (!purpose) {
  Branch (973:13): [True: 0, False: 0]
974
0
            pwallet->WalletLogPrintf("Warning: nonstandard purpose string '%s' for address '%s'\n", purpose_str, strAddress);
975
0
        }
976
0
        pwallet->m_address_book[DecodeDestination(strAddress)].purpose = purpose;
977
0
        return DBErrors::LOAD_OK;
978
0
    });
979
11.0k
    result = std::max(result, purpose_res.m_result);
980
981
    // Load destination data record
982
11.0k
    LoadResult dest_res = LoadRecords(pwallet, batch, DBKeys::DESTDATA,
983
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
984
0
        std::string strAddress, strKey, strValue;
985
0
        key >> strAddress;
986
0
        key >> strKey;
987
0
        value >> strValue;
988
0
        const CTxDestination& dest{DecodeDestination(strAddress)};
989
0
        if (strKey.compare("used") == 0) {
  Branch (989:13): [True: 0, False: 0]
990
            // Load "used" key indicating if an IsMine address has
991
            // previously been spent from with avoid_reuse option enabled.
992
            // The strValue is not used for anything currently, but could
993
            // hold more information in the future. Current values are just
994
            // "1" or "p" for present (which was written prior to
995
            // f5ba424cd44619d9b9be88b8593d69a7ba96db26).
996
0
            pwallet->LoadAddressPreviouslySpent(dest);
997
0
        } else if (strKey.starts_with("rr")) {
  Branch (997:20): [True: 0, False: 0]
998
            // Load "rr##" keys where ## is a decimal number, and strValue
999
            // is a serialized RecentRequestEntry object.
1000
0
            pwallet->LoadAddressReceiveRequest(dest, strKey.substr(2), strValue);
1001
0
        }
1002
0
        return DBErrors::LOAD_OK;
1003
0
    });
1004
11.0k
    result = std::max(result, dest_res.m_result);
1005
1006
11.0k
    return result;
1007
11.0k
}
1008
1009
static DBErrors LoadTxRecords(CWallet* pwallet, DatabaseBatch& batch, std::vector<Txid>& upgraded_txs, bool& any_unordered) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1010
11.0k
{
1011
11.0k
    AssertLockHeld(pwallet->cs_wallet);
1012
11.0k
    DBErrors result = DBErrors::LOAD_OK;
1013
1014
    // Load tx record
1015
11.0k
    any_unordered = false;
1016
11.0k
    LoadResult tx_res = LoadRecords(pwallet, batch, DBKeys::TX,
1017
11.0k
        [&any_unordered, &upgraded_txs] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
1018
0
        DBErrors result = DBErrors::LOAD_OK;
1019
0
        Txid hash;
1020
0
        key >> hash;
1021
        // LoadToWallet call below creates a new CWalletTx that fill_wtx
1022
        // callback fills with transaction metadata.
1023
0
        auto fill_wtx = [&](CWalletTx& wtx, bool new_tx) {
1024
0
            if(!new_tx) {
  Branch (1024:16): [True: 0, False: 0]
1025
                // There's some corruption here since the tx we just tried to load was already in the wallet.
1026
0
                err = "Error: Corrupt transaction found. This can be fixed by removing transactions from wallet and rescanning.";
1027
0
                result = DBErrors::CORRUPT;
1028
0
                return false;
1029
0
            }
1030
0
            value >> wtx;
1031
0
            if (wtx.GetHash() != hash)
  Branch (1031:17): [True: 0, False: 0]
1032
0
                return false;
1033
1034
            // Undo serialize changes in 31600
1035
0
            if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
  Branch (1035:17): [True: 0, False: 0]
  Branch (1035:55): [True: 0, False: 0]
1036
0
            {
1037
0
                if (!value.empty())
  Branch (1037:21): [True: 0, False: 0]
1038
0
                {
1039
0
                    uint8_t fTmp;
1040
0
                    uint8_t fUnused;
1041
0
                    std::string unused_string;
1042
0
                    value >> fTmp >> fUnused >> unused_string;
1043
0
                    pwallet->WalletLogPrintf("LoadWallet() upgrading tx ver=%d %d %s\n",
1044
0
                                       wtx.fTimeReceivedIsTxTime, fTmp, hash.ToString());
1045
0
                    wtx.fTimeReceivedIsTxTime = fTmp;
1046
0
                }
1047
0
                else
1048
0
                {
1049
0
                    pwallet->WalletLogPrintf("LoadWallet() repairing tx ver=%d %s\n", wtx.fTimeReceivedIsTxTime, hash.ToString());
1050
0
                    wtx.fTimeReceivedIsTxTime = 0;
1051
0
                }
1052
0
                upgraded_txs.push_back(hash);
1053
0
            }
1054
1055
0
            if (wtx.nOrderPos == -1)
  Branch (1055:17): [True: 0, False: 0]
1056
0
                any_unordered = true;
1057
1058
0
            return true;
1059
0
        };
1060
0
        if (!pwallet->LoadToWallet(hash, fill_wtx)) {
  Branch (1060:13): [True: 0, False: 0]
1061
            // Use std::max as fill_wtx may have already set result to CORRUPT
1062
0
            result = std::max(result, DBErrors::NEED_RESCAN);
1063
0
        }
1064
0
        return result;
1065
0
    });
1066
11.0k
    result = std::max(result, tx_res.m_result);
1067
1068
    // Load locked utxo record
1069
11.0k
    LoadResult locked_utxo_res = LoadRecords(pwallet, batch, DBKeys::LOCKED_UTXO,
1070
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
1071
0
        Txid hash;
1072
0
        uint32_t n;
1073
0
        key >> hash;
1074
0
        key >> n;
1075
0
        pwallet->LockCoin(COutPoint(hash, n));
1076
0
        return DBErrors::LOAD_OK;
1077
0
    });
1078
11.0k
    result = std::max(result, locked_utxo_res.m_result);
1079
1080
    // Load orderposnext record
1081
    // Note: There should only be one ORDERPOSNEXT record with nothing trailing the type
1082
11.0k
    LoadResult order_pos_res = LoadRecords(pwallet, batch, DBKeys::ORDERPOSNEXT,
1083
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) {
1084
0
        try {
1085
0
            value >> pwallet->nOrderPosNext;
1086
0
        } catch (const std::exception& e) {
1087
0
            err = e.what();
1088
0
            return DBErrors::NONCRITICAL_ERROR;
1089
0
        }
1090
0
        return DBErrors::LOAD_OK;
1091
0
    });
1092
11.0k
    result = std::max(result, order_pos_res.m_result);
1093
1094
    // After loading all tx records, abandon any coinbase that is no longer in the active chain.
1095
    // This could happen during an external wallet load, or if the user replaced the chain data.
1096
11.0k
    for (auto& [id, wtx] : pwallet->mapWallet) {
  Branch (1096:26): [True: 0, False: 11.0k]
1097
0
        if (wtx.IsCoinBase() && wtx.isInactive()) {
  Branch (1097:13): [True: 0, False: 0]
  Branch (1097:33): [True: 0, False: 0]
1098
0
            pwallet->AbandonTransaction(wtx);
1099
0
        }
1100
0
    }
1101
1102
11.0k
    return result;
1103
11.0k
}
1104
1105
static DBErrors LoadActiveSPKMs(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1106
11.0k
{
1107
11.0k
    AssertLockHeld(pwallet->cs_wallet);
1108
11.0k
    DBErrors result = DBErrors::LOAD_OK;
1109
1110
    // Load spk records
1111
11.0k
    std::set<std::pair<OutputType, bool>> seen_spks;
1112
22.1k
    for (const auto& spk_key : {DBKeys::ACTIVEEXTERNALSPK, DBKeys::ACTIVEINTERNALSPK}) {
  Branch (1112:30): [True: 22.1k, False: 11.0k]
1113
22.1k
        LoadResult spkm_res = LoadRecords(pwallet, batch, spk_key,
1114
22.1k
            [&seen_spks, &spk_key] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& strErr) {
1115
0
            uint8_t output_type;
1116
0
            key >> output_type;
1117
0
            uint256 id;
1118
0
            value >> id;
1119
1120
0
            bool internal = spk_key == DBKeys::ACTIVEINTERNALSPK;
1121
0
            auto [it, insert] = seen_spks.emplace(static_cast<OutputType>(output_type), internal);
1122
0
            if (!insert) {
  Branch (1122:17): [True: 0, False: 0]
1123
0
                strErr = "Multiple ScriptpubKeyMans specified for a single type";
1124
0
                return DBErrors::CORRUPT;
1125
0
            }
1126
0
            pwallet->LoadActiveScriptPubKeyMan(id, static_cast<OutputType>(output_type), /*internal=*/internal);
1127
0
            return DBErrors::LOAD_OK;
1128
0
        });
1129
22.1k
        result = std::max(result, spkm_res.m_result);
1130
22.1k
    }
1131
11.0k
    return result;
1132
11.0k
}
1133
1134
static DBErrors LoadDecryptionKeys(CWallet* pwallet, DatabaseBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
1135
11.0k
{
1136
11.0k
    AssertLockHeld(pwallet->cs_wallet);
1137
1138
    // Load decryption key (mkey) records
1139
11.0k
    LoadResult mkey_res = LoadRecords(pwallet, batch, DBKeys::MASTER_KEY,
1140
11.0k
        [] (CWallet* pwallet, DataStream& key, DataStream& value, std::string& err) {
1141
0
        if (!LoadEncryptionKey(pwallet, key, value, err)) {
  Branch (1141:13): [True: 0, False: 0]
1142
0
            return DBErrors::CORRUPT;
1143
0
        }
1144
0
        return DBErrors::LOAD_OK;
1145
0
    });
1146
11.0k
    return mkey_res.m_result;
1147
11.0k
}
1148
1149
DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
1150
11.0k
{
1151
11.0k
    DBErrors result = DBErrors::LOAD_OK;
1152
11.0k
    bool any_unordered = false;
1153
11.0k
    std::vector<Txid> upgraded_txs;
1154
1155
11.0k
    LOCK(pwallet->cs_wallet);
1156
1157
    // Last client version to open this wallet
1158
11.0k
    int last_client = CLIENT_VERSION;
1159
11.0k
    bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
1160
11.0k
    if (has_last_client) pwallet->WalletLogPrintf("Last client version = %d\n", last_client);
  Branch (1160:9): [True: 0, False: 11.0k]
1161
1162
11.0k
    try {
1163
11.0k
        if ((result = LoadMinVersion(pwallet, *m_batch)) != DBErrors::LOAD_OK) return result;
  Branch (1163:13): [True: 0, False: 11.0k]
1164
1165
        // Load wallet flags, so they are known when processing other records.
1166
        // The FLAGS key is absent during wallet creation.
1167
11.0k
        if ((result = LoadWalletFlags(pwallet, *m_batch)) != DBErrors::LOAD_OK) return result;
  Branch (1167:13): [True: 0, False: 11.0k]
1168
1169
#ifndef ENABLE_EXTERNAL_SIGNER
1170
        if (pwallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) {
1171
            pwallet->WalletLogPrintf("Error: External signer wallet being loaded without external signer support compiled\n");
1172
            return DBErrors::EXTERNAL_SIGNER_SUPPORT_REQUIRED;
1173
        }
1174
#endif
1175
1176
        // Load legacy wallet keys
1177
11.0k
        result = std::max(LoadLegacyWalletRecords(pwallet, *m_batch, last_client), result);
1178
1179
        // Load descriptors
1180
11.0k
        result = std::max(LoadDescriptorWalletRecords(pwallet, *m_batch, last_client), result);
1181
        // Early return if there are unknown descriptors. Later loading of ACTIVEINTERNALSPK and ACTIVEEXTERNALEXPK
1182
        // may reference the unknown descriptor's ID which can result in a misleading corruption error
1183
        // when in reality the wallet is simply too new.
1184
11.0k
        if (result == DBErrors::UNKNOWN_DESCRIPTOR) return result;
  Branch (1184:13): [True: 0, False: 11.0k]
1185
1186
        // Load address book
1187
11.0k
        result = std::max(LoadAddressBookRecords(pwallet, *m_batch), result);
1188
1189
        // Load tx records
1190
11.0k
        result = std::max(LoadTxRecords(pwallet, *m_batch, upgraded_txs, any_unordered), result);
1191
1192
        // Load SPKMs
1193
11.0k
        result = std::max(LoadActiveSPKMs(pwallet, *m_batch), result);
1194
1195
        // Load decryption keys
1196
11.0k
        result = std::max(LoadDecryptionKeys(pwallet, *m_batch), result);
1197
11.0k
    } catch (...) {
1198
        // Exceptions that can be ignored or treated as non-critical are handled by the individual loading functions.
1199
        // Any uncaught exceptions will be caught here and treated as critical.
1200
0
        result = DBErrors::CORRUPT;
1201
0
    }
1202
1203
    // Any wallet corruption at all: skip any rewriting or
1204
    // upgrading, we don't want to make it worse.
1205
11.0k
    if (result != DBErrors::LOAD_OK)
  Branch (1205:9): [True: 0, False: 11.0k]
1206
0
        return result;
1207
1208
11.0k
    for (const Txid& hash : upgraded_txs)
  Branch (1208:27): [True: 0, False: 11.0k]
1209
0
        WriteTx(pwallet->mapWallet.at(hash));
1210
1211
11.0k
    if (!has_last_client || last_client != CLIENT_VERSION) // Update
  Branch (1211:9): [True: 11.0k, False: 0]
  Branch (1211:29): [True: 0, False: 0]
1212
11.0k
        m_batch->Write(DBKeys::VERSION, CLIENT_VERSION);
1213
1214
11.0k
    if (any_unordered)
  Branch (1214:9): [True: 0, False: 11.0k]
1215
0
        result = pwallet->ReorderTransactions();
1216
1217
    // Upgrade all of the descriptor caches to cache the last hardened xpub
1218
    // This operation is not atomic, but if it fails, only new entries are added so it is backwards compatible
1219
11.0k
    try {
1220
11.0k
        pwallet->UpgradeDescriptorCache();
1221
11.0k
    } catch (...) {
1222
0
        result = DBErrors::CORRUPT;
1223
0
    }
1224
1225
    // Since it was accidentally possible to "encrypt" a wallet with private keys disabled, we should check if this is
1226
    // such a wallet and remove the encryption key records to avoid any future issues.
1227
    // Although wallets without private keys should not have *ckey records, we should double check that.
1228
    // Removing the mkey records is only safe if there are no *ckey records.
1229
11.0k
    if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS) && pwallet->HasEncryptionKeys() && !pwallet->HaveCryptedKeys()) {
  Branch (1229:9): [True: 0, False: 11.0k]
  Branch (1229:71): [True: 0, False: 0]
  Branch (1229:103): [True: 0, False: 0]
1230
0
        pwallet->WalletLogPrintf("Detected extraneous encryption keys in this wallet without private keys. Removing extraneous encryption keys.\n");
1231
0
        for (const auto& [id, _] : pwallet->mapMasterKeys) {
  Branch (1231:34): [True: 0, False: 0]
1232
0
            if (!EraseMasterKey(id)) {
  Branch (1232:17): [True: 0, False: 0]
1233
0
                pwallet->WalletLogPrintf("Error: Unable to remove extraneous encryption key '%u'. Wallet corrupt.\n", id);
1234
0
                return DBErrors::CORRUPT;
1235
0
            }
1236
0
        }
1237
0
        pwallet->mapMasterKeys.clear();
1238
0
    }
1239
1240
11.0k
    return result;
1241
11.0k
}
1242
1243
static bool RunWithinTxn(WalletBatch& batch, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func)
1244
11.0k
{
1245
11.0k
    if (!batch.TxnBegin()) {
  Branch (1245:9): [True: 0, False: 11.0k]
1246
0
        LogDebug(BCLog::WALLETDB, "Error: cannot create db txn for %s\n", process_desc);
1247
0
        return false;
1248
0
    }
1249
1250
    // Run procedure
1251
11.0k
    if (!func(batch)) {
  Branch (1251:9): [True: 0, False: 11.0k]
1252
0
        LogDebug(BCLog::WALLETDB, "Error: %s failed\n", process_desc);
1253
0
        batch.TxnAbort();
1254
0
        return false;
1255
0
    }
1256
1257
11.0k
    if (!batch.TxnCommit()) {
  Branch (1257:9): [True: 0, False: 11.0k]
1258
0
        LogDebug(BCLog::WALLETDB, "Error: cannot commit db txn for %s\n", process_desc);
1259
0
        return false;
1260
0
    }
1261
1262
    // All good
1263
11.0k
    return true;
1264
11.0k
}
1265
1266
bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func)
1267
11.0k
{
1268
11.0k
    WalletBatch batch(database);
1269
11.0k
    return RunWithinTxn(batch, process_desc, func);
1270
11.0k
}
1271
1272
bool WalletBatch::WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent)
1273
0
{
1274
0
    auto key{std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), std::string("used")))};
1275
0
    return previously_spent ? WriteIC(key, std::string("1")) : EraseIC(key);
  Branch (1275:12): [True: 0, False: 0]
1276
0
}
1277
1278
bool WalletBatch::WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request)
1279
0
{
1280
0
    return WriteIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), "rr" + id)), receive_request);
1281
0
}
1282
1283
bool WalletBatch::EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id)
1284
0
{
1285
0
    return EraseIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), "rr" + id)));
1286
0
}
1287
1288
bool WalletBatch::EraseAddressData(const CTxDestination& dest)
1289
0
{
1290
0
    DataStream prefix;
1291
0
    prefix << DBKeys::DESTDATA << EncodeDestination(dest);
1292
0
    return m_batch->ErasePrefix(prefix);
1293
0
}
1294
1295
bool WalletBatch::WriteWalletFlags(const uint64_t flags)
1296
99.8k
{
1297
99.8k
    return WriteIC(DBKeys::FLAGS, flags);
1298
99.8k
}
1299
1300
bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
1301
0
{
1302
0
    return std::all_of(types.begin(), types.end(), [&](const std::string& type) {
1303
0
        return m_batch->ErasePrefix(DataStream() << type);
1304
0
    });
1305
0
}
1306
1307
bool WalletBatch::TxnBegin()
1308
99.8k
{
1309
99.8k
    return m_batch->TxnBegin();
1310
99.8k
}
1311
1312
bool WalletBatch::TxnCommit()
1313
99.8k
{
1314
99.8k
    bool res = m_batch->TxnCommit();
1315
99.8k
    if (res) {
  Branch (1315:9): [True: 99.8k, False: 0]
1316
99.8k
        for (const auto& listener : m_txn_listeners) {
  Branch (1316:35): [True: 0, False: 99.8k]
1317
0
            listener.on_commit();
1318
0
        }
1319
        // txn finished, clear listeners
1320
99.8k
        m_txn_listeners.clear();
1321
99.8k
    }
1322
99.8k
    return res;
1323
99.8k
}
1324
1325
bool WalletBatch::TxnAbort()
1326
0
{
1327
0
    bool res = m_batch->TxnAbort();
1328
0
    if (res) {
  Branch (1328:9): [True: 0, False: 0]
1329
0
        for (const auto& listener : m_txn_listeners) {
  Branch (1329:35): [True: 0, False: 0]
1330
0
            listener.on_abort();
1331
0
        }
1332
        // txn finished, clear listeners
1333
0
        m_txn_listeners.clear();
1334
0
    }
1335
0
    return res;
1336
0
}
1337
1338
void WalletBatch::RegisterTxnListener(const DbTxnListener& l)
1339
0
{
1340
0
    assert(m_batch->HasActiveTxn());
  Branch (1340:5): [True: 0, False: 0]
1341
0
    m_txn_listeners.emplace_back(l);
1342
0
}
1343
1344
std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
1345
22.1k
{
1346
22.1k
    bool exists;
1347
22.1k
    try {
1348
22.1k
        exists = fs::symlink_status(path).type() != fs::file_type::not_found;
1349
22.1k
    } catch (const fs::filesystem_error& e) {
1350
0
        error = Untranslated(strprintf("Failed to access database path '%s': %s", fs::PathToString(path), e.code().message()));
1351
0
        status = DatabaseStatus::FAILED_BAD_PATH;
1352
0
        return nullptr;
1353
0
    }
1354
1355
22.1k
    std::optional<DatabaseFormat> format;
1356
22.1k
    if (exists) {
  Branch (1356:9): [True: 11.0k, False: 11.0k]
1357
11.0k
        if (IsBDBFile(BDBDataFile(path))) {
  Branch (1357:13): [True: 0, False: 11.0k]
1358
0
            format = DatabaseFormat::BERKELEY_RO;
1359
0
        }
1360
11.0k
        if (IsSQLiteFile(SQLiteDataFile(path))) {
  Branch (1360:13): [True: 0, False: 11.0k]
1361
0
            if (format) {
  Branch (1361:17): [True: 0, False: 0]
1362
0
                error = Untranslated(strprintf("Failed to load database path '%s'. Data is in ambiguous format.", fs::PathToString(path)));
1363
0
                status = DatabaseStatus::FAILED_BAD_FORMAT;
1364
0
                return nullptr;
1365
0
            }
1366
0
            format = DatabaseFormat::SQLITE;
1367
0
        }
1368
11.0k
    } else if (options.require_existing) {
  Branch (1368:16): [True: 0, False: 11.0k]
1369
0
        error = Untranslated(strprintf("Failed to load database path '%s'. Path does not exist.", fs::PathToString(path)));
1370
0
        status = DatabaseStatus::FAILED_NOT_FOUND;
1371
0
        return nullptr;
1372
0
    }
1373
1374
22.1k
    if (!format && options.require_existing) {
  Branch (1374:9): [True: 22.1k, False: 0]
  Branch (1374:20): [True: 11.0k, False: 11.0k]
1375
11.0k
        error = Untranslated(strprintf("Failed to load database path '%s'. Data is not in recognized format.", fs::PathToString(path)));
1376
11.0k
        status = DatabaseStatus::FAILED_BAD_FORMAT;
1377
11.0k
        return nullptr;
1378
11.0k
    }
1379
1380
11.0k
    if (format && options.require_create) {
  Branch (1380:9): [True: 0, False: 11.0k]
  Branch (1380:19): [True: 0, False: 0]
1381
0
        error = Untranslated(strprintf("Failed to create database path '%s'. Database already exists.", fs::PathToString(path)));
1382
0
        status = DatabaseStatus::FAILED_ALREADY_EXISTS;
1383
0
        return nullptr;
1384
0
    }
1385
1386
    // BERKELEY_RO can only be opened if require_format was set, which only occurs in migration.
1387
11.0k
    if (format && format == DatabaseFormat::BERKELEY_RO && (!options.require_format || options.require_format != DatabaseFormat::BERKELEY_RO)) {
  Branch (1387:9): [True: 0, False: 11.0k]
  Branch (1387:9): [True: 0, False: 11.0k]
  Branch (1387:19): [True: 0, False: 0]
  Branch (1387:61): [True: 0, False: 0]
  Branch (1387:88): [True: 0, False: 0]
1388
0
        error = Untranslated(strprintf("Failed to open database path '%s'. The wallet appears to be a Legacy wallet, please use the wallet migration tool (migratewallet RPC).", fs::PathToString(path)));
1389
0
        status = DatabaseStatus::FAILED_BAD_FORMAT;
1390
0
        return nullptr;
1391
0
    }
1392
1393
    // A db already exists so format is set, but options also specifies the format, so make sure they agree
1394
11.0k
    if (format && options.require_format && format != options.require_format) {
  Branch (1394:9): [True: 0, False: 11.0k]
  Branch (1394:19): [True: 0, False: 0]
  Branch (1394:45): [True: 0, False: 0]
1395
0
        error = Untranslated(strprintf("Failed to load database path '%s'. Data is not in required format.", fs::PathToString(path)));
1396
0
        status = DatabaseStatus::FAILED_BAD_FORMAT;
1397
0
        return nullptr;
1398
0
    }
1399
1400
    // Format is not set when a db doesn't already exist, so use the format specified by the options if it is set.
1401
11.0k
    if (!format && options.require_format) format = options.require_format;
  Branch (1401:9): [True: 11.0k, False: 0]
  Branch (1401:20): [True: 11.0k, False: 0]
1402
1403
11.0k
    if (!format) {
  Branch (1403:9): [True: 0, False: 11.0k]
1404
0
        format = DatabaseFormat::SQLITE;
1405
0
    }
1406
1407
11.0k
    if (format == DatabaseFormat::SQLITE) {
  Branch (1407:9): [True: 11.0k, False: 0]
1408
11.0k
        return MakeSQLiteDatabase(path, options, status, error);
1409
11.0k
    }
1410
1411
0
    if (format == DatabaseFormat::BERKELEY_RO) {
  Branch (1411:9): [True: 0, False: 0]
1412
0
        return MakeBerkeleyRODatabase(path, options, status, error);
1413
0
    }
1414
1415
0
    error = Untranslated(STR_INTERNAL_BUG("Could not determine wallet format"));
1416
0
    status = DatabaseStatus::FAILED_BAD_FORMAT;
1417
0
    return nullptr;
1418
0
}
1419
} // namespace wallet