scorers

package
v0.0.1-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: Apache-2.0, MIT, GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBadResponsesThreshold defines how many bad responses to tolerate before peer is deemed bad.
	DefaultBadResponsesThreshold = 6
	// DefaultBadResponsesDecayInterval defines how often to decay previous statistics.
	// Every interval bad responses counter will be decremented by 1.
	DefaultBadResponsesDecayInterval = time.Hour
	// DefaultBadResponsesPenaltyFactor defines the penalty factor applied to a peer based on their bad
	// response count.
	DefaultBadResponsesPenaltyFactor = 10
)
View Source
const (
	// DefaultBlockProviderProcessedBatchWeight is a default reward weight of a processed batch of blocks.
	DefaultBlockProviderProcessedBatchWeight = float64(0.1)
	// DefaultBlockProviderProcessedBlocksCap defines default value for processed blocks cap.
	// e.g. 20 * 64 := 20 batches of size 64 (with 0.05 per batch reward, 20 batches result in score of 1.0).
	DefaultBlockProviderProcessedBlocksCap = uint64(10 * 64)
	// DefaultBlockProviderDecayInterval defines how often the decaying routine is called.
	DefaultBlockProviderDecayInterval = 30 * time.Second
	// DefaultBlockProviderDecay defines default blocks that are to be subtracted from stats on each
	// decay interval. Effectively, this param provides minimum expected performance for a peer to remain
	// high scorer.
	DefaultBlockProviderDecay = uint64(1 * 64)
	// DefaultBlockProviderStalePeerRefreshInterval defines default interval at which peers should be given
	// opportunity to provide blocks (their score gets boosted, up until they are selected for
	// fetching).
	DefaultBlockProviderStalePeerRefreshInterval = 5 * time.Minute
)
View Source
const BadPeerScore = gossipThreshold

BadPeerScore defines score that is returned for a bad peer (all other metrics are ignored). The bad peer score was decided to be based on our determined gossip threshold, so that all the other scoring services have their relevant penalties on similar scales.

View Source
const ScoreRoundingFactor = 10000

ScoreRoundingFactor defines how many digits to keep in decimal part. This parameter is used in math.Round(score*ScoreRoundingFactor) / ScoreRoundingFactor.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadResponsesScorer

type BadResponsesScorer struct {
	// contains filtered or unexported fields
}

BadResponsesScorer represents bad responses scoring service.

func (*BadResponsesScorer) BadPeers

func (s *BadResponsesScorer) BadPeers() []peer.ID

BadPeers returns the peers that are considered bad.

func (*BadResponsesScorer) Count

func (s *BadResponsesScorer) Count(pid peer.ID) (int, error)

Count obtains the number of bad responses we have received from the given remote peer.

func (*BadResponsesScorer) Decay

func (s *BadResponsesScorer) Decay()

Decay reduces the bad responses of all peers, giving reformed peers a chance to join the network. This can be run periodically, although note that each time it runs it does give all bad peers another chance as well to clog up the network with bad responses, so should not be run too frequently; once an hour would be reasonable.

func (*BadResponsesScorer) Increment

func (s *BadResponsesScorer) Increment(pid peer.ID)

Increment increments the number of bad responses we have received from the given remote peer. If peer doesn't exist this method is no-op.

func (*BadResponsesScorer) IsBadPeer

func (s *BadResponsesScorer) IsBadPeer(pid peer.ID) bool

IsBadPeer states if the peer is to be considered bad. If the peer is unknown this will return `false`, which makes using this function easier than returning an error.

func (*BadResponsesScorer) Params

Params exposes scorer's parameters.

func (*BadResponsesScorer) Score

func (s *BadResponsesScorer) Score(pid peer.ID) float64

Score returns score (penalty) of bad responses peer produced.

type BadResponsesScorerConfig

type BadResponsesScorerConfig struct {
	// Threshold specifies number of bad responses tolerated, before peer is banned.
	Threshold int
	// DecayInterval specifies how often bad response stats should be decayed.
	DecayInterval time.Duration
}

BadResponsesScorerConfig holds configuration parameters for bad response scoring service.

type BlockProviderScorer

type BlockProviderScorer struct {
	// contains filtered or unexported fields
}

BlockProviderScorer represents block provider scoring service.

func (*BlockProviderScorer) BadPeers

func (_ *BlockProviderScorer) BadPeers() []peer.ID

BadPeers returns the peers that are considered bad. No peers are considered bad by block providers scorer.

func (*BlockProviderScorer) Decay

