neo icon indicating copy to clipboard operation
neo copied to clipboard

Restore UnconnectedPeers if OnTcpCommandFailed

Open vncoelho opened this issue 9 months ago • 3 comments

It looks like that when a node attempts to connect to a new peer, it moves the peer's endpoint from the UnconnectedPeers set to the ConnectingPeers set.

If this TCP connection fails, the OnTcpCommandFailed method correctly removes the endpoint from ConnectingPeers. However, the endpoint is not returned to the UnconnectedPeers set.

https://github.com/neo-project/neo/blob/9b9be47357e9065de524005755212ed54c3f6a11/src/Neo/Network/P2P/Peer.cs#L277

vncoelho avatar Jul 21 '25 13:07 vncoelho

So, maybe change to

            switch (cmd)
            {
                case Tcp.Connect connect:
                    var endpoint = ((IPEndPoint)connect.RemoteAddress).UnMap();
                    ImmutableInterlocked.Update(ref ConnectingPeers, p => p.Remove(endpoint));
                    // Add the failed endpoint back to the pool of unconnected peers to prevent peer-draining attacks.
                    // A simple re-add is shown here. A more advanced implementation would use a timed backoff.
                    ImmutableInterlocked.Update(ref UnconnectedPeers, p => p.Add(endpoint));
                    break;
            }
        }

vncoelho avatar Jul 21 '25 13:07 vncoelho

Can you verify that assumption, if that proceed I will apply the change and test here?

vncoelho avatar Jul 21 '25 13:07 vncoelho

I will check it

shargon avatar Jul 28 '25 07:07 shargon