From d0469b2e9386a7a4b268cb9725347e7517acace6 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Thu, 5 Oct 2017 13:41:45 -0400 Subject: [PATCH] banman: pass in default ban time as a parameter Removes the dependency on arg parsing. --- src/init.cpp | 2 +- src/net.cpp | 4 ++-- src/net.h | 3 ++- src/test/denialofservice_tests.cpp | 6 +++--- src/test/test_bitcoin.cpp | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index ded157d49..0f59dfbef 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1296,7 +1296,7 @@ bool AppInitMain(InitInterfaces& interfaces) // need to reindex later. assert(!g_banman); - g_banman = MakeUnique(GetDataDir() / "banlist.dat", &uiInterface); + g_banman = MakeUnique(GetDataDir() / "banlist.dat", &uiInterface, gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME)); assert(!g_connman); g_connman = std::unique_ptr(new CConnman(GetRand(std::numeric_limits::max()), GetRand(std::numeric_limits::max()))); diff --git a/src/net.cpp b/src/net.cpp index fd74c5829..f6491bc8d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -537,7 +537,7 @@ void BanMan::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t bant banEntry.banReason = banReason; if (bantimeoffset <= 0) { - bantimeoffset = gArgs.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME); + bantimeoffset = m_default_ban_time; sinceUnixEpoch = false; } banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset; @@ -2430,7 +2430,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions) return true; } -BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface) : clientInterface(client_interface), m_ban_db(std::move(ban_file)) +BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time) : clientInterface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time) { if (clientInterface) clientInterface->InitMessage(_("Loading banlist...")); diff --git a/src/net.h b/src/net.h index 5e6fc14b3..a6a536d68 100644 --- a/src/net.h +++ b/src/net.h @@ -133,7 +133,7 @@ public: // between nodes running old code and nodes running // new code. ~BanMan(); - BanMan(fs::path ban_file, CClientUIInterface* client_interface); + BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time); 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 @@ -157,6 +157,7 @@ private: bool setBannedIsDirty; CClientUIInterface* clientInterface = nullptr; CBanDB m_ban_db; + int64_t m_default_ban_time; }; class NetEventsInterface; diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp index 4a88613a8..29bb34bdd 100644 --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management) BOOST_AUTO_TEST_CASE(DoS_banning) { - auto banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr); + auto banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto connman = MakeUnique(0x1337, 0x1337); auto peerLogic = MakeUnique(connman.get(), banman.get(), scheduler, false); @@ -271,7 +271,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning) BOOST_AUTO_TEST_CASE(DoS_banscore) { - auto banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr); + auto banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto connman = MakeUnique(0x1337, 0x1337); auto peerLogic = MakeUnique(connman.get(), banman.get(), scheduler, false); @@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore) BOOST_AUTO_TEST_CASE(DoS_bantime) { - auto banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr); + auto banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto connman = MakeUnique(0x1337, 0x1337); auto peerLogic = MakeUnique(connman.get(), banman.get(), scheduler, false); diff --git a/src/test/test_bitcoin.cpp b/src/test/test_bitcoin.cpp index 7bce74b46..75bd8db67 100644 --- a/src/test/test_bitcoin.cpp +++ b/src/test/test_bitcoin.cpp @@ -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(GetDataDir() / "banlist.dat", nullptr); + g_banman = MakeUnique(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); g_connman = MakeUnique(0x1337, 0x1337); // Deterministic randomness for tests. }