From a2cf9b67f34270c2b84e87da7c573d64986c0d3f Mon Sep 17 00:00:00 2001 From: Yahya Hassanzadeh Date: Fri, 28 Oct 2022 09:45:39 -0700 Subject: [PATCH 1/2] adds with gossipsub tracker --- gossipsub.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/gossipsub.go b/gossipsub.go index 713bc623..3e604111 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -204,10 +204,22 @@ type GossipSubParams struct { IWantFollowupTime time.Duration } -// NewGossipSub returns a new PubSub object using GossipSubRouter as the router. +// NewGossipSub returns a new PubSub object using the default GossipSubRouter as the router. func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, error) { + rt := DefaultGossipSubRouter(h) + opts = append(opts, WithRawTracer(rt.tagTracer)) + return NewGossipSubWithRouter(ctx, h, rt, opts...) +} + +// NewGossipSubWithRouter returns a new PubSub object using the given router. +func NewGossipSubWithRouter(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option) (*PubSub, error) { + return NewPubSub(ctx, h, rt, opts...) +} + +// DefaultGossipSubRouter returns a new GossipSubRouter with default parameters. +func DefaultGossipSubRouter(h host.Host) *GossipSubRouter { params := DefaultGossipSubParams() - rt := &GossipSubRouter{ + return &GossipSubRouter{ peers: make(map[peer.ID]protocol.ID), mesh: make(map[string]map[peer.ID]struct{}), fanout: make(map[string]map[peer.ID]struct{}), @@ -225,10 +237,6 @@ func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, er tagTracer: newTagTracer(h.ConnManager()), params: params, } - - // hook the tag tracer - opts = append(opts, WithRawTracer(rt.tagTracer)) - return NewPubSub(ctx, h, rt, opts...) } // DefaultGossipSubParams returns the default gossip sub parameters @@ -1909,6 +1917,10 @@ func (gs *GossipSubRouter) getPeers(topic string, count int, filter func(peer.ID return peers } +func (gs *GossipSubRouter) WithTagTracerPubsubOption() Option { + return WithRawTracer(gs.tagTracer) +} + func peerListToMap(peers []peer.ID) map[peer.ID]struct{} { pmap := make(map[peer.ID]struct{}) for _, p := range peers { From 03b0d805018ea5e406a157eda8d708cce8b65b14 Mon Sep 17 00:00:00 2001 From: Yahya Hassanzadeh Date: Tue, 1 Nov 2022 14:06:47 -0700 Subject: [PATCH 2/2] renames and add godoc --- gossipsub.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gossipsub.go b/gossipsub.go index 3e604111..62dbd8e8 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -1917,7 +1917,11 @@ func (gs *GossipSubRouter) getPeers(topic string, count int, filter func(peer.ID return peers } -func (gs *GossipSubRouter) WithTagTracerPubsubOption() Option { +// WithDefaultTagTracer returns the tag tracer of the GossipSubRouter as a PubSub option. +// This is useful for cases where the GossipSubRouter is instantiated externally, and is +// injected into the GossipSub constructor as a dependency. This allows the tag tracer to be +// also injected into the GossipSub constructor as a PubSub option dependency. +func (gs *GossipSubRouter) WithDefaultTagTracer() Option { return WithRawTracer(gs.tagTracer) }