Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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 <common/args.h>
9#include <interfaces/chain.h>
10#include <logging.h>
11#include <scheduler.h>
12#include <util/fs.h>
13#include <util/string.h>
14#include <util/translation.h>
15#include <wallet/spend.h>
16#include <wallet/wallet.h>
17#include <wallet/walletdb.h>
18
19#include <univalue.h>
20
21#include <system_error>
22
24 if (gArgs.IsArgSet("-walletdir")) {
25 const fs::path wallet_dir{gArgs.GetPathArg("-walletdir")};
26 std::error_code error;
27 // The canonical path cleans the path, preventing >1 Berkeley
28 // environment instances for the same directory
31 chain.initError(
32 strprintf(_("Specified -walletdir \"%s\" does not exist"),
34 return false;
35 } else if (!fs::is_directory(canonical_wallet_dir)) {
36 chain.initError(
37 strprintf(_("Specified -walletdir \"%s\" is not a directory"),
39 return false;
40 // The canonical path transforms relative paths into absolute ones,
41 // so we check the non-canonical version
42 } else if (!wallet_dir.is_absolute()) {
43 chain.initError(
44 strprintf(_("Specified -walletdir \"%s\" is a relative path"),
46 return false;
47 }
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 (!gArgs.IsArgSet("wallet")) {
58 DatabaseOptions options;
59 DatabaseStatus status;
61 options.require_existing = true;
62 options.verify = false;
63 if (MakeWalletDatabase("", options, status, error_string)) {
65 wallets.push_back(""); // Default wallet name is ""
66 // Pass write=false because no need to write file and probably
67 // better not to. If unnamed wallet needs to be added next startup
68 // and the setting is empty, this code will just run again.
69 chain.updateRwSetting("wallet", wallets, /* write= */ false);
70 }
71 }
72
73 // Keep track of each wallet absolute path to detect duplicates.
74 std::set<fs::path> wallet_paths;
75
76 for (const auto &wallet : chain.getSettingsList("wallet")) {
77 const auto &wallet_file = wallet.get_str();
80
81 if (!wallet_paths.insert(path).second) {
82 chain.initWarning(
83 strprintf(_("Ignoring duplicate -wallet %s."), wallet_file));
84 continue;
85 }
86
87 DatabaseOptions options;
88 DatabaseStatus status;
89 options.require_existing = true;
90 options.verify = true;
92 if (!MakeWalletDatabase(wallet_file, options, status, error_string)) {
95 strprintf("Skipping -wallet path that doesn't exist. %s\n",
96 error_string.original)));
97 } else {
99 return false;
100 }
101 }
102 }
103
104 return true;
105}
106
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 options.require_existing = true;
118 // No need to verify, assuming verified earlier in VerifyWallets()
119 options.verify = false;
121 std::vector<bilingual_str> warnings;
122 std::unique_ptr<WalletDatabase> database =
123 MakeWalletDatabase(name, options, status, error);
124 if (!database && status == DatabaseStatus::FAILED_NOT_FOUND) {
125 continue;
126 }
127 std::shared_ptr<CWallet> pwallet =
128 database
129 ? CWallet::Create(chain, name, std::move(database),
130 options.create_flags, error, warnings)
131 : nullptr;
132
133 if (!warnings.empty()) {
134 chain.initWarning(Join(warnings, Untranslated("\n")));
135 }
136 if (!pwallet) {
137 chain.initError(error);
138 return false;
139 }
140 AddWallet(pwallet);
141 }
142 return true;
143 } catch (const std::runtime_error &e) {
144 chain.initError(Untranslated(e.what()));
145 return false;
146 }
147}
148
149void StartWallets(CScheduler &scheduler, const ArgsManager &args) {
150 for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
151 pwallet->postInitProcess();
152 }
153
154 // Schedule periodic wallet flushes and tx rebroadcasts
155 if (args.GetBoolArg("-flushwallet", DEFAULT_FLUSHWALLET)) {
156 scheduler.scheduleEvery(
157 [] {
159 return true;
160 },
161 std::chrono::milliseconds{500});
162 }
163 scheduler.scheduleEvery(
164 [] {
166 return true;
167 },
168 std::chrono::milliseconds{1000});
169}
170
172 for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
173 pwallet->Flush();
174 }
175}
176
178 for (const std::shared_ptr<CWallet> &pwallet : GetWallets()) {
179 pwallet->Close();
180 }
181}
182
184 auto wallets = GetWallets();
185 while (!wallets.empty()) {
186 auto wallet = wallets.back();
187 wallets.pop_back();
188 std::vector<bilingual_str> warnings;
189 RemoveWallet(wallet, std::nullopt, warnings);
190 UnloadWallet(std::move(wallet));
191 }
192}
ArgsManager gArgs
Definition args.cpp:38
void ForceSetArg(const std::string &strArg, const std::string &strValue)
Definition args.cpp:597
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition args.cpp:381
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition args.cpp:556
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition args.cpp:275
Simple class for background tasks that should be run periodically or once "after a while".
Definition scheduler.h:41
void scheduleEvery(Predicate p, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Repeat p until it return false.
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)
Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error.
Definition wallet.cpp:2707
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition chain.h:123
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 std::vector< util::SettingsValue > getSettingsList(const std::string &arg)=0
Get list of settings values.
virtual void initWarning(const bilingual_str &message)=0
Send init warning.
void StartWallets(CScheduler &scheduler, const ArgsManager &args)
Complete startup of wallets.
Definition load.cpp:149
bool VerifyWallets(interfaces::Chain &chain)
Responsible for reading and validating the -wallet arguments and verifying the wallet database.
Definition load.cpp:23
void UnloadWallets()
Close all wallets.
Definition load.cpp:183
void FlushWallets()
Flush all wallets in preparation for shutdown.
Definition load.cpp:171
void StopWallets()
Stop all wallets. Wallets will be flushed first.
Definition load.cpp:177
bool LoadWallets(interfaces::Chain &chain)
Load wallet databases.
Definition load.cpp:107
bool error(const char *fmt, const Args &...args)
Definition logging.h:226
#define LogPrintf(...)
Definition logging.h:207
static bool exists(const path &p)
Definition fs.h:102
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition fs.h:142
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition fs.h:165
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
Definition fs.cpp:39
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * name
Definition rest.cpp:47
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:63
bool verify
Definition db.h:226
uint64_t create_flags
Definition db.h:224
bool require_existing
Definition db.h:222
Bilingual messages:
Definition translation.h:17
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
bilingual_str _(const char *psz)
Translation function.
Definition translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition translation.h:36
DatabaseStatus
Definition db.h:229
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet, std::optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
Definition wallet.cpp:121
std::unique_ptr< WalletDatabase > MakeWalletDatabase(const std::string &name, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error_string)
Definition wallet.cpp:2675
void MaybeResendWalletTxs()
Called periodically by the schedule thread.
Definition wallet.cpp:1991
void UnloadWallet(std::shared_ptr< CWallet > &&wallet)
Explicitly unload and delete the wallet.
Definition wallet.cpp:202
std::vector< std::shared_ptr< CWallet > > GetWallets()
Definition wallet.cpp:151
bool AddWallet(const std::shared_ptr< CWallet > &wallet)
Definition wallet.cpp:107
void MaybeCompactWalletDB()
Compacts BDB state so that wallet.dat is self-contained (if there are changes)
static const bool DEFAULT_FLUSHWALLET
Overview of wallet database classes:
Definition walletdb.h:33
fs::path GetWalletDir()
Get the path of the wallet directory.