Skip to content

Commit 9eaeb7f

Browse files
committed
[build] Move wallet load functions to wallet/load unit
Moves the following wallet load functions to a new wallet/load unit in the libbitcoin_wallet library. All other functions in wallet/init remain in libbitcoin_server: - `VerifyWallets` - `LoadWallets` - `StartWallets` - `FlushWallets` - `StopWallets` - `UnloadWallets`
1 parent 91a25d1 commit 9eaeb7f

File tree

7 files changed

+159
-122
lines changed

7 files changed

+159
-122
lines changed

build_msvc/libbitcoin_server/libbitcoin_server.vcxproj.in

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
</ItemDefinitionGroup>
152152
<ItemGroup>
153153
@SOURCE_FILES@
154+
<ClCompile Include="..\..\src\wallet\init.cpp">
155+
<ObjectFileName>$(IntDir)wallet_init.obj</ObjectFileName>
156+
</ClCompile>
154157
</ItemGroup>
155158
<ItemGroup>
156159
<None Include="packages.config" />

src/Makefile.am

+5-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ BITCOIN_CORE_H = \
222222
wallet/db.h \
223223
wallet/feebumper.h \
224224
wallet/fees.h \
225+
wallet/load.h \
225226
wallet/psbtwallet.h \
226227
wallet/rpcwallet.h \
227228
wallet/wallet.h \
@@ -293,6 +294,9 @@ libbitcoin_server_a_SOURCES = \
293294
versionbits.cpp \
294295
$(BITCOIN_CORE_H)
295296

297+
if ENABLE_WALLET
298+
libbitcoin_server_a_SOURCES += wallet/init.cpp
299+
endif
296300
if !ENABLE_WALLET
297301
libbitcoin_server_a_SOURCES += dummywallet.cpp
298302
endif
@@ -319,7 +323,7 @@ libbitcoin_wallet_a_SOURCES = \
319323
wallet/db.cpp \
320324
wallet/feebumper.cpp \
321325
wallet/fees.cpp \
322-
wallet/init.cpp \
326+
wallet/load.cpp \
323327
wallet/psbtwallet.cpp \
324328
wallet/rpcdump.cpp \
325329
wallet/rpcwallet.cpp \

src/interfaces/wallet.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <wallet/feebumper.h>
3030
#include <wallet/fees.h>
3131
#include <wallet/rpcwallet.h>
32+
#include <wallet/load.h>
3233
#include <wallet/wallet.h>
3334
#include <wallet/walletutil.h>
3435

src/wallet/init.cpp

-101
Original file line numberDiff line numberDiff line change
@@ -131,58 +131,6 @@ bool WalletInit::ParameterInteraction() const
131131
return true;
132132
}
133133

134-
bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
135-
{
136-
if (gArgs.IsArgSet("-walletdir")) {
137-
fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
138-
boost::system::error_code error;
139-
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
140-
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
141-
if (error || !fs::exists(wallet_dir)) {
142-
chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string()));
143-
return false;
144-
} else if (!fs::is_directory(wallet_dir)) {
145-
chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string()));
146-
return false;
147-
// The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
148-
} else if (!wallet_dir.is_absolute()) {
149-
chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string()));
150-
return false;
151-
}
152-
gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
153-
}
154-
155-
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
156-
157-
chain.initMessage(_("Verifying wallet(s)..."));
158-
159-
// Parameter interaction code should have thrown an error if -salvagewallet
160-
// was enabled with more than wallet file, so the wallet_files size check
161-
// here should have no effect.
162-
bool salvage_wallet = gArgs.GetBoolArg("-salvagewallet", false) && wallet_files.size() <= 1;
163-
164-
// Keep track of each wallet absolute path to detect duplicates.
165-
std::set<fs::path> wallet_paths;
166-
167-
for (const auto& wallet_file : wallet_files) {
168-
WalletLocation location(wallet_file);
169-
170-
if (!wallet_paths.insert(location.GetPath()).second) {
171-
chain.initError(strprintf(_("Error loading wallet %s. Duplicate -wallet filename specified."), wallet_file));
172-
return false;
173-
}
174-
175-
std::string error_string;
176-
std::string warning_string;
177-
bool verify_success = CWallet::Verify(chain, location, salvage_wallet, error_string, warning_string);
178-
if (!error_string.empty()) chain.initError(error_string);
179-
if (!warning_string.empty()) chain.initWarning(warning_string);
180-
if (!verify_success) return false;
181-
}
182-
183-
return true;
184-
}
185-
186134
void WalletInit::Construct(InitInterfaces& interfaces) const
187135
{
188136
if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
@@ -192,52 +140,3 @@ void WalletInit::Construct(InitInterfaces& interfaces) const
192140
gArgs.SoftSetArg("-wallet", "");
193141
interfaces.chain_clients.emplace_back(interfaces::MakeWalletClient(*interfaces.chain, gArgs.GetArgs("-wallet")));
194142
}
195-
196-
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
197-
{
198-
for (const std::string& walletFile : wallet_files) {
199-
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile));
200-
if (!pwallet) {
201-
return false;
202-
}
203-
AddWallet(pwallet);
204-
}
205-
206-
return true;
207-
}
208-
209-
void StartWallets(CScheduler& scheduler)
210-
{
211-
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
212-
pwallet->postInitProcess();
213-
}
214-
215-
// Schedule periodic wallet flushes and tx rebroadcasts
216-
scheduler.scheduleEvery(MaybeCompactWalletDB, 500);
217-
scheduler.scheduleEvery(MaybeResendWalletTxs, 1000);
218-
}
219-
220-
void FlushWallets()
221-
{
222-
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
223-
pwallet->Flush(false);
224-
}
225-
}
226-
227-
void StopWallets()
228-
{
229-
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
230-
pwallet->Flush(true);
231-
}
232-
}
233-
234-
void UnloadWallets()
235-
{
236-
auto wallets = GetWallets();
237-
while (!wallets.empty()) {
238-
auto wallet = wallets.back();
239-
wallets.pop_back();
240-
RemoveWallet(wallet);
241-
UnloadWallet(std::move(wallet));
242-
}
243-
}

