From 5ed695a84aeffae6c68078aa1e4f753429302f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 10 Jul 2019 15:06:04 +0200 Subject: [PATCH] Expose some node settings --- node/builder.go | 20 ++++++++++---------- node/node_test.go | 7 ++++--- node/options.go | 10 +++++----- node/opts_test.go | 20 ++++++++++++++++++++ node/testopts.go | 19 ------------------- 5 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 node/opts_test.go delete mode 100644 node/testopts.go diff --git a/node/builder.go b/node/builder.go index b9199761f..ec3bfb313 100644 --- a/node/builder.go +++ b/node/builder.go @@ -68,7 +68,7 @@ const ( _nInvokes // keep this last ) -type settings struct { +type Settings struct { // modules is a map of constructors for DI // // In most cases the index will be a reflect. Type of element returned by @@ -80,13 +80,13 @@ type settings struct { // type, and must be applied in correct order invokes []fx.Option - online bool // Online option applied - config bool // Config option applied + Online bool // Online option applied + Config bool // Config option applied } // Override option changes constructor for a given type func Override(typ, constructor interface{}) Option { - return func(s *settings) error { + return func(s *Settings) error { if i, ok := typ.(invoke); ok { s.invokes[i] = fx.Invoke(constructor) return nil @@ -127,8 +127,8 @@ func Online() Option { return Options( // make sure that online is applied before Config. // This is important because Config overrides some of Online units - func(s *settings) error { s.online = true; return nil }, - applyIf(func(s *settings) bool { return s.config }, + func(s *Settings) error { s.Online = true; return nil }, + ApplyIf(func(s *Settings) bool { return s.Config }, Error(errors.New("the Online option must be set before Config option")), ), @@ -182,12 +182,12 @@ func Online() Option { ) } -// Config sets up constructors based on the provided config +// Config sets up constructors based on the provided Config func Config(cfg *config.Root) Option { return Options( - func(s *settings) error { s.config = true; return nil }, + func(s *Settings) error { s.Config = true; return nil }, - applyIf(func(s *settings) bool { return s.online }, + ApplyIf(func(s *Settings) bool { return s.Online }, Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)), ), ) @@ -196,7 +196,7 @@ func Config(cfg *config.Root) Option { // New builds and starts new Filecoin node func New(ctx context.Context, opts ...Option) (api.API, error) { resAPI := &API{} - settings := settings{ + settings := Settings{ modules: map[interface{}]fx.Option{}, invokes: make([]fx.Option, _nInvokes), } diff --git a/node/node_test.go b/node/node_test.go index fd050dd7e..496e0a4e9 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -1,7 +1,8 @@ -package node +package node_test import ( "context" + "github.com/filecoin-project/go-lotus/node" "net/http/httptest" "testing" @@ -21,8 +22,8 @@ func builder(t *testing.T, n int) []api.API { for i := 0; i < n; i++ { var err error - out[i], err = New(ctx, - Online(), + out[i], err = node.New(ctx, + node.Online(), MockHost(mn), ) if err != nil { diff --git a/node/options.go b/node/options.go index b1436c96b..4d2ed9ced 100644 --- a/node/options.go +++ b/node/options.go @@ -8,11 +8,11 @@ import ( // change how the node is constructed // // Options are applied in sequence -type Option func(*settings) error +type Option func(*Settings) error // Options groups multiple options into one func Options(opts ...Option) Option { - return func(s *settings) error { + return func(s *Settings) error { for _, opt := range opts { if err := opt(s); err != nil { return err @@ -24,13 +24,13 @@ func Options(opts ...Option) Option { // Error is a special option which returns an error when applied func Error(err error) Option { - return func(_ *settings) error { + return func(_ *Settings) error { return err } } -func applyIf(check func(s *settings) bool, opts ...Option) Option { - return func(s *settings) error { +func ApplyIf(check func(s *Settings) bool, opts ...Option) Option { + return func(s *Settings) error { if check(s) { return Options(opts...)(s) } diff --git a/node/opts_test.go b/node/opts_test.go new file mode 100644 index 000000000..c6cdf1bfa --- /dev/null +++ b/node/opts_test.go @@ -0,0 +1,20 @@ +package node_test + +import ( + "errors" + "github.com/filecoin-project/go-lotus/node" + + "github.com/filecoin-project/go-lotus/node/modules/lp2p" + mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" +) + +func MockHost(mn mocknet.Mocknet) node.Option { + return node.Options( + node.ApplyIf(func(s *node.Settings) bool { return !s.Online }, + node.Error(errors.New("MockHost must be specified after Online")), + ), + + node.Override(new(lp2p.RawHost), lp2p.MockHost), + node.Override(new(mocknet.Mocknet), mn), + ) +} diff --git a/node/testopts.go b/node/testopts.go deleted file mode 100644 index 4ed4eab23..000000000 --- a/node/testopts.go +++ /dev/null @@ -1,19 +0,0 @@ -package node - -import ( - "errors" - - "github.com/filecoin-project/go-lotus/node/modules/lp2p" - mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" -) - -func MockHost(mn mocknet.Mocknet) Option { - return Options( - applyIf(func(s *settings) bool { return !s.online }, - Error(errors.New("MockHost must be specified after Online")), - ), - - Override(new(lp2p.RawHost), lp2p.MockHost), - Override(new(mocknet.Mocknet), mn), - ) -}