lotus/node/modules/lp2p/pubsub.go

46 lines
1.1 KiB
Go
Raw Normal View History

2019-07-02 12:40:25 +00:00
package lp2p
import (
2019-11-19 23:31:55 +00:00
"context"
host "github.com/libp2p/go-libp2p-core/host"
2019-11-19 23:31:55 +00:00
peer "github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
2019-11-19 23:31:55 +00:00
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/fx"
"github.com/filecoin-project/lotus/node/modules/helpers"
)
2019-11-20 20:31:00 +00:00
type PubsubOpt func(host.Host) pubsub.Option
2019-11-19 23:31:55 +00:00
2019-11-20 20:31:00 +00:00
func PubsubTracer() PubsubOpt {
return func(host host.Host) pubsub.Option {
pi, err := peer.AddrInfoFromP2pAddr(ma.StringCast("/ip4/147.75.67.199/tcp/4001/p2p/QmTd6UvR47vUidRNZ1ZKXHrAFhqTJAD27rKL9XYghEKgKX"))
if err != nil {
panic(err)
}
tr, err := pubsub.NewRemoteTracer(context.TODO(), host, *pi)
if err != nil {
panic(err)
}
2019-11-19 23:31:55 +00:00
2019-11-20 20:31:00 +00:00
return pubsub.WithEventTracer(tr)
}
2019-11-19 23:31:55 +00:00
}
2019-11-20 20:31:00 +00:00
func GossipSub(pubsubOptions ...PubsubOpt) interface{} {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) (service *pubsub.PubSub, err error) {
2019-11-20 20:31:00 +00:00
return pubsub.NewGossipSub(helpers.LifecycleCtx(mctx, lc), host, paresOpts(host, pubsubOptions)...)
}
}
2019-11-20 20:31:00 +00:00
func paresOpts(host host.Host, in []PubsubOpt) []pubsub.Option {
out := make([]pubsub.Option, len(in))
for k, v := range in {
out[k] = v(host)
}
2019-11-20 20:31:00 +00:00
return out
}