Merge bitcoin/bitcoin#25149: refactor: Add thread safety annotation to BanMan::SweepBanned()

ab7538832043a6c15e45a178fb6bb6298a00108f refactor: Remove redundant scope in `BanMan::SweepBanned()` (Hennadii Stepanov)
52c0b3e859089c0ac98e7261ded6391b4f8eeeaf refactor: Add thread safety annotation to `BanMan::SweepBanned()` (Hennadii Stepanov)
3919059deb60d6ead9defc9d213a3f0c2ab72e90 refactor: Move code from ctor into private `BanMan::LoadBanlist()` (Hennadii Stepanov)

Pull request description:

  This PR adds a proper thread safety annotation to `BanMan::SweepBanned()`.

  Also a simple refactoring applied.

ACKs for top commit:
  ajtowns:
    ACK ab7538832043a6c15e45a178fb6bb6298a00108f
  w0xlt:
    ACK ab75388320
  theStack:
    Code-review ACK ab7538832043a6c15e45a178fb6bb6298a00108f

Tree-SHA512: 8699079c308454f3ffe72be2e77f0935214156bd509f9338b1104f8d128bfdd02ee06ee8c1c99b2eefdf317a00edd555d52990caaeb1ed4540eedc1e3c9d1faf
This commit is contained in:
MacroFake 2022-05-24 09:14:52 +02:00 committed by pasta
parent 6269c6f1db
commit 76279c1a37
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
2 changed files with 29 additions and 21 deletions

View File

@ -16,6 +16,19 @@
BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time) BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
: m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time) : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
{ {
LoadBanlist();
DumpBanlist();
}
BanMan::~BanMan()
{
DumpBanlist();
}
void BanMan::LoadBanlist()
{
LOCK(m_cs_banned);
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated); if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated);
int64_t n_start = GetTimeMillis(); int64_t n_start = GetTimeMillis();
@ -29,13 +42,6 @@ BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t
m_banned = {}; m_banned = {};
m_is_dirty = true; m_is_dirty = true;
} }
DumpBanlist();
}
BanMan::~BanMan()
{
DumpBanlist();
} }
void BanMan::DumpBanlist() void BanMan::DumpBanlist()
@ -183,23 +189,24 @@ void BanMan::GetBanned(banmap_t& banmap)
void BanMan::SweepBanned() void BanMan::SweepBanned()
{ {
AssertLockHeld(m_cs_banned);
int64_t now = GetTime(); int64_t now = GetTime();
bool notify_ui = false; bool notify_ui = false;
{ banmap_t::iterator it = m_banned.begin();
LOCK(m_cs_banned); while (it != m_banned.end()) {
banmap_t::iterator it = m_banned.begin(); CSubNet sub_net = (*it).first;
while (it != m_banned.end()) { CBanEntry ban_entry = (*it).second;
CSubNet sub_net = (*it).first; if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
CBanEntry ban_entry = (*it).second; m_banned.erase(it++);
if (!sub_net.IsValid() || now > ban_entry.nBanUntil) { m_is_dirty = true;
m_banned.erase(it++); notify_ui = true;
m_is_dirty = true; LogPrint(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
notify_ui = true; } else {
LogPrint(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString()); ++it;
} else
++it;
} }
} }
// update UI // update UI
if (notify_ui && m_client_interface) { if (notify_ui && m_client_interface) {
m_client_interface->BannedListChanged(); m_client_interface->BannedListChanged();

View File

@ -81,11 +81,12 @@ public:
void DumpBanlist(); void DumpBanlist();
private: private:
void LoadBanlist() EXCLUSIVE_LOCKS_REQUIRED(!m_cs_banned);
bool BannedSetIsDirty(); bool BannedSetIsDirty();
//!set the "dirty" flag for the banlist //!set the "dirty" flag for the banlist
void SetBannedSetDirty(bool dirty = true); void SetBannedSetDirty(bool dirty = true);
//!clean unused entries (if bantime has expired) //!clean unused entries (if bantime has expired)
void SweepBanned(); void SweepBanned() EXCLUSIVE_LOCKS_REQUIRED(m_cs_banned);
RecursiveMutex m_cs_banned; RecursiveMutex m_cs_banned;
banmap_t m_banned GUARDED_BY(m_cs_banned); banmap_t m_banned GUARDED_BY(m_cs_banned);