func (s *BlockProviderScorer) Decay()

Decay updates block provider counters by decaying them. This urges peers to keep up the performance to continue getting a high score (and allows new peers to contest previously high scoring ones).

func (*BlockProviderScorer) FormatScorePretty

func (s *BlockProviderScorer) FormatScorePretty(pid peer.ID) string

FormatScorePretty returns full scoring information in a human-readable format.

func (*BlockProviderScorer) IncrementProcessedBlocks

func (s *BlockProviderScorer) IncrementProcessedBlocks(pid peer.ID, cnt uint64)

IncrementProcessedBlocks increments the number of blocks that have been successfully processed.

func (*BlockProviderScorer) IsBadPeer

func (_ *BlockProviderScorer) IsBadPeer(_ peer.ID) bool

IsBadPeer states if the peer is to be considered bad. Block provider scorer cannot guarantee that lower score of a peer is indeed a sign of a bad peer. Therefore this scorer never marks peers as bad, and relies on scores to probabilistically sort out low-scorers (see WeightSorted method).

func (*BlockProviderScorer) MaxScore

func (s *BlockProviderScorer) MaxScore() float64

MaxScore exposes maximum score attainable by peers.

func (*BlockProviderScorer) Params

Params exposes scorer's parameters.

func (*BlockProviderScorer) ProcessedBlocks

func (s *BlockProviderScorer) ProcessedBlocks(pid peer.ID) uint64

ProcessedBlocks returns number of peer returned blocks that are successfully processed.

func (*BlockProviderScorer) Score

func (s *BlockProviderScorer) Score(pid peer.ID) float64

Score calculates and returns block provider score.

func (*BlockProviderScorer) Sorted

func (s *BlockProviderScorer) Sorted(
	pids []peer.ID, scoreFn func(pid peer.ID, score float64) float64,
) []peer.ID

Sorted returns a list of block providers sorted by score in descending order. When custom scorer function is provided, items are returned in order provided by it.

func (*BlockProviderScorer) Touch

func (s *BlockProviderScorer) Touch(pid peer.ID, t ...time.Time)

Touch updates last access time for a given peer. This allows to detect peers that are stale and boost their scores to increase chances in block fetching participation.

func (*BlockProviderScorer) WeightSorted

func (s *BlockProviderScorer) WeightSorted(
	r *rand.Rand, pids []peer.ID, scoreFn func(pid peer.ID, score float64) float64,
) []peer.ID

WeightSorted returns a list of block providers weight sorted by score, where items are selected probabilistically with more "heavy" items having a higher chance of being picked.

type BlockProviderScorerConfig

type BlockProviderScorerConfig struct {
	// ProcessedBatchWeight defines a reward for a single processed batch of blocks.
	ProcessedBatchWeight float64
	// ProcessedBlocksCap defines the highest number of processed blocks that are counted towards peer's score.
	// Once that cap is attained, peer is considered good to fetch from (and several peers having the
	// same score, are picked at random). To stay at max score, peer must continue to perform, as
	// stats decays quickly.
	ProcessedBlocksCap uint64
	// DecayInterval defines how often stats should be decayed.
	DecayInterval time.Duration
	// Decay specifies number of blocks subtracted from stats on each decay step.
	Decay uint64
	// StalePeerRefreshInterval is an interval at which peers should be given an opportunity
	// to provide blocks (scores are boosted to max up until such peers are selected).
	StalePeerRefreshInterval time.Duration
}

BlockProviderScorerConfig holds configuration parameters for block providers scoring service.

type Config

type Config struct {
	BadResponsesScorerConfig  *BadResponsesScorerConfig
	BlockProviderScorerConfig *BlockProviderScorerConfig
	PeerStatusScorerConfig    *PeerStatusScorerConfig
	GossipScorerConfig        *GossipScorerConfig
}

Config holds configuration parameters for scoring service.

type GossipScorer

type GossipScorer struct {
	// contains filtered or unexported fields
}

GossipScorer represents scorer that evaluates peers based on their gossip performance. Gossip scoring metrics are periodically calculated in libp2p's internal pubsub module.

func (*GossipScorer) BadPeers

func (s *GossipScorer) BadPeers() []peer.ID

BadPeers returns the peers that are considered bad.

func (*GossipScorer) GossipData

func (s *GossipScorer) GossipData(pid peer.ID) (float64, float64, map[string]*pbrpc.TopicScoreSnapshot, error)

GossipData gets the gossip related information of the given remote peer. This can return nil if there is no known gossip record the peer. This will error if the peer does not exist.

