From c7c422abbdb18f734c6c158ec1f976b1803f4e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 20 Nov 2019 21:31:00 +0100 Subject: [PATCH] Use config for pubsub tracing --- node/builder.go | 3 +++ node/config/def.go | 3 ++- node/modules/lp2p/pubsub.go | 36 +++++++++++++++++++++--------------- node/options.go | 6 ++++++ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/node/builder.go b/node/builder.go index 02082a3e5..27d7c5aee 100644 --- a/node/builder.go +++ b/node/builder.go @@ -312,6 +312,9 @@ func ConfigFullNode(c interface{}) Option { return Options( ConfigCommon(&cfg.Common), Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)), + If(cfg.Metrics.PubsubTracing, + Override(new(*pubsub.PubSub), lp2p.GossipSub(lp2p.PubsubTracer())), + ), ) } diff --git a/node/config/def.go b/node/config/def.go index d704b428b..9391708a6 100644 --- a/node/config/def.go +++ b/node/config/def.go @@ -41,7 +41,8 @@ type Libp2p struct { // // Full Node type Metrics struct { - Nickname string + Nickname string + PubsubTracing bool } // // Storage Miner diff --git a/node/modules/lp2p/pubsub.go b/node/modules/lp2p/pubsub.go index da1a0c9c7..657c714f1 100644 --- a/node/modules/lp2p/pubsub.go +++ b/node/modules/lp2p/pubsub.go @@ -12,28 +12,34 @@ import ( "github.com/filecoin-project/lotus/node/modules/helpers" ) -func withTracer(host host.Host, opts []pubsub.Option) []pubsub.Option { - pi, err := peer.AddrInfoFromP2pAddr(ma.StringCast("/ip4/147.75.67.199/tcp/4001/p2p/QmTd6UvR47vUidRNZ1ZKXHrAFhqTJAD27rKL9XYghEKgKX")) - if err != nil { - panic(err) - } +type PubsubOpt func(host.Host) pubsub.Option - tr, err := pubsub.NewRemoteTracer(context.TODO(), host, *pi) - if err != nil { - panic(err) - } +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) + } - return append(opts, pubsub.WithEventTracer(tr)) + tr, err := pubsub.NewRemoteTracer(context.TODO(), host, *pi) + if err != nil { + panic(err) + } + + return pubsub.WithEventTracer(tr) + } } -func FloodSub(pubsubOptions ...pubsub.Option) interface{} { +func GossipSub(pubsubOptions ...PubsubOpt) interface{} { return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) (service *pubsub.PubSub, err error) { - return pubsub.NewFloodSub(helpers.LifecycleCtx(mctx, lc), host, pubsubOptions...) + return pubsub.NewGossipSub(helpers.LifecycleCtx(mctx, lc), host, paresOpts(host, pubsubOptions)...) } } -func GossipSub(pubsubOptions ...pubsub.Option) interface{} { - return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) (service *pubsub.PubSub, err error) { - return pubsub.NewGossipSub(helpers.LifecycleCtx(mctx, lc), host, withTracer(host, pubsubOptions)...) +func paresOpts(host host.Host, in []PubsubOpt) []pubsub.Option { + out := make([]pubsub.Option, len(in)) + for k, v := range in { + out[k] = v(host) } + return out } diff --git a/node/options.go b/node/options.go index 9c8a79710..75a57cb9b 100644 --- a/node/options.go +++ b/node/options.go @@ -40,6 +40,12 @@ func ApplyIf(check func(s *Settings) bool, opts ...Option) Option { } } +func If(b bool, opts ...Option) Option { + return ApplyIf(func(s *Settings) bool { + return b + }, opts...) +} + // Override option changes constructor for a given type func Override(typ, constructor interface{}) Option { return func(s *Settings) error {