peer_scores.go 989 Bytes
Newer Older
1 2 3 4 5 6 7 8
package p2p

import (
	log "github.com/ethereum/go-ethereum/log"
	pubsub "github.com/libp2p/go-libp2p-pubsub"
)

// ConfigurePeerScoring configures the peer scoring parameters for the pubsub
9
func ConfigurePeerScoring(gossipConf GossipSetupConfigurables, scorer Scorer, log log.Logger) []pubsub.Option {
10 11
	// If we want to completely disable scoring config here, we can use the [peerScoringParams]
	// to return early without returning any [pubsub.Option].
12
	scoreParams := gossipConf.PeerScoringParams()
Andreas Bigger's avatar
Andreas Bigger committed
13
	opts := []pubsub.Option{}
14 15
	if scoreParams != nil {
		peerScoreThresholds := NewPeerScoreThresholds()
16 17 18
		// Create copy of params before modifying the AppSpecificScore
		params := scoreParams.PeerScoring
		params.AppSpecificScore = scorer.ApplicationScore
Andreas Bigger's avatar
Andreas Bigger committed
19
		opts = []pubsub.Option{
20
			pubsub.WithPeerScore(&params, &peerScoreThresholds),
Andreas Bigger's avatar
Andreas Bigger committed
21 22 23
			pubsub.WithPeerScoreInspect(scorer.SnapshotHook(), peerScoreInspectFrequency),
		}
	} else {
24
		log.Info("Peer scoring disabled")
25 26 27
	}
	return opts
}