From f08263662f9fbbd1ab0df8d9860efc42ca23d89e Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Thu, 4 Jul 2019 14:04:39 +0200 Subject: [PATCH] Use config for listen addresses in libp2p License: MIT Signed-off-by: Jakub Sztandera --- node/builder.go | 11 +++++------ node/config/def.go | 14 +++++++++++++- node/modules/lp2p/addrs.go | 37 ++++++++++++++++++------------------- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/node/builder.go b/node/builder.go index 5b94dcae1..1711e296b 100644 --- a/node/builder.go +++ b/node/builder.go @@ -13,16 +13,12 @@ import ( "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/node/config" "github.com/filecoin-project/go-lotus/node/modules" "github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/filecoin-project/go-lotus/node/modules/lp2p" ) -var defaultListenAddrs = []string{ // TODO: better defaults? - "/ip4/0.0.0.0/tcp/4001", - "/ip6/::/tcp/4001", -} - // New builds and starts new Filecoin node func New(ctx context.Context) (api.API, error) { var resAPI api.Struct @@ -31,6 +27,9 @@ func New(ctx context.Context) (api.API, error) { app := fx.New( fx.Provide(as(ctx, new(helpers.MetricsCtx))), + fx.Provide(func() (*config.Root, error) { + return config.FromFile("./config.toml") + }), //fx.Provide(modules.RandomPeerID), randomIdentity(), @@ -63,7 +62,7 @@ func New(ctx context.Context) (api.API, error) { fx.Invoke( lp2p.PstoreAddSelfKeys, - lp2p.StartListening(defaultListenAddrs), + lp2p.StartListening, ), ), diff --git a/node/config/def.go b/node/config/def.go index 5c1042e0a..02296b749 100644 --- a/node/config/def.go +++ b/node/config/def.go @@ -4,7 +4,8 @@ import "time" // Root is starting point of the config type Root struct { - API API + API API + Libp2p Libp2p } // API contains configs for API endpoint @@ -13,6 +14,11 @@ type API struct { Timeout Duration } +// Libp2p contains configs for libp2p +type Libp2p struct { + ListenAddresses []string +} + // Default returns the default config func Default() *Root { def := Root{ @@ -20,6 +26,12 @@ func Default() *Root { ListenAddress: "/ip6/::1/tcp/1234/http", Timeout: Duration(30 * time.Second), }, + Libp2p: Libp2p{ + ListenAddresses: []string{ + "/ip4/0.0.0.0/tcp/4001", + "/ip6/::/tcp/4001", + }, + }, } return &def } diff --git a/node/modules/lp2p/addrs.go b/node/modules/lp2p/addrs.go index 8d34ef94e..efbe4a03b 100644 --- a/node/modules/lp2p/addrs.go +++ b/node/modules/lp2p/addrs.go @@ -3,6 +3,7 @@ package lp2p import ( "fmt" + "github.com/filecoin-project/go-lotus/node/config" "github.com/libp2p/go-libp2p" host "github.com/libp2p/go-libp2p-core/host" p2pbhost "github.com/libp2p/go-libp2p/p2p/host/basic" @@ -94,24 +95,22 @@ func listenAddresses(addresses []string) ([]ma.Multiaddr, error) { return listen, nil } -func StartListening(addresses []string) func(host host.Host) error { - return func(host host.Host) error { - listenAddrs, err := listenAddresses(addresses) - if err != nil { - return err - } - - // Actually start listening: - if err := host.Network().Listen(listenAddrs...); err != nil { - return err - } - - // list out our addresses - addrs, err := host.Network().InterfaceListenAddresses() - if err != nil { - return err - } - log.Infof("Swarm listening at: %s", addrs) - return nil +func StartListening(host host.Host, cfg *config.Root) error { + listenAddrs, err := listenAddresses(cfg.Libp2p.ListenAddresses) + if err != nil { + return err } + + // Actually start listening: + if err := host.Network().Listen(listenAddrs...); err != nil { + return err + } + + // list out our addresses + addrs, err := host.Network().InterfaceListenAddresses() + if err != nil { + return err + } + log.Infof("Swarm listening at: %s", addrs) + return nil }