func (*GossipScorer) IsBadPeer

func (s *GossipScorer) IsBadPeer(pid peer.ID) bool

IsBadPeer states if the peer is to be considered bad.

func (*GossipScorer) Score

func (s *GossipScorer) Score(pid peer.ID) float64

Score returns calculated peer score.

func (*GossipScorer) SetGossipData

func (s *GossipScorer) SetGossipData(pid peer.ID, gScore float64,
	bPenalty float64, topicScores map[string]*pbrpc.TopicScoreSnapshot)

SetGossipData sets the gossip related data of a peer.

type GossipScorerConfig

type GossipScorerConfig struct{}

GossipScorerConfig holds configuration parameters for gossip scoring service.

type PeerStatusScorer

type PeerStatusScorer struct {
	// contains filtered or unexported fields
}

PeerStatusScorer represents scorer that evaluates peers based on their statuses. Peer statuses are updated by regularly polling peers (see sync/rpc_status.go).

func (*PeerStatusScorer) BadPeers

func (s *PeerStatusScorer) BadPeers() []peer.ID

BadPeers returns the peers that are considered bad.

func (*PeerStatusScorer) IsBadPeer

func (s *PeerStatusScorer) IsBadPeer(pid peer.ID) bool

IsBadPeer states if the peer is to be considered bad.

func (*PeerStatusScorer) PeerStatus

func (s *PeerStatusScorer) PeerStatus(pid peer.ID) (*pb.Status, error)

PeerStatus gets the chain state of the given remote peer. This can return nil if there is no known chain state for the peer. This will error if the peer does not exist.

func (*PeerStatusScorer) Score

func (s *PeerStatusScorer) Score(pid peer.ID) float64

Score returns calculated peer score.

func (*PeerStatusScorer) SetHeadSlot

func (s *PeerStatusScorer) SetHeadSlot(slot primitives.Slot)

SetHeadSlot updates known head slot.

func (*PeerStatusScorer) SetPeerStatus

func (s *PeerStatusScorer) SetPeerStatus(pid peer.ID, chainState *pb.Status, validationError error)

SetPeerStatus sets chain state data for a given peer.

type PeerStatusScorerConfig

type PeerStatusScorerConfig struct{}

PeerStatusScorerConfig holds configuration parameters for peer status scoring service.

type Scorer

type Scorer interface {
	Score(pid peer.ID) float64
	IsBadPeer(pid peer.ID) bool
	BadPeers() []peer.ID
}

Scorer defines minimum set of methods every peer scorer must expose.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service manages peer scorers that are used to calculate overall peer score.

func NewService

func NewService(ctx context.Context, store *peerdata.Store, config *Config) *Service

NewService provides fully initialized peer scoring service.

func (*Service) ActiveScorersCount

func (s *Service) ActiveScorersCount() int

ActiveScorersCount returns number of scorers that can affect score (have non-zero weight).

func (*Service) BadPeers

func (s *Service) BadPeers() []peer.ID

BadPeers returns the peers that are considered bad by any of registered scorers.

func (*Service) BadResponsesScorer

func (s *Service) BadResponsesScorer() *BadResponsesScorer

BadResponsesScorer exposes bad responses scoring service.

func (*Service) BlockProviderScorer

func (s *Service) BlockProviderScorer() *BlockProviderScorer

BlockProviderScorer exposes block provider scoring service.

func (*Service) GossipScorer

func (s *Service) GossipScorer() *GossipScorer

GossipScorer exposes the peer's gossip scoring service.

func (*Service) IsBadPeer

func (s *Service) IsBadPeer(pid peer.ID) bool

IsBadPeer traverses all the scorers to see if any of them classifies peer as bad.

func (*Service) IsBadPeerNoLock

func (s *Service) IsBadPeerNoLock(pid peer.ID) bool

IsBadPeerNoLock is a lock-free version of IsBadPeer.

func (*Service) PeerStatusScorer

func (s *Service) PeerStatusScorer() *PeerStatusScorer

PeerStatusScorer exposes peer chain status scoring service.

func (*Service) Score

func (s *Service) Score(pid peer.ID) float64

Score returns calculated peer score across all tracked metrics.

func (*Service) ScoreNoLock

func (s *Service) ScoreNoLock(pid peer.ID) float64

ScoreNoLock is a lock-free version of Score.

func (*Service) ValidationError

func (s *Service) ValidationError(pid peer.ID) error

ValidationError returns peer data validation error, which potentially provides more information why peer is considered bad.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL