Skip to content

Commit 9f83b8b

Browse files
MarcoFalkeFabcien
authored andcommitted
Remove confusing CAddrDB
Summary: Backport of [[bitcoin/bitcoin#22915 | core#22915]]. Depends on D12302. Test Plan: ninja all check-all Reviewers: #bitcoin_abc, PiRK Reviewed By: #bitcoin_abc, PiRK Subscribers: PiRK Differential Revision: https://reviews.bitcoinabc.org/D12304
1 parent f83a3e8 commit 9f83b8b

File tree

11 files changed

+38
-47
lines changed

11 files changed

+38
-47
lines changed

src/addrdb.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,20 @@ bool CBanDB::Read(banmap_t &banSet) {
136136
return DeserializeFileDB(chainParams, m_ban_list_path, banSet);
137137
}
138138

139-
CAddrDB::CAddrDB(const CChainParams &chainParamsIn)
140-
: chainParams(chainParamsIn) {
141-
pathAddr = gArgs.GetDataDirNet() / "peers.dat";
142-
}
143-
144-
bool CAddrDB::Write(const CAddrMan &addr) {
139+
bool DumpPeerAddresses(const CChainParams &chainParams, const ArgsManager &args,
140+
const CAddrMan &addr) {
141+
const auto pathAddr = args.GetDataDirNet() / "peers.dat";
145142
return SerializeFileDB(chainParams, "peers", pathAddr, addr);
146143
}
147144

148-
bool CAddrDB::Read(CAddrMan &addr) {
145+
bool ReadPeerAddresses(const CChainParams &chainParams, const ArgsManager &args,
146+
CAddrMan &addr) {
147+
const auto pathAddr = args.GetDataDirNet() / "peers.dat";
149148
return DeserializeFileDB(chainParams, pathAddr, addr);
150149
}
151150

152-
bool CAddrDB::Read(CAddrMan &addr, CDataStream &ssPeers) {
151+
bool ReadFromStream(const CChainParams &chainParams, CAddrMan &addr,
152+
CDataStream &ssPeers) {
153153
bool ret = DeserializeDB(chainParams, ssPeers, addr, false);
154154
if (!ret) {
155155
// Ensure addrman is left in a clean state

src/addrdb.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@
1313
#include <string>
1414
#include <vector>
1515

16-
class CAddress;
16+
class ArgsManager;
1717
class CAddrMan;
18+
class CAddress;
1819
class CDataStream;
1920
class CChainParams;
2021

22+
bool DumpPeerAddresses(const CChainParams &chainParams, const ArgsManager &args,
23+
const CAddrMan &addr);
24+
bool ReadPeerAddresses(const CChainParams &chainParams, const ArgsManager &args,
25+
CAddrMan &addr);
26+
/** Only used by tests. */
27+
bool ReadFromStream(const CChainParams &chainParams, CAddrMan &addr,
28+
CDataStream &ssPeers);
29+
2130
class CBanEntry {
2231
public:
2332
static const int CURRENT_VERSION = 1;
@@ -45,19 +54,6 @@ class CBanEntry {
4554
}
4655
};
4756

48-
/** Access to the (IP) address database (peers.dat) */
49-
class CAddrDB {
50-
private:
51-
fs::path pathAddr;
52-
const CChainParams &chainParams;
53-
54-
public:
55-
explicit CAddrDB(const CChainParams &chainParams);
56-
bool Write(const CAddrMan &addr);
57-
bool Read(CAddrMan &addr);
58-
bool Read(CAddrMan &addr, CDataStream &ssPeers);
59-
};
60-
6157
/** Access to the banlist database (banlist.dat) */
6258
class CBanDB {
6359
private:

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
#include <util/check.h>
1315

1416
#include <cmath>

src/addrman.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
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 <util/system.h>
20-
21-
#include <tinyformat.h>
2215

2316
#include <cstdint>
24-
#include <iostream>
2517
#include <optional>
2618
#include <set>
2719
#include <unordered_map>

src/init.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,16 +2406,15 @@ bool AppInitMain(Config &config, RPCServer &rpcServer,
24062406
// Load addresses from peers.dat
24072407
uiInterface.InitMessage(_("Loading P2P addresses...").translated);
24082408
int64_t nStart = GetTimeMillis();
2409-
CAddrDB adb(chainparams);
2410-
if (adb.Read(*node.addrman)) {
2409+
if (ReadPeerAddresses(chainparams, args, *node.addrman)) {
24112410
LogPrintf("Loaded %i addresses from peers.dat %dms\n",
24122411
node.addrman->size(), GetTimeMillis() - nStart);
24132412
} else {
24142413
// Addrman can be in an inconsistent state after failure, reset it
24152414
node.addrman = std::make_unique<CAddrMan>(
24162415
asmap, /* consistency_check_ratio= */ check_addrman);
24172416
LogPrintf("Recreating peers.dat\n");
2418-
adb.Write(*node.addrman);
2417+
DumpPeerAddresses(chainparams, args, *node.addrman);
24192418
}
24202419
}
24212420

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 <avalanche/avalanche.h>
1314
#include <banman.h>
1415
#include <clientversion.h>
@@ -26,6 +27,7 @@
2627
#include <scheduler.h>
2728
#include <util/sock.h>
2829
#include <util/strencodings.h>
30+
#include <util/system.h>
2931
#include <util/thread.h>
3032
#include <util/translation.h>
3133

@@ -2055,8 +2057,7 @@ void CConnman::ThreadDNSAddressSeed() {
20552057
void CConnman::DumpAddresses() {
20562058
int64_t nStart = GetTimeMillis();
20572059

2058-
CAddrDB adb(config->GetChainParams());
2059-
adb.Write(addrman);
2060+
DumpPeerAddresses(config->GetChainParams(), ::gArgs, addrman);
20602061

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

src/net.h

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

10-
#include <addrdb.h>
1110
#include <addrman.h>
1211
#include <avalanche/proofid.h>
1312
#include <avalanche/proofradixtreeadapter.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 <QAbstractTableModel>

src/qt/walletframe.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <qt/walletcontroller.h>
1111
#include <qt/walletmodel.h>
1212
#include <qt/walletview.h>
13+
#include <util/system.h>
1314

1415
#include <QGroupBox>
1516
#include <QHBoxLayout>

src/test/addrman_tests.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include <addrdb.h>
66
#include <addrman.h>
77
#include <chainparams.h>
8+
#include <clientversion.h>
89
#include <hash.h>
910
#include <netbase.h>
1011
#include <random.h>
12+
#include <streams.h>
1113
#include <util/asmap.h>
1214
#include <util/string.h>
1315

@@ -984,7 +986,7 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks) {
984986
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
985987
}
986988

987-
BOOST_AUTO_TEST_CASE(caddrdb_read) {
989+
BOOST_AUTO_TEST_CASE(load_addrman) {
988990
CAddrManUncorrupted addrmanUncorrupted;
989991
addrmanUncorrupted.MakeDeterministic();
990992

@@ -1020,19 +1022,18 @@ BOOST_AUTO_TEST_CASE(caddrdb_read) {
10201022
BOOST_CHECK(addrman1.size() == 3);
10211023
BOOST_CHECK(exceptionThrown == false);
10221024

1023-
// Test that CAddrDB::Read creates an addrman with the correct number of
1025+
// Test that ReadFromStream creates an addrman with the correct number of
10241026
// addrs.
10251027
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
10261028

10271029
CAddrMan addrman2(/* asmap= */ std::vector<bool>(),
10281030
/* consistency_check_ratio= */ 100);
1029-
CAddrDB adb(Params());
10301031
BOOST_CHECK(addrman2.size() == 0);
1031-
BOOST_CHECK(adb.Read(addrman2, ssPeers2));
1032+
BOOST_CHECK(ReadFromStream(Params(), addrman2, ssPeers2));
10321033
BOOST_CHECK(addrman2.size() == 3);
10331034
}
10341035

1035-
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted) {
1036+
BOOST_AUTO_TEST_CASE(load_addrman_corrupted) {
10361037
CAddrManCorrupted addrmanCorrupted;
10371038
addrmanCorrupted.MakeDeterministic();
10381039

@@ -1049,20 +1050,18 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted) {
10491050
} catch (const std::exception &) {
10501051
exceptionThrown = true;
10511052
}
1052-
// Even through de-serialization failed addrman is not left in a clean
1053-
// state.
1053+
// Even though de-serialization failed addrman is not left in a clean state.
10541054
BOOST_CHECK(addrman1.size() == 1);
10551055
BOOST_CHECK(exceptionThrown);
10561056

1057-
// Test that CAddrDB::Read leaves addrman in a clean state if
1057+
// Test that ReadFromStream leaves addrman in a clean state if
10581058
// de-serialization fails.
10591059
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
10601060

10611061
CAddrMan addrman2(/* asmap= */ std::vector<bool>(),
10621062
/* consistency_check_ratio= */ 100);
1063-
CAddrDB adb(Params());
10641063
BOOST_CHECK(addrman2.size() == 0);
1065-
BOOST_CHECK(!adb.Read(addrman2, ssPeers2));
1064+
BOOST_CHECK(!ReadFromStream(Params(), addrman2, ssPeers2));
10661065
BOOST_CHECK(addrman2.size() == 0);
10671066
}
10681067

0 commit comments

Comments
 (0)