Skip to content

Commit 42aa5d5

Browse files
committed
[net] Add NoBan status to NodeEvictionCandidate
1 parent 55c9e2d commit 42aa5d5

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/net.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,15 @@ static void EraseLastKElements(
948948
elements.erase(std::remove_if(elements.end() - eraseSize, elements.end(), predicate), elements.end());
949949
}
950950

951+
void ProtectNoBanConnections(std::vector<NodeEvictionCandidate>& eviction_candidates)
952+
{
953+
eviction_candidates.erase(std::remove_if(eviction_candidates.begin(), eviction_candidates.end(),
954+
[](NodeEvictionCandidate const& n) {
955+
return n.m_noban;
956+
}),
957+
eviction_candidates.end());
958+
}
959+
951960
void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& eviction_candidates)
952961
{
953962
// Protect the half of the remaining nodes which have been connected the longest.
@@ -1025,6 +1034,8 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
10251034
{
10261035
// Protect connections with certain characteristics
10271036

1037+
ProtectNoBanConnections(vEvictionCandidates);
1038+
10281039
// Deterministically select 4 peers to protect by netgroup.
10291040
// An attacker cannot predict which netgroups will be protected
10301041
EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
@@ -1096,8 +1107,6 @@ bool CConnman::AttemptToEvictConnection()
10961107

10971108
LOCK(m_nodes_mutex);
10981109
for (const CNode* node : m_nodes) {
1099-
if (node->HasPermission(NetPermissionFlags::NoBan))
1100-
continue;
11011110
if (!node->IsInboundConn())
11021111
continue;
11031112
if (node->fDisconnect)
@@ -1115,6 +1124,7 @@ bool CConnman::AttemptToEvictConnection()
11151124
Desig(prefer_evict) node->m_prefer_evict,
11161125
Desig(m_is_local) node->addr.IsLocal(),
11171126
Desig(m_network) node->ConnectedThroughNetwork(),
1127+
Desig(m_noban) node->HasPermission(NetPermissionFlags::NoBan),
11181128
};
11191129
vEvictionCandidates.push_back(candidate);
11201130
}

src/net.h

+1
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ struct NodeEvictionCandidate
12611261
bool prefer_evict;
12621262
bool m_is_local;
12631263
Network m_network;
1264+
bool m_noban;
12641265
};
12651266

12661267
/**

src/test/fuzz/node_eviction.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ FUZZ_TARGET(node_eviction)
3232
/*prefer_evict=*/fuzzed_data_provider.ConsumeBool(),
3333
/*m_is_local=*/fuzzed_data_provider.ConsumeBool(),
3434
/*m_network=*/fuzzed_data_provider.PickValueInArray(ALL_NETWORKS),
35+
/*m_noban=*/fuzzed_data_provider.ConsumeBool(),
3536
});
3637
}
3738
// Make a copy since eviction_candidates may be in some valid but otherwise

src/test/util/net.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candida
5858
/*prefer_evict=*/random_context.randbool(),
5959
/*m_is_local=*/random_context.randbool(),
6060
/*m_network=*/ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
61+
/*m_noban=*/false,
6162
});
6263
}
6364
return candidates;

0 commit comments

Comments
 (0)