From 5706edad5fa1a80a1d290fbd8d31102521d1e196 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 7 Sep 2021 10:48:45 +0200 Subject: [PATCH] merge bitcoin#22915: Remove confusing CAddrDB --- src/addrdb.cpp | 13 +++++-------- src/addrdb.h | 18 ++++++------------ src/addrman.cpp | 2 ++ src/addrman.h | 11 ++--------- src/init.cpp | 5 ++--- src/net.cpp | 5 +++-- src/net.h | 1 - src/qt/bantablemodel.h | 1 + src/qt/walletframe.cpp | 1 + src/test/addrman_tests.cpp | 16 ++++++++-------- src/test/fuzz/data_stream.cpp | 3 ++- src/test/fuzz/integer.cpp | 1 + 12 files changed, 33 insertions(+), 44 deletions(-) diff --git a/src/addrdb.cpp b/src/addrdb.cpp index 85b8c84f8d..41ee606870 100644 --- a/src/addrdb.cpp +++ b/src/addrdb.cpp @@ -171,22 +171,19 @@ bool CBanDB::Read(banmap_t& banSet) return true; } -CAddrDB::CAddrDB() -{ - pathAddr = GetDataDir() / "peers.dat"; -} - -bool CAddrDB::Write(const CAddrMan& addr) +bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr) { + const auto pathAddr = GetDataDir() / "peers.dat"; return SerializeFileDB("peers", pathAddr, addr, CLIENT_VERSION); } -bool CAddrDB::Read(CAddrMan& addr) +bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr) { + const auto pathAddr = GetDataDir() / "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 -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/addrman.cpp b/src/addrman.cpp index 7b49ccfeab..affb95031b 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -5,10 +5,12 @@ #include +#include #include #include #include #include +#include #include #include diff --git a/src/addrman.h b/src/addrman.h index 099d57ad3e..3159f2e0d9 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -6,23 +6,16 @@ #ifndef BITCOIN_ADDRMAN_H #define BITCOIN_ADDRMAN_H -#include -#include #include -#include +#include #include #include -#include -#include #include #include -#include -#include -#include +#include #include #include -#include #include #include diff --git a/src/init.cpp b/src/init.cpp index f9d1b221e0..adee949365 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1698,14 +1698,13 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc // 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(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 4d6afdbc48..c77a8947af 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -26,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -2324,8 +2326,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/net.h b/src/net.h index ced7d17df0..838899c582 100644 --- a/src/net.h +++ b/src/net.h @@ -6,7 +6,6 @@ #ifndef BITCOIN_NET_H #define BITCOIN_NET_H -#include #include #include #include diff --git a/src/qt/bantablemodel.h b/src/qt/bantablemodel.h index 57f559fc14..4b5b38e43f 100644 --- a/src/qt/bantablemodel.h +++ b/src/qt/bantablemodel.h @@ -5,6 +5,7 @@ #ifndef BITCOIN_QT_BANTABLEMODEL_H #define BITCOIN_QT_BANTABLEMODEL_H +#include #include #include diff --git a/src/qt/walletframe.cpp b/src/qt/walletframe.cpp index 64409e4d52..36eeebaf82 100644 --- a/src/qt/walletframe.cpp +++ b/src/qt/walletframe.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp index c4f9bc049c..7986981ec2 100644 --- a/src/test/addrman_tests.cpp +++ b/src/test/addrman_tests.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -1001,8 +1002,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; @@ -1037,17 +1037,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(), /* 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; @@ -1063,16 +1063,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(), /* deterministic */ false, /* consistency_check_ratio */ 100); BOOST_CHECK(addrman2.size() == 0); - BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2)); + BOOST_CHECK(!ReadFromStream(addrman2, ssPeers2)); } BOOST_AUTO_TEST_SUITE_END() diff --git a/src/test/fuzz/data_stream.cpp b/src/test/fuzz/data_stream.cpp index 7419b28f90..bda4b8ada9 100644 --- a/src/test/fuzz/data_stream.cpp +++ b/src/test/fuzz/data_stream.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include @@ -22,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(), /* deterministic */ false, /* consistency_check_ratio */ 0); - CAddrDB::Read(addr_man, data_stream); + ReadFromStream(addr_man, data_stream); } diff --git a/src/test/fuzz/integer.cpp b/src/test/fuzz/integer.cpp index c77778dc6b..1bd129a59e 100644 --- a/src/test/fuzz/integer.cpp +++ b/src/test/fuzz/integer.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include