src/wallet/load.cpp

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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/system.h>
11+
#include <wallet/wallet.h>
12+
13+
bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
14+
{
15+
if (gArgs.IsArgSet("-walletdir")) {
16+
fs::path wallet_dir = gArgs.GetArg("-walletdir", "");
17+
boost::system::error_code error;
18+
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
19+
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
20+
if (error || !fs::exists(wallet_dir)) {
21+
chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string()));
22+
return false;
23+
} else if (!fs::is_directory(wallet_dir)) {
24+
chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string()));
25+
return false;
26+
// The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
27+
} else if (!wallet_dir.is_absolute()) {
28+
chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string()));
29+
return false;
30+
}
31+
gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string());
32+
}
33+
34+
LogPrintf("Using wallet directory %s\n", GetWalletDir().string());
35+
36+
chain.initMessage(_("Verifying wallet(s)..."));
37+
38+
// Parameter interaction code should have thrown an error if -salvagewallet
39+
// was enabled with more than wallet file, so the wallet_files size check
40+
// here should have no effect.
41+
bool salvage_wallet = gArgs.GetBoolArg("-salvagewallet", false) && wallet_files.size() <= 1;
42+
43+
// Keep track of each wallet absolute path to detect duplicates.
44+
std::set<fs::path> wallet_paths;
45+
46+
for (const auto& wallet_file : wallet_files) {
47+
WalletLocation location(wallet_file);
48+
49+
if (!wallet_paths.insert(location.GetPath()).second) {
50+
chain.initError(strprintf(_("Error loading wallet %s. Duplicate -wallet filename specified."), wallet_file));
51+
return false;
52+
}
53+
54+
std::string error_string;
55+
std::string warning_string;
56+
bool verify_success = CWallet::Verify(chain, location, salvage_wallet, error_string, warning_string);
57+
if (!error_string.empty()) chain.initError(error_string);
58+
if (!warning_string.empty()) chain.initWarning(warning_string);
59+
if (!verify_success) return false;
60+
}
61+
62+
return true;
63+
}
64+
65+
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
66+
{
67+
for (const std::string& walletFile : wallet_files) {
68+
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, WalletLocation(walletFile));
69+
if (!pwallet) {
70+
return false;
71+
}
72+
AddWallet(pwallet);
73+
}
74+
75+
return true;
76+
}
77+
78+
void StartWallets(CScheduler& scheduler)
79+
{
80+
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
81+
pwallet->postInitProcess();
82+
}
83+
84+
// Schedule periodic wallet flushes and tx rebroadcasts
85+
scheduler.scheduleEvery(MaybeCompactWalletDB, 500);
86+
scheduler.scheduleEvery(MaybeResendWalletTxs, 1000);
87+
}
88+
89+
void FlushWallets()
90+
{
91+
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
92+
pwallet->Flush(false);
93+
}
94+
}
95+
96+
void StopWallets()
97+
{
98+
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
99+
pwallet->Flush(true);
100+
}
101+
}
102+
103+
void UnloadWallets()
104+
{
105+
auto wallets = GetWallets();
106+
while (!wallets.empty()) {
107+
auto wallet = wallets.back();
108+
wallets.pop_back();
109+
RemoveWallet(wallet);
110+
UnloadWallet(std::move(wallet));
111+
}
112+
}

src/wallet/load.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
#ifndef BITCOIN_WALLET_LOAD_H
7+
#define BITCOIN_WALLET_LOAD_H
8+
9+
#include <string>
10+
#include <vector>
11+
12+
class CScheduler;
13+
14+
namespace interfaces {
15+
class Chain;
16+
} // namespace interfaces
17+
18+
//! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
19+
//! This function will perform salvage on the wallet if requested, as long as only one wallet is
20+
//! being loaded (WalletParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
21+
bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files);
22+
23+
//! Load wallet databases.
24+
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files);
25+
26+
//! Complete startup of wallets.
27+
void StartWallets(CScheduler& scheduler);
28+
29+
//! Flush all wallets in preparation for shutdown.
30+
void FlushWallets();
31+
32+
//! Stop all wallets. Wallets will be flushed first.
33+
void StopWallets();
34+
35+
//! Close all wallets.
36+
void UnloadWallets();
37+
38+
#endif // BITCOIN_WALLET_LOAD_H

src/wallet/wallet.h

-20
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,6 @@
3535
#include <utility>
3636
#include <vector>
3737

38-
//! Responsible for reading and validating the -wallet arguments and verifying the wallet database.
39-
//! This function will perform salvage on the wallet if requested, as long as only one wallet is
40-
//! being loaded (WalletParameterInteraction forbids -salvagewallet, -zapwallettxes or -upgradewallet with multiwallet).
41-
bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files);
42-
43-
//! Load wallet databases.
44-
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files);
45-
46-
//! Complete startup of wallets.
47-
void StartWallets(CScheduler& scheduler);
48-
49-
//! Flush all wallets in preparation for shutdown.
50-
void FlushWallets();
51-
52-
//! Stop all wallets. Wallets will be flushed first.
53-
void StopWallets();
54-
55-
//! Close all wallets.
56-
void UnloadWallets();
57-
5838
//! Explicitly unload and delete the wallet.
5939
//! Blocks the current thread after signaling the unload intent so that all
6040
//! wallet clients release the wallet.

0 commit comments

Comments
 (0)