Commit 3b5c2fc6 authored by felipe's avatar felipe Committed by GitHub

feat(op-node): clean peer state when disconnectPeer is called and log intercept blocks (#9706)

* feat(op-node): clean peer state when disconnectPeer is called and log intercept blocks

* nit
parent ae1b66a2
......@@ -98,21 +98,39 @@ func (g *ExpiryConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) {
if !g.BlockingConnectionGater.InterceptPeerDial(p) {
return false
}
return g.peerBanExpiryCheck(p)
peerBan := g.peerBanExpiryCheck(p)
if !peerBan {
log.Warn("peer is temporarily banned", "peer_id", p)
}
return peerBan
}
func (g *ExpiryConnectionGater) InterceptAddrDial(id peer.ID, ma multiaddr.Multiaddr) (allow bool) {
if !g.BlockingConnectionGater.InterceptAddrDial(id, ma) {
return false
}
return g.peerBanExpiryCheck(id) && g.addrBanExpiryCheck(ma)
peerBan := g.peerBanExpiryCheck(id)
if !peerBan {
log.Warn("peer id is temporarily banned", "peer_id", id, "multi_addr", ma)
return false
}
addrBan := g.addrBanExpiryCheck(ma)
if !addrBan {
log.Warn("peer address is temporarily banned", "peer_id", id, "multi_addr", ma)
return false
}
return true
}
func (g *ExpiryConnectionGater) InterceptAccept(mas network.ConnMultiaddrs) (allow bool) {
if !g.BlockingConnectionGater.InterceptAccept(mas) {
return false
}
return g.addrBanExpiryCheck(mas.RemoteMultiaddr())
addrBan := g.addrBanExpiryCheck(mas.RemoteMultiaddr())
if !addrBan {
log.Warn("peer address is temporarily banned", "multi_addr", mas.RemoteMultiaddr())
}
return addrBan
}
func (g *ExpiryConnectionGater) InterceptSecured(direction network.Direction, id peer.ID, mas network.ConnMultiaddrs) (allow bool) {
......@@ -125,5 +143,9 @@ func (g *ExpiryConnectionGater) InterceptSecured(direction network.Direction, id
}
// InterceptSecured is called after InterceptAccept, we already checked the addrs.
// This leaves just the peer-ID expiry to check on inbound connections.
return g.peerBanExpiryCheck(id)
peerBan := g.peerBanExpiryCheck(id)
if !peerBan {
log.Warn("peer id is temporarily banned", "peer_id", id, "multi_addr", mas.RemoteMultiaddr())
}
return peerBan
}
package gating
import (
"github.com/ethereum/go-ethereum/log"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
......@@ -22,22 +23,43 @@ func AddScoring(gater BlockingConnectionGater, scores Scores, minScore float64)
return &ScoringConnectionGater{BlockingConnectionGater: gater, scores: scores, minScore: minScore}
}
func (g *ScoringConnectionGater) checkScore(p peer.ID) (allow bool) {
func (g *ScoringConnectionGater) checkScore(p peer.ID) (allow bool, score float64) {
score, err := g.scores.GetPeerScore(p)
if err != nil {
return false
return false, score
}
return score >= g.minScore
return score >= g.minScore, score
}
func (g *ScoringConnectionGater) InterceptPeerDial(p peer.ID) (allow bool) {
return g.BlockingConnectionGater.InterceptPeerDial(p) && g.checkScore(p)
if !g.BlockingConnectionGater.InterceptPeerDial(p) {
return false
}
check, score := g.checkScore(p)
if !check {
log.Warn("peer has failed checkScore", "peer_id", p, "score", score, "min_score", g.minScore)
}
return check
}
func (g *ScoringConnectionGater) InterceptAddrDial(id peer.ID, ma multiaddr.Multiaddr) (allow bool) {
return g.BlockingConnectionGater.InterceptAddrDial(id, ma) && g.checkScore(id)
if !g.BlockingConnectionGater.InterceptAddrDial(id, ma) {
return false
}
check, score := g.checkScore(id)
if !check {
log.Warn("peer has failed checkScore", "peer_id", id, "score", score, "min_score", g.minScore)
}
return check
}
func (g *ScoringConnectionGater) InterceptSecured(dir network.Direction, id peer.ID, mas network.ConnMultiaddrs) (allow bool) {
return g.BlockingConnectionGater.InterceptSecured(dir, id, mas) && g.checkScore(id)
if !g.BlockingConnectionGater.InterceptSecured(dir, id, mas) {
return false
}
check, score := g.checkScore(id)
if !check {
log.Warn("peer has failed checkScore", "peer_id", id, "score", score, "min_score", g.minScore)
}
return check
}
......@@ -208,11 +208,16 @@ func TestP2PFull(t *testing.T) {
require.Equal(t, uint(1), stats.Connected)
// disconnect
hostBId := hostB.ID().String()
peerDump, err = p2pClientA.Peers(ctx, false)
require.Nil(t, err)
data = peerDump.Peers[hostBId]
require.NotNil(t, data)
require.NoError(t, p2pClientA.DisconnectPeer(ctx, hostB.ID()))
peerDump, err = p2pClientA.Peers(ctx, false)
require.Nil(t, err)
data = peerDump.Peers[hostB.ID().String()]
require.Equal(t, data.Connectedness, network.NotConnected)
data = peerDump.Peers[hostBId]
require.Nil(t, data)
// reconnect
addrsB, err := peer.AddrInfoToP2pAddrs(&peer.AddrInfo{ID: hostB.ID(), Addrs: hostB.Addrs()})
......
......@@ -378,5 +378,9 @@ func (s *APIBackend) ConnectPeer(ctx context.Context, addr string) error {
func (s *APIBackend) DisconnectPeer(_ context.Context, id peer.ID) error {
recordDur := s.m.RecordRPCServerRequest("opp2p_disconnectPeer")
defer recordDur()
return s.node.Host().Network().ClosePeer(id)
err := s.node.Host().Network().ClosePeer(id)
ps := s.node.Host().Peerstore()
ps.RemovePeer(id)
ps.ClearAddrs(id)
return err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment