prysm-pulse/shared/p2p/connection_manager.go
Preston Van Loon 991ee7e81b "Super sync" and naive p2p reputation (#2550)
* checkpoint on super sync with reputation

* ensure handling only expected peers msg

* exclusive of finalized block

* skip block saved already

* clean up struct

* remove 2 more fields

* _

* everything builds, but doesnt test yet

* lint

* fix p2p tests

* space

* space

* space

* fmt

* fmt
2019-05-09 16:02:24 -05:00

46 lines
1.2 KiB
Go

package p2p
import (
"math"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-connmgr"
"github.com/libp2p/go-libp2p-peer"
)
// Reputation reward values.
const (
RepRewardValidBlock = 4
RepRewardValidAttestation = 1
RepPenalityInvalidProtobuf = -1000
RepPenalityInitialSyncFailure = -500
RepPenalityInvalidBlock = -10
RepPenalityInvalidAttestation = -5
)
func optionConnectionManager(maxPeers int) libp2p.Option {
if maxPeers < 5 {
log.Warn("Max peers < 5. Defaulting to 5 max peers")
maxPeers = 5
}
minPeers := int(math.Max(5, float64(maxPeers-5)))
cm := connmgr.NewConnManager(minPeers, maxPeers, 5*time.Minute)
return libp2p.ConnectionManager(cm)
}
// Reputation adds (or subtracts) a given reward/penalty against a peer.
// Eventually, the lowest scoring peers will be pruned from the connections.
func (s *Server) Reputation(peer peer.ID, val int) {
s.host.ConnManager().TagPeer(peer, TagReputation, val)
}
// Disconnect will close all connections to the given peer.
func (s *Server) Disconnect(peer peer.ID) {
if err := s.host.Network().ClosePeer(peer); err != nil {
log.WithError(err).WithField("peer", peer.Pretty()).Error("Failed to close conn with peer")
}
}