Some net commands / apis
This commit is contained in:
+85
@@ -0,0 +1,85 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"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"
|
||||
"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())
|
||||
}
|
||||
|
||||
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),
|
||||
)
|
||||
}
|
||||
|
||||
func idAPI(id peer.ID) interface{} {
|
||||
return func(ctx context.Context) (peer.ID, error) {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
|
||||
func versionAPI() interface{} {
|
||||
return func(context.Context) (api.Version, error) {
|
||||
return api.Version{
|
||||
Version: build.Version,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
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: []multiaddr.Multiaddr{
|
||||
conn.RemoteMultiaddr(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
|
||||
func netConnectAPI(h host.Host) interface{} {
|
||||
return func(ctx context.Context, p peer.AddrInfo) error {
|
||||
return errors.New("nope")
|
||||
}
|
||||
}
|
||||
+1
-24
@@ -20,7 +20,6 @@ import (
|
||||
"go.uber.org/fx"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/api"
|
||||
"github.com/filecoin-project/go-lotus/build"
|
||||
"github.com/filecoin-project/go-lotus/chain"
|
||||
"github.com/filecoin-project/go-lotus/node/config"
|
||||
"github.com/filecoin-project/go-lotus/node/hello"
|
||||
@@ -222,8 +221,7 @@ func New(ctx context.Context, opts ...Option) (api.API, error) {
|
||||
fx.Options(ctors...),
|
||||
fx.Options(settings.invokes...),
|
||||
|
||||
fx.Invoke(versionAPI(&resAPI.Internal.Version)),
|
||||
fx.Invoke(idAPI(&resAPI.Internal.ID)),
|
||||
apiOption(&resAPI),
|
||||
)
|
||||
|
||||
// TODO: we probably should have a 'firewall' for Closing signal
|
||||
@@ -250,24 +248,3 @@ func randomIdentity() Option {
|
||||
Override(new(peer.ID), peer.IDFromPublicKey),
|
||||
)
|
||||
}
|
||||
|
||||
// API IMPL
|
||||
|
||||
// TODO: figure out a better way, this isn't usable in long term
|
||||
func idAPI(set *func(ctx context.Context) (peer.ID, error)) func(id peer.ID) {
|
||||
return func(id peer.ID) {
|
||||
*set = func(ctx context.Context) (peer.ID, error) {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func versionAPI(set *func(context.Context) (api.Version, error)) func() {
|
||||
return func() {
|
||||
*set = func(context.Context) (api.Version, error) {
|
||||
return api.Version{
|
||||
Version: build.Version,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user