github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/p2p/config/gossipsub.go (about)

     1  package p2pconfig
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // ResourceManagerConfig returns the resource manager configuration for the libp2p node.
     8  // The resource manager is used to limit the number of open connections and streams (as well as any other resources
     9  // used by libp2p) for each peer.
    10  type ResourceManagerConfig struct {
    11  	Override             ResourceManagerOverrideScope `mapstructure:"limits-override"`        // override limits for specific peers, protocols, etc.
    12  	MemoryLimitRatio     float64                      `mapstructure:"memory-limit-ratio"`     // maximum allowed fraction of memory to be allocated by the libp2p resources in (0,1]
    13  	FileDescriptorsRatio float64                      `mapstructure:"file-descriptors-ratio"` // maximum allowed fraction of file descriptors to be allocated by the libp2p resources in (0,1]
    14  }
    15  
    16  type ResourceManagerOverrideScope struct {
    17  	// System is the limit for the resource at the entire system.
    18  	// For a specific limit, the system-wide dictates the maximum allowed value across all peers and protocols at the entire node level.
    19  	System ResourceManagerOverrideLimit `mapstructure:"system"`
    20  
    21  	// Transient is the limit for the resource at the transient scope. Transient limits are used for resources that have not fully established and are under negotiation.
    22  	Transient ResourceManagerOverrideLimit `mapstructure:"transient"`
    23  
    24  	// Protocol is the limit for the resource at the protocol scope, e.g., DHT, GossipSub, etc. It dictates the maximum allowed resource across all peers for that protocol.
    25  	Protocol ResourceManagerOverrideLimit `mapstructure:"protocol"`
    26  
    27  	// Peer is the limit for the resource at the peer scope. It dictates the maximum allowed resource for a specific peer.
    28  	Peer ResourceManagerOverrideLimit `mapstructure:"peer"`
    29  
    30  	// Connection is the limit for the resource for a pair of (peer, protocol), e.g., (peer1, DHT), (peer1, GossipSub), etc. It dictates the maximum allowed resource for a protocol and a peer.
    31  	PeerProtocol ResourceManagerOverrideLimit `mapstructure:"peer-protocol"`
    32  }
    33  
    34  // ResourceManagerOverrideLimit is the configuration for the resource manager override limit at a certain scope.
    35  // Any value that is not set will be ignored and the default value will be used.
    36  type ResourceManagerOverrideLimit struct {
    37  	// System is the limit for the resource at the entire system. if not set, the default value will be used.
    38  	// For a specific limit, the system-wide dictates the maximum allowed value across all peers and protocols at the entire node scope.
    39  	StreamsInbound int `validate:"gte=0" mapstructure:"streams-inbound"`
    40  
    41  	// StreamsOutbound is the max number of outbound streams allowed, at the resource scope.
    42  	StreamsOutbound int `validate:"gte=0" mapstructure:"streams-outbound"`
    43  
    44  	// ConnectionsInbound is the max number of inbound connections allowed, at the resource scope.
    45  	ConnectionsInbound int `validate:"gte=0" mapstructure:"connections-inbound"`
    46  
    47  	// ConnectionsOutbound is the max number of outbound connections allowed, at the resource scope.
    48  	ConnectionsOutbound int `validate:"gte=0" mapstructure:"connections-outbound"`
    49  
    50  	// FD is the max number of file descriptors allowed, at the resource scope.
    51  	FD int `validate:"gte=0" mapstructure:"fd"`
    52  
    53  	// Memory is the max amount of memory allowed (bytes), at the resource scope.
    54  	Memory int `validate:"gte=0" mapstructure:"memory-bytes"`
    55  }
    56  
    57  // GossipSubParameters keys.
    58  const (
    59  	RpcInspectorKey         = "rpc-inspector"
    60  	RpcTracerKey            = "rpc-tracer"
    61  	PeerScoringEnabledKey   = "peer-scoring-enabled"
    62  	ScoreParamsKey          = "scoring-parameters"
    63  	SubscriptionProviderKey = "subscription-provider"
    64  )
    65  
    66  // GossipSubParameters is the configuration for the GossipSub pubsub implementation.
    67  type GossipSubParameters struct {
    68  	// RpcInspectorParameters configuration for all gossipsub RPC control message inspectors.
    69  	RpcInspector RpcInspectorParameters `mapstructure:"rpc-inspector"`
    70  
    71  	// GossipSubScoringRegistryConfig is the configuration for the GossipSub score registry.
    72  	// GossipSubTracerParameters is the configuration for the gossipsub tracer. GossipSub tracer is used to trace the local mesh events and peer scores.
    73  	RpcTracer GossipSubTracerParameters `mapstructure:"rpc-tracer"`
    74  
    75  	// ScoringParameters is whether to enable GossipSub peer scoring.
    76  	PeerScoringEnabled   bool                           `mapstructure:"peer-scoring-enabled"`
    77  	SubscriptionProvider SubscriptionProviderParameters `mapstructure:"subscription-provider"`
    78  	ScoringParameters    ScoringParameters              `mapstructure:"scoring-parameters"`
    79  }
    80  
    81  const (
    82  	DecayIntervalKey = "decay-interval"
    83  )
    84  
    85  // ScoringParameters are the parameters for the score option.
    86  // Parameters are "numerical values" that are used to compute or build components that compute the score of a peer in GossipSub system.
    87  type ScoringParameters struct {
    88  	PeerScoring               PeerScoringParameters     `validate:"required" mapstructure:"peer-scoring"`
    89  	ScoringRegistryParameters ScoringRegistryParameters `validate:"required" mapstructure:"scoring-registry"`
    90  }
    91  
    92  // SubscriptionProviderParameters keys.
    93  const (
    94  	UpdateIntervalKey = "update-interval"
    95  	CacheSizeKey      = "cache-size"
    96  )
    97  
    98  type SubscriptionProviderParameters struct {
    99  	// UpdateInterval is the interval for updating the list of topics the node have subscribed to; as well as the list of all
   100  	// peers subscribed to each of those topics. This is used to penalize peers that have an invalid subscription based on their role.
   101  	UpdateInterval time.Duration `validate:"gt=0s" mapstructure:"update-interval"`
   102  
   103  	// CacheSize is the size of the cache that keeps the list of peers subscribed to each topic as the local node.
   104  	// This is the local view of the current node towards the subscription status of other nodes in the system.
   105  	// The cache must be large enough to accommodate the maximum number of nodes in the system, otherwise the view of the local node will be incomplete
   106  	// due to cache eviction.
   107  	CacheSize uint32 `validate:"gt=0" mapstructure:"cache-size"`
   108  }
   109  
   110  // GossipSubTracerParameters keys.
   111  const (
   112  	LocalMeshLogIntervalKey              = "local-mesh-logging-interval"
   113  	ScoreTracerIntervalKey               = "score-tracer-interval"
   114  	RPCSentTrackerCacheSizeKey           = "rpc-sent-tracker-cache-size"
   115  	RPCSentTrackerQueueCacheSizeKey      = "rpc-sent-tracker-queue-cache-size"
   116  	RPCSentTrackerNumOfWorkersKey        = "rpc-sent-tracker-workers"
   117  	DuplicateMessageCacheTrackerKey      = "duplicate-message-tracker"
   118  	DuplicateMessageCacheTrackerSizeKey  = "cache-size"
   119  	DuplicateMessageCacheTrackerDecayKey = "decay"
   120  )
   121  
   122  // GossipSubTracerParameters is the config for the gossipsub tracer. GossipSub tracer is used to trace the local mesh events and peer scores.
   123  type GossipSubTracerParameters struct {
   124  	DuplicateMessageTrackerConfig DuplicateMessageTrackerConfig `validate:"required" mapstructure:"duplicate-message-tracker"`
   125  	// LocalMeshLogInterval is the interval at which the local mesh is logged.
   126  	LocalMeshLogInterval time.Duration `validate:"gt=0s" mapstructure:"local-mesh-logging-interval"`
   127  	// ScoreTracerInterval is the interval at which the score tracer logs the peer scores.
   128  	ScoreTracerInterval time.Duration `validate:"gt=0s" mapstructure:"score-tracer-interval"`
   129  	// RPCSentTrackerCacheSize cache size of the rpc sent tracker used by the gossipsub mesh tracer.
   130  	RPCSentTrackerCacheSize uint32 `validate:"gt=0" mapstructure:"rpc-sent-tracker-cache-size"`
   131  	// RPCSentTrackerQueueCacheSize cache size of the rpc sent tracker queue used for async tracking.
   132  	RPCSentTrackerQueueCacheSize uint32 `validate:"gt=0" mapstructure:"rpc-sent-tracker-queue-cache-size"`
   133  	// RpcSentTrackerNumOfWorkers number of workers for rpc sent tracker worker pool.
   134  	RpcSentTrackerNumOfWorkers int `validate:"gt=0" mapstructure:"rpc-sent-tracker-workers"`
   135  }
   136  
   137  // DuplicateMessageTrackerConfig duplicate message cache config.
   138  type DuplicateMessageTrackerConfig struct {
   139  	// CacheSize cache size of the gossipsub duplicate message tracker.
   140  	CacheSize uint32 `validate:"gt=0" mapstructure:"cache-size"`
   141  	// Decay rate of decay for the peer duplicate message counters.
   142  	Decay float64 `validate:"gt=0,lt=1" mapstructure:"decay"`
   143  	// SkipDecayThreshold the threshold for which when the counter is below this value, the decay function will not be called
   144  	SkipDecayThreshold float64 `validate:"gt=0,lt=1" mapstructure:"skip-decay-threshold"`
   145  }
   146  
   147  // ResourceScope is the scope of the resource, e.g., system, transient, protocol, peer, peer-protocol.
   148  type ResourceScope string
   149  
   150  func (r ResourceScope) String() string {
   151  	return string(r)
   152  }
   153  
   154  const (
   155  	// ResourceScopeSystem is the system scope; the system scope dictates the maximum allowed value across all peers and protocols at the entire node level.
   156  	ResourceScopeSystem ResourceScope = "system"
   157  	// ResourceScopeTransient is the transient scope; the transient scope is used for resources that have not fully established and are under negotiation.
   158  	ResourceScopeTransient ResourceScope = "transient"
   159  	// ResourceScopeProtocol is the protocol scope; the protocol scope dictates the maximum allowed resource across all peers for that protocol.
   160  	ResourceScopeProtocol ResourceScope = "protocol"
   161  	// ResourceScopePeer is the peer scope; the peer scope dictates the maximum allowed resource for a specific peer.
   162  	ResourceScopePeer ResourceScope = "peer"
   163  	// ResourceScopePeerProtocol is the peer-protocol scope; the peer-protocol scope dictates the maximum allowed resource for a protocol and a peer.
   164  	ResourceScopePeerProtocol ResourceScope = "peer-protocol"
   165  )