Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Libp2p Gossipsub Peer Gater #6479

Merged
merged 13 commits into from
Sep 23, 2024
6 changes: 6 additions & 0 deletions config/default-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,12 @@ network-config:
# keep the entire network's size. Otherwise, the local node's view of the network will be incomplete due to cache eviction.
# Recommended size is 10x the number of peers in the network.
cache-size: 10000
peer-gater:
enable: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make this enabled so the full name is gossipsub-peer-gater-enabed. it's more consistent with our other configs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topic-delivery-weights-override:
consensus-committee: 1.5
sync-committee: .75

# Application layer spam prevention
alsp-spam-record-cache-size: 1000
alsp-spam-report-queue-size: 10_000
Expand Down
4 changes: 4 additions & 0 deletions insecure/corruptlibp2p/pubsub_adapter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func (c *CorruptPubSubAdapterConfig) WithRpcInspector(_ p2p.GossipSubRPCInspecto
// CorruptPubSub does not support inspector suite. This is a no-op.
}

func (c *CorruptPubSubAdapterConfig) WithPeerGater(_ map[string]float64) {
// CorruptPubSub does not need peer gater. This is a no-op.
}

func (c *CorruptPubSubAdapterConfig) Build() []corrupt.Option {
return c.options
}
Expand Down
16 changes: 16 additions & 0 deletions network/netconf/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/onflow/flow-go/network/channels"
p2pconfig "github.com/onflow/flow-go/network/p2p/config"
)

Expand Down Expand Up @@ -200,6 +201,10 @@ func AllFlagNames() []string {
BuildFlagName(gossipsubKey, p2pconfig.ScoreParamsKey, p2pconfig.ScoringRegistryKey, p2pconfig.MisbehaviourPenaltiesKey, p2pconfig.IWantKey),
BuildFlagName(gossipsubKey, p2pconfig.ScoreParamsKey, p2pconfig.ScoringRegistryKey, p2pconfig.MisbehaviourPenaltiesKey, p2pconfig.PublishKey),
BuildFlagName(gossipsubKey, p2pconfig.ScoreParamsKey, p2pconfig.ScoringRegistryKey, p2pconfig.MisbehaviourPenaltiesKey, p2pconfig.ClusterPrefixedReductionFactorKey),

BuildFlagName(gossipsubKey, p2pconfig.PeerGaterKey, p2pconfig.EnableKey),
BuildFlagName(gossipsubKey, p2pconfig.PeerGaterKey, p2pconfig.TopicDeliveryWeightsKey, channels.ConsensusCommittee.String()),
BuildFlagName(gossipsubKey, p2pconfig.PeerGaterKey, p2pconfig.TopicDeliveryWeightsKey, channels.SyncCommittee.String()),
}

for _, scope := range []string{systemScope, transientScope, protocolScope, peerScope, peerProtocolScope} {
Expand Down Expand Up @@ -597,6 +602,16 @@ func InitializeNetworkFlags(flags *pflag.FlagSet, config *Config) {
config.GossipSub.ScoringParameters.ScoringRegistryParameters.MisbehaviourPenalties.ClusterPrefixedReductionFactor,
"the factor used to reduce the penalty for control message misbehaviours on cluster prefixed topics")

flags.Bool(BuildFlagName(gossipsubKey, p2pconfig.PeerGaterKey, p2pconfig.EnableKey),
config.GossipSub.PeerGaterParameters.Enabled,
"enable the libp2p peer gater")
flags.Float64(BuildFlagName(gossipsubKey, p2pconfig.PeerGaterKey, p2pconfig.TopicDeliveryWeightsKey, channels.ConsensusCommittee.String()),
config.GossipSub.PeerGaterParameters.TopicDeliveryWeightsOverride.ConsensusCommittee,
"topic delivery weights override for the consensus-committee topic")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the goal for the usage string should be to describe what this flag does. I don't really understand the current string, because it talks about technical details, but not what I should be expecting as a result of it.

Just as an example what I mean, you could write something like:

This is a heuristic for reducing load from nodes that ask a lot of questions,
while relaying fewer up-to-date liveness-critical messages. We do that by
assigning liveness-critical messages (such as messages on the consensus-committee
topic) a higher weight, that is added to a peer's score for successfully delivering this
such message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flags.Float64(BuildFlagName(gossipsubKey, p2pconfig.PeerGaterKey, p2pconfig.TopicDeliveryWeightsKey, channels.SyncCommittee.String()),
config.GossipSub.PeerGaterParameters.TopicDeliveryWeightsOverride.SyncCommittee,
"topic delivery weights override for the sync-committee topic")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly here.


}

// LoadLibP2PResourceManagerFlags loads all CLI flags for the libp2p resource manager configuration on the provided pflag set.
Expand Down Expand Up @@ -666,6 +681,7 @@ func SetAliases(conf *viper.Viper) error {
// mapping should be from network-p2pconfig.key1.key2.key3... to network-config-key1-key2-key3...
m[strings.Join(s[1:], "-")] = key
}

