Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13711118
D14992.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
7 KB
Subscribers
None
D14992.diff
View Options
diff --git a/src/addrdb.h b/src/addrdb.h
--- a/src/addrdb.h
+++ b/src/addrdb.h
@@ -7,10 +7,11 @@
#define BITCOIN_ADDRDB_H
#include <fs.h>
-#include <net_types.h> // For banmap_t
+#include <net_types.h>
#include <serialize.h>
+#include <util/result.h>
-#include <optional>
+#include <memory>
#include <string>
#include <vector>
@@ -19,7 +20,6 @@
class CAddress;
class CDataStream;
class CChainParams;
-struct bilingual_str;
bool DumpPeerAddresses(const CChainParams &chainParams, const ArgsManager &args,
const AddrMan &addr);
@@ -67,10 +67,9 @@
};
/** Returns an error string on failure */
-std::optional<bilingual_str> LoadAddrman(const CChainParams &chainparams,
- const std::vector<bool> &asmap,
- const ArgsManager &args,
- std::unique_ptr<AddrMan> &addrman);
+util::Result<std::unique_ptr<AddrMan>>
+LoadAddrman(const CChainParams &chainparams, const std::vector<bool> &asmap,
+ const ArgsManager &args);
/**
* Dump the anchor IP address database (anchors.dat)
diff --git a/src/addrdb.cpp b/src/addrdb.cpp
--- a/src/addrdb.cpp
+++ b/src/addrdb.cpp
@@ -159,15 +159,14 @@
DeserializeDB(chainParams, ssPeers, addr, false);
}
-std::optional<bilingual_str> LoadAddrman(const CChainParams &chainparams,
- const std::vector<bool> &asmap,
- const ArgsManager &args,
- std::unique_ptr<AddrMan> &addrman) {
+util::Result<std::unique_ptr<AddrMan>>
+LoadAddrman(const CChainParams &chainparams, const std::vector<bool> &asmap,
+ const ArgsManager &args) {
auto check_addrman = std::clamp<int32_t>(
args.GetIntArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0,
1000000);
- addrman = std::make_unique<AddrMan>(
- asmap, /* consistency_check_ratio= */ check_addrman);
+ auto addrman{std::make_unique<AddrMan>(
+ asmap, /* consistency_check_ratio= */ check_addrman)};
int64_t nStart = GetTimeMillis();
const auto path_addr{args.GetDataDirNet() / "peers.dat"};
@@ -184,9 +183,9 @@
DumpPeerAddresses(chainparams, args, *addrman);
} catch (const InvalidAddrManVersionError &) {
if (!RenameOver(path_addr, fs::path(path_addr) + ".bak")) {
- addrman = nullptr;
- return strprintf(_("Failed to rename invalid peers.dat file. "
- "Please move or delete it and try again."));
+ return util::Error{
+ strprintf(_("Failed to rename invalid peers.dat file. "
+ "Please move or delete it and try again."))};
}
// Addrman can be in an inconsistent state after failure, reset it
addrman = std::make_unique<AddrMan>(
@@ -196,17 +195,19 @@
fs::quoted(fs::PathToString(path_addr)));
DumpPeerAddresses(chainparams, args, *addrman);
} catch (const std::exception &e) {
- addrman = nullptr;
- return strprintf(
+ return util::Error{strprintf(
_("Invalid or corrupt peers.dat (%s). If you believe this is a "
"bug, please report it to %s. As a workaround, you can move the "
"file (%s) out of the way (rename, move, or delete) to have a "
"new one created on the next start."),
e.what(), PACKAGE_BUGREPORT,
- fs::quoted(fs::PathToString(path_addr)));
+ fs::quoted(fs::PathToString(path_addr)))};
}
- return std::nullopt;
+ // std::move should be unneccessary but is temporarily needed to work
+ // around clang bug
+ // (https://github.com/bitcoin/bitcoin/pull/25977#issuecomment-1564350880)
+ return {std::move(addrman)};
}
void DumpAnchors(const CChainParams &chainParams,
diff --git a/src/init.cpp b/src/init.cpp
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2254,10 +2254,11 @@
}
uiInterface.InitMessage(_("Loading P2P addresses...").translated);
- if (const auto error{
- LoadAddrman(chainparams, asmap, args, node.addrman)}) {
- return InitError(*error);
+ auto addrman{LoadAddrman(chainparams, asmap, args)};
+ if (!addrman) {
+ return InitError(util::ErrorString(addrman));
}
+ node.addrman = std::move(*addrman);
}
assert(!node.banman);
@@ -2554,10 +2555,11 @@
// Step 8: load indexers
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
- if (const auto error{WITH_LOCK(
- cs_main, return CheckLegacyTxindex(
- *Assert(chainman.m_blockman.m_block_tree_db)))}) {
- return InitError(*error);
+ auto result{
+ WITH_LOCK(cs_main, return CheckLegacyTxindex(*Assert(
+ chainman.m_blockman.m_block_tree_db)))};
+ if (!result) {
+ return InitError(util::ErrorString(result));
}
g_txindex =
diff --git a/src/txdb.h b/src/txdb.h
--- a/src/txdb.h
+++ b/src/txdb.h
@@ -11,7 +11,11 @@
#include <dbwrapper.h>
#include <flatfile.h>
#include <fs.h>
+#include <util/result.h>
+#include <cstddef>
+#include <cstdint>
+#include <functional>
#include <memory>
#include <optional>
#include <string>
@@ -21,11 +25,11 @@
struct BlockHash;
class CBlockFileInfo;
class CBlockIndex;
+class COutPoint;
namespace Consensus {
struct Params;
};
-struct bilingual_str;
//! min. -dbcache (MiB)
static constexpr int64_t MIN_DB_CACHE_MB = 4;
@@ -133,6 +137,7 @@
bool Upgrade();
};
-std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
+[[nodiscard]] util::Result<void>
+CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
#endif // BITCOIN_TXDB_H
diff --git a/src/txdb.cpp b/src/txdb.cpp
--- a/src/txdb.cpp
+++ b/src/txdb.cpp
@@ -33,26 +33,28 @@
static constexpr uint8_t DB_TXINDEX_BLOCK{'T'};
// uint8_t DB_TXINDEX{'t'}
-std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB &block_tree_db) {
+util::Result<void> CheckLegacyTxindex(CBlockTreeDB &block_tree_db) {
CBlockLocator ignored{};
if (block_tree_db.Read(DB_TXINDEX_BLOCK, ignored)) {
- return _("The -txindex upgrade started by a previous version can not "
- "be completed. Restart with the previous version or run a "
- "full -reindex.");
+ return util::Error{
+ _("The -txindex upgrade started by a previous version can not "
+ "be completed. Restart with the previous version or run a "
+ "full -reindex.")};
}
bool txindex_legacy_flag{false};
block_tree_db.ReadFlag("txindex", txindex_legacy_flag);
if (txindex_legacy_flag) {
// Disable legacy txindex and warn once about occupied disk space
if (!block_tree_db.WriteFlag("txindex", false)) {
- return Untranslated(
- "Failed to write block index db flag 'txindex'='0'");
+ return util::Error{Untranslated(
+ "Failed to write block index db flag 'txindex'='0'")};
}
- return _("The block index db contains a legacy 'txindex'. To clear the "
- "occupied disk space, run a full -reindex, otherwise ignore "
- "this error. This error message will not be displayed again.");
+ return util::Error{
+ _("The block index db contains a legacy 'txindex'. To clear the "
+ "occupied disk space, run a full -reindex, otherwise ignore "
+ "this error. This error message will not be displayed again.")};
}
- return std::nullopt;
+ return {};
}
namespace {
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Apr 26, 10:34 (5 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5573275
Default Alt Text
D14992.diff (7 KB)
Attached To
D14992: refactor: Replace std::optional<bilingual_str> with util::Result
Event Timeline
Log In to Comment