Skip to content

Commit b93a54b

Browse files
fanquakevijaydasmp
authored andcommitted
Merge bitcoin#25977: refactor: Replace std::optional<bilingual_str> with util::Result
8aa8f73 refactor: Replace std::optional<bilingual_str> with util::Result (Ryan Ofsky) 5f49cb1 util: Add void support to util::Result (MarcoFalke) Pull request description: Replace uses of `std::optional<bilingual_str>` with `util::Result` as suggested bitcoin#25648 (comment), bitcoin#27632 (comment), bitcoin#27632 (comment), bitcoin#24313 (comment) ACKs for top commit: MarcoFalke: very nice ACK 8aa8f73 🏏 TheCharlatan: ACK 8aa8f73 hebasto: ACK 8aa8f73, I have reviewed the code and it looks OK. Tree-SHA512: 6c2f218bc445184ce93ec2b907e61666a05f26f2c9447f69fcb504aa8291b0c693d913f659dfd19813a9e24615546c72cbe2ca419218fd867ff0694c4a1b6a30 Redundant std::move
1 parent 46443b2 commit b93a54b

5 files changed

Lines changed: 17 additions & 15 deletions

File tree

src/addrdb.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ void ReadFromStream(AddrMan& addr, CDataStream& ssPeers)
182182
DeserializeDB(ssPeers, addr, false);
183183
}
184184

185-
std::optional<bilingual_str> LoadAddrman(const NetGroupManager& netgroupman, const ArgsManager& args, std::unique_ptr<AddrMan>& addrman)
185+
util::Result<std::unique_ptr<AddrMan>> LoadAddrman(const NetGroupManager& netgroupman, const ArgsManager& args)
186186
{
187187
auto check_addrman = std::clamp<int32_t>(args.GetIntArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
188-
addrman = std::make_unique<AddrMan>(netgroupman, /*deterministic=*/false, /*consistency_check_ratio=*/check_addrman);
188+
auto addrman{std::make_unique<AddrMan>(netgroupman, /*deterministic=*/false, /*consistency_check_ratio=*/check_addrman)};
189189

190190
const auto start{SteadyClock::now()};
191191
const auto path_addr{gArgs.GetDataDirNet() / "peers.dat"};
@@ -208,19 +208,17 @@ std::optional<bilingual_str> LoadAddrman(const NetGroupManager& netgroupman, con
208208
DumpPeerAddresses(args, *addrman);
209209
} catch (const InvalidAddrManVersionError&) {
210210
if (!RenameOver(path_addr, (fs::path)path_addr + ".bak")) {
211-
addrman = nullptr;
212-
return strprintf(_("Failed to rename invalid peers.dat file. Please move or delete it and try again."));
211+
return util::Error{strprintf(_("Failed to rename invalid peers.dat file. Please move or delete it and try again."))};
213212
}
214213
// Addrman can be in an inconsistent state after failure, reset it
215214
addrman = std::make_unique<AddrMan>(netgroupman, /*deterministic=*/false, /*consistency_check_ratio=*/check_addrman);
216215
LogPrintf("Creating new peers.dat because the file version was not compatible (%s). Original backed up to peers.dat.bak\n", fs::quoted(fs::PathToString(path_addr)));
217216
DumpPeerAddresses(args, *addrman);
218217
} catch (const std::exception& e) {
219-
addrman = nullptr;
220-
return 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."),
221-
e.what(), PACKAGE_BUGREPORT, fs::quoted(fs::PathToString(path_addr)));
218+
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."),
219+
e.what(), PACKAGE_BUGREPORT, fs::quoted(fs::PathToString(path_addr)))};
222220
}
223-
return std::nullopt;
221+
return addrman; // std::move should be unneccessary but is temporarily needed to work around clang bug (https://github.com/bitcoin/bitcoin/pull/25977#issuecomment-1561270092)
224222
}
225223

226224
void DumpAnchors(const fs::path& anchors_db_path, const std::vector<CAddress>& anchors)

src/addrdb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <fs.h>
1010
#include <net_types.h> // For banmap_t
1111
#include <univalue.h>
12+
#include <util/result.h>
1213

1314
#include <memory>
1415
#include <optional>
@@ -50,7 +51,7 @@ class CBanDB
5051
};
5152

5253
/** Returns an error string on failure */
53-
std::optional<bilingual_str> LoadAddrman(const NetGroupManager& netgroupman, const ArgsManager& args, std::unique_ptr<AddrMan>& addrman);
54+
util::Result<std::unique_ptr<AddrMan>> LoadAddrman(const NetGroupManager& netgroupman, const ArgsManager& args);
5455

5556
/**
5657
* Dump the anchor IP address database (anchors.dat)

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
16471647
// Initialize addrman
16481648
assert(!node.addrman);
16491649
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
1650-
if (const auto error{LoadAddrman(*node.netgroupman, args, node.addrman)}) {
1651-
return InitError(*error);
1652-
}
1650+
auto addrman{LoadAddrman(*node.netgroupman, args)};
1651+
if (!addrman) return InitError(util::ErrorString(addrman));
1652+
node.addrman = std::move(*addrman);
16531653
}
16541654

16551655
std::string sem_str = args.GetArg("-socketevents", DEFAULT_SOCKETEVENTS);

src/test/util/txmempool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ CTxMemPool::Options MemPoolOptionsForTest(const NodeContext& node)
2020
// chainparams.DefaultConsistencyChecks for tests
2121
.check_ratio = 1,
2222
};
23-
const auto err{ApplyArgsManOptions(*node.args, ::Params(), mempool_opts)};
24-
Assert(!err);
23+
const auto result{ApplyArgsManOptions(*node.args, ::Params(), mempool_opts)};
24+
Assert(result);
2525
return mempool_opts;
2626
}
2727
*/

src/util/result.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ struct Error {
3131
//! `std::optional<T>` can be updated to return `util::Result<T>` and return
3232
//! error strings usually just replacing `return std::nullopt;` with `return
3333
//! util::Error{error_string};`.
34-
template <class T>
34+
template <class M>
3535
class Result
3636
{
3737
private:
38+
using T = std::conditional_t<std::is_same_v<M, void>, std::monostate, M>;
39+
3840
std::variant<bilingual_str, T> m_variant;
3941

4042
template <typename FT>
4143
friend bilingual_str ErrorString(const Result<FT>& result);
4244

4345
public:
46+
Result() : m_variant{std::in_place_index_t<1>{}, std::monostate{}} {} // constructor for void
4447
Result(T obj) : m_variant{std::in_place_index_t<1>{}, std::move(obj)} {}
4548
Result(Error error) : m_variant{std::in_place_index_t<0>{}, std::move(error.message)} {}
4649

0 commit comments

Comments
 (0)