diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-08-21 11:22:21 +0200 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-09-07 11:05:16 +0200 |
commit | fade9a1a4db71241ccad03fdacfb626453952963 (patch) | |
tree | 08c9e5b360cd324eac820a7600cfc39707be9ec2 | |
parent | fa7f77b7d1709bf35808fced0d67b6e97b784d63 (diff) | |
download | bitcoin-fade9a1a4db71241ccad03fdacfb626453952963.tar.xz |
Remove confusing CAddrDB
The class only stores the file path, reading it from a global. Globals
are confusing and make testing harder.
The method reading from a stream does not even use any class members, so
putting it in a class is also confusing.
-rw-r--r-- | src/addrdb.cpp | 13 | ||||
-rw-r--r-- | src/addrdb.h | 18 | ||||
-rw-r--r-- | src/init.cpp | 5 | ||||
-rw-r--r-- | src/net.cpp | 3 | ||||
-rw-r--r-- | src/test/addrman_tests.cpp | 14 | ||||
-rw-r--r-- | src/test/fuzz/data_stream.cpp | 2 |
6 files changed, 22 insertions, 33 deletions
diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 345dbdfb16..856f318961 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -170,22 +170,19 @@ bool CBanDB::Read(banmap_t& banSet) return true; } -CAddrDB::CAddrDB() -{ - pathAddr = gArgs.GetDataDirNet() / "peers.dat"; -} - -bool CAddrDB::Write(const CAddrMan& addr) +bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr) { + const auto pathAddr = args.GetDataDirNet() / "peers.dat"; return SerializeFileDB("peers", pathAddr, addr, CLIENT_VERSION); } -bool CAddrDB::Read(CAddrMan& addr) +bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr) { + const auto pathAddr = args.GetDataDirNet() / "peers.dat"; return DeserializeFileDB(pathAddr, addr, CLIENT_VERSION); } -bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers) +bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers) { return DeserializeDB(ssPeers, addr, false); } diff --git a/src/addrdb.h b/src/addrdb.h index 26b1c5880f..c31c126ee3 100644 --- a/src/addrdb.h +++ b/src/addrdb.h @@ -12,21 +12,15 @@ #include <vector> -class CAddress; +class ArgsManager; class CAddrMan; +class CAddress; class CDataStream; -/** Access to the (IP) address database (peers.dat) */ -class CAddrDB -{ -private: - fs::path pathAddr; -public: - CAddrDB(); - bool Write(const CAddrMan& addr); - bool Read(CAddrMan& addr); - static bool Read(CAddrMan& addr, CDataStream& ssPeers); -}; +bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr); +bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr); +/** Only used by tests. */ +bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers); /** Access to the banlist database (banlist.json) */ class CBanDB diff --git a/src/init.cpp b/src/init.cpp index b744298667..2869cf4daf 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1206,14 +1206,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) // Load addresses from peers.dat uiInterface.InitMessage(_("Loading P2P addresses…").translated); int64_t nStart = GetTimeMillis(); - CAddrDB adb; - if (adb.Read(*node.addrman)) { + 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<CAddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman); LogPrintf("Recreating peers.dat\n"); - adb.Write(*node.addrman); + DumpPeerAddresses(args, *node.addrman); } } diff --git a/src/net.cpp b/src/net.cpp index 48947ee967..c31b2d899e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1749,8 +1749,7 @@ void CConnman::DumpAddresses() { int64_t nStart = GetTimeMillis(); - CAddrDB adb; - adb.Write(addrman); + DumpPeerAddresses(::gArgs, addrman); LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n", addrman.size(), GetTimeMillis() - nStart); diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index 4ed06d588b..835b18d42e 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -1003,7 +1003,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks) BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0"); } -BOOST_AUTO_TEST_CASE(caddrdb_read) +BOOST_AUTO_TEST_CASE(load_addrman) { CAddrManUncorrupted addrmanUncorrupted; @@ -1038,17 +1038,17 @@ BOOST_AUTO_TEST_CASE(caddrdb_read) BOOST_CHECK(addrman1.size() == 3); BOOST_CHECK(exceptionThrown == false); - // Test that CAddrDB::Read creates an addrman with the correct number of addrs. + // Test that ReadFromStream creates an addrman with the correct number of addrs. CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted); CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100); BOOST_CHECK(addrman2.size() == 0); - BOOST_CHECK(CAddrDB::Read(addrman2, ssPeers2)); + BOOST_CHECK(ReadFromStream(addrman2, ssPeers2)); BOOST_CHECK(addrman2.size() == 3); } -BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted) +BOOST_AUTO_TEST_CASE(load_addrman_corrupted) { CAddrManCorrupted addrmanCorrupted; @@ -1064,16 +1064,16 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted) } catch (const std::exception&) { exceptionThrown = true; } - // Even through de-serialization failed addrman is not left in a clean state. + // Even though de-serialization failed addrman is not left in a clean state. BOOST_CHECK(addrman1.size() == 1); BOOST_CHECK(exceptionThrown); - // Test that CAddrDB::Read fails if peers.dat is corrupt + // Test that ReadFromStream fails if peers.dat is corrupt CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted); CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100); BOOST_CHECK(addrman2.size() == 0); - BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2)); + BOOST_CHECK(!ReadFromStream(addrman2, ssPeers2)); } diff --git a/src/test/fuzz/data_stream.cpp b/src/test/fuzz/data_stream.cpp index 9c1022047e..08e9c91ba3 100644 --- a/src/test/fuzz/data_stream.cpp +++ b/src/test/fuzz/data_stream.cpp @@ -23,5 +23,5 @@ FUZZ_TARGET_INIT(data_stream_addr_man, initialize_data_stream_addr_man) FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; CDataStream data_stream = ConsumeDataStream(fuzzed_data_provider); CAddrMan addr_man(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0); - CAddrDB::Read(addr_man, data_stream); + ReadFromStream(addr_man, data_stream); } |