aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/bloom.cpp6
-rw-r--r--src/common/init.cpp74
-rw-r--r--src/common/init.h39
-rw-r--r--src/common/interfaces.cpp53
-rw-r--r--src/common/url.cpp2
-rw-r--r--src/common/url.h2
6 files changed, 171 insertions, 5 deletions
diff --git a/src/common/bloom.cpp b/src/common/bloom.cpp
index aa3fcf1ce2..fd3276b5a7 100644
--- a/src/common/bloom.cpp
+++ b/src/common/bloom.cpp
@@ -1,4 +1,4 @@
-// Copyright (c) 2012-2021 The Bitcoin Core developers
+// Copyright (c) 2012-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -60,7 +60,7 @@ void CBloomFilter::insert(Span<const unsigned char> vKey)
void CBloomFilter::insert(const COutPoint& outpoint)
{
- CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
+ DataStream stream{};
stream << outpoint;
insert(MakeUCharSpan(stream));
}
@@ -81,7 +81,7 @@ bool CBloomFilter::contains(Span<const unsigned char> vKey) const
bool CBloomFilter::contains(const COutPoint& outpoint) const
{
- CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
+ DataStream stream{};
stream << outpoint;
return contains(MakeUCharSpan(stream));
}
diff --git a/src/common/init.cpp b/src/common/init.cpp
new file mode 100644
index 0000000000..e8fa7a14fd
--- /dev/null
+++ b/src/common/init.cpp
@@ -0,0 +1,74 @@
+// Copyright (c) 2023 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <chainparams.h>
+#include <common/init.h>
+#include <tinyformat.h>
+#include <util/fs.h>
+#include <util/system.h>
+#include <util/translation.h>
+
+#include <algorithm>
+#include <exception>
+#include <optional>
+
+namespace common {
+std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn settings_abort_fn)
+{
+ try {
+ if (!CheckDataDirOption(args)) {
+ return ConfigError{ConfigStatus::FAILED, strprintf(_("Specified data directory \"%s\" does not exist."), args.GetArg("-datadir", ""))};
+ }
+ std::string error;
+ if (!args.ReadConfigFiles(error, true)) {
+ return ConfigError{ConfigStatus::FAILED, strprintf(_("Error reading configuration file: %s"), error)};
+ }
+
+ // Check for chain settings (Params() calls are only valid after this clause)
+ SelectParams(args.GetChainName());
+
+ // 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");
+ }
+ const auto net_path{args.GetDataDirNet()};
+ if (!fs::exists(net_path)) {
+ fs::create_directories(net_path / "wallets");
+ }
+
+ // Create settings.json if -nosettings was not specified.
+ if (args.GetSettingsPath()) {
+ std::vector<std::string> details;
+ if (!args.ReadSettingsFile(&details)) {
+ const bilingual_str& message = _("Settings file could not be read");
+ if (!settings_abort_fn) {
+ return ConfigError{ConfigStatus::FAILED, message, details};
+ } else if (settings_abort_fn(message, details)) {
+ return ConfigError{ConfigStatus::ABORTED, message, details};
+ } else {
+ details.clear(); // User chose to ignore the error and proceed.
+ }
+ }
+ if (!args.WriteSettingsFile(&details)) {
+ const bilingual_str& message = _("Settings file could not be written");
+ return ConfigError{ConfigStatus::FAILED_WRITE, message, details};
+ }
+ }
+ } catch (const std::exception& e) {
+ return ConfigError{ConfigStatus::FAILED, Untranslated(e.what())};
+ }
+ return {};
+}
+} // namespace common
diff --git a/src/common/init.h b/src/common/init.h
new file mode 100644
index 0000000000..380ac3ac7e
--- /dev/null
+++ b/src/common/init.h
@@ -0,0 +1,39 @@
+// Copyright (c) 2023 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#ifndef BITCOIN_COMMON_INIT_H
+#define BITCOIN_COMMON_INIT_H
+
+#include <util/translation.h>
+
+#include <functional>
+#include <optional>
+#include <string>
+#include <vector>
+
+class ArgsManager;
+
+namespace common {
+enum class ConfigStatus {
+ FAILED, //!< Failed generically.
+ FAILED_WRITE, //!< Failed to write settings.json
+ ABORTED, //!< Aborted by user
+};
+
+struct ConfigError {
+ ConfigStatus status;
+ bilingual_str message{};
+ std::vector<std::string> details{};
+};
+
+//! Callback function to let the user decide whether to abort loading if
+//! settings.json file exists and can't be parsed, or to ignore the error and
+//! overwrite the file.
+using SettingsAbortFn = std::function<bool(const bilingual_str& message, const std::vector<std::string>& details)>;
+
+/* Read config files, and create datadir and settings.json if they don't exist. */
+std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn settings_abort_fn = nullptr);
+} // namespace common
+
+#endif // BITCOIN_COMMON_INIT_H
diff --git a/src/common/interfaces.cpp b/src/common/interfaces.cpp
new file mode 100644
index 0000000000..c8bbe2b3c0
--- /dev/null
+++ b/src/common/interfaces.cpp
@@ -0,0 +1,53 @@
+// Copyright (c) 2021-2022 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <interfaces/echo.h>
+#include <interfaces/handler.h>
+
+#include <boost/signals2/connection.hpp>
+#include <memory>
+#include <utility>
+
+namespace common {
+namespace {
+class CleanupHandler : public interfaces::Handler
+{
+public:
+ explicit CleanupHandler(std::function<void()> cleanup) : m_cleanup(std::move(cleanup)) {}
+ ~CleanupHandler() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
+ void disconnect() override { if (!m_cleanup) return; m_cleanup(); m_cleanup = nullptr; }
+ std::function<void()> m_cleanup;
+};
+
+class SignalHandler : public interfaces::Handler
+{
+public:
+ explicit SignalHandler(boost::signals2::connection connection) : m_connection(std::move(connection)) {}
+
+ void disconnect() override { m_connection.disconnect(); }
+
+ boost::signals2::scoped_connection m_connection;
+};
+
+class EchoImpl : public interfaces::Echo
+{
+public:
+ std::string echo(const std::string& echo) override { return echo; }
+};
+} // namespace
+} // namespace common
+
+namespace interfaces {
+std::unique_ptr<Handler> MakeCleanupHandler(std::function<void()> cleanup)
+{
+ return std::make_unique<common::CleanupHandler>(std::move(cleanup));
+}
+
+std::unique_ptr<Handler> MakeSignalHandler(boost::signals2::connection connection)
+{
+ return std::make_unique<common::SignalHandler>(std::move(connection));
+}
+
+std::unique_ptr<Echo> MakeEcho() { return std::make_unique<common::EchoImpl>(); }
+} // namespace interfaces
diff --git a/src/common/url.cpp b/src/common/url.cpp
index 5200d55096..053e1a825c 100644
--- a/src/common/url.cpp
+++ b/src/common/url.cpp
@@ -1,4 +1,4 @@
-// Copyright (c) 2015-2019 The Bitcoin Core developers
+// Copyright (c) 2015-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
diff --git a/src/common/url.h b/src/common/url.h
index 7bbd8b60de..b16b8241af 100644
--- a/src/common/url.h
+++ b/src/common/url.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2015-2020 The Bitcoin Core developers
+// Copyright (c) 2015-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.