Bitcoin ABC  0.22.13
P2P Digital Currency
load.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 #include <wallet/walletdb.h>
15 
16 bool VerifyWallets(const CChainParams &chainParams, interfaces::Chain &chain,
17  const std::vector<std::string> &wallet_files) {
18  if (gArgs.IsArgSet("-walletdir")) {
19  fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
20  boost::system::error_code error;
21  // The canonical path cleans the path, preventing >1 Berkeley
22  // environment instances for the same directory
23  fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
24  if (error || !fs::exists(wallet_dir)) {
25  chain.initError(
26  strprintf(_("Specified -walletdir \"%s\" does not exist"),
27  wallet_dir.string()));
28  return false;
29  } else if (!fs::is_directory(wallet_dir)) {
30  chain.initError(
31  strprintf(_("Specified -walletdir \"%s\" is not a directory"),
32  wallet_dir.string()));
33  return false;
34  // The canonical path transforms relative paths into absolute ones,
35  // so we check the non-canonical version
36  } else if (!wallet_dir.is_absolute()) {
37  chain.initError(
38  strprintf(_("Specified -walletdir \"%s\" is a relative path"),
39  wallet_dir.string()));
40  return false;
41  }
42  gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
43  }
44 
45  LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
46 
47  chain.initMessage(_("Verifying wallet(s)...").translated);
48 
49  // Keep track of each wallet absolute path to detect duplicates.
50  std::set<fs::path> wallet_paths;
51 
52  for (const auto &wallet_file : wallet_files) {
53  WalletLocation location(wallet_file);
54 
55  if (!wallet_paths.insert(location.GetPath()).second) {
56  chain.initError(strprintf(_("Error loading wallet %s. Duplicate "
57  "-wallet filename specified."),
58  wallet_file));
59  return false;
60  }
61 
62  bilingual_str error_string;
63  std::vector<bilingual_str> warnings;
64  bool verify_success = CWallet::Verify(chainParams, chain, location,
65  error_string, warnings);
66  if (!warnings.empty()) {
67  chain.initWarning(Join(warnings, Untranslated("\n")));
68  }
69  if (!verify_success) {
70  chain.initError(error_string);
71  return false;
72  }
73  }
74 
75  return true;
76 }
77 
78 bool LoadWallets(const CChainParams &chainParams, interfaces::Chain &chain,
79  const std::vector<std::string> &wallet_files) {
80  try {
81  for (const std::string &walletFile : wallet_files) {
83  std::vector<bilingual_str> warnings;
84  std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(
85  chainParams, chain, WalletLocation(walletFile), error,
86  warnings);
87  if (!warnings.empty()) {
88  chain.initWarning(Join(warnings, Untranslated("\n")));
89  }
90  if (!pwallet) {
91  chain.initError(error);
92  return false;
93  }
94  AddWallet(pwallet);
95  }
96  return true;
97  } catch (const std::runtime_error &e) {
98  chain.initError(Untranslated(e.what()));
99  return false;
100  }
101 }
102 
103 void StartWallets(CScheduler &scheduler, const ArgsManager &args) {
104  for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
105  pwallet->postInitProcess();
106  }
107 
108  // Schedule periodic wallet flushes and tx rebroadcasts
109  if (args.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
110  scheduler.scheduleEvery(
111  [] {
113  return true;
114  },
115  std::chrono::milliseconds{500});
116  }
117  scheduler.scheduleEvery(
118  [] {
120  return true;
121  },
122  std::chrono::milliseconds{1000});
123 }
124 
125 void FlushWallets() {
126  for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
127  pwallet->Flush(false);
128  }
129 }
130 
131 void StopWallets() {
132  for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
133  pwallet->Flush(true);
134  }
135 }
136 
138  auto wallets = GetWallets();
139  while (!wallets.empty()) {
140  auto wallet = wallets.back();
141  wallets.pop_back();
142  RemoveWallet(wallet);
143  UnloadWallet(std::move(wallet));
144  }
145 }
const fs::path & GetPath() const
Get wallet absolute path.
Definition: walletutil.h:92
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:390
std::vector< std::shared_ptr< CWallet > > GetWallets()
Definition: dummywallet.cpp:53
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition: wallet.cpp:129
Bilingual messages:
Definition: translation.h:17
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:34
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
bool AddWallet(const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:50
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:174
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:47
void MaybeResendWalletTxs()
Called periodically by the schedule thread.
Definition: wallet.cpp:2241
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:498
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition: system.cpp:523
static bool Verify(const CChainParams &chainParams, interfaces::Chain &chain, const WalletLocation &location, bilingual_str &error_string, std::vector< bilingual_str > &warnings)
Verify wallet naming and perform salvage on the wallet if required.
Definition: wallet.cpp:4113
void StopWallets()
Stop all wallets. Wallets will be flushed first.
Definition: load.cpp:131
bool LoadWallets(const CChainParams &chainParams, interfaces::Chain &chain, const std::vector< std::string > &wallet_files)
Load wallet databases.
Definition: load.cpp:78
static std::shared_ptr< CWallet > CreateWalletFromFile(const CChainParams &chainParams, interfaces::Chain &chain, const WalletLocation &location, bilingual_str &error, std::vector< bilingual_str > &warnings, uint64_t wallet_creation_flags=0)
Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error...
Definition: wallet.cpp:4164
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
void scheduleEvery(Predicate p, std::chrono::milliseconds delta)
Repeat p until it return false.
Definition: scheduler.cpp:127
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
Definition: walletdb.cpp:934
bool VerifyWallets(const CChainParams &chainParams, 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:16
void FlushWallets()
Flush all wallets in preparation for shutdown.
Definition: load.cpp:125
virtual void initWarning(const bilingual_str &message)=0
Send init warning.
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:105
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:479
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition: walletdb.h:34
virtual void initMessage(const std::string &message)=0
Send init message.
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet)
Definition: wallet.cpp:63
ArgsManager gArgs
Definition: system.cpp:76
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: dummywallet.cpp:45
The WalletLocation class provides wallet information.
Definition: walletutil.h:80
void UnloadWallets()
Close all wallets.
Definition: load.cpp:137
virtual void initError(const bilingual_str &message)=0
Send init error.
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
void StartWallets(CScheduler &scheduler, const ArgsManager &args)
Complete startup of wallets.
Definition: load.cpp:103