2019-07-02 12:40:25 +00:00
|
|
|
package lp2p
|
2019-07-01 09:09:48 +00:00
|
|
|
|
|
|
|
import (
|
2019-11-19 23:31:55 +00:00
|
|
|
"context"
|
|
|
|
|
2019-07-01 09:09:48 +00:00
|
|
|
host "github.com/libp2p/go-libp2p-core/host"
|
2019-11-19 23:31:55 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
2019-07-01 09:09:48 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-11-19 23:31:55 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2019-07-01 09:09:48 +00:00
|
|
|
"go.uber.org/fx"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2019-07-01 09:09:48 +00:00
|
|
|
)
|
|
|
|
|
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{} {
|
2019-07-01 09:09:48 +00:00
|
|
|
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-07-01 09:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-01 09:09:48 +00:00
|
|
|
}
|
2019-11-20 20:31:00 +00:00
|
|
|
return out
|
2019-07-01 09:09:48 +00:00
|
|
|
}
|