Documentation ¶
Overview ¶
Package peers provides information about peers at the eth2 protocol level. "Protocol level" is the level above the network level, so this layer never sees or interacts with (for example) hosts that are uncontactable due to being down, firewalled, etc. Instead, this works with peers that are contactable but may or may not be of the correct fork version, not currently required due to the number of current connections, etc.
A peer can have one of a number of states:
- connected if we are able to talk to the remote peer - connecting if we are attempting to be able to talk to the remote peer - disconnecting if we are attempting to stop being able to talk to the remote peer - disconnected if we are not able to talk to the remote peer
For convenience, there are two aggregate states expressed in functions:
- active if we are connecting or connected - inactive if we are disconnecting or disconnected
Peer information is persistent for the run of the service. This allows for collection of useful long-term statistics such as number of bad responses obtained from the peer, giving the basis for decisions to not talk to known-bad peers.
Index ¶
- Constants
- Variables
- type BadResponsesScorer
- func (s *BadResponsesScorer) BadPeers() []peer.ID
- func (s *BadResponsesScorer) Count(pid peer.ID) (int, error)
- func (s *BadResponsesScorer) Decay()
- func (s *BadResponsesScorer) Increment(pid peer.ID)
- func (s *BadResponsesScorer) IsBadPeer(pid peer.ID) bool
- func (s *BadResponsesScorer) Params() *BadResponsesScorerConfig
- func (s *BadResponsesScorer) Score(pid peer.ID) float64
- type BadResponsesScorerConfig
- type BlockProviderScorer
- func (s *BlockProviderScorer) Decay()
- func (s *BlockProviderScorer) FormatScorePretty(pid peer.ID) string
- func (s *BlockProviderScorer) IncrementProcessedBlocks(pid peer.ID, cnt uint64)
- func (s *BlockProviderScorer) MaxScore() float64
- func (s *BlockProviderScorer) Params() *BlockProviderScorerConfig
- func (s *BlockProviderScorer) ProcessedBlocks(pid peer.ID) uint64
- func (s *BlockProviderScorer) Score(pid peer.ID) float64
- func (s *BlockProviderScorer) Sorted(pids []peer.ID, scoreFn func(pid peer.ID, score float64) float64) []peer.ID
- func (s *BlockProviderScorer) Touch(pid peer.ID, t ...time.Time)
- func (s *BlockProviderScorer) WeightSorted(r *rand.Rand, pids []peer.ID, scoreFn func(pid peer.ID, score float64) float64) []peer.ID
- type BlockProviderScorerConfig
- type PeerConnectionState
- type PeerScorerConfig
- type PeerScorerManager
- type Status
- func (p *Status) Active() []peer.ID
- func (p *Status) Add(record *enr.Record, pid peer.ID, address ma.Multiaddr, ...)
- func (p *Status) Address(pid peer.ID) (ma.Multiaddr, error)
- func (p *Status) All() []peer.ID
- func (p *Status) Bad() []peer.ID
- func (p *Status) BestFinalized(maxPeers int, ourFinalizedEpoch uint64) (uint64, []peer.ID)
- func (p *Status) BestNonFinalized(minPeers int, ourFinalizedEpoch uint64) (uint64, []peer.ID)
- func (p *Status) ChainState(pid peer.ID) (*pb.Status, error)
- func (p *Status) ChainStateLastUpdated(pid peer.ID) (time.Time, error)
- func (p *Status) CommitteeIndices(pid peer.ID) ([]uint64, error)
- func (p *Status) Connected() []peer.ID
- func (p *Status) Connecting() []peer.ID
- func (p *Status) ConnectionState(pid peer.ID) (PeerConnectionState, error)
- func (p *Status) Direction(pid peer.ID) (network.Direction, error)
- func (p *Status) Disconnected() []peer.ID
- func (p *Status) Disconnecting() []peer.ID
- func (p *Status) ENR(pid peer.ID) (*enr.Record, error)
- func (p *Status) HighestEpoch() uint64
- func (p *Status) Inactive() []peer.ID
- func (p *Status) IsActive(pid peer.ID) bool
- func (p *Status) IsBad(pid peer.ID) bool
- func (p *Status) MaxPeerLimit() int
- func (p *Status) Metadata(pid peer.ID) (*pb.MetaData, error)
- func (p *Status) Prune()
- func (p *Status) Scorers() *PeerScorerManager
- func (p *Status) SetChainState(pid peer.ID, chainState *pb.Status)
- func (p *Status) SetConnectionState(pid peer.ID, state PeerConnectionState)
- func (p *Status) SetMetadata(pid peer.ID, metaData *pb.MetaData)
- func (p *Status) SubscribedToSubnet(index uint64) []peer.ID
- type StatusConfig
Constants ¶
const ( // DefaultBadResponsesThreshold defines how many bad responses to tolerate before peer is deemed bad. DefaultBadResponsesThreshold = 6 // DefaultBadResponsesWeight is a default weight. Since score represents penalty, it has negative weight. DefaultBadResponsesWeight = -1.0 // DefaultBadResponsesDecayInterval defines how often to decay previous statistics. // Every interval bad responses counter will be decremented by 1. DefaultBadResponsesDecayInterval = time.Hour )
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 )
const ScoreRoundingFactor = 10000
ScoreRoundingFactor defines how many digits to keep in decimal part. This parameter is used in math.Round(score*ScoreRoundingFactor) / ScoreRoundingFactor.
Variables ¶
var ( // ErrPeerUnknown is returned when there is an attempt to obtain data from a peer that is not known. ErrPeerUnknown = errors.New("peer unknown") )
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 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 ¶
func (s *BadResponsesScorer) Params() *BadResponsesScorerConfig
Params exposes scorer's parameters.
type BadResponsesScorerConfig ¶
type BadResponsesScorerConfig struct { // Threshold specifies number of bad responses tolerated, before peer is banned. Threshold int // Weight defines weight of bad response/threshold ratio on overall score. Weight float64 // 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) 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) MaxScore ¶
func (s *BlockProviderScorer) MaxScore() float64
MaxScore exposes maximum score attainable by peers.
func (*BlockProviderScorer) Params ¶
func (s *BlockProviderScorer) Params() *BlockProviderScorerConfig
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 PeerConnectionState ¶
type PeerConnectionState ethpb.ConnectionState
PeerConnectionState is the state of the connection.
const ( // PeerDisconnected means there is no connection to the peer. PeerDisconnected PeerConnectionState = iota // PeerDisconnecting means there is an on-going attempt to disconnect from the peer. PeerDisconnecting // PeerConnected means the peer has an active connection. PeerConnected // PeerConnecting means there is an on-going attempt to connect to the peer. PeerConnecting )
type PeerScorerConfig ¶
type PeerScorerConfig struct { BadResponsesScorerConfig *BadResponsesScorerConfig BlockProviderScorerConfig *BlockProviderScorerConfig }
PeerScorerConfig holds configuration parameters for scoring service.
type PeerScorerManager ¶
type PeerScorerManager struct {
// contains filtered or unexported fields
}
PeerScorerManager keeps track of peer scorers that are used to calculate overall peer score.
func (*PeerScorerManager) BadResponsesScorer ¶
func (m *PeerScorerManager) BadResponsesScorer() *BadResponsesScorer
BadResponsesScorer exposes bad responses scoring service.
func (*PeerScorerManager) BlockProviderScorer ¶
func (m *PeerScorerManager) BlockProviderScorer() *BlockProviderScorer
BlockProviderScorer exposes block provider scoring service.
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status is the structure holding the peer status information.
func NewStatus ¶
func NewStatus(ctx context.Context, config *StatusConfig) *Status
NewStatus creates a new status entity.
func (*Status) Add ¶
func (p *Status) Add(record *enr.Record, pid peer.ID, address ma.Multiaddr, direction network.Direction)
Add adds a peer. If a peer already exists with this ID its address and direction are updated with the supplied data.
func (*Status) Address ¶
Address returns the multiaddress of the given remote peer. This will error if the peer does not exist.
func (*Status) BestFinalized ¶
BestFinalized returns the highest finalized epoch equal to or higher than ours that is agreed upon by the majority of peers. This method may not return the absolute highest finalized, but the finalized epoch in which most peers can serve blocks. Ideally, all peers would be reporting the same finalized epoch but some may be behind due to their own latency, or because of their finalized epoch at the time we queried them. Returns the best finalized root, epoch number, and list of peers that are at or beyond that epoch.
func (*Status) BestNonFinalized ¶ added in v1.0.0
BestNonFinalized returns the highest known epoch, which is higher than ours, and is shared by at least minPeers.
func (*Status) ChainState ¶
ChainState 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 (*Status) ChainStateLastUpdated ¶
ChainStateLastUpdated gets the last time the chain state of the given remote peer was updated. This will error if the peer does not exist.
func (*Status) CommitteeIndices ¶ added in v0.3.8
CommitteeIndices retrieves the committee subnets the peer is subscribed to.
func (*Status) Connecting ¶
Connecting returns the peers that are connecting.
func (*Status) ConnectionState ¶
func (p *Status) ConnectionState(pid peer.ID) (PeerConnectionState, error)
ConnectionState gets the connection state of the given remote peer. This will error if the peer does not exist.
func (*Status) Direction ¶
Direction returns the direction of the given remote peer. This will error if the peer does not exist.
func (*Status) Disconnected ¶
Disconnected returns the peers that are disconnected.
func (*Status) Disconnecting ¶
Disconnecting returns the peers that are disconnecting.
func (*Status) HighestEpoch ¶ added in v1.0.0
HighestEpoch returns the highest epoch reported epoch amongst peers.
func (*Status) IsActive ¶ added in v0.3.8
IsActive checks if a peers is active and returns the result appropriately.
func (*Status) IsBad ¶
IsBad 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 (*Status) MaxPeerLimit ¶ added in v1.0.0
MaxPeerLimit returns the max peer limit stored in the current peer store.
func (*Status) Metadata ¶ added in v1.0.0
Metadata returns a copy of the metadata corresponding to the provided peer id.
func (*Status) Prune ¶ added in v1.0.0
func (p *Status) Prune()
Prune clears out and removes outdated and disconnected peers.
func (*Status) Scorers ¶ added in v1.0.0
func (p *Status) Scorers() *PeerScorerManager
Scorers exposes peer scoring management service.
func (*Status) SetChainState ¶
SetChainState sets the chain state of the given remote peer.
func (*Status) SetConnectionState ¶
func (p *Status) SetConnectionState(pid peer.ID, state PeerConnectionState)
SetConnectionState sets the connection state of the given remote peer.
func (*Status) SetMetadata ¶ added in v1.0.0
SetMetadata sets the metadata of the given remote peer.
type StatusConfig ¶ added in v1.0.0
type StatusConfig struct { // PeerLimit specifies maximum amount of concurrent peers that are expected to be connect to the node. PeerLimit int // ScorerParams holds peer scorer configuration params. ScorerParams *PeerScorerConfig }
StatusConfig represents peer status service params.