Bitcoin Core  24.99.0
P2P Digital Currency
load.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 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/check.h>
12 #include <util/string.h>
13 #include <util/system.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 namespace wallet {
26 {
29 
30  if (args.IsArgSet("-walletdir")) {
31  const fs::path wallet_dir{args.GetPathArg("-walletdir")};
32  std::error_code error;
33  // The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
34  // It also lets the fs::exists and fs::is_directory checks below pass on windows, since they return false
35  // if a path has trailing slashes, and it strips trailing slashes.
36  fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
37  if (error || !fs::exists(canonical_wallet_dir)) {
38  chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), fs::PathToString(wallet_dir)));
39  return false;
40  } else if (!fs::is_directory(canonical_wallet_dir)) {
41  chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), fs::PathToString(wallet_dir)));
42  return false;
43  // The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
44  } else if (!wallet_dir.is_absolute()) {
45  chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), fs::PathToString(wallet_dir)));
46  return false;
47  }
48  args.ForceSetArg("-walletdir", fs::PathToString(canonical_wallet_dir));
49  }
50 
51  LogPrintf("Using wallet directory %s\n", fs::PathToString(GetWalletDir()));
52 
53  chain.initMessage(_("Verifying wallet(s)…").translated);
54 
55  // For backwards compatibility if an unnamed top level wallet exists in the
56  // wallets directory, include it in the default list of wallets to load.
57  if (!args.IsArgSet("wallet")) {
58  DatabaseOptions options;
59  DatabaseStatus status;
60  ReadDatabaseArgs(args, options);
61  bilingual_str error_string;
62  options.require_existing = true;
63  options.verify = false;
64  if (MakeWalletDatabase("", options, status, error_string)) {
66  wallets.push_back(""); // Default wallet name is ""
67  // Pass write=false because no need to write file and probably
68  // better not to. If unnamed wallet needs to be added next startup
69  // and the setting is empty, this code will just run again.
70  chain.updateRwSetting("wallet", wallets, /* write= */ false);
71  }
72  }
73 
74  // Keep track of each wallet absolute path to detect duplicates.
75  std::set<fs::path> wallet_paths;
76 
77  for (const auto& wallet : chain.getSettingsList("wallet")) {
78  const auto& wallet_file = wallet.get_str();
79  const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(wallet_file));
80 
81  if (!wallet_paths.insert(path).second) {
82  chain.initWarning(strprintf(_("Ignoring duplicate -wallet %s."), wallet_file));
83  continue;
84  }
85 
86  DatabaseOptions options;
87  DatabaseStatus status;
88  ReadDatabaseArgs(args, options);
89  options.require_existing = true;
90  options.verify = true;
91  bilingual_str error_string;
92  if (!MakeWalletDatabase(wallet_file, options, status, error_string)) {
93  if (status == DatabaseStatus::FAILED_NOT_FOUND) {
94  chain.initWarning(Untranslated(strprintf("Skipping -wallet path that doesn't exist. %s", error_string.original)));
95  } else {
96  chain.initError(error_string);
97  return false;
98  }
99  }
100  }
101 
102  return true;
103 }
104 
106 {
107  interfaces::Chain& chain = *context.chain;
108  try {
109  std::set<fs::path> wallet_paths;
110  for (const auto& wallet : chain.getSettingsList("wallet")) {
111  const auto& name = wallet.get_str();
112  if (!wallet_paths.insert(fs::PathFromString(name)).second) {
113  continue;
114  }
115  DatabaseOptions options;
116  DatabaseStatus status;
117  ReadDatabaseArgs(*context.args, options);
118  options.require_existing = true;
119  options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
121  std::vector<bilingual_str> warnings;
122  std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
123  if (!database && status == DatabaseStatus::FAILED_NOT_FOUND) {
124  continue;
125  }
126  chain.initMessage(_("Loading wallet…").translated);
127  std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(context, name, std::move(database), options.create_flags, error, warnings) : nullptr;
128  if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
129  if (!pwallet) {
130  chain.initError(error);
131  return false;
132  }
133 
134  NotifyWalletLoaded(context, pwallet);
135  AddWallet(context, pwallet);
136  }
137  return true;
138  } catch (const std::runtime_error& e) {
139  chain.initError(Untranslated(e.what()));
140  return false;
141  }
142 }
143 
145 {
146  for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
147  pwallet->postInitProcess();
148  }
149 
150  // Schedule periodic wallet flushes and tx rebroadcasts
151  if (context.args->GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
152  scheduler.scheduleEvery([&context] { MaybeCompactWalletDB(context); }, std::chrono::milliseconds{500});
153  }
154  scheduler.scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
155 }
156 
158 {
159  for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
160  pwallet->Flush();
161  }
162 }
163 
165 {
166  for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
167  pwallet->Close();
168  }
169 }
170 
172 {
173  auto wallets = GetWallets(context);
174  while (!wallets.empty()) {
175  auto wallet = wallets.back();
176  wallets.pop_back();
177  std::vector<bilingual_str> warnings;
178  RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt, warnings);
179  UnloadWallet(std::move(wallet));
180  }
181 }
182 } // namespace wallet
#define Assert(val)
Identity function.
Definition: check.h:74
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:693
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:500
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:654
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition: system.cpp:393
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:39
void scheduleEvery(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Repeat f until the scheduler is stopped.
Definition: scheduler.cpp:110
void push_back(UniValue val)
Definition: univalue.cpp:104
@ VARR
Definition: univalue.h:20
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:118
virtual bool updateRwSetting(const std::string &name, const util::SettingsValue &value, bool write=true)=0
Write a setting to <datadir>/settings.json.
virtual void initMessage(const std::string &message)=0
Send init message.
virtual void initError(const bilingual_str &message)=0
Send init error.
virtual void initWarning(const bilingual_str &message)=0
Send init warning.
virtual std::vector< util::SettingsValue > getSettingsList(const std::string &arg)=0
Get list of settings values.
static std::shared_ptr< CWallet > Create(WalletContext &context, 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:2790
#define LogPrintf(...)
Definition: logging.h:234
static bool exists(const path &p)
Definition: fs.h:88
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:150
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:173
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition: fs.cpp:35
Definition: node.h:39
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition: wallet.cpp:207
void ReadDatabaseArgs(const ArgsManager &args, DatabaseOptions &options)
Definition: db.cpp:142
void MaybeResendWalletTxs(WalletContext &context)
Called periodically by the schedule thread.
Definition: wallet.cpp:1979
std::vector< std::shared_ptr< CWallet > > GetWallets(WalletContext &context)
Definition: wallet.cpp:146
void MaybeCompactWalletDB(WalletContext &context)
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:1054
bool VerifyWallets(WalletContext &context)
Responsible for reading and validating the -wallet arguments and verifying the wallet database.
Definition: load.cpp:25
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: walletutil.cpp:11
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition: wallet.cpp:2766
void NotifyWalletLoaded(WalletContext &context, const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:175
void StartWallets(WalletContext &context, CScheduler &scheduler)
Complete startup of wallets.
Definition: load.cpp:144
bool AddWallet(WalletContext &context, const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:108
void UnloadWallets(WalletContext &context)
Close all wallets.
Definition: load.cpp:171
bool LoadWallets(WalletContext &context)
Load wallet databases.
Definition: load.cpp:105
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:42
void FlushWallets(WalletContext &context)
Flush all wallets in preparation for shutdown.
Definition: load.cpp:157
void StopWallets(WalletContext &context)
Stop all wallets. Wallets will be flushed first.
Definition: load.cpp:164
DatabaseStatus
Definition: db.h:219
bool RemoveWallet(WalletContext &context, const std::shared_ptr< CWallet > &wallet, std::optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
Definition: wallet.cpp:120
WalletContext context
ArgsManager args
const char * name
Definition: rest.cpp:46
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
Definition: string.h:68
Bilingual messages:
Definition: translation.h:18
std::string original
Definition: translation.h:19
bool require_existing
Definition: db.h:206
bool verify
Check data integrity on load.
Definition: db.h:213
uint64_t create_flags
Definition: db.h:209
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35
interfaces::Chain * chain
Definition: context.h:36
ArgsManager * args
Definition: context.h:37
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:65
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:48