Skip to content

Commit

Permalink
banman: pass the banfile path in
Browse files Browse the repository at this point in the history
There's no need to hard-code the path here. Passing it in means that there are
no ordering concerns wrt establishing the datadir.
  • Loading branch information
theuni authored and dongcarl committed Jan 16, 2019
1 parent 4c0d961 commit 2e56702
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 18 deletions.
7 changes: 3 additions & 4 deletions src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,18 @@ bool DeserializeFileDB(const fs::path& path, Data& data)

}

CBanDB::CBanDB()
CBanDB::CBanDB(fs::path ban_list_path) : m_ban_list_path(std::move(ban_list_path))
{
pathBanlist = GetDataDir() / "banlist.dat";
}

bool CBanDB::Write(const banmap_t& banSet)
{
return SerializeFileDB("banlist", pathBanlist, banSet);
return SerializeFileDB("banlist", m_ban_list_path, banSet);
}

bool CBanDB::Read(banmap_t& banSet)
{
return DeserializeFileDB(pathBanlist, banSet);
return DeserializeFileDB(m_ban_list_path, banSet);
}

CAddrDB::CAddrDB()
Expand Down
4 changes: 2 additions & 2 deletions src/addrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class CAddrDB
class CBanDB
{
private:
fs::path pathBanlist;
const fs::path m_ban_list_path;
public:
CBanDB();
explicit CBanDB(fs::path ban_list_path);
bool Write(const banmap_t& banSet);
bool Read(banmap_t& banSet);
};
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ bool AppInitMain(InitInterfaces& interfaces)
// need to reindex later.

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

Expand Down
9 changes: 3 additions & 6 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,9 @@ void BanMan::DumpBanlist()

int64_t nStart = GetTimeMillis();

CBanDB bandb;
banmap_t banmap;
GetBanned(banmap);
if (bandb.Write(banmap)) {
if (m_ban_db.Write(banmap)) {
SetBannedSetDirty(false);
}

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

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

int64_t nStart = GetTimeMillis();
setBannedIsDirty = false;
CBanDB bandb;
banmap_t banmap;
if (bandb.Read(banmap)) {
if (m_ban_db.Read(banmap)) {
SetBanned(banmap); // thread save setter
SetBannedSetDirty(false); // no need to write down, just read data
SweepBanned(); // sweep out unused entries
Expand Down
3 changes: 2 additions & 1 deletion src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class BanMan
// between nodes running old code and nodes running
// new code.
~BanMan();
BanMan(CClientUIInterface* client_interface);
BanMan(fs::path ban_file, CClientUIInterface* client_interface);
void Ban(const CNetAddr& netAddr, const BanReason& reason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
void Ban(const CSubNet& subNet, const BanReason& reason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
void ClearBanned(); // needed for unit testing
Expand All @@ -156,6 +156,7 @@ class BanMan
CCriticalSection cs_setBanned;
bool setBannedIsDirty;
CClientUIInterface* clientInterface = nullptr;
CBanDB m_ban_db;
};

class NetEventsInterface;
Expand Down
6 changes: 3 additions & 3 deletions src/test/denialofservice_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)

BOOST_AUTO_TEST_CASE(DoS_banning)
{
auto banman = MakeUnique<BanMan>(nullptr);
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);

Expand Down Expand Up @@ -271,7 +271,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)

BOOST_AUTO_TEST_CASE(DoS_banscore)
{
auto banman = MakeUnique<BanMan>(nullptr);
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);

Expand Down Expand Up @@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)

BOOST_AUTO_TEST_CASE(DoS_bantime)
{
auto banman = MakeUnique<BanMan>(nullptr);
auto banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr);
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);

Expand Down
2 changes: 1 addition & 1 deletion src/test/test_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck);

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

Expand Down

0 comments on commit 2e56702

Please sign in to comment.