| 194 | } |
| 195 | |
| 196 | util::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 | |
| 228 | void DumpAnchors(const fs::path& anchors_db_path, const std::vector<CAddress>& anchors) |
| 229 | { |
no test coverage detected