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.
This commit is contained in:
Cory Fields 2017-10-05 13:35:20 -04:00 committed by Carl Dong
parent 4c0d961eb0
commit 2e56702ece
7 changed files with 15 additions and 18 deletions

View file

@ -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) 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) bool CBanDB::Read(banmap_t& banSet)
{ {
return DeserializeFileDB(pathBanlist, banSet); return DeserializeFileDB(m_ban_list_path, banSet);
} }
CAddrDB::CAddrDB() CAddrDB::CAddrDB()

View file

@ -92,9 +92,9 @@ public:
class CBanDB class CBanDB
{ {
private: private:
fs::path pathBanlist; const fs::path m_ban_list_path;
public: public:
CBanDB(); explicit CBanDB(fs::path ban_list_path);
bool Write(const banmap_t& banSet); bool Write(const banmap_t& banSet);
bool Read(banmap_t& banSet); bool Read(banmap_t& banSet);
}; };

View file

@ -1296,7 +1296,7 @@ bool AppInitMain(InitInterfaces& interfaces)
// need to reindex later. // need to reindex later.
assert(!g_banman); assert(!g_banman);
g_banman = MakeUnique<BanMan>(&uiInterface); g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface);
assert(!g_connman); 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()))); g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));

View file

@ -466,10 +466,9 @@ void BanMan::DumpBanlist()
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
CBanDB bandb;
banmap_t banmap; banmap_t banmap;
GetBanned(banmap); GetBanned(banmap);
if (bandb.Write(banmap)) { if (m_ban_db.Write(banmap)) {
SetBannedSetDirty(false); SetBannedSetDirty(false);
} }
@ -2431,16 +2430,14 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
return true; 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...")); if (clientInterface) clientInterface->InitMessage(_("Loading banlist..."));
// Load addresses from banlist.dat
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
setBannedIsDirty = false; setBannedIsDirty = false;
CBanDB bandb;
banmap_t banmap; banmap_t banmap;
if (bandb.Read(banmap)) { if (m_ban_db.Read(banmap)) {
SetBanned(banmap); // thread save setter SetBanned(banmap); // thread save setter
SetBannedSetDirty(false); // no need to write down, just read data SetBannedSetDirty(false); // no need to write down, just read data
SweepBanned(); // sweep out unused entries SweepBanned(); // sweep out unused entries

View file

@ -133,7 +133,7 @@ public:
// between nodes running old code and nodes running // between nodes running old code and nodes running
// new code. // new code.
~BanMan(); ~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 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 Ban(const CSubNet& subNet, const BanReason& reason, int64_t bantimeoffset = 0, bool sinceUnixEpoch = false);
void ClearBanned(); // needed for unit testing void ClearBanned(); // needed for unit testing
@ -156,6 +156,7 @@ private:
CCriticalSection cs_setBanned; CCriticalSection cs_setBanned;
bool setBannedIsDirty; bool setBannedIsDirty;
CClientUIInterface* clientInterface = nullptr; CClientUIInterface* clientInterface = nullptr;
CBanDB m_ban_db;
}; };
class NetEventsInterface; class NetEventsInterface;

View file

@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
BOOST_AUTO_TEST_CASE(DoS_banning) 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 connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);
@ -271,7 +271,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
BOOST_AUTO_TEST_CASE(DoS_banscore) 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 connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);
@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
BOOST_AUTO_TEST_CASE(DoS_bantime) 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 connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false); auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), banman.get(), scheduler, false);

View file

@ -94,7 +94,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
for (int i=0; i < nScriptCheckThreads-1; i++) for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck); 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. g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
} }