prysm-pulse/shared/deprecated-p2p/connection_manager.go
Preston Van Loon 0b8cbd06b6
Add flag for testing new p2p (#3243)
* refactor a bit to select p2p

* lint

* fix build

* fix build

* fix build

* fix build

* fix build
2019-08-19 17:20:56 -04:00

51 lines
1.3 KiB
Go

package p2p
import (
"math"
"time"
"github.com/libp2p/go-libp2p"
connmgr "github.com/libp2p/go-libp2p-connmgr"
peer "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, 20*time.Second)
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) {
ti := s.host.ConnManager().GetTagInfo(peer)
if ti != nil {
val += ti.Value
}
s.host.ConnManager().TagPeer(peer, TagReputation, val)
}
// Disconnect will close all connections to the given peer.
func (s *Server) Disconnect(peer peer.ID) error {
if err := s.host.Network().ClosePeer(peer); err != nil {
log.WithError(err).WithField("peer", peer.Pretty()).Error("Failed to close conn with peer")
}
return nil
}