API struct instead of DI magic

This commit is contained in:
Łukasz Magiera 2019-07-09 12:58:13 +02:00
parent 82890165de
commit d852b3f7ef
2 changed files with 28 additions and 69 deletions

View File

@ -2,91 +2,50 @@ package node
import (
"context"
"reflect"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/build"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/fx"
)
var errTyp = reflect.TypeOf(new(error)).Elem()
// TODO: type checking, this isn't JS
func provideApi(f interface{}, toProvide interface{}) fx.Option {
rf := reflect.ValueOf(f)
tp := reflect.ValueOf(toProvide).Elem()
ins := make([]reflect.Type, rf.Type().NumIn())
for i := range ins {
ins[i] = rf.Type().In(i)
}
ctyp := reflect.FuncOf(ins, []reflect.Type{errTyp}, rf.Type().IsVariadic())
return fx.Invoke(reflect.MakeFunc(ctyp, func(args []reflect.Value) (results []reflect.Value) {
provided := rf.Call(args)
tp.Set(provided[0].Elem().Convert(tp.Type()))
return []reflect.Value{reflect.ValueOf(new(error)).Elem()}
}).Interface())
type API struct {
Host host.Host
}
func apiOption(resAPI *api.Struct) fx.Option {
in := &resAPI.Internal
return fx.Options(
provideApi(versionAPI, &in.Version),
provideApi(idAPI, &in.ID),
provideApi(netPeersAPI, &in.NetPeers),
provideApi(netConnectAPI, &in.NetConnect),
provideApi(netAddrsListenAPI, &in.NetAddrsListen),
)
func (a *API) ID(context.Context) (peer.ID, error) {
return a.Host.ID(), nil
}
func idAPI(id peer.ID) interface{} {
return func(ctx context.Context) (peer.ID, error) {
return id, nil
}
func (a *API) Version(context.Context) (api.Version, error) {
return api.Version{
Version: build.Version,
}, nil
}
func versionAPI() interface{} {
return func(context.Context) (api.Version, error) {
return api.Version{
Version: build.Version,
}, nil
}
}
func (a *API) NetPeers(context.Context) ([]peer.AddrInfo, error) {
conns := a.Host.Network().Conns()
out := make([]peer.AddrInfo, len(conns))
func netPeersAPI(h host.Host) interface{} {
return func(ctx context.Context) ([]peer.AddrInfo, error) {
conns := h.Network().Conns()
out := make([]peer.AddrInfo, len(conns))
for i, conn := range conns {
out[i] = peer.AddrInfo{
ID: conn.RemotePeer(),
Addrs: []ma.Multiaddr{
conn.RemoteMultiaddr(),
},
}
for i, conn := range conns {
out[i] = peer.AddrInfo{
ID: conn.RemotePeer(),
Addrs: []ma.Multiaddr{
conn.RemoteMultiaddr(),
},
}
return out, nil
}
return out, nil
}
func netConnectAPI(h host.Host) interface{} {
return func(ctx context.Context, p peer.AddrInfo) error {
return h.Connect(ctx, p)
}
func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return a.Host.Connect(ctx, p)
}
func netAddrsListenAPI(h host.Host) interface{} {
return func(context.Context) ([]ma.Multiaddr, error) {
return h.Addrs(), nil
}
func (a *API) NetAddrsListen(context.Context) ([]ma.Multiaddr, error) {
return a.Host.Addrs(), nil
}
var _ api.API = &API{}

View File

@ -193,7 +193,7 @@ func Config(cfg *config.Root) Option {
// New builds and starts new Filecoin node
func New(ctx context.Context, opts ...Option) (api.API, error) {
var resAPI api.Struct
resAPI := &API{}
settings := settings{
modules: map[interface{}]fx.Option{},
invokes: make([]fx.Option, _nInvokes),
@ -221,7 +221,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
fx.Options(ctors...),
fx.Options(settings.invokes...),
apiOption(&resAPI),
fx.Extract(resAPI),
)
// TODO: we probably should have a 'firewall' for Closing signal
@ -231,7 +231,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
return nil, err
}
return &resAPI, nil
return resAPI, nil
}
// In-memory / testing