github.com/koko1123/flow-go-1@v0.29.6/network/p2p/p2pnode/gossipSubAdapterConfig.go (about)

     1  package p2pnode
     2  
     3  import (
     4  	pubsub "github.com/libp2p/go-libp2p-pubsub"
     5  	pb "github.com/libp2p/go-libp2p-pubsub/pb"
     6  	"github.com/libp2p/go-libp2p/core/peer"
     7  	"github.com/libp2p/go-libp2p/core/routing"
     8  	discoveryrouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
     9  
    10  	"github.com/koko1123/flow-go-1/network/p2p"
    11  )
    12  
    13  // GossipSubAdapterConfig is a wrapper around libp2p pubsub options that
    14  // implements the PubSubAdapterConfig interface for the Flow network.
    15  type GossipSubAdapterConfig struct {
    16  	options []pubsub.Option
    17  }
    18  
    19  var _ p2p.PubSubAdapterConfig = (*GossipSubAdapterConfig)(nil)
    20  
    21  func NewGossipSubAdapterConfig(base *p2p.BasePubSubAdapterConfig) *GossipSubAdapterConfig {
    22  	return &GossipSubAdapterConfig{
    23  		options: defaultPubsubOptions(base),
    24  	}
    25  }
    26  
    27  func (g *GossipSubAdapterConfig) WithRoutingDiscovery(routing routing.ContentRouting) {
    28  	g.options = append(g.options, pubsub.WithDiscovery(discoveryrouting.NewRoutingDiscovery(routing)))
    29  }
    30  
    31  func (g *GossipSubAdapterConfig) WithSubscriptionFilter(filter p2p.SubscriptionFilter) {
    32  	g.options = append(g.options, pubsub.WithSubscriptionFilter(filter))
    33  }
    34  
    35  func (g *GossipSubAdapterConfig) WithScoreOption(option p2p.ScoreOptionBuilder) {
    36  	g.options = append(g.options, option.BuildFlowPubSubScoreOption())
    37  }
    38  
    39  func (g *GossipSubAdapterConfig) WithMessageIdFunction(f func([]byte) string) {
    40  	g.options = append(g.options, pubsub.WithMessageIdFn(func(pmsg *pb.Message) string {
    41  		return f(pmsg.Data)
    42  	}))
    43  }
    44  
    45  func (g *GossipSubAdapterConfig) WithAppSpecificRpcInspector(f func(peer.ID, *pubsub.RPC) error) {
    46  	g.options = append(g.options, pubsub.WithAppSpecificRpcInspector(f))
    47  }
    48  
    49  func (g *GossipSubAdapterConfig) Build() []pubsub.Option {
    50  	return g.options
    51  }
    52  
    53  func defaultPubsubOptions(base *p2p.BasePubSubAdapterConfig) []pubsub.Option {
    54  	return []pubsub.Option{
    55  		pubsub.WithMessageSigning(true),
    56  		pubsub.WithStrictSignatureVerification(true),
    57  		pubsub.WithMaxMessageSize(base.MaxMessageSize),
    58  	}
    59  }