Expose some node settings
This commit is contained in:
parent
4797e6a473
commit
5ed695a84a
@ -68,7 +68,7 @@ const (
|
|||||||
_nInvokes // keep this last
|
_nInvokes // keep this last
|
||||||
)
|
)
|
||||||
|
|
||||||
type settings struct {
|
type Settings struct {
|
||||||
// modules is a map of constructors for DI
|
// modules is a map of constructors for DI
|
||||||
//
|
//
|
||||||
// In most cases the index will be a reflect. Type of element returned by
|
// 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
|
// type, and must be applied in correct order
|
||||||
invokes []fx.Option
|
invokes []fx.Option
|
||||||
|
|
||||||
online bool // Online option applied
|
Online bool // Online option applied
|
||||||
config bool // Config option applied
|
Config bool // Config option applied
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override option changes constructor for a given type
|
// Override option changes constructor for a given type
|
||||||
func Override(typ, constructor interface{}) Option {
|
func Override(typ, constructor interface{}) Option {
|
||||||
return func(s *settings) error {
|
return func(s *Settings) error {
|
||||||
if i, ok := typ.(invoke); ok {
|
if i, ok := typ.(invoke); ok {
|
||||||
s.invokes[i] = fx.Invoke(constructor)
|
s.invokes[i] = fx.Invoke(constructor)
|
||||||
return nil
|
return nil
|
||||||
@ -127,8 +127,8 @@ func Online() Option {
|
|||||||
return Options(
|
return Options(
|
||||||
// make sure that online is applied before Config.
|
// make sure that online is applied before Config.
|
||||||
// This is important because Config overrides some of Online units
|
// This is important because Config overrides some of Online units
|
||||||
func(s *settings) error { s.online = true; return nil },
|
func(s *Settings) error { s.Online = true; return nil },
|
||||||
applyIf(func(s *settings) bool { return s.config },
|
ApplyIf(func(s *Settings) bool { return s.Config },
|
||||||
Error(errors.New("the Online option must be set before Config option")),
|
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 {
|
func Config(cfg *config.Root) Option {
|
||||||
return Options(
|
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)),
|
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@ -196,7 +196,7 @@ func Config(cfg *config.Root) Option {
|
|||||||
// New builds and starts new Filecoin node
|
// New builds and starts new Filecoin node
|
||||||
func New(ctx context.Context, opts ...Option) (api.API, error) {
|
func New(ctx context.Context, opts ...Option) (api.API, error) {
|
||||||
resAPI := &API{}
|
resAPI := &API{}
|
||||||
settings := settings{
|
settings := Settings{
|
||||||
modules: map[interface{}]fx.Option{},
|
modules: map[interface{}]fx.Option{},
|
||||||
invokes: make([]fx.Option, _nInvokes),
|
invokes: make([]fx.Option, _nInvokes),
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
package node
|
package node_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/filecoin-project/go-lotus/node"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -21,8 +22,8 @@ func builder(t *testing.T, n int) []api.API {
|
|||||||
|
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
var err error
|
var err error
|
||||||
out[i], err = New(ctx,
|
out[i], err = node.New(ctx,
|
||||||
Online(),
|
node.Online(),
|
||||||
MockHost(mn),
|
MockHost(mn),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -8,11 +8,11 @@ import (
|
|||||||
// change how the node is constructed
|
// change how the node is constructed
|
||||||
//
|
//
|
||||||
// Options are applied in sequence
|
// Options are applied in sequence
|
||||||
type Option func(*settings) error
|
type Option func(*Settings) error
|
||||||
|
|
||||||
// Options groups multiple options into one
|
// Options groups multiple options into one
|
||||||
func Options(opts ...Option) Option {
|
func Options(opts ...Option) Option {
|
||||||
return func(s *settings) error {
|
return func(s *Settings) error {
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
if err := opt(s); err != nil {
|
if err := opt(s); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -24,13 +24,13 @@ func Options(opts ...Option) Option {
|
|||||||
|
|
||||||
// Error is a special option which returns an error when applied
|
// Error is a special option which returns an error when applied
|
||||||
func Error(err error) Option {
|
func Error(err error) Option {
|
||||||
return func(_ *settings) error {
|
return func(_ *Settings) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyIf(check func(s *settings) bool, opts ...Option) Option {
|
func ApplyIf(check func(s *Settings) bool, opts ...Option) Option {
|
||||||
return func(s *settings) error {
|
return func(s *Settings) error {
|
||||||
if check(s) {
|
if check(s) {
|
||||||
return Options(opts...)(s)
|
return Options(opts...)(s)
|
||||||
}
|
}
|
||||||
|
20
node/opts_test.go
Normal file
20
node/opts_test.go
Normal file
@ -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),
|
||||||
|
)
|
||||||
|
}
|
@ -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),
|
|
||||||
)
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user