Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p: no peer reconnect if explicitly disconnected #2115

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ type Server struct {
checkpointAddPeer chan *conn

// State of run loop and listenLoop.
inboundHistory expHeap
inboundHistory expHeap
disconnectEnodeSet map[enode.ID]struct{}
MatusKysel marked this conversation as resolved.
Show resolved Hide resolved
}

type peerOpFunc func(map[enode.ID]*Peer)
Expand Down Expand Up @@ -513,6 +514,7 @@ func (srv *Server) Start() (err error) {
srv.removetrusted = make(chan *enode.Node)
srv.peerOp = make(chan peerOpFunc)
srv.peerOpDone = make(chan struct{})
srv.disconnectEnodeSet = make(map[enode.ID]struct{})

if err := srv.setupLocalNode(); err != nil {
return err
Expand Down Expand Up @@ -836,6 +838,9 @@ running:
case pd := <-srv.delpeer:
// A peer disconnected.
d := common.PrettyDuration(mclock.Now() - pd.created)
if !pd.requested && pd.err == DiscRequested {
srv.disconnectEnodeSet[pd.ID()] = struct{}{}
}
delete(peers, pd.ID())
srv.log.Debug("Removing p2p peer", "peercount", len(peers), "id", pd.ID(), "duration", d, "req", pd.requested, "err", pd.err)
srv.dialsched.peerRemoved(pd.rw)
Expand Down Expand Up @@ -889,6 +894,11 @@ func (srv *Server) addPeerChecks(peers map[enode.ID]*Peer, inboundCount int, c *
if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, c.caps) == 0 {
return DiscUselessPeer
}

if _, ok := srv.disconnectEnodeSet[c.node.ID()]; ok {
return errors.New("explicitly disconnected peer previously")
}

// Repeat the post-handshake checks because the
// peer set might have changed since those checks were performed.
return srv.postHandshakeChecks(peers, inboundCount, c)
Expand Down Expand Up @@ -967,6 +977,7 @@ func (srv *Server) checkInboundConn(remoteIP net.IP) error {
if remoteIP == nil {
return nil
}

// Reject connections that do not match NetRestrict.
if srv.NetRestrict != nil && !srv.NetRestrict.Contains(remoteIP) {
return fmt.Errorf("not in netrestrict list")
Expand Down