LCOV - code coverage report
Current view: top level - src/wallet - load.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 92.9 % 113 105
Test Date: 2025-07-01 05:14:16 Functions: 100.0 % 4 4
Branches: 52.8 % 218 115

             Branch data     Line data    Source code
       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 <wallet/load.h>
       7                 :             : 
       8                 :             : #include <common/args.h>
       9                 :             : #include <interfaces/chain.h>
      10                 :             : #include <scheduler.h>
      11                 :             : #include <util/check.h>
      12                 :             : #include <util/fs.h>
      13                 :             : #include <util/string.h>
      14                 :             : #include <util/translation.h>
      15                 :             : #include <wallet/context.h>
      16                 :             : #include <wallet/spend.h>
      17                 :             : #include <wallet/wallet.h>
      18                 :             : #include <wallet/walletdb.h>
      19                 :             : 
      20                 :             : #include <univalue.h>
      21                 :             : 
      22                 :             : #include <system_error>
      23                 :             : 
      24                 :             : using util::Join;
      25                 :             : 
      26                 :             : namespace wallet {
      27                 :         392 : bool VerifyWallets(WalletContext& context)
      28                 :             : {
      29                 :         392 :     interfaces::Chain& chain = *context.chain;
      30                 :         392 :     ArgsManager& args = *Assert(context.args);
      31                 :             : 
      32   [ +  -  +  + ]:         392 :     if (args.IsArgSet("-walletdir")) {
      33   [ +  -  +  - ]:          48 :         const fs::path wallet_dir{args.GetPathArg("-walletdir")};
      34         [ +  - ]:          24 :         std::error_code error;
      35                 :             :         // The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
      36                 :             :         // It also lets the fs::exists and fs::is_directory checks below pass on windows, since they return false
      37                 :             :         // if a path has trailing slashes, and it strips trailing slashes.
      38         [ +  - ]:          48 :         fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
      39   [ +  +  -  + ]:          43 :         if (error || !fs::exists(canonical_wallet_dir)) {
      40   [ +  -  +  -  :          15 :             chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), fs::PathToString(wallet_dir)));
                   +  - ]
      41                 :           5 :             return false;
      42   [ +  -  +  + ]:          19 :         } else if (!fs::is_directory(canonical_wallet_dir)) {
      43   [ +  -  +  -  :          15 :             chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), fs::PathToString(wallet_dir)));
                   +  - ]
      44                 :           5 :             return false;
      45                 :             :         // The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
      46         [ +  + ]:          14 :         } else if (!wallet_dir.is_absolute()) {
      47   [ +  -  +  -  :           9 :             chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), fs::PathToString(wallet_dir)));
                   +  - ]
      48                 :           3 :             return false;
      49                 :             :         }
      50   [ +  -  +  -  :          33 :         args.ForceSetArg("-walletdir", fs::PathToString(canonical_wallet_dir));
                   +  - ]
      51                 :          48 :     }
      52                 :             : 
      53   [ +  -  +  - ]:        1137 :     LogPrintf("Using wallet directory %s\n", fs::PathToString(GetWalletDir()));
      54                 :             : 
      55         [ +  - ]:         379 :     chain.initMessage(_("Verifying wallet(s)…"));
      56                 :             : 
      57                 :             :     // For backwards compatibility if an unnamed top level wallet exists in the
      58                 :             :     // wallets directory, include it in the default list of wallets to load.
      59   [ +  -  +  + ]:         379 :     if (!args.IsArgSet("wallet")) {
      60         [ +  - ]:         266 :         DatabaseOptions options;
      61                 :         266 :         DatabaseStatus status;
      62         [ +  - ]:         266 :         ReadDatabaseArgs(args, options);
      63         [ +  - ]:         266 :         bilingual_str error_string;
      64                 :         266 :         options.require_existing = true;
      65                 :         266 :         options.verify = false;
      66   [ +  -  +  -  :         266 :         if (MakeWalletDatabase("", options, status, error_string)) {
                   +  + ]
      67                 :           1 :             common::SettingsValue wallets(common::SettingsValue::VARR);
      68   [ +  -  +  - ]:           1 :             wallets.push_back(""); // Default wallet name is ""
      69                 :             :             // Pass write=false because no need to write file and probably
      70                 :             :             // better not to. If unnamed wallet needs to be added next startup
      71                 :             :             // and the setting is empty, this code will just run again.
      72   [ +  -  +  - ]:           2 :             chain.overwriteRwSetting("wallet", std::move(wallets), interfaces::SettingsAction::SKIP_WRITE);
      73                 :           1 :         }
      74                 :         266 :     }
      75                 :             : 
      76                 :             :     // Keep track of each wallet absolute path to detect duplicates.
      77         [ +  - ]:         379 :     std::set<fs::path> wallet_paths;
      78                 :             : 
      79   [ +  -  +  -  :         474 :     for (const auto& wallet : chain.getSettingsList("wallet")) {
                   +  + ]
      80         [ +  + ]:         108 :         if (!wallet.isStr()) {
      81   [ +  -  +  - ]:           8 :             chain.initError(_("Invalid value detected for '-wallet' or '-nowallet'. "
      82                 :             :                               "'-wallet' requires a string value, while '-nowallet' accepts only '1' to disable all wallets"));
      83                 :           8 :             return false;
      84                 :             :         }
      85         [ +  - ]:         100 :         const auto& wallet_file = wallet.get_str();
      86   [ +  -  +  -  :         200 :         const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(wallet_file));
                   +  - ]
      87                 :             : 
      88   [ +  -  +  + ]:         100 :         if (!wallet_paths.insert(path).second) {
      89   [ +  -  +  - ]:           2 :             chain.initWarning(strprintf(_("Ignoring duplicate -wallet %s."), wallet_file));
      90                 :           2 :             continue;
      91                 :             :         }
      92                 :             : 
      93         [ +  - ]:          98 :         DatabaseOptions options;
      94                 :          98 :         DatabaseStatus status;
      95         [ +  - ]:          98 :         ReadDatabaseArgs(args, options);
      96                 :          98 :         options.require_existing = true;
      97                 :          98 :         options.verify = true;
      98         [ +  - ]:          98 :         bilingual_str error_string;
      99   [ +  -  +  + ]:          98 :         if (!MakeWalletDatabase(wallet_file, options, status, error_string)) {
     100         [ -  + ]:           6 :             if (status == DatabaseStatus::FAILED_NOT_FOUND) {
     101   [ #  #  #  #  :           0 :                 chain.initWarning(Untranslated(strprintf("Skipping -wallet path that doesn't exist. %s", error_string.original)));
                   #  # ]
     102         [ +  + ]:           6 :             } else if (status == DatabaseStatus::FAILED_LEGACY_DISABLED) {
     103                 :             :                 // Skipping legacy wallets as they will not be loaded.
     104                 :             :                 // This will be properly communicated to the user during the loading process.
     105                 :           1 :                 continue;
     106                 :             :             } else {
     107         [ +  - ]:           5 :                 chain.initError(error_string);
     108                 :           5 :                 return false;
     109                 :             :             }
     110                 :             :         }
     111                 :         206 :     }
     112                 :             : 
     113                 :         366 :     return true;
     114                 :         379 : }
     115                 :             : 
     116                 :         336 : bool LoadWallets(WalletContext& context)
     117                 :             : {
     118                 :         336 :     interfaces::Chain& chain = *context.chain;
     119                 :         336 :     try {
     120         [ +  - ]:         336 :         std::set<fs::path> wallet_paths;
     121   [ +  -  +  -  :         429 :         for (const auto& wallet : chain.getSettingsList("wallet")) {
                   +  + ]
     122         [ -  + ]:          93 :             if (!wallet.isStr()) {
     123   [ #  #  #  # ]:           0 :                 chain.initError(_("Invalid value detected for '-wallet' or '-nowallet'. "
     124                 :             :                                   "'-wallet' requires a string value, while '-nowallet' accepts only '1' to disable all wallets"));
     125                 :           0 :                 return false;
     126                 :             :             }
     127         [ +  - ]:          93 :             const auto& name = wallet.get_str();
     128   [ +  -  +  -  :         186 :             if (!wallet_paths.insert(fs::PathFromString(name)).second) {
                   +  + ]
     129                 :           2 :                 continue;
     130                 :             :             }
     131         [ +  - ]:          91 :             DatabaseOptions options;
     132                 :          91 :             DatabaseStatus status;
     133         [ +  - ]:          91 :             ReadDatabaseArgs(*context.args, options);
     134                 :          91 :             options.require_existing = true;
     135                 :          91 :             options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
     136         [ +  - ]:          91 :             bilingual_str error;
     137                 :          91 :             std::vector<bilingual_str> warnings;
     138         [ +  - ]:          91 :             std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
     139         [ +  + ]:          91 :             if (!database) {
     140         [ -  + ]:           1 :                 if (status == DatabaseStatus::FAILED_NOT_FOUND) continue;
     141         [ +  - ]:           1 :                 if (status == DatabaseStatus::FAILED_LEGACY_DISABLED) {
     142                 :             :                     // Inform user that legacy wallet is not loaded and suggest upgrade options
     143         [ +  - ]:           1 :                     chain.initWarning(error);
     144                 :           1 :                     continue;
     145                 :             :                 }
     146                 :             :             }
     147   [ +  -  +  - ]:          90 :             chain.initMessage(_("Loading wallet…"));
     148   [ +  -  +  - ]:          90 :             std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(context, name, std::move(database), options.create_flags, error, warnings) : nullptr;
     149   [ +  +  +  -  :          92 :             if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
          +  -  +  -  +  
                      - ]
     150         [ -  + ]:          90 :             if (!pwallet) {
     151         [ #  # ]:           0 :                 chain.initError(error);
     152         [ #  # ]:           0 :                 return false;
     153                 :             :             }
     154                 :             : 
     155         [ +  - ]:          90 :             NotifyWalletLoaded(context, pwallet);
     156         [ +  - ]:          90 :             AddWallet(context, pwallet);
     157                 :         182 :         }
     158                 :         336 :         return true;
     159         [ -  - ]:         336 :     } catch (const std::runtime_error& e) {
     160   [ -  -  -  -  :           0 :         chain.initError(Untranslated(e.what()));
                   -  - ]
     161                 :           0 :         return false;
     162                 :           0 :     }
     163                 :             : }
     164                 :             : 
     165                 :         336 : void StartWallets(WalletContext& context)
     166                 :             : {
     167         [ +  + ]:         426 :     for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
     168         [ +  - ]:          90 :         pwallet->postInitProcess();
     169                 :         336 :     }
     170                 :             : 
     171         [ +  - ]:         518 :     context.scheduler->scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
     172                 :         336 : }
     173                 :             : 
     174                 :         794 : void UnloadWallets(WalletContext& context)
     175                 :             : {
     176                 :         794 :     auto wallets = GetWallets(context);
     177         [ +  + ]:        2095 :     while (!wallets.empty()) {
     178         [ +  - ]:         507 :         auto wallet = wallets.back();
     179                 :         507 :         wallets.pop_back();
     180                 :         507 :         std::vector<bilingual_str> warnings;
     181         [ +  - ]:         507 :         RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt, warnings);
     182         [ +  - ]:         507 :         WaitForDeleteWallet(std::move(wallet));
     183         [ -  + ]:         507 :     }
     184                 :         794 : }
     185                 :             : } // namespace wallet
        

Generated by: LCOV version 2.0-1