Skip to content

Commit

Permalink
Fix wallet directory initialization
Browse files Browse the repository at this point in the history
util/system datadir code should not care about wallets or create any wallet paths
If -walletdir is specified, wallet init code should use that directory
If -walletdir is specified and path is not a directory, return a fatal error
If -walletdir is not specified, use <datadir>/wallets if it is directory.
If -walletdir is not specified and <datadir>/wallets does not exist:
<datadir>/wallets wallet should call ListWalletDir ListDatabases on <datadir>
If list is empty wallet should create <datadir>/wallets as a directory,
otherwise it should create it as a symlink pointing to .
discussion at bitcoin#16220
  • Loading branch information
BrandonOdiwuor committed Sep 21, 2023
1 parent 3966b0a commit 8963ac1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
14 changes: 2 additions & 12 deletions src/common/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,11 @@ std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn setting
// Create datadir if it does not exist.
const auto base_path{args.GetDataDirBase()};
if (!fs::exists(base_path)) {
// When creating a *new* datadir, also create a "wallets" subdirectory,
// whether or not the wallet is enabled now, so if the wallet is enabled
// in the future, it will use the "wallets" subdirectory for creating
// and listing wallets, rather than the top-level directory where
// wallets could be mixed up with other files. For backwards
// compatibility, wallet code will use the "wallets" subdirectory only
// if it already exists, but never create it itself. There is discussion
// in https://github.com/bitcoin/bitcoin/issues/16220 about ways to
// change wallet code so it would no longer be necessary to create
// "wallets" subdirectories here.
fs::create_directories(base_path / "wallets");
fs::create_directories(base_path);
}
const auto net_path{args.GetDataDirNet()};
if (!fs::exists(net_path)) {
fs::create_directories(net_path / "wallets");
fs::create_directories(net_path);
}

// Show an error or warning if there is a bitcoin.conf file in the
Expand Down
7 changes: 7 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
g_wallet_init_interface.Construct(node);
uiInterface.InitWallet();

// Initialize wallet directory
bilingual_str error;
g_wallet_init_interface.InitWalletDir(node, error);
if (!error.empty()) {
return InitError(error);
}

/* Register RPC commands regardless of -server setting so they will be
* available in the GUI RPC console even if external calls are disabled.
*/
Expand Down
27 changes: 27 additions & 0 deletions src/wallet/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class WalletInit : public WalletInitInterface

//! Add wallets that should be opened to list of chain clients.
void Construct(NodeContext& node) const override;

//! Initialize wallet directory
void InitWalletDir(NodeContext& node, bilingual_str& error) const override;
};

void WalletInit::AddWalletOptions(ArgsManager& argsman) const
Expand Down Expand Up @@ -136,6 +139,30 @@ void WalletInit::Construct(NodeContext& node) const
node.wallet_loader = wallet_loader.get();
node.chain_clients.emplace_back(std::move(wallet_loader));
}

void WalletInit::InitWalletDir(NodeContext& node, bilingual_str& error) const
{
ArgsManager& args = *Assert(node.args);

fs::path path;

if (args.IsArgSet("-walletdir")) {
path = args.GetPathArg("-walletdir");
if (!fs::is_directory(path)) {
error = strprintf(_("Could not find the -walletdir=%s provided"), fs::PathToString(path));
}
} else {
path = args.GetDataDirNet();
if (!fs::is_directory(path / "wallets")) {
std::vector<fs::path> db_paths = ListDatabases(path);
if (db_paths.empty()) {
fs::create_directories(path / "wallets");
} else {
fs::create_symlink(path, path / "wallets");
}
}
}
}
} // namespace wallet

const WalletInitInterface& g_wallet_init_interface = wallet::WalletInit();
2 changes: 2 additions & 0 deletions src/walletinitinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class WalletInitInterface {
virtual bool ParameterInteraction() const = 0;
/** Add wallets that should be opened to list of chain clients. */
virtual void Construct(node::NodeContext& node) const = 0;
/** Initialize wallet directory */
virtual void InitWalletDir(node::NodeContext& node, bilingual_str& error) const = 0;

virtual ~WalletInitInterface() {}
};
Expand Down

0 comments on commit 8963ac1

Please sign in to comment.