Skip to content

Commit a3c2707

Browse files
committed
[net] Add connection type to NodeEvictionCandidate
1 parent 42aa5d5 commit a3c2707

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

Diff for: src/net.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,15 @@ void ProtectNoBanConnections(std::vector<NodeEvictionCandidate>& eviction_candid
957957
eviction_candidates.end());
958958
}
959959

960+
void ProtectOutboundConnections(std::vector<NodeEvictionCandidate>& eviction_candidates)
961+
{
962+
eviction_candidates.erase(std::remove_if(eviction_candidates.begin(), eviction_candidates.end(),
963+
[](NodeEvictionCandidate const& n) {
964+
return n.m_conn_type != ConnectionType::INBOUND;
965+
}),
966+
eviction_candidates.end());
967+
}
968+
960969
void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& eviction_candidates)
961970
{
962971
// Protect the half of the remaining nodes which have been connected the longest.
@@ -1036,6 +1045,8 @@ void ProtectEvictionCandidatesByRatio(std::vector<NodeEvictionCandidate>& evicti
10361045

10371046
ProtectNoBanConnections(vEvictionCandidates);
10381047

1048+
ProtectOutboundConnections(vEvictionCandidates);
1049+
10391050
// Deterministically select 4 peers to protect by netgroup.
10401051
// An attacker cannot predict which netgroups will be protected
10411052
EraseLastKElements(vEvictionCandidates, CompareNetGroupKeyed, 4);
@@ -1107,8 +1118,6 @@ bool CConnman::AttemptToEvictConnection()
11071118

11081119
LOCK(m_nodes_mutex);
11091120
for (const CNode* node : m_nodes) {
1110-
if (!node->IsInboundConn())
1111-
continue;
11121121
if (node->fDisconnect)
11131122
continue;
11141123
NodeEvictionCandidate candidate{
@@ -1125,6 +1134,7 @@ bool CConnman::AttemptToEvictConnection()
11251134
Desig(m_is_local) node->addr.IsLocal(),
11261135
Desig(m_network) node->ConnectedThroughNetwork(),
11271136
Desig(m_noban) node->HasPermission(NetPermissionFlags::NoBan),
1137+
Desig(m_conn_type) node->m_conn_type,
11281138
};
11291139
vEvictionCandidates.push_back(candidate);
11301140
}

Diff for: src/net.h

+1
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ struct NodeEvictionCandidate
12621262
bool m_is_local;
12631263
Network m_network;
12641264
bool m_noban;
1265+
ConnectionType m_conn_type;
12651266
};
12661267

12671268
/**

Diff for: src/test/fuzz/node_eviction.cpp

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

Diff for: src/test/util/net.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(int n_candida
5959
/*m_is_local=*/random_context.randbool(),
6060
/*m_network=*/ALL_NETWORKS[random_context.randrange(ALL_NETWORKS.size())],
6161
/*m_noban=*/false,
62+
/*m_conn_type=*/ConnectionType::INBOUND,
6263
});
6364
}
6465
return candidates;

0 commit comments

Comments
 (0)