From fa5aeec80c6cdca9ca027d80dff3b397911ff2c2 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Sat, 21 Aug 2021 13:49:16 +0200 Subject: [PATCH] Move LoadAddrman from init to addrdb Init should only concern itself with the initialization order, not the detailed initialization logic of every module. Also, inlining logic into a method that is ~800 lines of code, makes it impossible to unit test on its own. --- src/addrdb.cpp | 18 ++++++++++++++++++ src/addrdb.h | 5 +++++ src/init.cpp | 14 ++------------ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 856f3189619..fd85e717e32 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace { template @@ -187,6 +188,23 @@ bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers) return DeserializeDB(ssPeers, addr, false); } +std::optional LoadAddrman(const std::vector& asmap, const ArgsManager& args, std::unique_ptr& addrman) +{ + auto check_addrman = std::clamp(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000); + addrman = std::make_unique(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman); + + int64_t nStart = GetTimeMillis(); + if (ReadPeerAddresses(args, *addrman)) { + LogPrintf("Loaded %i addresses from peers.dat %dms\n", addrman->size(), GetTimeMillis() - nStart); + } else { + // Addrman can be in an inconsistent state after failure, reset it + addrman = std::make_unique(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman); + LogPrintf("Recreating peers.dat\n"); + DumpPeerAddresses(args, *addrman); + } + return std::nullopt; +} + void DumpAnchors(const fs::path& anchors_db_path, const std::vector& anchors) { LOG_TIME_SECONDS(strprintf("Flush %d outbound block-relay-only peer addresses to anchors.dat", anchors.size())); diff --git a/src/addrdb.h b/src/addrdb.h index c31c126ee31..99d8a975f4d 100644 --- a/src/addrdb.h +++ b/src/addrdb.h @@ -10,12 +10,14 @@ #include // For banmap_t #include +#include #include class ArgsManager; class CAddrMan; class CAddress; class CDataStream; +struct bilingual_str; bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr); bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr); @@ -46,6 +48,9 @@ public: bool Read(banmap_t& banSet); }; +/** Returns an error string on failure */ +std::optional LoadAddrman(const std::vector& asmap, const ArgsManager& args, std::unique_ptr& addrman); + /** * Dump the anchor IP address database (anchors.dat) * diff --git a/src/init.cpp b/src/init.cpp index 2869cf4daf7..ba1c88ad919 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1200,19 +1200,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) LogPrintf("Using /16 prefix for IP bucketing\n"); } - auto check_addrman = std::clamp(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000); - node.addrman = std::make_unique(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman); - - // Load addresses from peers.dat uiInterface.InitMessage(_("Loading P2P addresses…").translated); - int64_t nStart = GetTimeMillis(); - if (ReadPeerAddresses(args, *node.addrman)) { - LogPrintf("Loaded %i addresses from peers.dat %dms\n", node.addrman->size(), GetTimeMillis() - nStart); - } else { - // Addrman can be in an inconsistent state after failure, reset it - node.addrman = std::make_unique(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman); - LogPrintf("Recreating peers.dat\n"); - DumpPeerAddresses(args, *node.addrman); + if (const auto error{LoadAddrman(asmap, args, node.addrman)}) { + return InitError(*error); } }