Skip to content

Commit 2e56702

Browse files
theunidongcarl
authored andcommitted
banman: pass the banfile path in
There's no need to hard-code the path here. Passing it in means that there are no ordering concerns wrt establishing the datadir.
1 parent 4c0d961 commit 2e56702

File tree

7 files changed

+15
-18
lines changed

7 files changed

+15
-18
lines changed

src/addrdb.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,18 @@ bool DeserializeFileDB(const fs::path& path, Data& data)
105105

106106
}
107107

108-
CBanDB::CBanDB()
108+
CBanDB::CBanDB(fs::path ban_list_path) : m_ban_list_path(std::move(ban_list_path))
109109
{
110-
pathBanlist = GetDataDir() / "banlist.dat";
111110
}
112111

113112
bool CBanDB::Write(const banmap_t& banSet)
114113
{
115-
return SerializeFileDB("banlist", pathBanlist, banSet);
114+
return SerializeFileDB("banlist", m_ban_list_path, banSet);
116115
}
117116

118117
bool CBanDB::Read(banmap_t& banSet)
119118
{
120-
return DeserializeFileDB(pathBanlist, banSet);
119+
return DeserializeFileDB(m_ban_list_path, banSet);
121120
}
122121

123122
CAddrDB::CAddrDB()

src/addrdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ class CAddrDB
9292
class CBanDB
9393
{
9494
private:
95-
fs::path pathBanlist;
95+
const fs::path m_ban_list_path;
9696
public:
97-
CBanDB();
97+
explicit CBanDB(fs::path ban_list_path);
9898
bool Write(const banmap_t& banSet);
9999
bool Read(banmap_t& banSet);
100100
};

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ bool AppInitMain(InitInterfaces& interfaces)
12961296
// need to reindex later.
12971297

12981298
assert(!g_banman);
1299-
g_banman = MakeUnique<BanMan>(&uiInterface);
1299+
g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface);
13001300
assert(!g_connman);
13011301
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
13021302

src/net.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,9 @@ void BanMan::DumpBanlist()
466466

467467
int64_t nStart = GetTimeMillis();
468468

469-
CBanDB bandb;
470469
banmap_t banmap;
471470
GetBanned(banmap);
472-
if (bandb.Write(banmap)) {
471+
if (m_ban_db.Write(banmap)) {
473472
SetBannedSetDirty(false);
474473
}
475474

@@ -2431,16 +2430,14 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
24312430
return true;
24322431
}
24332432

2434-
BanMan::BanMan(CClientUIInterface* client_interface) : clientInterface(client_interface)
2433+
BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface) : clientInterface(client_interface), m_ban_db(std::move(ban_file))
24352434
{
24362435
if (clientInterface) clientInterface->InitMessage(_("Loading banlist..."));
2437-
// Load addresses from banlist.dat
24382436

24392437
int64_t nStart = GetTimeMillis();
24402438
setBannedIsDirty = false;
2441-
CBanDB bandb;
24422439
banmap_t banmap;
2443-
if (bandb.Read(banmap)) {
2440+
if (m_ban_db.Read(banmap)) {
24442441
SetBanned(banmap); // thread save setter
24452442
SetBannedSetDirty(false); // no need to write down, just read data
24462443
SweepBanned(); // sweep out unused entries

src/net.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class BanMan
133133
// between nodes running old code and nodes running
134134
// new code.
135135
~BanMan();
136-
BanMan(CClientUIInterface* client_interface);
136+
BanMan(fs::path ban_file, CClientUIInterface* client_interface);
137137
void Ban(const CNetAddr& netAddr, const BanReason& reason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
138138
void Ban(const CSubNet& subNet, const BanReason& reason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
139139
void ClearBanned(); // needed for unit testing
@@ -156,6 +156,7 @@ class BanMan
156156
CCriticalSection cs_setBanned;
157157
bool setBannedIsDirty;
158158
CClientUIInterface* clientInterface = nullptr;
159+
CBanDB m_ban_db;
159160
};
160161

161162
class NetEventsInterface;

src/test/denialofservice_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
216216

217217
BOOST_AUTO_TEST_CASE(DoS_banning)
218218
{
219-
auto banman = MakeUnique<BanMan>(nullptr);
219+
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
220220
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
221221
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);
222222

@@ -271,7 +271,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
271271

272272
BOOST_AUTO_TEST_CASE(DoS_banscore)
273273
{
274-
auto banman = MakeUnique<BanMan>(nullptr);
274+
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
275275
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
276276
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);
277277

@@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
318318

319319
BOOST_AUTO_TEST_CASE(DoS_bantime)
320320
{
321-
auto banman = MakeUnique<BanMan>(nullptr);
321+
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
322322
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
323323
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);
324324

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
9494
for (int i=0; i < nScriptCheckThreads-1; i++)
9595
threadGroup.create_thread(&ThreadScriptCheck);
9696

97-
g_banman = MakeUnique<BanMan>(nullptr);
97+
g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
9898
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
9999
}
100100

0 commit comments

Comments
 (0)