-
Notifications
You must be signed in to change notification settings - Fork 186
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
Abstracts GossipSubRouter interface #505
Closed
yhassanzadeh13
wants to merge
7
commits into
libp2p:master
from
yhassanzadeh13:yahya/gossipsub-router-interface
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c93e47a
Update go.mod
yhassanzadeh13 19b4efd
Refactor GossipSub Construction (#1)
yhassanzadeh13 7c7c05a
decouples options
yhassanzadeh13 c557ad4
Merge remote-tracking branch 'libp2p/master' into yahya/gossipsub-rou…
yhassanzadeh13 5f71a48
fixes conflict
yhassanzadeh13 1c1c946
reverts back module
yhassanzadeh13 ab8365c
fixes peer score helper
yhassanzadeh13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,7 +206,10 @@ type GossipSubParams struct { | |
|
||
// 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) | ||
rt, err := DefaultGossipSubRouter(h) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create default gossipsub router: %w", err) | ||
} | ||
opts = append(opts, WithRawTracer(rt.tagTracer)) | ||
return NewGossipSubWithRouter(ctx, h, rt, opts...) | ||
} | ||
|
@@ -216,10 +219,12 @@ func NewGossipSubWithRouter(ctx context.Context, h host.Host, rt PubSubRouter, o | |
return NewPubSub(ctx, h, rt, opts...) | ||
} | ||
|
||
type GossipSubRouterOption func(*GossipSubRouter) error | ||
|
||
// DefaultGossipSubRouter returns a new GossipSubRouter with default parameters. | ||
func DefaultGossipSubRouter(h host.Host) *GossipSubRouter { | ||
func DefaultGossipSubRouter(h host.Host, opts ...GossipSubRouterOption) (*GossipSubRouter, error) { | ||
params := DefaultGossipSubParams() | ||
return &GossipSubRouter{ | ||
rt := &GossipSubRouter{ | ||
peers: make(map[peer.ID]protocol.ID), | ||
mesh: make(map[string]map[peer.ID]struct{}), | ||
fanout: make(map[string]map[peer.ID]struct{}), | ||
|
@@ -237,6 +242,14 @@ func DefaultGossipSubRouter(h host.Host) *GossipSubRouter { | |
tagTracer: newTagTracer(h.ConnManager()), | ||
params: params, | ||
} | ||
|
||
for _, opt := range opts { | ||
if err := opt(rt); err != nil { | ||
return nil, fmt.Errorf("failed to apply gossipsub router option: %w", err) | ||
} | ||
} | ||
|
||
return rt, nil | ||
} | ||
|
||
// DefaultGossipSubParams returns the default gossip sub parameters | ||
|
@@ -277,7 +290,7 @@ func DefaultGossipSubParams() GossipSubParams { | |
// WithPeerScore is a gossipsub router option that enables peer scoring. | ||
func WithPeerScore(params *PeerScoreParams, thresholds *PeerScoreThresholds) Option { | ||
return func(ps *PubSub) error { | ||
gs, ok := ps.rt.(*GossipSubRouter) | ||
gs, ok := ps.rt.(GossipPubSubRouter) | ||
if !ok { | ||
return fmt.Errorf("pubsub router is not gossipsub") | ||
} | ||
|
@@ -294,21 +307,17 @@ func WithPeerScore(params *PeerScoreParams, thresholds *PeerScoreThresholds) Opt | |
return err | ||
} | ||
|
||
gs.score = newPeerScore(params) | ||
gs.gossipThreshold = thresholds.GossipThreshold | ||
gs.publishThreshold = thresholds.PublishThreshold | ||
gs.graylistThreshold = thresholds.GraylistThreshold | ||
gs.acceptPXThreshold = thresholds.AcceptPXThreshold | ||
gs.opportunisticGraftThreshold = thresholds.OpportunisticGraftThreshold | ||
gs.SetPeerScore(newPeerScore(params)) | ||
gs.SetPeerScoreThresholds(thresholds) | ||
|
||
gs.gossipTracer = newGossipTracer() | ||
gs.SetGossipTracer(newGossipTracer()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this very ugly. |
||
|
||
// hook the tracer | ||
if ps.tracer != nil { | ||
ps.tracer.raw = append(ps.tracer.raw, gs.score, gs.gossipTracer) | ||
ps.tracer.raw = append(ps.tracer.raw, gs.GetPeerScore(), gs.GetGossipTracer()) | ||
} else { | ||
ps.tracer = &pubsubTracer{ | ||
raw: []RawTracer{gs.score, gs.gossipTracer}, | ||
raw: []RawTracer{gs.GetPeerScore(), gs.GetGossipTracer()}, | ||
pid: ps.host.ID(), | ||
idGen: ps.idGen, | ||
} | ||
|
@@ -321,31 +330,19 @@ func WithPeerScore(params *PeerScoreParams, thresholds *PeerScoreThresholds) Opt | |
// WithFloodPublish is a gossipsub router option that enables flood publishing. | ||
// When this is enabled, published messages are forwarded to all peers with score >= | ||
// to publishThreshold | ||
func WithFloodPublish(floodPublish bool) Option { | ||
return func(ps *PubSub) error { | ||
gs, ok := ps.rt.(*GossipSubRouter) | ||
if !ok { | ||
return fmt.Errorf("pubsub router is not gossipsub") | ||
} | ||
|
||
func WithFloodPublish(floodPublish bool) GossipSubRouterOption { | ||
return func(gs *GossipSubRouter) error { | ||
gs.floodPublish = floodPublish | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// WithPeerExchange is a gossipsub router option that enables Peer eXchange on PRUNE. | ||
// This should generally be enabled in bootstrappers and well connected/trusted nodes | ||
// used for bootstrapping. | ||
func WithPeerExchange(doPX bool) Option { | ||
return func(ps *PubSub) error { | ||
gs, ok := ps.rt.(*GossipSubRouter) | ||
if !ok { | ||
return fmt.Errorf("pubsub router is not gossipsub") | ||
} | ||
|
||
func WithPeerExchange(doPX bool) GossipSubRouterOption { | ||
return func(gs *GossipSubRouter) error { | ||
gs.doPX = doPX | ||
|
||
return nil | ||
} | ||
} | ||
|
@@ -357,7 +354,7 @@ func WithPeerExchange(doPX bool) Option { | |
// symmetrically configured at both ends. | ||
func WithDirectPeers(pis []peer.AddrInfo) Option { | ||
return func(ps *PubSub) error { | ||
gs, ok := ps.rt.(*GossipSubRouter) | ||
gs, ok := ps.rt.(GossipPubSubRouter) | ||
if !ok { | ||
return fmt.Errorf("pubsub router is not gossipsub") | ||
} | ||
|
@@ -368,10 +365,10 @@ func WithDirectPeers(pis []peer.AddrInfo) Option { | |
ps.host.Peerstore().AddAddrs(pi.ID, pi.Addrs, peerstore.PermanentAddrTTL) | ||
} | ||
|
||
gs.direct = direct | ||
gs.SetDirectPeers(direct) | ||
|
||
if gs.tagTracer != nil { | ||
gs.tagTracer.direct = direct | ||
if gs.GetTagTracer() != nil { | ||
gs.GetTagTracer().direct = direct | ||
} | ||
|
||
return nil | ||
|
@@ -382,25 +379,17 @@ func WithDirectPeers(pis []peer.AddrInfo) Option { | |
// heartbeat ticks between attempting to reconnect direct peers that are not | ||
// currently connected. A "tick" is based on the heartbeat interval, which is | ||
// 1s by default. The default value for direct connect ticks is 300. | ||
func WithDirectConnectTicks(t uint64) Option { | ||
return func(ps *PubSub) error { | ||
gs, ok := ps.rt.(*GossipSubRouter) | ||
if !ok { | ||
return fmt.Errorf("pubsub router is not gossipsub") | ||
} | ||
func WithDirectConnectTicks(t uint64) GossipSubRouterOption { | ||
return func(gs *GossipSubRouter) error { | ||
gs.params.DirectConnectTicks = t | ||
return nil | ||
} | ||
} | ||
|
||
// WithGossipSubParams is a gossip sub router option that allows a custom | ||
// config to be set when instantiating the gossipsub router. | ||
func WithGossipSubParams(cfg GossipSubParams) Option { | ||
return func(ps *PubSub) error { | ||
gs, ok := ps.rt.(*GossipSubRouter) | ||
if !ok { | ||
return fmt.Errorf("pubsub router is not gossipsub") | ||
} | ||
func WithGossipSubParams(cfg GossipSubParams) GossipSubRouterOption { | ||
return func(gs *GossipSubRouter) error { | ||
// Overwrite current config and associated variables in the router. | ||
gs.params = cfg | ||
gs.connect = make(chan connectInfo, cfg.MaxPendingConnections) | ||
|
@@ -410,6 +399,25 @@ func WithGossipSubParams(cfg GossipSubParams) Option { | |
} | ||
} | ||
|
||
type GossipPubSubRouter interface { | ||
PubSubRouter | ||
|
||
SetPeerScore(*PeerScore) | ||
GetPeerScore() *PeerScore | ||
|
||
SetPeerScoreThresholds(*PeerScoreThresholds) | ||
|
||
SetGossipTracer(*GossipTracer) | ||
GetGossipTracer() *GossipTracer | ||
|
||
GetTagTracer() *TagTracer | ||
|
||
SetDirectPeers(map[peer.ID]struct{}) | ||
|
||
SetPeerGater(*PeerGater) | ||
GetPeerGater() *PeerGater | ||
} | ||
|
||
// GossipSubRouter is a router that implements the gossipsub protocol. | ||
// For each topic we have joined, we maintain an overlay through which | ||
// messages flow; this is the mesh map. | ||
|
@@ -437,10 +445,10 @@ type GossipSubRouter struct { | |
|
||
mcache *MessageCache | ||
tracer *pubsubTracer | ||
score *peerScore | ||
gossipTracer *gossipTracer | ||
tagTracer *tagTracer | ||
gate *peerGater | ||
score *PeerScore | ||
gossipTracer *GossipTracer | ||
tagTracer *TagTracer | ||
gate *PeerGater | ||
|
||
// config for gossipsub parameters | ||
params GossipSubParams | ||
|
@@ -476,6 +484,8 @@ type GossipSubRouter struct { | |
heartbeatTicks uint64 | ||
} | ||
|
||
var _ GossipPubSubRouter = (*GossipSubRouter)(nil) | ||
|
||
type connectInfo struct { | ||
p peer.ID | ||
spr *record.Envelope | ||
|
@@ -1917,6 +1927,46 @@ func (gs *GossipSubRouter) getPeers(topic string, count int, filter func(peer.ID | |
return peers | ||
} | ||
|
||
func (gs *GossipSubRouter) SetPeerScore(score *PeerScore) { | ||
gs.score = score | ||
} | ||
|
||
func (gs *GossipSubRouter) GetPeerScore() *PeerScore { | ||
return gs.score | ||
} | ||
|
||
func (gs *GossipSubRouter) SetPeerScoreThresholds(thresholds *PeerScoreThresholds) { | ||
gs.gossipThreshold = thresholds.GossipThreshold | ||
gs.publishThreshold = thresholds.PublishThreshold | ||
gs.graylistThreshold = thresholds.GraylistThreshold | ||
gs.acceptPXThreshold = thresholds.AcceptPXThreshold | ||
gs.opportunisticGraftThreshold = thresholds.OpportunisticGraftThreshold | ||
} | ||
|
||
func (gs *GossipSubRouter) SetGossipTracer(tracer *GossipTracer) { | ||
gs.gossipTracer = tracer | ||
} | ||
|
||
func (gs *GossipSubRouter) GetGossipTracer() *GossipTracer { | ||
return gs.gossipTracer | ||
} | ||
|
||
func (gs *GossipSubRouter) GetTagTracer() *TagTracer { | ||
return gs.tagTracer | ||
} | ||
|
||
func (gs *GossipSubRouter) SetDirectPeers(direct map[peer.ID]struct{}) { | ||
gs.direct = direct | ||
} | ||
|
||
func (gs *GossipSubRouter) SetPeerGater(gater *PeerGater) { | ||
gs.gate = gater | ||
} | ||
|
||
func (gs *GossipSubRouter) GetPeerGater() *PeerGater { | ||
return gs.gate | ||
} | ||
|
||
// 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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to find a way to absorb this (the tag tracer) into the defaults, it is very error prone to require it to be explicitly instantaited.