Use config for listen addresses in libp2p

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-04 14:04:39 +02:00
parent 808a1e9deb
commit f08263662f
3 changed files with 36 additions and 26 deletions

View File

@ -13,16 +13,12 @@ import (
"github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build" "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"
"github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/filecoin-project/go-lotus/node/modules/helpers"
"github.com/filecoin-project/go-lotus/node/modules/lp2p" "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 // New builds and starts new Filecoin node
func New(ctx context.Context) (api.API, error) { func New(ctx context.Context) (api.API, error) {
var resAPI api.Struct var resAPI api.Struct
@ -31,6 +27,9 @@ func New(ctx context.Context) (api.API, error) {
app := fx.New( app := fx.New(
fx.Provide(as(ctx, new(helpers.MetricsCtx))), fx.Provide(as(ctx, new(helpers.MetricsCtx))),
fx.Provide(func() (*config.Root, error) {
return config.FromFile("./config.toml")
}),
//fx.Provide(modules.RandomPeerID), //fx.Provide(modules.RandomPeerID),
randomIdentity(), randomIdentity(),
@ -63,7 +62,7 @@ func New(ctx context.Context) (api.API, error) {
fx.Invoke( fx.Invoke(
lp2p.PstoreAddSelfKeys, lp2p.PstoreAddSelfKeys,
lp2p.StartListening(defaultListenAddrs), lp2p.StartListening,
), ),
), ),

View File

@ -5,6 +5,7 @@ import "time"
// Root is starting point of the config // Root is starting point of the config
type Root struct { type Root struct {
API API API API
Libp2p Libp2p
} }
// API contains configs for API endpoint // API contains configs for API endpoint
@ -13,6 +14,11 @@ type API struct {
Timeout Duration Timeout Duration
} }
// Libp2p contains configs for libp2p
type Libp2p struct {
ListenAddresses []string
}
// Default returns the default config // Default returns the default config
func Default() *Root { func Default() *Root {
def := Root{ def := Root{
@ -20,6 +26,12 @@ func Default() *Root {
ListenAddress: "/ip6/::1/tcp/1234/http", ListenAddress: "/ip6/::1/tcp/1234/http",
Timeout: Duration(30 * time.Second), Timeout: Duration(30 * time.Second),
}, },
Libp2p: Libp2p{
ListenAddresses: []string{
"/ip4/0.0.0.0/tcp/4001",
"/ip6/::/tcp/4001",
},
},
} }
return &def return &def
} }

View File

@ -3,6 +3,7 @@ package lp2p
import ( import (
"fmt" "fmt"
"github.com/filecoin-project/go-lotus/node/config"
"github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-core/host" host "github.com/libp2p/go-libp2p-core/host"
p2pbhost "github.com/libp2p/go-libp2p/p2p/host/basic" p2pbhost "github.com/libp2p/go-libp2p/p2p/host/basic"
@ -94,9 +95,8 @@ func listenAddresses(addresses []string) ([]ma.Multiaddr, error) {
return listen, nil return listen, nil
} }
func StartListening(addresses []string) func(host host.Host) error { func StartListening(host host.Host, cfg *config.Root) error {
return func(host host.Host) error { listenAddrs, err := listenAddresses(cfg.Libp2p.ListenAddresses)
listenAddrs, err := listenAddresses(addresses)
if err != nil { if err != nil {
return err return err
} }
@ -114,4 +114,3 @@ func StartListening(addresses []string) func(host host.Host) error {
log.Infof("Swarm listening at: %s", addrs) log.Infof("Swarm listening at: %s", addrs)
return nil return nil
} }
}