MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / LoadAddrman

Function LoadAddrman

src/addrdb.cpp:196–226  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

194}
195
196util::Result<std::unique_ptr<AddrMan>> LoadAddrman(const NetGroupManager& netgroupman, const ArgsManager& args)
197{
198 auto check_addrman = std::clamp<int32_t>(args.GetIntArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
199 bool deterministic = HasTestOption(args, "addrman"); // use a deterministic addrman only for tests
200
201 auto addrman{std::make_unique<AddrMan>(netgroupman, deterministic, /*consistency_check_ratio=*/check_addrman)};
202
203 const auto start{SteadyClock::now()};
204 const auto path_addr{args.GetDataDirNet() / "peers.dat"};
205 try {
206 DeserializeFileDB(path_addr, *addrman);
207 LogInfo("Loaded %i addresses from peers.dat %dms", addrman->Size(), Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
208 } catch (const DbNotFoundError&) {
209 // Addrman can be in an inconsistent state after failure, reset it
210 addrman = std::make_unique<AddrMan>(netgroupman, deterministic, /*consistency_check_ratio=*/check_addrman);
211 LogInfo("Creating peers.dat because the file was not found (%s)", fs::quoted(fs::PathToString(path_addr)));
212 DumpPeerAddresses(args, *addrman);
213 } catch (const InvalidAddrManVersionError&) {
214 if (!RenameOver(path_addr, (fs::path)path_addr + ".bak")) {
215 return util::Error{strprintf(_("Failed to rename invalid peers.dat file. Please move or delete it and try again."))};
216 }
217 // Addrman can be in an inconsistent state after failure, reset it
218 addrman = std::make_unique<AddrMan>(netgroupman, deterministic, /*consistency_check_ratio=*/check_addrman);
219 LogWarning("Creating new peers.dat because the file version was not compatible (%s). Original backed up to peers.dat.bak", fs::quoted(fs::PathToString(path_addr)));
220 DumpPeerAddresses(args, *addrman);
221 } catch (const std::exception& e) {
222 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."),
223 e.what(), CLIENT_BUGREPORT, fs::quoted(fs::PathToString(path_addr)))};
224 }
225 return addrman;
226}
227
228void DumpAnchors(const fs::path& anchors_db_path, const std::vector<CAddress>& anchors)
229{

Callers 1

AppInitMainFunction · 0.85

Calls 10

HasTestOptionFunction · 0.85
DeserializeFileDBFunction · 0.85
quotedFunction · 0.85
PathToStringFunction · 0.85
DumpPeerAddressesFunction · 0.85
RenameOverFunction · 0.85
_Function · 0.85
GetDataDirNetMethod · 0.80
SizeMethod · 0.45
whatMethod · 0.45

Tested by

no test coverage detected