libp2p / go-libp2p-pubsub

The PubSub implementation for go-libp2p
https://github.com/libp2p/specs/tree/master/pubsub
Other
317 stars 182 forks source link

add support for custom protocol matching function #440

Closed richard-ramos closed 3 years ago

richard-ramos commented 3 years ago

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)
)
vyzo commented 3 years ago

Also, can you add a test?

richard-ramos commented 3 years ago

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

vyzo commented 3 years ago

thank you!