Skip to content

Commit 8966499

Browse files
author
MarcoFalke
committed
Merge bitcoin#22915: Remove confusing CAddrDB
fade9a1 Remove confusing CAddrDB (MarcoFalke) fa7f77b Fix addrdb includes (MarcoFalke) fa3f5d0 Move addrman includes from .h to .cpp (MarcoFalke) Pull request description: Split out from bitcoin#22762 to avoid having to carry it around in (an)other rebase(s) ACKs for top commit: ryanofsky: Code review ACK fade9a1 lsilva01: Code Review ACK bitcoin@fade9a1 Tree-SHA512: 7615fb0b6235d0c1e6f8cd6263dd18c4d95890567c2b797fe6fce6cb12cc85ce6eacbe07dbb6d81b05d179ef03b42edfd61c940e35a1044ce6d363b54c2dae5c
2 parents 8805e06 + fade9a1 commit 8966499

File tree

13 files changed

+37
-46
lines changed

13 files changed

+37
-46
lines changed

src/addrdb.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,19 @@ bool CBanDB::Read(banmap_t& banSet)
170170
return true;
171171
}
172172

173-
CAddrDB::CAddrDB()
174-
{
175-
pathAddr = gArgs.GetDataDirNet() / "peers.dat";
176-
}
177-
178-
bool CAddrDB::Write(const CAddrMan& addr)
173+
bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr)
179174
{
175+
const auto pathAddr = args.GetDataDirNet() / "peers.dat";
180176
return SerializeFileDB("peers", pathAddr, addr, CLIENT_VERSION);
181177
}
182178

183-
bool CAddrDB::Read(CAddrMan& addr)
179+
bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr)
184180
{
181+
const auto pathAddr = args.GetDataDirNet() / "peers.dat";
185182
return DeserializeFileDB(pathAddr, addr, CLIENT_VERSION);
186183
}
187184

188-
bool CAddrDB::Read(CAddrMan& addr, CDataStream& ssPeers)
185+
bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers)
189186
{
190187
return DeserializeDB(ssPeers, addr, false);
191188
}

src/addrdb.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,15 @@
1212

1313
#include <vector>
1414

15-
class CAddress;
15+
class ArgsManager;
1616
class CAddrMan;
17+
class CAddress;
1718
class CDataStream;
1819

19-
/** Access to the (IP) address database (peers.dat) */
20-
class CAddrDB
21-
{
22-
private:
23-
fs::path pathAddr;
24-
public:
25-
CAddrDB();
26-
bool Write(const CAddrMan& addr);
27-
bool Read(CAddrMan& addr);
28-
static bool Read(CAddrMan& addr, CDataStream& ssPeers);
29-
};
20+
bool DumpPeerAddresses(const ArgsManager& args, const CAddrMan& addr);
21+
bool ReadPeerAddresses(const ArgsManager& args, CAddrMan& addr);
22+
/** Only used by tests. */
23+
bool ReadFromStream(CAddrMan& addr, CDataStream& ssPeers);
3024

3125
/** Access to the banlist database (banlist.json) */
3226
class CBanDB

src/addrman.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
#include <addrman.h>
77

8+
#include <clientversion.h>
89
#include <hash.h>
910
#include <logging.h>
1011
#include <netaddress.h>
1112
#include <serialize.h>
13+
#include <streams.h>
1214

1315
#include <cmath>
1416
#include <optional>

src/addrman.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@
66
#ifndef BITCOIN_ADDRMAN_H
77
#define BITCOIN_ADDRMAN_H
88

9-
#include <clientversion.h>
10-
#include <config/bitcoin-config.h>
119
#include <fs.h>
12-
#include <hash.h>
10+
#include <logging.h>
1311
#include <netaddress.h>
1412
#include <protocol.h>
15-
#include <random.h>
16-
#include <streams.h>
1713
#include <sync.h>
1814
#include <timedata.h>
19-
#include <tinyformat.h>
20-
#include <util/system.h>
2115

22-
#include <iostream>
16+
#include <cstdint>
2317
#include <optional>
2418
#include <set>
25-
#include <stdint.h>
2619
#include <unordered_map>
2720
#include <vector>
2821

src/init.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,14 +1206,13 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12061206
// Load addresses from peers.dat
12071207
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
12081208
int64_t nStart = GetTimeMillis();
1209-
CAddrDB adb;
1210-
if (adb.Read(*node.addrman)) {
1209+
if (ReadPeerAddresses(args, *node.addrman)) {
12111210
LogPrintf("Loaded %i addresses from peers.dat %dms\n", node.addrman->size(), GetTimeMillis() - nStart);
12121211
} else {
12131212
// Addrman can be in an inconsistent state after failure, reset it
12141213
node.addrman = std::make_unique<CAddrMan>(asmap, /* deterministic */ false, /* consistency_check_ratio */ check_addrman);
12151214
LogPrintf("Recreating peers.dat\n");
1216-
adb.Write(*node.addrman);
1215+
DumpPeerAddresses(args, *node.addrman);
12171216
}
12181217
}
12191218

