Documentation ¶
Index ¶
- Constants
- func DefaultDecayFunction(cfg p2pconfig.SpamRecordCacheDecay) netcache.PreprocessorFunc
- func GeometricDecay(score float64, decay float64, lastUpdated time.Time) (float64, error)
- func HasValidFlowIdentity(idProvider module.IdentityProvider, pid peer.ID) (*flow.Identity, error)
- func InitAppScoreRecordStateFunc(maximumSpamPenaltyDecayFactor float64) func() p2p.GossipSubSpamRecord
- func IsInvalidPeerIDError(this error) bool
- func NewInvalidPeerIDError(peerId peer.ID, status peerIdStatus) error
- type GossipSubAppSpecificScoreRegistry
- type GossipSubAppSpecificScoreRegistryConfig
- type InvalidPeerIDError
- type NoopInvCtrlMsgNotifConsumer
- type ScoreOption
- type ScoreOptionConfig
- type SpamRecordInitFunc
- type SubscriptionCache
- type SubscriptionProvider
- type SubscriptionProviderConfig
- type SubscriptionValidator
Constants ¶
const ( // PeerIdStatusUnknown indicates that the peer id is unknown. PeerIdStatusUnknown peerIdStatus = "unknown identity" // PeerIdStatusEjected indicates that the peer id belongs to an identity that has been ejected. PeerIdStatusEjected peerIdStatus = "ejected identity" )
const (
// NotificationSilencedMsg log messages for silenced notifications
NotificationSilencedMsg = "ignoring invalid control message notification for peer during silence period"
)
Variables ¶
This section is empty.
Functions ¶
func DefaultDecayFunction ¶ added in v0.31.0
func DefaultDecayFunction(cfg p2pconfig.SpamRecordCacheDecay) netcache.PreprocessorFunc
DefaultDecayFunction is the default decay function that is used to decay the application specific penalty of a peer. It is used if no decay function is provided in the configuration. It decays the application specific penalty of a peer if it is negative.
func GeometricDecay ¶ added in v0.31.0
GeometricDecay returns the decayed score based on the decay factor and the time since the last update.
The decayed score is calculated as follows: penalty = score * decay^t where t is the time since the last update in seconds. Args: - score: the score to be decayed. - decay: the decay factor, it should be in the range of (0, 1]. - lastUpdated: the time when the penalty was last updated. Returns:
- the decayed score.
- an error if the decay factor is not in the range of (0, 1] or the decayed score is NaN. it also returns an error if the last updated time is in the future (to avoid overflow). The error is considered irrecoverable (unless the parameters can be adjusted).
func HasValidFlowIdentity ¶ added in v0.28.1
HasValidFlowIdentity checks if the peer has a valid Flow identity.
func InitAppScoreRecordStateFunc ¶ added in v0.33.1
func InitAppScoreRecordStateFunc(maximumSpamPenaltyDecayFactor float64) func() p2p.GossipSubSpamRecord
InitAppScoreRecordStateFunc returns a callback that initializes the gossipsub spam record state for a peer. Returns:
- a func that returns a gossipsub spam record with the default decay value and 0 penalty.
func IsInvalidPeerIDError ¶
func NewInvalidPeerIDError ¶
Types ¶
type GossipSubAppSpecificScoreRegistry ¶ added in v0.31.0
type GossipSubAppSpecificScoreRegistry struct { component.Component // contains filtered or unexported fields }
GossipSubAppSpecificScoreRegistry is the registry for the application specific score of peers in the GossipSub protocol. The application specific score is part of the overall score of a peer, and is used to determine the peer's score based on its behavior related to the application (Flow protocol). This registry holds the view of the local peer of the application specific score of other peers in the network based on what it has observed from the network. Similar to the GossipSub score, the application specific score is meant to be private to the local peer, and is not shared with other peers in the network.
func NewGossipSubAppSpecificScoreRegistry ¶ added in v0.31.0
func NewGossipSubAppSpecificScoreRegistry(config *GossipSubAppSpecificScoreRegistryConfig) (*GossipSubAppSpecificScoreRegistry, error)
NewGossipSubAppSpecificScoreRegistry returns a new GossipSubAppSpecificScoreRegistry. Args:
config: the config for the registry.
Returns:
a new GossipSubAppSpecificScoreRegistry.
error: if the configuration is invalid, an error is returned; any returned error is an irrecoverable error and indicates a bug or misconfiguration.
func (*GossipSubAppSpecificScoreRegistry) AppSpecificScoreFunc ¶ added in v0.31.0
func (r *GossipSubAppSpecificScoreRegistry) AppSpecificScoreFunc() func(peer.ID) float64
AppSpecificScoreFunc returns the application specific score function that is called by the GossipSub protocol to determine the application specific score of a peer. The application specific score is part of the overall score of a peer, and is used to determine the peer's score based This function reads the application specific score of a peer from the cache, and if the penalty is not found in the cache, it computes it. If the score is not found in the cache, it is computed and added to the cache. Also if the score is expired, it is computed and added to the cache. Returns: - func(peer.ID) float64: the application specific score function. Implementation must be thread-safe.
func (*GossipSubAppSpecificScoreRegistry) OnInvalidControlMessageNotification ¶ added in v0.31.0
func (r *GossipSubAppSpecificScoreRegistry) OnInvalidControlMessageNotification(notification *p2p.InvCtrlMsgNotif)
OnInvalidControlMessageNotification is called when a new invalid control message notification is distributed. Any error on consuming event must handle internally. The implementation must be concurrency safe, but can be blocking. Note: there is no real-time guarantee on processing the notification.
type GossipSubAppSpecificScoreRegistryConfig ¶ added in v0.31.0
type GossipSubAppSpecificScoreRegistryConfig struct { Parameters p2pconfig.AppSpecificScoreParameters `validate:"required"` Logger zerolog.Logger `validate:"required"` // Validator is the subscription validator used to validate the subscriptions of peers, and determine if a peer is // authorized to subscribe to a topic. Validator p2p.SubscriptionValidator `validate:"required"` // Penalty encapsulates the penalty unit for each control message type misbehaviour. Penalty p2pconfig.MisbehaviourPenalties `validate:"required"` // IdProvider is the identity provider used to translate peer ids at the networking layer to Flow identifiers (if // an authorized peer is found). IdProvider module.IdentityProvider `validate:"required"` // GetDuplicateMessageCount callback used to get a gauge of the number of duplicate messages detected for each peer. GetDuplicateMessageCount func(id peer.ID) float64 // SpamRecordCacheFactory is a factory function that returns a new GossipSubSpamRecordCache. It is used to initialize the spamScoreCache. // The cache is used to store the application specific penalty of peers. SpamRecordCacheFactory func() p2p.GossipSubSpamRecordCache `validate:"required"` // AppScoreCacheFactory is a factory function that returns a new GossipSubApplicationSpecificScoreCache. It is used to initialize the appScoreCache. // The cache is used to store the application specific score of peers. AppScoreCacheFactory func() p2p.GossipSubApplicationSpecificScoreCache `validate:"required"` HeroCacheMetricsFactory metrics.HeroCacheMetricsFactory `validate:"required"` NetworkingType network.NetworkingType `validate:"required"` // ScoringRegistryStartupSilenceDuration defines the duration of time, after the node startup, // during which the scoring registry remains inactive before penalizing nodes. ScoringRegistryStartupSilenceDuration time.Duration AppSpecificScoreParams p2pconfig.ApplicationSpecificScoreParameters `validate:"required"` DuplicateMessageThreshold float64 `validate:"gt=0"` Collector module.GossipSubScoringRegistryMetrics `validate:"required"` }
GossipSubAppSpecificScoreRegistryConfig is the configuration for the GossipSubAppSpecificScoreRegistry. Configurations are the "union of parameters and other components" that are used to compute or build components that compute or maintain the application specific score of peers.
type InvalidPeerIDError ¶
type InvalidPeerIDError struct {
// contains filtered or unexported fields
}
InvalidPeerIDError indicates that a peer has an invalid peer id, i.e., it is not held by an authorized Flow identity.
func (InvalidPeerIDError) Error ¶
func (e InvalidPeerIDError) Error() string
type NoopInvCtrlMsgNotifConsumer ¶ added in v0.33.30
type NoopInvCtrlMsgNotifConsumer struct { }
NoopInvCtrlMsgNotifConsumer is a no-op implementation of the p2p.GossipSubInvCtrlMsgNotifConsumer interface. It is used to consume invalid control message notifications from the GossipSub pubsub system and take no action. It is mainly used for cases when the peer scoring system is disabled.
func NewNoopInvCtrlMsgNotifConsumer ¶ added in v0.33.30
func NewNoopInvCtrlMsgNotifConsumer() *NoopInvCtrlMsgNotifConsumer
func (NoopInvCtrlMsgNotifConsumer) OnInvalidControlMessageNotification ¶ added in v0.33.30
func (n NoopInvCtrlMsgNotifConsumer) OnInvalidControlMessageNotification(_ *p2p.InvCtrlMsgNotif)
type ScoreOption ¶
ScoreOption is a functional option for configuring the peer scoring system. TODO: rename it to ScoreManager.
func NewScoreOption ¶
func NewScoreOption(cfg *ScoreOptionConfig, provider p2p.SubscriptionProvider) (*ScoreOption, error)
NewScoreOption creates a new penalty option with the given configuration.
func (*ScoreOption) BuildFlowPubSubScoreOption ¶
func (s *ScoreOption) BuildFlowPubSubScoreOption() (*pubsub.PeerScoreParams, *pubsub.PeerScoreThresholds)
func (*ScoreOption) OnInvalidControlMessageNotification ¶ added in v0.33.30
func (s *ScoreOption) OnInvalidControlMessageNotification(notif *p2p.InvCtrlMsgNotif)
OnInvalidControlMessageNotification is called when a new invalid control message notification is distributed. Any error on consuming event must handle internally. The implementation must be concurrency safe and non-blocking. Note: there is no real-time guarantee on processing the notification.
func (*ScoreOption) TopicScoreParams ¶ added in v0.32.0
func (s *ScoreOption) TopicScoreParams(topic *pubsub.Topic) *pubsub.TopicScoreParams
TopicScoreParams returns the topic score parameters for the given topic. If the topic score parameters are not set, it returns the default topic score parameters. The custom topic parameters are set at the initialization of the score option. Args: - topic: the topic for which the score parameters are requested. Returns:
- the topic score parameters for the given topic, or the default topic score parameters if the topic score parameters are not set.
type ScoreOptionConfig ¶ added in v0.31.0
type ScoreOptionConfig struct {
// contains filtered or unexported fields
}
func NewScoreOptionConfig ¶ added in v0.31.0
func NewScoreOptionConfig(logger zerolog.Logger, params p2pconfig.ScoringParameters, hcMetricsFactory metrics.HeroCacheMetricsFactory, scoringRegistryMetricsCollector module.GossipSubScoringRegistryMetrics, idProvider module.IdentityProvider, getDuplicateMessageCount func(id peer.ID) float64, networkingType network.NetworkingType) *ScoreOptionConfig
NewScoreOptionConfig creates a new configuration for the GossipSub peer scoring option. Args: - logger: the logger to use. - hcMetricsFactory: HeroCache metrics factory to create metrics for the scoring-related caches. - idProvider: the identity provider to use. - networkingType: the networking type to use, public or private. Returns: - a new configuration for the GossipSub peer scoring option.
func (*ScoreOptionConfig) OverrideAppSpecificScoreFunction ¶ added in v0.32.0
func (c *ScoreOptionConfig) OverrideAppSpecificScoreFunction(appSpecificScoreFunction func(peer.ID) float64)
OverrideAppSpecificScoreFunction sets the app specific penalty function for the penalty option. It is used to calculate the app specific penalty of a peer. If the app specific penalty function is not set, the default one is used. Note that it is always safer to use the default one, unless you know what you are doing. It is safe to call this method multiple times, the last call will be used.
func (*ScoreOptionConfig) OverrideTopicScoreParams ¶ added in v0.32.0
func (c *ScoreOptionConfig) OverrideTopicScoreParams(topic channels.Topic, topicScoreParams *pubsub.TopicScoreParams)
OverrideTopicScoreParams overrides the topic score parameters for the given topic. It is used to override the default topic score parameters for a specific topic. If the topic score parameters are not set, the default ones will be used.
type SpamRecordInitFunc ¶ added in v0.33.1
type SpamRecordInitFunc func() p2p.GossipSubSpamRecord
type SubscriptionCache ¶ added in v0.33.1
type SubscriptionCache interface { // GetSubscribedTopics returns the list of topics a peer is subscribed to. // Returns: // - []string: the list of topics the peer is subscribed to. // - bool: true if there is a record for the peer, false otherwise. GetSubscribedTopics(pid peer.ID) ([]string, bool) // MoveToNextUpdateCycle moves the subscription cache to the next update cycle. // A new update cycle is started when the subscription cache is first created, and then every time the subscription cache // is updated. The update cycle is used to keep track of the last time the subscription cache was updated. It is used to // implement a notion of time in the subscription cache. // Returns: // - uint64: the current update cycle. MoveToNextUpdateCycle() uint64 // AddTopicForPeer appends a topic to the list of topics a peer is subscribed to. If the peer is not subscribed to any // topics yet, a new record is created. // If the last update cycle is older than the current cycle, the list of topics for the peer is first cleared, and then // the topic is added to the list. This is to ensure that the list of topics for a peer is always up to date. // Args: // - pid: the peer id of the peer. // - topic: the topic to add. // Returns: // - []string: the list of topics the peer is subscribed to after the update. // - error: an error if the update failed; any returned error is an irrecoverable error and indicates a bug or misconfiguration. // Implementation must be thread-safe. AddWithInitTopicForPeer(pid peer.ID, topic string) ([]string, error) }
SubscriptionCache implements an in-memory cache that keeps track of the topics a peer is subscribed to. The cache is modeled abstracted to be used in update cycles, i.e., every regular interval of time, the cache is updated for all peers.
type SubscriptionProvider ¶
SubscriptionProvider provides a list of topics a peer is subscribed to.
func NewSubscriptionProvider ¶
func NewSubscriptionProvider(cfg *SubscriptionProviderConfig) (*SubscriptionProvider, error)
func (*SubscriptionProvider) GetSubscribedTopics ¶
func (s *SubscriptionProvider) GetSubscribedTopics(pid peer.ID) []string
GetSubscribedTopics returns all the subscriptions of a peer within the pubsub network.
type SubscriptionProviderConfig ¶ added in v0.33.1
type SubscriptionProviderConfig struct { Logger zerolog.Logger `validate:"required"` TopicProviderOracle func() p2p.TopicProvider `validate:"required"` IdProvider module.IdentityProvider `validate:"required"` HeroCacheMetricsFactory metrics.HeroCacheMetricsFactory `validate:"required"` Params *p2pconfig.SubscriptionProviderParameters `validate:"required"` NetworkingType network.NetworkingType `validate:"required"` }
type SubscriptionValidator ¶
SubscriptionValidator validates that a peer is subscribed to topics that it is allowed to subscribe to. It is used to penalize peers that subscribe to topics that they are not allowed to subscribe to in GossipSub.
func NewSubscriptionValidator ¶
func NewSubscriptionValidator(logger zerolog.Logger, provider p2p.SubscriptionProvider) *SubscriptionValidator
func (*SubscriptionValidator) CheckSubscribedToAllowedTopics ¶
CheckSubscribedToAllowedTopics checks if a peer is subscribed to topics that it is allowed to subscribe to. Args:
pid: the peer ID of the peer to check role: the role of the peer to check
Returns: error: if the peer is subscribed to topics that it is not allowed to subscribe to, an InvalidSubscriptionError is returned. The error is benign, i.e., it does not indicate an illegal state in the execution of the code. We expect this error when there are malicious peers in the network. But such errors should not lead to a crash of the node.