Bitcoin Core  0.19.99
P2P Digital Currency
load.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <interfaces/chain.h>
9 #include <scheduler.h>
10 #include <util/string.h>
11 #include <util/system.h>
12 #include <util/translation.h>
13 #include <wallet/wallet.h>
14 
15 bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
16 {
17  if (gArgs.IsArgSet("-walletdir")) {
18  fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
19  boost::system::error_code error;
20  // The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
21  fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
22  if (error || !fs::exists(wallet_dir)) {
23  chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist").translated, wallet_dir.string()));
24  return false;
25  } else if (!fs::is_directory(wallet_dir)) {
26  chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory").translated, wallet_dir.string()));
27  return false;
28  // The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
29  } else if (!wallet_dir.is_absolute()) {
30  chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path").translated, wallet_dir.string()));
31  return false;
32  }
33  gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
34  }
35 
36  LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
37 
38  chain.initMessage(_("Verifying wallet(s)...").translated);
39 
40  // Parameter interaction code should have thrown an error if -salvagewallet
41  // was enabled with more than wallet file, so the wallet_files size check
42  // here should have no effect.
43  bool salvage_wallet = gArgs.GetBoolArg("-salvagewallet", false) && wallet_files.size() <= 1;
44 
45  // Keep track of each wallet absolute path to detect duplicates.
46  std::set<fs::path> wallet_paths;
47 
48  for (const auto& wallet_file : wallet_files) {
49  WalletLocation location(wallet_file);
50 
51  if (!wallet_paths.insert(location.GetPath()).second) {
52  chain.initError(strprintf(_("Error loading wallet %s. Duplicate -wallet filename specified.").translated, wallet_file));
53  return false;
54  }
55 
56  std::string error_string;
57  std::vector<std::string> warnings;
58  bool verify_success = CWallet::Verify(chain, location, salvage_wallet, error_string, warnings);
59  if (!error_string.empty()) chain.initError(error_string);
60  if (!warnings.empty()) chain.initWarning(Join(warnings, "\n"));
61  if (!verify_success) return false;
62  }
63 
64  return true;
65 }
66 
67 bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
68 {
69  for (const std::string& walletFile : wallet_files) {
70  std::string error;
71  std::vector<std::string> warnings;
72  std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile), error, warnings);
73  if (!warnings.empty()) chain.initWarning(Join(warnings, "\n"));
74  if (!pwallet) {
75  chain.initError(error);
76  return false;
77  }
78  AddWallet(pwallet);
79  }
80 
81  return true;
82 }
83 
84 void StartWallets(CScheduler& scheduler)
85 {
86  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
87  pwallet->postInitProcess();
88  }
89 
90  // Schedule periodic wallet flushes and tx rebroadcasts
91  scheduler.scheduleEvery(MaybeCompactWalletDB, 500);
92  scheduler.scheduleEvery(MaybeResendWalletTxs, 1000);
93 }
94 
96 {
97  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
98  pwallet->Flush(false);
99  }
100 }
101 
103 {
104  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
105  pwallet->Flush(true);
106  }
107 }
108 
110 {
111  auto wallets = GetWallets();
112  while (!wallets.empty()) {
113  auto wallet = wallets.back();
114  wallets.pop_back();
115  RemoveWallet(wallet);
116  UnloadWallet(std::move(wallet));
117  }
118 }
const fs::path & GetPath() const
Get wallet absolute path.
Definition: walletutil.h:80
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:362
std::vector< std::shared_ptr< CWallet > > GetWallets()
Definition: dummywallet.cpp:70
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition: wallet.cpp:128
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bool AddWallet(const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:53
static std::shared_ptr< CWallet > CreateWalletFromFile(interfaces::Chain &chain, const WalletLocation &location, std::string &error, std::vector< std::string > &warnings, uint64_t wallet_creation_flags=0)
Definition: wallet.cpp:3649
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:163
void scheduleEvery(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:145
void MaybeResendWalletTxs()
Called periodically by the schedule thread.
Definition: wallet.cpp:2017
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:384
bool LoadWallets(interfaces::Chain &chain, const std::vector< std::string > &wallet_files)
Load wallet databases.
Definition: load.cpp:67
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:406
virtual void initError(const std::string &message)=0
Send init error.
void StartWallets(CScheduler &scheduler)
Complete startup of wallets.
Definition: load.cpp:84
void StopWallets()
Stop all wallets. Wallets will be flushed first.
Definition: load.cpp:102
std::string Join(const std::vector< T > &list, const std::string &separator, UnaryOp unary_op)
Join a list of items.
Definition: string.h:32
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:36
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:662
void FlushWallets()
Flush all wallets in preparation for shutdown.
Definition: load.cpp:95
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:54
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:372
virtual void initWarning(const std::string &message)=0
Send init warning.
virtual void initMessage(const std::string &message)=0
Send init message.
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:64
ArgsManager gArgs
Definition: system.cpp:76
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: dummywallet.cpp:60
The WalletLocation class provides wallet information.
Definition: walletutil.h:67
static bool Verify(interfaces::Chain &chain, const WalletLocation &location, bool salvage_wallet, std::string &error_string, std::vector< std::string > &warnings)
Verify wallet naming and perform salvage on the wallet if required.
Definition: wallet.cpp:3592
void UnloadWallets()
Close all wallets.
Definition: load.cpp:109
bool VerifyWallets(interfaces::Chain &chain, const std::vector< std::string > &wallet_files)
Responsible for reading and validating the -wallet arguments and verifying the wallet database...
Definition: load.cpp:15
bool error(const char *fmt, const Args &... args)
Definition: system.h:49