// each flag name should correspond to exactly one key in our config store after it is loaded with the default config
for _, flagName := range AllFlagNames() {
fullKey, ok := m[flagName]
Expand Down
4 changes: 4 additions & 0 deletions network/p2p/builder/gossipsub/gossipSubBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ func (g *Builder) Build(ctx irrecoverable.SignalerContext) (p2p.PubSubAdapter, e
})
gossipSubConfigs.WithMessageIdFunction(utils.MessageID)

if g.gossipSubCfg.PeerGaterParameters.Enabled {
gossipSubConfigs.WithPeerGater(g.gossipSubCfg.PeerGaterParameters.TopicDeliveryWeightsOverride.ToMap())
}

if g.routingSystem != nil {
gossipSubConfigs.WithRoutingDiscovery(g.routingSystem)
}
Expand Down
31 changes: 31 additions & 0 deletions network/p2p/config/gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const (
PeerScoringEnabledKey = "peer-scoring-enabled"
ScoreParamsKey = "scoring-parameters"
SubscriptionProviderKey = "subscription-provider"
PeerGaterKey = "peer-gater"
TopicDeliveryWeightsKey = "topic-delivery-weights-override"
)

// GossipSubParameters is the configuration for the GossipSub pubsub implementation.
Expand All @@ -76,6 +78,9 @@ type GossipSubParameters struct {
PeerScoringEnabled bool `mapstructure:"peer-scoring-enabled"`
SubscriptionProvider SubscriptionProviderParameters `mapstructure:"subscription-provider"`
ScoringParameters ScoringParameters `mapstructure:"scoring-parameters"`

// PeerGaterParameters is the configuration for the libp2p peer gater.
PeerGaterParameters PeerGaterParameters `mapstructure:"peer-gater"`
}

const (
Expand All @@ -89,6 +94,32 @@ type ScoringParameters struct {
ScoringRegistryParameters ScoringRegistryParameters `validate:"required" mapstructure:"scoring-registry"`
}

// PeerGaterParameters are the parameters for the libp2p peer gater. This config provides operators the ability
// to override topic delivery weights for the peer gater. The default weight is 1.0.
// Parameters are "numerical values" that are used to compute or build components that compute the score of a peer in GossipSub system.
type PeerGaterParameters struct {
// Enabled enables the peer gater.
Enabled bool `validate:"required" mapstructure:"enable"`
// TopicDeliveryWeightsOverride topic delivery weights that will override the default value for the specified channel.
TopicDeliveryWeightsOverride TopicDeliveryWeightsOverride `validate:"required" mapstructure:"topic-delivery-weights-override"`
}

// TopicDeliveryWeightsOverride topic delivery weights used to override the default topic delivery weight 1.0 of the peer gater.
// Parameters are "numerical values" that are used to compute or build components that compute the score of a peer in GossipSub system.
type TopicDeliveryWeightsOverride struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be difficult to allow arbitrary strings here instead of a fixed list? It might be useful to be able to add overrides for other channels without having to rollout a new image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConsensusCommittee float64 `validate:"required" mapstructure:"consensus-committee"`
SyncCommittee float64 `validate:"required" mapstructure:"sync-committee"`
}

// ToMap returns the topic delivery weights configured on this struct as a map[string]float64 .
// Note: When new topic delivery weights are added to the struct this func should be updated.
func (t *TopicDeliveryWeightsOverride) ToMap() map[string]float64 {
return map[string]float64{
"consensus-committee": t.ConsensusCommittee,
"sync-committee": t.SyncCommittee,
}
}

// SubscriptionProviderParameters keys.
const (
UpdateIntervalKey = "update-interval"
Expand Down
5 changes: 5 additions & 0 deletions network/p2p/mock/pub_sub_adapter_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions network/p2p/node/gossipSubAdapterConfig.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package p2pnode

import (
"time"

pubsub "github.com/libp2p/go-libp2p-pubsub"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -97,6 +99,16 @@ func (g *GossipSubAdapterConfig) WithTracer(tracer p2p.PubSubTracer) {
g.options = append(g.options, pubsub.WithRawTracer(tracer))
}

// WithPeerGater adds a peer gater option to the config.
// Args:
// - params: the topic delivery weights to use
// Returns:
// -None
func (g *GossipSubAdapterConfig) WithPeerGater(topicDeliveryWeights map[string]float64) {
peerGaterParams := pubsub.NewPeerGaterParams(pubsub.DefaultPeerGaterThreshold, pubsub.DefaultPeerGaterGlobalDecay, pubsub.ScoreParameterDecay(10*time.Minute)).WithTopicDeliveryWeights(topicDeliveryWeights)
g.options = append(g.options, pubsub.WithPeerGater(peerGaterParams))
}

// ScoreTracer returns the tracer for the peer score.
// Args:
// - None
Expand Down
1 change: 1 addition & 0 deletions network/p2p/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type PubSubAdapterConfig interface {
// This is used to expose the local scoring table of the GossipSub node to its higher level components.
WithScoreTracer(tracer PeerScoreTracer)
WithRpcInspector(GossipSubRPCInspector)
WithPeerGater(topicDeliveryWeights map[string]float64)
}

// GossipSubRPCInspector abstracts the general behavior of an app specific RPC inspector specifically
Expand Down
Loading