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/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,
),
),

View File

@ -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
}

View File

@ -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
}