Skip to content

Commit aa5cd3c

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25149: refactor: Add thread safety annotation to BanMan::SweepBanned()
ab75388 refactor: Remove redundant scope in `BanMan::SweepBanned()` (Hennadii Stepanov) 52c0b3e refactor: Add thread safety annotation to `BanMan::SweepBanned()` (Hennadii Stepanov) 3919059 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 ab75388 w0xlt: ACK bitcoin/bitcoin@ab75388 theStack: Code-review ACK ab75388 Tree-SHA512: 8699079c308454f3ffe72be2e77f0935214156bd509f9338b1104f8d128bfdd02ee06ee8c1c99b2eefdf317a00edd555d52990caaeb1ed4540eedc1e3c9d1faf
2 parents 44037a2 + ab75388 commit aa5cd3c

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

src/banman.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
1717
: m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
1818
{
19+
LoadBanlist();
20+
DumpBanlist();
21+
}
22+
23+
BanMan::~BanMan()
24+
{
25+
DumpBanlist();
26+
}
27+
28+
void BanMan::LoadBanlist()
29+
{
30+
LOCK(m_cs_banned);
31+
1932
if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated);
2033

2134
int64_t n_start = GetTimeMillis();
@@ -29,13 +42,6 @@ BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t
2942
m_banned = {};
3043
m_is_dirty = true;
3144
}
32-
33-
DumpBanlist();
34-
}
35-
36-
BanMan::~BanMan()
37-
{
38-
DumpBanlist();
3945
}
4046

4147
void BanMan::DumpBanlist()
@@ -173,23 +179,24 @@ void BanMan::GetBanned(banmap_t& banmap)
173179

174180
void BanMan::SweepBanned()
175181
{
182+
AssertLockHeld(m_cs_banned);
183+
176184
int64_t now = GetTime();
177185
bool notify_ui = false;
178-
{
179-
LOCK(m_cs_banned);
180-
banmap_t::iterator it = m_banned.begin();
181-
while (it != m_banned.end()) {
182-
CSubNet sub_net = (*it).first;
183-
CBanEntry ban_entry = (*it).second;
184-
if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
185-
m_banned.erase(it++);
186-
m_is_dirty = true;
187-
notify_ui = true;
188-
LogPrint(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
189-
} else
190-
++it;
186+
banmap_t::iterator it = m_banned.begin();
187+
while (it != m_banned.end()) {
188+
CSubNet sub_net = (*it).first;
189+
CBanEntry ban_entry = (*it).second;
190+
if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
191+
m_banned.erase(it++);
192+
m_is_dirty = true;
193+
notify_ui = true;
194+
LogPrint(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
195+
} else {
196+
++it;
191197
}
192198
}
199+
193200
// update UI
194201
if (notify_ui && m_client_interface) {
195202
m_client_interface->BannedListChanged();

src/banman.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ class BanMan
8080
void DumpBanlist();
8181

8282
private:
83+
void LoadBanlist() EXCLUSIVE_LOCKS_REQUIRED(!m_cs_banned);
8384
bool BannedSetIsDirty();
8485
//!set the "dirty" flag for the banlist
8586
void SetBannedSetDirty(bool dirty = true);
8687
//!clean unused entries (if bantime has expired)
87-
void SweepBanned();
88+
void SweepBanned() EXCLUSIVE_LOCKS_REQUIRED(m_cs_banned);
8889

8990
RecursiveMutex m_cs_banned;
9091
banmap_t m_banned GUARDED_BY(m_cs_banned);

0 commit comments

Comments
 (0)