Bitcoin Core  0.20.99
P2P Digital Currency
load.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <fs.h>
9 #include <interfaces/chain.h>
10 #include <scheduler.h>
11 #include <util/string.h>
12 #include <util/system.h>
13 #include <util/translation.h>
14 #include <wallet/wallet.h>
15 #include <wallet/walletdb.h>
16 
17 #include <univalue.h>
18 
20 {
21  if (gArgs.IsArgSet("-walletdir")) {
22  fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
23  boost::system::error_code error;
24  // The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
25  fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
26  if (error || !fs::exists(wallet_dir)) {
27  chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string()));
28  return false;
29  } else if (!fs::is_directory(wallet_dir)) {
30  chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string()));
31  return false;
32  // The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
33  } else if (!wallet_dir.is_absolute()) {
34  chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string()));
35  return false;
36  }
37  gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
38  }
39 
40  LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
41 
42  chain.initMessage(_("Verifying wallet(s)...").translated);
43 
44  // For backwards compatibility if an unnamed top level wallet exists in the
45  // wallets directory, include it in the default list of wallets to load.
46  if (!gArgs.IsArgSet("wallet")) {
47  DatabaseOptions options;
48  DatabaseStatus status;
49  bilingual_str error_string;
50  options.require_existing = true;
51  options.verify = false;
52  if (MakeWalletDatabase("", options, status, error_string)) {
53  gArgs.LockSettings([&](util::Settings& settings) {
55  wallets.push_back(""); // Default wallet name is ""
56  settings.rw_settings["wallet"] = wallets;
57  });
58  }
59  }
60 
61  // Keep track of each wallet absolute path to detect duplicates.
62  std::set<fs::path> wallet_paths;
63 
64  for (const auto& wallet_file : gArgs.GetArgs("-wallet")) {
65  const fs::path path = fs::absolute(wallet_file, GetWalletDir());
66 
67  if (!wallet_paths.insert(path).second) {
68  chain.initError(strprintf(_("Error loading wallet %s. Duplicate -wallet filename specified."), wallet_file));
69  return false;
70  }
71 
72  DatabaseOptions options;
73  DatabaseStatus status;
74  options.verify = true;
75  bilingual_str error_string;
76  if (!MakeWalletDatabase(wallet_file, options, status, error_string)) {
77  chain.initError(error_string);
78  return false;
79  }
80  }
81 
82  return true;
83 }
84 
86 {
87  try {
88  for (const std::string& name : gArgs.GetArgs("-wallet")) {
89  DatabaseOptions options;
90  DatabaseStatus status;
91  options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
93  std::vector<bilingual_str> warnings;
94  std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
95  std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings) : nullptr;
96  if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
97  if (!pwallet) {
98  chain.initError(error);
99  return false;
100  }
101  AddWallet(pwallet);
102  }
103  return true;
104  } catch (const std::runtime_error& e) {
105  chain.initError(Untranslated(e.what()));
106  return false;
107  }
108 }
109 
110 void StartWallets(CScheduler& scheduler, const ArgsManager& args)
111 {
112  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
113  pwallet->postInitProcess();
114  }
115 
116  // Schedule periodic wallet flushes and tx rebroadcasts
117  if (args.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
118  scheduler.scheduleEvery(MaybeCompactWalletDB, std::chrono::milliseconds{500});
119  }
120  scheduler.scheduleEvery(MaybeResendWalletTxs, std::chrono::milliseconds{1000});
121 }
122 
124 {
125  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
126  pwallet->Flush();
127  }
128 }
129 
131 {
132  for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
133  pwallet->Close();
134  }
135 }
136 
138 {
139  auto wallets = GetWallets();
140  while (!wallets.empty()) {
141  auto wallet = wallets.back();
142  wallets.pop_back();
143  std::vector<bilingual_str> warnings;
144  RemoveWallet(wallet, nullopt, warnings);
145  UnloadWallet(std::move(wallet));
146  }
147 }
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
Stored settings.
Definition: settings.h:31
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:370
bool LoadWallets(interfaces::Chain &chain)
Load wallet databases.
Definition: load.cpp:85
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition: wallet.cpp:178
void scheduleEvery(Function f, std::chrono::milliseconds delta)
Repeat f until the scheduler is stopped.
Definition: scheduler.cpp:108
Bilingual messages:
Definition: translation.h:16
auto Join(const std::vector< T > &list, const BaseType &separator, UnaryOp unary_op) -> decltype(unary_op(list.at(0)))
Join a list of items.
Definition: string.h:36
#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:95
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:40
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:166
static auto & nullopt
Substitute for C++17 std::nullopt.
Definition: optional.h:24
std::vector< std::shared_ptr< CWallet > > GetWallets()
Definition: wallet.cpp:132
void MaybeResendWalletTxs()
Called periodically by the schedule thread.
Definition: wallet.cpp:2128
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:478
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:500
void StopWallets()
Stop all wallets. Wallets will be flushed first.
Definition: load.cpp:130
bool verify
Definition: db.h:210
bool VerifyWallets(interfaces::Chain &chain)
Responsible for reading and validating the -wallet arguments and verifying the wallet database...
Definition: load.cpp:19
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: walletutil.cpp:13
const char * name
Definition: rest.cpp:41
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:57
bool require_existing
Definition: db.h:205
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition: wallet.cpp:3786
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:936
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet, Optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:106
void FlushWallets()
Flush all wallets in preparation for shutdown.
Definition: load.cpp:123
virtual void initWarning(const bilingual_str &message)=0
Send init warning.
static std::shared_ptr< CWallet > Create(interfaces::Chain &chain, const std::string &name, std::unique_ptr< WalletDatabase > database, uint64_t wallet_creation_flags, bilingual_str &error, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:3810
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:83
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:466
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:32
virtual void initMessage(const std::string &message)=0
Send init message.
ArgsManager gArgs
Definition: system.cpp:76
uint64_t create_flags
Definition: db.h:208
void UnloadWallets()
Close all wallets.
Definition: load.cpp:137
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:32
void LockSettings(Fn &&fn)
Access settings with lock held.
Definition: system.h:376
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: system.cpp:361
DatabaseStatus
Definition: db.h:213
virtual void initError(const bilingual_str &message)=0
Send init error.
bool error(const char *fmt, const Args &... args)
Definition: system.h:52
void StartWallets(CScheduler &scheduler, const ArgsManager &args)
Complete startup of wallets.
Definition: load.cpp:110