Skip to content

Commit

Permalink
p2p: no peer reconnect if explicitly disconnected
Browse files Browse the repository at this point in the history
based on enode
  • Loading branch information
weiihann committed Jan 3, 2024
1 parent 267c5c0 commit 085a15c
Showing 1 changed file with 12 additions and 1 deletion.
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{}
}

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

0 comments on commit 085a15c

Please sign in to comment.