net: nodes with ForceInbound permission force eviction

Github-Pull: #27600
Rebased-From: 8bc203073a0b8b7e1a6851bcf3f1688e54b10a73
This commit is contained in:
Matthew Zipkin 2023-05-08 15:42:04 -04:00 committed by Luke Dashjr
parent 711dadb546
commit 067f80e1b5
2 changed files with 13 additions and 4 deletions

View File

@ -1653,7 +1653,7 @@ std::pair<size_t, bool> CConnman::SocketSendData(CNode& node) const
* to forge. In order to partition a node the attacker must be
* simultaneously better at all of them than honest peers.
*/
bool CConnman::AttemptToEvictConnection()
bool CConnman::AttemptToEvictConnection(bool force)
{
std::vector<NodeEvictionCandidate> vEvictionCandidates;
{
@ -1681,7 +1681,7 @@ bool CConnman::AttemptToEvictConnection()
vEvictionCandidates.push_back(candidate);
}
}
const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates));
const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates), force);
if (!node_id_to_evict) {
return false;
}
@ -1776,7 +1776,9 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
if (nInbound >= m_max_inbound)
{
if (!AttemptToEvictConnection()) {
// If the inbound connection attempt is granted ForceInbound permission, try a little harder
// to make room by evicting a peer we may not have otherwise evicted.
if (!AttemptToEvictConnection(NetPermissions::HasFlag(permission_flags, NetPermissionFlags::ForceInbound))) {
// No connection to evict, disconnect the new connection
LogPrint(BCLog::NET, "failed to find an eviction candidate - connection dropped (full)\n");
return;

View File

@ -1338,7 +1338,14 @@ private:
*/
bool AlreadyConnectedToAddress(const CAddress& addr);
bool AttemptToEvictConnection();
/**
* Attempt to disconnect a connected peer.
* Used to make room for new inbound connections, returns true if successful.
* @param[in] force Try to evict a random inbound ban-able peer if
* all connections are otherwise protected.
*/
bool AttemptToEvictConnection(bool force);
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure, ConnectionType conn_type, bool use_v2transport) EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex);
void AddWhitelistPermissionFlags(NetPermissionFlags& flags, const CNetAddr &addr, const std::vector<NetWhitelistPermissions>& ranges) const;