src/net.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <net.h>
1111

12+
#include <addrdb.h>
1213
#include <banman.h>
1314
#include <clientversion.h>
1415
#include <compat.h>
@@ -24,6 +25,7 @@
2425
#include <scheduler.h>
2526
#include <util/sock.h>
2627
#include <util/strencodings.h>
28+
#include <util/system.h>
2729
#include <util/thread.h>
2830
#include <util/trace.h>
2931
#include <util/translation.h>
@@ -1747,8 +1749,7 @@ void CConnman::DumpAddresses()
17471749
{
17481750
int64_t nStart = GetTimeMillis();
17491751

1750-
CAddrDB adb;
1751-
adb.Write(addrman);
1752+
DumpPeerAddresses(::gArgs, addrman);
17521753

17531754
LogPrint(BCLog::NET, "Flushed %d addresses to peers.dat %dms\n",
17541755
addrman.size(), GetTimeMillis() - nStart);

src/net.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef BITCOIN_NET_H
77
#define BITCOIN_NET_H
88

9-
#include <addrdb.h>
109
#include <addrman.h>
1110
#include <amount.h>
1211
#include <bloom.h>

src/qt/bantablemodel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_QT_BANTABLEMODEL_H
66
#define BITCOIN_QT_BANTABLEMODEL_H
77

8+
#include <addrdb.h>
89
#include <net.h>
910

1011
#include <memory>

src/qt/walletframe.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <qt/psbtoperationsdialog.h>
1212
#include <qt/walletmodel.h>
1313
#include <qt/walletview.h>
14+
#include <util/system.h>
1415

1516
#include <cassert>
1617

src/test/addrman_tests.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
#include <addrdb.h>
66
#include <addrman.h>
77
#include <chainparams.h>
8+
#include <clientversion.h>
9+
#include <hash.h>
10+
#include <netbase.h>
11+
#include <random.h>
812
#include <test/data/asmap.raw.h>
913
#include <test/util/setup_common.h>
1014
#include <util/asmap.h>
1115
#include <util/string.h>
12-
#include <hash.h>
13-
#include <netbase.h>
14-
#include <random.h>
1516

1617
#include <boost/test/unit_test.hpp>
1718

@@ -1002,7 +1003,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
10021003
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
10031004
}
10041005

1005-
BOOST_AUTO_TEST_CASE(caddrdb_read)
1006+
BOOST_AUTO_TEST_CASE(load_addrman)
10061007
{
10071008
CAddrManUncorrupted addrmanUncorrupted;
10081009

@@ -1037,17 +1038,17 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
10371038
BOOST_CHECK(addrman1.size() == 3);
10381039
BOOST_CHECK(exceptionThrown == false);
10391040

1040-
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
1041+
// Test that ReadFromStream creates an addrman with the correct number of addrs.
10411042
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
10421043

10431044
CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
10441045
BOOST_CHECK(addrman2.size() == 0);
1045-
BOOST_CHECK(CAddrDB::Read(addrman2, ssPeers2));
1046+
BOOST_CHECK(ReadFromStream(addrman2, ssPeers2));
10461047
BOOST_CHECK(addrman2.size() == 3);
10471048
}
10481049

10491050

1050-
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
1051+
BOOST_AUTO_TEST_CASE(load_addrman_corrupted)
10511052
{
10521053
CAddrManCorrupted addrmanCorrupted;
10531054

@@ -1063,16 +1064,16 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
10631064
} catch (const std::exception&) {
10641065
exceptionThrown = true;
10651066
}
1066-
// Even through de-serialization failed addrman is not left in a clean state.
1067+
// Even though de-serialization failed addrman is not left in a clean state.
10671068
BOOST_CHECK(addrman1.size() == 1);
10681069
BOOST_CHECK(exceptionThrown);
10691070

1070-
// Test that CAddrDB::Read fails if peers.dat is corrupt
1071+
// Test that ReadFromStream fails if peers.dat is corrupt
10711072
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
10721073

10731074
CAddrMan addrman2(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 100);
10741075
BOOST_CHECK(addrman2.size() == 0);
1075-
BOOST_CHECK(!CAddrDB::Read(addrman2, ssPeers2));
1076+
BOOST_CHECK(!ReadFromStream(addrman2, ssPeers2));
10761077
}
10771078

10781079

0 commit comments

Comments
 (0)