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 support for custom protocol matching function #440

Merged
merged 2 commits into from
Jul 30, 2021
Merged

add support for custom protocol matching function #440

merged 2 commits into from
Jul 30, 2021

Conversation

richard-ramos
Copy link
Contributor

This allows setting a custom protocol matching function.

fullTextMatch := func(expectedProtocol string) func(string) bool {
    return func(protocolToVerify string) bool {
	return protocolToVerify == expectedProtocol
    }
}

// create a new PubSub service using the GossipSub router
ps, err := pubsub.NewGossipSub(ctx, h, pubsub.WithProtocolMatchFunction(fullTextMatch))

Could potentially be used to allow semver matching

protoMinor := protocol.ID("/testing/1.2.0")
mfunc, err := helpers.MultistreamSemverMatcher(protoMinor)
if err != nil {
    t.Fatal(err)
}
ps, err := pubsub.NewGossipSub(ctx, h, 
     pubsub.WithProtocolMatchFunction(mfunc),
     pubsub.WithGossipSubProtocols([]protocol.ID{pubsub.GossipSubID_v11, pubsub.GossipSubID_v10, pubsub.FloodSubID, protoMinor}, pubsub.GossipSubDefaultFeatures)
)

Copy link
Collaborator

@vyzo vyzo left a comment

Choose a reason for hiding this comment

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

Thank you, this looks useful.

A couple of small changes are needed:

  • It should probably be combined with the appropriate feature test function in gossipsub.
    Mind adding some about this in the documentation?
  • No need to use a pointer! functions can be tested against nil with !=

pubsub.go Outdated Show resolved Hide resolved
pubsub.go Outdated Show resolved Hide resolved
pubsub.go Outdated Show resolved Hide resolved
pubsub.go Outdated Show resolved Hide resolved
@vyzo
Copy link
Collaborator

vyzo commented Jul 29, 2021

Also, can you add a test?

@richard-ramos richard-ramos requested a review from vyzo July 29, 2021 21:54
Copy link
Collaborator

@vyzo vyzo left a comment

Choose a reason for hiding this comment

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

thank you, this is looking good. Just a couple of minor things to fix and we can merge.

pubsub.go Outdated
@@ -235,6 +240,7 @@ func NewPubSub(ctx context.Context, h host.Host, rt PubSubRouter, opts ...Option
peerOutboundQueueSize: 32,
signID: h.ID(),
signKey: nil,
protoMatchFunc: nil,
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't need to initialize this, the default is fine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that we do it for signKey to be explicit about it, as these are grouped together.

pubsub.go Outdated
Comment on lines 489 to 490
// be used by the protocol handler on the Host's Mux. Can be combined with
// WithGossipSubProtocols feature function for checking if a protocol is supported
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would say here "Should be combined ... for checking if certain protocol features are supported"

@richard-ramos
Copy link
Contributor Author

I finished the required changes. Thank you for the code review!

@richard-ramos richard-ramos requested a review from vyzo July 30, 2021 12:59
@vyzo vyzo merged commit 37d36d7 into libp2p:master Jul 30, 2021
@vyzo
Copy link
Collaborator

vyzo commented Jul 30, 2021

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants