From 9a536e7cc0e52d6b5e0b01a352bccabf5468ab2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 11 Oct 2019 05:16:12 +0200 Subject: [PATCH] config: Allow overriding bootstrap peers --- node/builder.go | 6 ++++++ node/config/def.go | 1 + node/modules/core.go | 21 +++++++++++++-------- node/modules/dtypes/bootstrap.go | 5 +++++ 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 node/modules/dtypes/bootstrap.go diff --git a/node/builder.go b/node/builder.go index 54e75c6c7..33a11e1c1 100644 --- a/node/builder.go +++ b/node/builder.go @@ -203,6 +203,8 @@ func Online() Option { ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull }, // TODO: Fix offline mode + Override(new(dtypes.BootstrapPeers), modules.BuiltinBootstrap), + Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages), Override(new(*store.ChainStore), modules.ChainStore), @@ -294,6 +296,10 @@ func Config(cfg *config.Root) Option { ApplyIf(func(s *Settings) bool { return s.Online }, Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)), + ApplyIf(func(s *Settings) bool { return len(cfg.Libp2p.BootstrapPeers) > 0 }, + Override(new(dtypes.BootstrapPeers), modules.ConfigBootstrap(cfg.Libp2p.BootstrapPeers)), + ), + ApplyIf(func(s *Settings) bool { return s.nodeType == nodeFull }, Override(HeadMetricsKey, metrics.SendHeadNotifs(cfg.Metrics.Nickname)), ), diff --git a/node/config/def.go b/node/config/def.go index 11480a593..3b1020840 100644 --- a/node/config/def.go +++ b/node/config/def.go @@ -19,6 +19,7 @@ type API struct { // Libp2p contains configs for libp2p type Libp2p struct { ListenAddresses []string + BootstrapPeers []string } type Metrics struct { diff --git a/node/modules/core.go b/node/modules/core.go index bade468bf..8aaa52393 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/lib/addrutil" "github.com/filecoin-project/go-lotus/node/modules/helpers" "github.com/gbrlsnchs/jwt/v3" logging "github.com/ipfs/go-log" @@ -76,7 +77,17 @@ func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*dtypes.APIAlg, err return (*dtypes.APIAlg)(jwt.NewHS256(key.PrivateKey)), nil } -func Bootstrap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) { +func ConfigBootstrap(peers []string) func() (dtypes.BootstrapPeers, error) { + return func() (dtypes.BootstrapPeers, error) { + return addrutil.ParseAddresses(context.TODO(), peers) + } +} + +func BuiltinBootstrap() (dtypes.BootstrapPeers, error) { + return build.BuiltinBootstrap() +} + +func Bootstrap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, pinfos dtypes.BootstrapPeers) { ctx, cancel := context.WithCancel(mctx) lc.Append(fx.Hook{ @@ -97,13 +108,7 @@ func Bootstrap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host) { log.Warn("No peers connected, performing automatic bootstrap") - pis, err := build.BuiltinBootstrap() - if err != nil { - log.Error("Getting bootstrap addrs: ", err) - return - } - - for _, pi := range pis { + for _, pi := range pinfos { if err := host.Connect(ctx, pi); err != nil { log.Warn("bootstrap connect failed: ", err) } diff --git a/node/modules/dtypes/bootstrap.go b/node/modules/dtypes/bootstrap.go new file mode 100644 index 000000000..0b46f63c3 --- /dev/null +++ b/node/modules/dtypes/bootstrap.go @@ -0,0 +1,5 @@ +package dtypes + +import "github.com/libp2p/go-libp2p-core/peer" + +type BootstrapPeers []peer.AddrInfo