api: Separate the Net interface from Common

This commit is contained in:
Łukasz Magiera 2021-06-29 14:07:00 +02:00
parent f07a370711
commit 49e26cce7d
26 changed files with 485 additions and 487 deletions

View File

@ -4,15 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
apitypes "github.com/filecoin-project/lotus/api/types"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/filecoin-project/go-jsonrpc/auth" "github.com/filecoin-project/go-jsonrpc/auth"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol"
apitypes "github.com/filecoin-project/lotus/api/types"
) )
// MODIFYING THE API INTERFACE // MODIFYING THE API INTERFACE
@ -27,55 +23,23 @@ import (
// * Generate openrpc blobs // * Generate openrpc blobs
type Common interface { type Common interface {
// MethodGroup: Auth // MethodGroup: Auth
AuthVerify(ctx context.Context, token string) ([]auth.Permission, error) //perm:read AuthVerify(ctx context.Context, token string) ([]auth.Permission, error) //perm:read
AuthNew(ctx context.Context, perms []auth.Permission) ([]byte, error) //perm:admin AuthNew(ctx context.Context, perms []auth.Permission) ([]byte, error) //perm:admin
// MethodGroup: Net // MethodGroup: Log
NetConnectedness(context.Context, peer.ID) (network.Connectedness, error) //perm:read LogList(context.Context) ([]string, error) //perm:write
NetPeers(context.Context) ([]peer.AddrInfo, error) //perm:read LogSetLevel(context.Context, string, string) error //perm:write
NetConnect(context.Context, peer.AddrInfo) error //perm:write
NetAddrsListen(context.Context) (peer.AddrInfo, error) //perm:read
NetDisconnect(context.Context, peer.ID) error //perm:write
NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error) //perm:read
NetPubsubScores(context.Context) ([]PubsubScore, error) //perm:read
NetAutoNatStatus(context.Context) (NatInfo, error) //perm:read
NetAgentVersion(ctx context.Context, p peer.ID) (string, error) //perm:read
NetPeerInfo(context.Context, peer.ID) (*ExtendedPeerInfo, error) //perm:read
// NetBandwidthStats returns statistics about the nodes total bandwidth
// usage and current rate across all peers and protocols.
NetBandwidthStats(ctx context.Context) (metrics.Stats, error) //perm:read
// NetBandwidthStatsByPeer returns statistics about the nodes bandwidth
// usage and current rate per peer
NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) //perm:read
// NetBandwidthStatsByProtocol returns statistics about the nodes bandwidth
// usage and current rate per protocol
NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) //perm:read
// ConnectionGater API
NetBlockAdd(ctx context.Context, acl NetBlockList) error //perm:admin
NetBlockRemove(ctx context.Context, acl NetBlockList) error //perm:admin
NetBlockList(ctx context.Context) (NetBlockList, error) //perm:read
// MethodGroup: Common // MethodGroup: Common
// Discover returns an OpenRPC document describing an RPC API.
Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) //perm:read
// ID returns peerID of libp2p node backing this API
ID(context.Context) (peer.ID, error) //perm:read
// Version provides information about API provider // Version provides information about API provider
Version(context.Context) (APIVersion, error) //perm:read Version(context.Context) (APIVersion, error) //perm:read
LogList(context.Context) ([]string, error) //perm:write // Discover returns an OpenRPC document describing an RPC API.
LogSetLevel(context.Context, string, string) error //perm:write Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) //perm:read
// trigger graceful shutdown // trigger graceful shutdown
Shutdown(context.Context) error //perm:admin Shutdown(context.Context) error //perm:admin
@ -105,8 +69,3 @@ type APIVersion struct {
func (v APIVersion) String() string { func (v APIVersion) String() string {
return fmt.Sprintf("%s+api%s", v.Version, v.APIVersion.String()) return fmt.Sprintf("%s+api%s", v.Version, v.APIVersion.String())
} }
type NatInfo struct {
Reachability network.Reachability
PublicAddr string
}

View File

@ -58,6 +58,7 @@ const LookbackNoLimit = abi.ChainEpoch(-1)
// FullNode API is a low-level interface to the Filecoin network full node // FullNode API is a low-level interface to the Filecoin network full node
type FullNode interface { type FullNode interface {
Common Common
Net
// MethodGroup: Chain // MethodGroup: Chain
// The Chain method group contains methods for interacting with the // The Chain method group contains methods for interacting with the

68
api/api_net.go Normal file
View File

@ -0,0 +1,68 @@
package api
import (
"context"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
)
// MODIFYING THE API INTERFACE
//
// When adding / changing methods in this file:
// * Do the change here
// * Adjust implementation in `node/impl/`
// * Run `make gen` - this will:
// * Generate proxy structs
// * Generate mocks
// * Generate markdown docs
// * Generate openrpc blobs
type Net interface {
// MethodGroup: Net
NetConnectedness(context.Context, peer.ID) (network.Connectedness, error) //perm:read
NetPeers(context.Context) ([]peer.AddrInfo, error) //perm:read
NetConnect(context.Context, peer.AddrInfo) error //perm:write
NetAddrsListen(context.Context) (peer.AddrInfo, error) //perm:read
NetDisconnect(context.Context, peer.ID) error //perm:write
NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error) //perm:read
NetPubsubScores(context.Context) ([]PubsubScore, error) //perm:read
NetAutoNatStatus(context.Context) (NatInfo, error) //perm:read
NetAgentVersion(ctx context.Context, p peer.ID) (string, error) //perm:read
NetPeerInfo(context.Context, peer.ID) (*ExtendedPeerInfo, error) //perm:read
// NetBandwidthStats returns statistics about the nodes total bandwidth
// usage and current rate across all peers and protocols.
NetBandwidthStats(ctx context.Context) (metrics.Stats, error) //perm:read
// NetBandwidthStatsByPeer returns statistics about the nodes bandwidth
// usage and current rate per peer
NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) //perm:read
// NetBandwidthStatsByProtocol returns statistics about the nodes bandwidth
// usage and current rate per protocol
NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) //perm:read
// ConnectionGater API
NetBlockAdd(ctx context.Context, acl NetBlockList) error //perm:admin
NetBlockRemove(ctx context.Context, acl NetBlockList) error //perm:admin
NetBlockList(ctx context.Context) (NetBlockList, error) //perm:read
// MethodGroup: Common
// ID returns peerID of libp2p node backing this API
ID(context.Context) (peer.ID, error) //perm:read
}
type CommonNet interface {
Common
Net
}
type NatInfo struct {
Reachability network.Reachability
PublicAddr string
}

View File

@ -41,6 +41,7 @@ import (
// StorageMiner is a low-level interface to the Filecoin network storage miner node // StorageMiner is a low-level interface to the Filecoin network storage miner node
type StorageMiner interface { type StorageMiner interface {
Common Common
Net
ActorAddress(context.Context) (address.Address, error) //perm:read ActorAddress(context.Context) (address.Address, error) //perm:read

View File

@ -16,14 +16,10 @@ import (
) )
// NewCommonRPCV0 creates a new http jsonrpc client. // NewCommonRPCV0 creates a new http jsonrpc client.
func NewCommonRPCV0(ctx context.Context, addr string, requestHeader http.Header) (api.Common, jsonrpc.ClientCloser, error) { func NewCommonRPCV0(ctx context.Context, addr string, requestHeader http.Header) (api.CommonNet, jsonrpc.ClientCloser, error) {
var res v0api.CommonStruct var res v0api.CommonNetStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res), requestHeader)
&res.Internal,
},
requestHeader,
)
return &res, closer, err return &res, closer, err
} }
@ -31,11 +27,9 @@ func NewCommonRPCV0(ctx context.Context, addr string, requestHeader http.Header)
// NewFullNodeRPCV0 creates a new http jsonrpc client. // NewFullNodeRPCV0 creates a new http jsonrpc client.
func NewFullNodeRPCV0(ctx context.Context, addr string, requestHeader http.Header) (v0api.FullNode, jsonrpc.ClientCloser, error) { func NewFullNodeRPCV0(ctx context.Context, addr string, requestHeader http.Header) (v0api.FullNode, jsonrpc.ClientCloser, error) {
var res v0api.FullNodeStruct var res v0api.FullNodeStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res), requestHeader)
&res.CommonStruct.Internal,
&res.Internal,
}, requestHeader)
return &res, closer, err return &res, closer, err
} }
@ -44,10 +38,7 @@ func NewFullNodeRPCV0(ctx context.Context, addr string, requestHeader http.Heade
func NewFullNodeRPCV1(ctx context.Context, addr string, requestHeader http.Header) (api.FullNode, jsonrpc.ClientCloser, error) { func NewFullNodeRPCV1(ctx context.Context, addr string, requestHeader http.Header) (api.FullNode, jsonrpc.ClientCloser, error) {
var res v1api.FullNodeStruct var res v1api.FullNodeStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res), requestHeader)
&res.CommonStruct.Internal,
&res.Internal,
}, requestHeader)
return &res, closer, err return &res, closer, err
} }
@ -78,15 +69,10 @@ func NewStorageMinerRPCV0(ctx context.Context, addr string, requestHeader http.H
var res v0api.StorageMinerStruct var res v0api.StorageMinerStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res), requestHeader,
&res.CommonStruct.Internal,
&res.Internal,
},
requestHeader,
append([]jsonrpc.Option{ append([]jsonrpc.Option{
rpcenc.ReaderParamEncoder(pushUrl), rpcenc.ReaderParamEncoder(pushUrl),
}, opts...)..., }, opts...)...)
)
return &res, closer, err return &res, closer, err
} }
@ -99,9 +85,7 @@ func NewWorkerRPCV0(ctx context.Context, addr string, requestHeader http.Header)
var res api.WorkerStruct var res api.WorkerStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res),
&res.Internal,
},
requestHeader, requestHeader,
rpcenc.ReaderParamEncoder(pushUrl), rpcenc.ReaderParamEncoder(pushUrl),
jsonrpc.WithNoReconnect(), jsonrpc.WithNoReconnect(),
@ -115,9 +99,7 @@ func NewWorkerRPCV0(ctx context.Context, addr string, requestHeader http.Header)
func NewGatewayRPCV1(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.Gateway, jsonrpc.ClientCloser, error) { func NewGatewayRPCV1(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.Gateway, jsonrpc.ClientCloser, error) {
var res api.GatewayStruct var res api.GatewayStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res),
&res.Internal,
},
requestHeader, requestHeader,
opts..., opts...,
) )
@ -129,9 +111,7 @@ func NewGatewayRPCV1(ctx context.Context, addr string, requestHeader http.Header
func NewGatewayRPCV0(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (v0api.Gateway, jsonrpc.ClientCloser, error) { func NewGatewayRPCV0(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (v0api.Gateway, jsonrpc.ClientCloser, error) {
var res v0api.GatewayStruct var res v0api.GatewayStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res),
&res.Internal,
},
requestHeader, requestHeader,
opts..., opts...,
) )
@ -142,9 +122,7 @@ func NewGatewayRPCV0(ctx context.Context, addr string, requestHeader http.Header
func NewWalletRPCV0(ctx context.Context, addr string, requestHeader http.Header) (api.Wallet, jsonrpc.ClientCloser, error) { func NewWalletRPCV0(ctx context.Context, addr string, requestHeader http.Header) (api.Wallet, jsonrpc.ClientCloser, error) {
var res api.WalletStruct var res api.WalletStruct
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin", closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
[]interface{}{ api.GetInternalStructs(&res),
&res.Internal,
},
requestHeader, requestHeader,
) )

View File

@ -34,7 +34,7 @@ func main() {
doc := docgen_openrpc.NewLotusOpenRPCDocument(Comments, GroupDocs) doc := docgen_openrpc.NewLotusOpenRPCDocument(Comments, GroupDocs)
i, _, _, _ := docgen.GetAPIType(os.Args[2], os.Args[3]) i, _, _ := docgen.GetAPIType(os.Args[2], os.Args[3])
doc.RegisterReceiverName("Filecoin", i) doc.RegisterReceiverName("Filecoin", i)
out, err := doc.Discover() out, err := doc.Discover()

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"reflect"
"sort" "sort"
"strings" "strings"
@ -15,7 +16,7 @@ func main() {
groups := make(map[string]*docgen.MethodGroup) groups := make(map[string]*docgen.MethodGroup)
_, t, permStruct, commonPermStruct := docgen.GetAPIType(os.Args[2], os.Args[3]) _, t, permStruct := docgen.GetAPIType(os.Args[2], os.Args[3])
for i := 0; i < t.NumMethod(); i++ { for i := 0; i < t.NumMethod(); i++ {
m := t.Method(i) m := t.Method(i)
@ -88,13 +89,17 @@ func main() {
fmt.Printf("### %s\n", m.Name) fmt.Printf("### %s\n", m.Name)
fmt.Printf("%s\n\n", m.Comment) fmt.Printf("%s\n\n", m.Comment)
meth, ok := permStruct.FieldByName(m.Name) var meth reflect.StructField
if !ok { var ok bool
meth, ok = commonPermStruct.FieldByName(m.Name) for _, ps := range permStruct {
if !ok { meth, ok = ps.FieldByName(m.Name)
panic("no perms for method: " + m.Name) if ok {
break
} }
} }
if !ok {
panic("no perms for method: " + m.Name)
}
perms := meth.Tag.Get("perm") perms := meth.Tag.Get("perm")

View File

@ -266,25 +266,27 @@ func init() {
addExample(map[string]interface{}{"abc": 123}) addExample(map[string]interface{}{"abc": 123})
} }
func GetAPIType(name, pkg string) (i interface{}, t, permStruct, commonPermStruct reflect.Type) { func GetAPIType(name, pkg string) (i interface{}, t reflect.Type, permStruct []reflect.Type) {
switch pkg { switch pkg {
case "api": // latest case "api": // latest
switch name { switch name {
case "FullNode": case "FullNode":
i = &api.FullNodeStruct{} i = &api.FullNodeStruct{}
t = reflect.TypeOf(new(struct{ api.FullNode })).Elem() t = reflect.TypeOf(new(struct{ api.FullNode })).Elem()
permStruct = reflect.TypeOf(api.FullNodeStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(api.FullNodeStruct{}.Internal))
commonPermStruct = reflect.TypeOf(api.CommonStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(api.CommonStruct{}.Internal))
permStruct = append(permStruct, reflect.TypeOf(api.NetStruct{}.Internal))
case "StorageMiner": case "StorageMiner":
i = &api.StorageMinerStruct{} i = &api.StorageMinerStruct{}
t = reflect.TypeOf(new(struct{ api.StorageMiner })).Elem() t = reflect.TypeOf(new(struct{ api.StorageMiner })).Elem()
permStruct = reflect.TypeOf(api.StorageMinerStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(api.StorageMinerStruct{}.Internal))
commonPermStruct = reflect.TypeOf(api.CommonStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(api.CommonStruct{}.Internal))
permStruct = append(permStruct, reflect.TypeOf(api.NetStruct{}.Internal))
case "Worker": case "Worker":
i = &api.WorkerStruct{} i = &api.WorkerStruct{}
t = reflect.TypeOf(new(struct{ api.Worker })).Elem() t = reflect.TypeOf(new(struct{ api.Worker })).Elem()
permStruct = reflect.TypeOf(api.WorkerStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(api.WorkerStruct{}.Internal))
commonPermStruct = reflect.TypeOf(api.WorkerStruct{}.Internal)
default: default:
panic("unknown type") panic("unknown type")
} }
@ -293,8 +295,9 @@ func GetAPIType(name, pkg string) (i interface{}, t, permStruct, commonPermStruc
case "FullNode": case "FullNode":
i = v0api.FullNodeStruct{} i = v0api.FullNodeStruct{}
t = reflect.TypeOf(new(struct{ v0api.FullNode })).Elem() t = reflect.TypeOf(new(struct{ v0api.FullNode })).Elem()
permStruct = reflect.TypeOf(v0api.FullNodeStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(v0api.FullNodeStruct{}.Internal))
commonPermStruct = reflect.TypeOf(v0api.CommonStruct{}.Internal) permStruct = append(permStruct, reflect.TypeOf(v0api.CommonStruct{}.Internal))
permStruct = append(permStruct, reflect.TypeOf(v0api.NetStruct{}.Internal))
default: default:
panic("unknown type") panic("unknown type")
} }

View File

@ -16,28 +16,33 @@ const (
var AllPermissions = []auth.Permission{PermRead, PermWrite, PermSign, PermAdmin} var AllPermissions = []auth.Permission{PermRead, PermWrite, PermSign, PermAdmin}
var DefaultPerms = []auth.Permission{PermRead} var DefaultPerms = []auth.Permission{PermRead}
func permissionedProxies(in, out interface{}) {
outs := GetInternalStructs(out)
for _, o := range outs {
auth.PermissionedProxy(AllPermissions, DefaultPerms, in, o)
}
}
func PermissionedStorMinerAPI(a StorageMiner) StorageMiner { func PermissionedStorMinerAPI(a StorageMiner) StorageMiner {
var out StorageMinerStruct var out StorageMinerStruct
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal) permissionedProxies(a, &out)
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.CommonStruct.Internal)
return &out return &out
} }
func PermissionedFullAPI(a FullNode) FullNode { func PermissionedFullAPI(a FullNode) FullNode {
var out FullNodeStruct var out FullNodeStruct
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal) permissionedProxies(a, &out)
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.CommonStruct.Internal)
return &out return &out
} }
func PermissionedWorkerAPI(a Worker) Worker { func PermissionedWorkerAPI(a Worker) Worker {
var out WorkerStruct var out WorkerStruct
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal) permissionedProxies(a, &out)
return &out return &out
} }
func PermissionedWalletAPI(a Wallet) Wallet { func PermissionedWalletAPI(a Wallet) Wallet {
var out WalletStruct var out WalletStruct
auth.PermissionedProxy(AllPermissions, DefaultPerms, a, &out.Internal) permissionedProxies(a, &out)
return &out return &out
} }

View File

@ -35,7 +35,7 @@ import (
metrics "github.com/libp2p/go-libp2p-core/metrics" metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-libp2p-core/protocol"
xerrors "golang.org/x/xerrors" xerrors "golang.org/x/xerrors"
) )
@ -60,44 +60,10 @@ type CommonStruct struct {
Discover func(p0 context.Context) (apitypes.OpenRPCDocument, error) `perm:"read"` Discover func(p0 context.Context) (apitypes.OpenRPCDocument, error) `perm:"read"`
ID func(p0 context.Context) (peer.ID, error) `perm:"read"`
LogList func(p0 context.Context) ([]string, error) `perm:"write"` LogList func(p0 context.Context) ([]string, error) `perm:"write"`
LogSetLevel func(p0 context.Context, p1 string, p2 string) error `perm:"write"` LogSetLevel func(p0 context.Context, p1 string, p2 string) error `perm:"write"`
NetAddrsListen func(p0 context.Context) (peer.AddrInfo, error) `perm:"read"`
NetAgentVersion func(p0 context.Context, p1 peer.ID) (string, error) `perm:"read"`
NetAutoNatStatus func(p0 context.Context) (NatInfo, error) `perm:"read"`
NetBandwidthStats func(p0 context.Context) (metrics.Stats, error) `perm:"read"`
NetBandwidthStatsByPeer func(p0 context.Context) (map[string]metrics.Stats, error) `perm:"read"`
NetBandwidthStatsByProtocol func(p0 context.Context) (map[protocol.ID]metrics.Stats, error) `perm:"read"`
NetBlockAdd func(p0 context.Context, p1 NetBlockList) error `perm:"admin"`
NetBlockList func(p0 context.Context) (NetBlockList, error) `perm:"read"`
NetBlockRemove func(p0 context.Context, p1 NetBlockList) error `perm:"admin"`
NetConnect func(p0 context.Context, p1 peer.AddrInfo) error `perm:"write"`
NetConnectedness func(p0 context.Context, p1 peer.ID) (network.Connectedness, error) `perm:"read"`
NetDisconnect func(p0 context.Context, p1 peer.ID) error `perm:"write"`
NetFindPeer func(p0 context.Context, p1 peer.ID) (peer.AddrInfo, error) `perm:"read"`
NetPeerInfo func(p0 context.Context, p1 peer.ID) (*ExtendedPeerInfo, error) `perm:"read"`
NetPeers func(p0 context.Context) ([]peer.AddrInfo, error) `perm:"read"`
NetPubsubScores func(p0 context.Context) ([]PubsubScore, error) `perm:"read"`
Session func(p0 context.Context) (uuid.UUID, error) `perm:"read"` Session func(p0 context.Context) (uuid.UUID, error) `perm:"read"`
Shutdown func(p0 context.Context) error `perm:"admin"` Shutdown func(p0 context.Context) error `perm:"admin"`
@ -109,9 +75,26 @@ type CommonStruct struct {
type CommonStub struct { type CommonStub struct {
} }
type CommonNetStruct struct {
CommonStruct
NetStruct
Internal struct {
}
}
type CommonNetStub struct {
CommonStub
NetStub
}
type FullNodeStruct struct { type FullNodeStruct struct {
CommonStruct CommonStruct
NetStruct
Internal struct { Internal struct {
BeaconGetEntry func(p0 context.Context, p1 abi.ChainEpoch) (*types.BeaconEntry, error) `perm:"read"` BeaconGetEntry func(p0 context.Context, p1 abi.ChainEpoch) (*types.BeaconEntry, error) `perm:"read"`
@ -473,6 +456,8 @@ type FullNodeStruct struct {
type FullNodeStub struct { type FullNodeStub struct {
CommonStub CommonStub
NetStub
} }
type GatewayStruct struct { type GatewayStruct struct {
@ -542,6 +527,47 @@ type GatewayStruct struct {
type GatewayStub struct { type GatewayStub struct {
} }
type NetStruct struct {
Internal struct {
ID func(p0 context.Context) (peer.ID, error) `perm:"read"`
NetAddrsListen func(p0 context.Context) (peer.AddrInfo, error) `perm:"read"`
NetAgentVersion func(p0 context.Context, p1 peer.ID) (string, error) `perm:"read"`
NetAutoNatStatus func(p0 context.Context) (NatInfo, error) `perm:"read"`
NetBandwidthStats func(p0 context.Context) (metrics.Stats, error) `perm:"read"`
NetBandwidthStatsByPeer func(p0 context.Context) (map[string]metrics.Stats, error) `perm:"read"`
NetBandwidthStatsByProtocol func(p0 context.Context) (map[protocol.ID]metrics.Stats, error) `perm:"read"`
NetBlockAdd func(p0 context.Context, p1 NetBlockList) error `perm:"admin"`
NetBlockList func(p0 context.Context) (NetBlockList, error) `perm:"read"`
NetBlockRemove func(p0 context.Context, p1 NetBlockList) error `perm:"admin"`
NetConnect func(p0 context.Context, p1 peer.AddrInfo) error `perm:"write"`
NetConnectedness func(p0 context.Context, p1 peer.ID) (network.Connectedness, error) `perm:"read"`
NetDisconnect func(p0 context.Context, p1 peer.ID) error `perm:"write"`
NetFindPeer func(p0 context.Context, p1 peer.ID) (peer.AddrInfo, error) `perm:"read"`
NetPeerInfo func(p0 context.Context, p1 peer.ID) (*ExtendedPeerInfo, error) `perm:"read"`
NetPeers func(p0 context.Context) ([]peer.AddrInfo, error) `perm:"read"`
NetPubsubScores func(p0 context.Context) ([]PubsubScore, error) `perm:"read"`
}
}
type NetStub struct {
}
type SignableStruct struct { type SignableStruct struct {
Internal struct { Internal struct {
Sign func(p0 context.Context, p1 SignFunc) error `` Sign func(p0 context.Context, p1 SignFunc) error ``
@ -554,6 +580,8 @@ type SignableStub struct {
type StorageMinerStruct struct { type StorageMinerStruct struct {
CommonStruct CommonStruct
NetStruct
Internal struct { Internal struct {
ActorAddress func(p0 context.Context) (address.Address, error) `perm:"read"` ActorAddress func(p0 context.Context) (address.Address, error) `perm:"read"`
@ -747,6 +775,8 @@ type StorageMinerStruct struct {
type StorageMinerStub struct { type StorageMinerStub struct {
CommonStub CommonStub
NetStub
} }
type WalletStruct struct { type WalletStruct struct {
@ -871,14 +901,6 @@ func (s *CommonStub) Discover(p0 context.Context) (apitypes.OpenRPCDocument, err
return *new(apitypes.OpenRPCDocument), xerrors.New("method not supported") return *new(apitypes.OpenRPCDocument), xerrors.New("method not supported")
} }
func (s *CommonStruct) ID(p0 context.Context) (peer.ID, error) {
return s.Internal.ID(p0)
}
func (s *CommonStub) ID(p0 context.Context) (peer.ID, error) {
return *new(peer.ID), xerrors.New("method not supported")
}
func (s *CommonStruct) LogList(p0 context.Context) ([]string, error) { func (s *CommonStruct) LogList(p0 context.Context) ([]string, error) {
return s.Internal.LogList(p0) return s.Internal.LogList(p0)
} }
@ -895,134 +917,6 @@ func (s *CommonStub) LogSetLevel(p0 context.Context, p1 string, p2 string) error
return xerrors.New("method not supported") return xerrors.New("method not supported")
} }
func (s *CommonStruct) NetAddrsListen(p0 context.Context) (peer.AddrInfo, error) {
return s.Internal.NetAddrsListen(p0)
}
func (s *CommonStub) NetAddrsListen(p0 context.Context) (peer.AddrInfo, error) {
return *new(peer.AddrInfo), xerrors.New("method not supported")
}
func (s *CommonStruct) NetAgentVersion(p0 context.Context, p1 peer.ID) (string, error) {
return s.Internal.NetAgentVersion(p0, p1)
}
func (s *CommonStub) NetAgentVersion(p0 context.Context, p1 peer.ID) (string, error) {
return "", xerrors.New("method not supported")
}
func (s *CommonStruct) NetAutoNatStatus(p0 context.Context) (NatInfo, error) {
return s.Internal.NetAutoNatStatus(p0)
}
func (s *CommonStub) NetAutoNatStatus(p0 context.Context) (NatInfo, error) {
return *new(NatInfo), xerrors.New("method not supported")
}
func (s *CommonStruct) NetBandwidthStats(p0 context.Context) (metrics.Stats, error) {
return s.Internal.NetBandwidthStats(p0)
}
func (s *CommonStub) NetBandwidthStats(p0 context.Context) (metrics.Stats, error) {
return *new(metrics.Stats), xerrors.New("method not supported")
}
func (s *CommonStruct) NetBandwidthStatsByPeer(p0 context.Context) (map[string]metrics.Stats, error) {
return s.Internal.NetBandwidthStatsByPeer(p0)
}
func (s *CommonStub) NetBandwidthStatsByPeer(p0 context.Context) (map[string]metrics.Stats, error) {
return *new(map[string]metrics.Stats), xerrors.New("method not supported")
}
func (s *CommonStruct) NetBandwidthStatsByProtocol(p0 context.Context) (map[protocol.ID]metrics.Stats, error) {
return s.Internal.NetBandwidthStatsByProtocol(p0)
}
func (s *CommonStub) NetBandwidthStatsByProtocol(p0 context.Context) (map[protocol.ID]metrics.Stats, error) {
return *new(map[protocol.ID]metrics.Stats), xerrors.New("method not supported")
}
func (s *CommonStruct) NetBlockAdd(p0 context.Context, p1 NetBlockList) error {
return s.Internal.NetBlockAdd(p0, p1)
}
func (s *CommonStub) NetBlockAdd(p0 context.Context, p1 NetBlockList) error {
return xerrors.New("method not supported")
}
func (s *CommonStruct) NetBlockList(p0 context.Context) (NetBlockList, error) {
return s.Internal.NetBlockList(p0)
}
func (s *CommonStub) NetBlockList(p0 context.Context) (NetBlockList, error) {
return *new(NetBlockList), xerrors.New("method not supported")
}
func (s *CommonStruct) NetBlockRemove(p0 context.Context, p1 NetBlockList) error {
return s.Internal.NetBlockRemove(p0, p1)
}
func (s *CommonStub) NetBlockRemove(p0 context.Context, p1 NetBlockList) error {
return xerrors.New("method not supported")
}
func (s *CommonStruct) NetConnect(p0 context.Context, p1 peer.AddrInfo) error {
return s.Internal.NetConnect(p0, p1)
}
func (s *CommonStub) NetConnect(p0 context.Context, p1 peer.AddrInfo) error {
return xerrors.New("method not supported")
}
func (s *CommonStruct) NetConnectedness(p0 context.Context, p1 peer.ID) (network.Connectedness, error) {
return s.Internal.NetConnectedness(p0, p1)
}
func (s *CommonStub) NetConnectedness(p0 context.Context, p1 peer.ID) (network.Connectedness, error) {
return *new(network.Connectedness), xerrors.New("method not supported")
}
func (s *CommonStruct) NetDisconnect(p0 context.Context, p1 peer.ID) error {
return s.Internal.NetDisconnect(p0, p1)
}
func (s *CommonStub) NetDisconnect(p0 context.Context, p1 peer.ID) error {
return xerrors.New("method not supported")
}
func (s *CommonStruct) NetFindPeer(p0 context.Context, p1 peer.ID) (peer.AddrInfo, error) {
return s.Internal.NetFindPeer(p0, p1)
}
func (s *CommonStub) NetFindPeer(p0 context.Context, p1 peer.ID) (peer.AddrInfo, error) {
return *new(peer.AddrInfo), xerrors.New("method not supported")
}
func (s *CommonStruct) NetPeerInfo(p0 context.Context, p1 peer.ID) (*ExtendedPeerInfo, error) {
return s.Internal.NetPeerInfo(p0, p1)
}
func (s *CommonStub) NetPeerInfo(p0 context.Context, p1 peer.ID) (*ExtendedPeerInfo, error) {
return nil, xerrors.New("method not supported")
}
func (s *CommonStruct) NetPeers(p0 context.Context) ([]peer.AddrInfo, error) {
return s.Internal.NetPeers(p0)
}
func (s *CommonStub) NetPeers(p0 context.Context) ([]peer.AddrInfo, error) {
return *new([]peer.AddrInfo), xerrors.New("method not supported")
}
func (s *CommonStruct) NetPubsubScores(p0 context.Context) ([]PubsubScore, error) {
return s.Internal.NetPubsubScores(p0)
}
func (s *CommonStub) NetPubsubScores(p0 context.Context) ([]PubsubScore, error) {
return *new([]PubsubScore), xerrors.New("method not supported")
}
func (s *CommonStruct) Session(p0 context.Context) (uuid.UUID, error) { func (s *CommonStruct) Session(p0 context.Context) (uuid.UUID, error) {
return s.Internal.Session(p0) return s.Internal.Session(p0)
} }
@ -2711,6 +2605,142 @@ func (s *GatewayStub) WalletBalance(p0 context.Context, p1 address.Address) (typ
return *new(types.BigInt), xerrors.New("method not supported") return *new(types.BigInt), xerrors.New("method not supported")
} }
func (s *NetStruct) ID(p0 context.Context) (peer.ID, error) {
return s.Internal.ID(p0)
}
func (s *NetStub) ID(p0 context.Context) (peer.ID, error) {
return *new(peer.ID), xerrors.New("method not supported")
}
func (s *NetStruct) NetAddrsListen(p0 context.Context) (peer.AddrInfo, error) {
return s.Internal.NetAddrsListen(p0)
}
func (s *NetStub) NetAddrsListen(p0 context.Context) (peer.AddrInfo, error) {
return *new(peer.AddrInfo), xerrors.New("method not supported")
}
func (s *NetStruct) NetAgentVersion(p0 context.Context, p1 peer.ID) (string, error) {
return s.Internal.NetAgentVersion(p0, p1)
}
func (s *NetStub) NetAgentVersion(p0 context.Context, p1 peer.ID) (string, error) {
return "", xerrors.New("method not supported")
}
func (s *NetStruct) NetAutoNatStatus(p0 context.Context) (NatInfo, error) {
return s.Internal.NetAutoNatStatus(p0)
}
func (s *NetStub) NetAutoNatStatus(p0 context.Context) (NatInfo, error) {
return *new(NatInfo), xerrors.New("method not supported")
}
func (s *NetStruct) NetBandwidthStats(p0 context.Context) (metrics.Stats, error) {
return s.Internal.NetBandwidthStats(p0)
}
func (s *NetStub) NetBandwidthStats(p0 context.Context) (metrics.Stats, error) {
return *new(metrics.Stats), xerrors.New("method not supported")
}
func (s *NetStruct) NetBandwidthStatsByPeer(p0 context.Context) (map[string]metrics.Stats, error) {
return s.Internal.NetBandwidthStatsByPeer(p0)
}
func (s *NetStub) NetBandwidthStatsByPeer(p0 context.Context) (map[string]metrics.Stats, error) {
return *new(map[string]metrics.Stats), xerrors.New("method not supported")
}
func (s *NetStruct) NetBandwidthStatsByProtocol(p0 context.Context) (map[protocol.ID]metrics.Stats, error) {
return s.Internal.NetBandwidthStatsByProtocol(p0)
}
func (s *NetStub) NetBandwidthStatsByProtocol(p0 context.Context) (map[protocol.ID]metrics.Stats, error) {
return *new(map[protocol.ID]metrics.Stats), xerrors.New("method not supported")
}
func (s *NetStruct) NetBlockAdd(p0 context.Context, p1 NetBlockList) error {
return s.Internal.NetBlockAdd(p0, p1)
}
func (s *NetStub) NetBlockAdd(p0 context.Context, p1 NetBlockList) error {
return xerrors.New("method not supported")
}
func (s *NetStruct) NetBlockList(p0 context.Context) (NetBlockList, error) {
return s.Internal.NetBlockList(p0)
}
func (s *NetStub) NetBlockList(p0 context.Context) (NetBlockList, error) {
return *new(NetBlockList), xerrors.New("method not supported")
}
func (s *NetStruct) NetBlockRemove(p0 context.Context, p1 NetBlockList) error {
return s.Internal.NetBlockRemove(p0, p1)
}
func (s *NetStub) NetBlockRemove(p0 context.Context, p1 NetBlockList) error {
return xerrors.New("method not supported")
}
func (s *NetStruct) NetConnect(p0 context.Context, p1 peer.AddrInfo) error {
return s.Internal.NetConnect(p0, p1)
}
func (s *NetStub) NetConnect(p0 context.Context, p1 peer.AddrInfo) error {
return xerrors.New("method not supported")
}
func (s *NetStruct) NetConnectedness(p0 context.Context, p1 peer.ID) (network.Connectedness, error) {
return s.Internal.NetConnectedness(p0, p1)
}
func (s *NetStub) NetConnectedness(p0 context.Context, p1 peer.ID) (network.Connectedness, error) {
return *new(network.Connectedness), xerrors.New("method not supported")
}
func (s *NetStruct) NetDisconnect(p0 context.Context, p1 peer.ID) error {
return s.Internal.NetDisconnect(p0, p1)
}
func (s *NetStub) NetDisconnect(p0 context.Context, p1 peer.ID) error {
return xerrors.New("method not supported")
}
func (s *NetStruct) NetFindPeer(p0 context.Context, p1 peer.ID) (peer.AddrInfo, error) {
return s.Internal.NetFindPeer(p0, p1)
}
func (s *NetStub) NetFindPeer(p0 context.Context, p1 peer.ID) (peer.AddrInfo, error) {
return *new(peer.AddrInfo), xerrors.New("method not supported")
}
func (s *NetStruct) NetPeerInfo(p0 context.Context, p1 peer.ID) (*ExtendedPeerInfo, error) {
return s.Internal.NetPeerInfo(p0, p1)
}
func (s *NetStub) NetPeerInfo(p0 context.Context, p1 peer.ID) (*ExtendedPeerInfo, error) {
return nil, xerrors.New("method not supported")
}
func (s *NetStruct) NetPeers(p0 context.Context) ([]peer.AddrInfo, error) {
return s.Internal.NetPeers(p0)
}
func (s *NetStub) NetPeers(p0 context.Context) ([]peer.AddrInfo, error) {
return *new([]peer.AddrInfo), xerrors.New("method not supported")
}
func (s *NetStruct) NetPubsubScores(p0 context.Context) ([]PubsubScore, error) {
return s.Internal.NetPubsubScores(p0)
}
func (s *NetStub) NetPubsubScores(p0 context.Context) ([]PubsubScore, error) {
return *new([]PubsubScore), xerrors.New("method not supported")
}
func (s *SignableStruct) Sign(p0 context.Context, p1 SignFunc) error { func (s *SignableStruct) Sign(p0 context.Context, p1 SignFunc) error {
return s.Internal.Sign(p0, p1) return s.Internal.Sign(p0, p1)
} }
@ -3713,8 +3743,10 @@ func (s *WorkerStub) WaitQuiet(p0 context.Context) error {
var _ ChainIO = new(ChainIOStruct) var _ ChainIO = new(ChainIOStruct)
var _ Common = new(CommonStruct) var _ Common = new(CommonStruct)
var _ CommonNet = new(CommonNetStruct)
var _ FullNode = new(FullNodeStruct) var _ FullNode = new(FullNodeStruct)
var _ Gateway = new(GatewayStruct) var _ Gateway = new(GatewayStruct)
var _ Net = new(NetStruct)
var _ Signable = new(SignableStruct) var _ Signable = new(SignableStruct)
var _ StorageMiner = new(StorageMinerStruct) var _ StorageMiner = new(StorageMinerStruct)
var _ Wallet = new(WalletStruct) var _ Wallet = new(WalletStruct)

30
api/proxy_util.go Normal file
View File

@ -0,0 +1,30 @@
package api
import "reflect"
var _internalField = "Internal"
// GetInternalStructs extracts all pointers to 'Internal' sub-structs from the provided pointer to a proxy struct
func GetInternalStructs(in interface{}) []interface{} {
return getInternalStructs(reflect.ValueOf(in).Elem())
}
func getInternalStructs(rv reflect.Value) []interface{} {
var out []interface{}
internal := rv.FieldByName(_internalField)
ii := internal.Addr().Interface()
out = append(out, ii)
for i := 0; i < rv.NumField(); i++ {
if rv.Type().Field(i).Name == _internalField {
continue
}
sub := getInternalStructs(rv.Field(i))
out = append(out, sub...)
}
return out
}

36
api/proxy_util_test.go Normal file
View File

@ -0,0 +1,36 @@
package api
import (
"testing"
"github.com/stretchr/testify/require"
)
type StrA struct {
StrB
Internal struct {
A int
}
}
type StrB struct {
Internal struct {
B int
}
}
func TestGetInternalStructs(t *testing.T) {
var proxy StrA
sts := GetInternalStructs(&proxy)
require.Len(t, sts, 2)
sa := sts[0].(*struct{ A int })
sa.A = 3
sb := sts[1].(*struct{ B int })
sb.B = 4
require.Equal(t, 3, proxy.Internal.A)
require.Equal(t, 4, proxy.StrB.Internal.B)
}

View File

@ -46,6 +46,7 @@ import (
// FullNode API is a low-level interface to the Filecoin network full node // FullNode API is a low-level interface to the Filecoin network full node
type FullNode interface { type FullNode interface {
Common Common
Net
// MethodGroup: Chain // MethodGroup: Chain
// The Chain method group contains methods for interacting with the // The Chain method group contains methods for interacting with the

View File

@ -5,8 +5,15 @@ import (
) )
type Common = api.Common type Common = api.Common
type Net = api.Net
type CommonNet = api.CommonNet
type CommonStruct = api.CommonStruct type CommonStruct = api.CommonStruct
type CommonStub = api.CommonStub type CommonStub = api.CommonStub
type NetStruct = api.NetStruct
type NetStub = api.NetStub
type CommonNetStruct = api.CommonNetStruct
type CommonNetStub = api.CommonNetStub
type StorageMiner = api.StorageMiner type StorageMiner = api.StorageMiner
type StorageMinerStruct = api.StorageMinerStruct type StorageMinerStruct = api.StorageMinerStruct

View File

@ -30,6 +30,8 @@ import (
type FullNodeStruct struct { type FullNodeStruct struct {
CommonStruct CommonStruct
NetStruct
Internal struct { Internal struct {
BeaconGetEntry func(p0 context.Context, p1 abi.ChainEpoch) (*types.BeaconEntry, error) `perm:"read"` BeaconGetEntry func(p0 context.Context, p1 abi.ChainEpoch) (*types.BeaconEntry, error) `perm:"read"`
@ -389,6 +391,8 @@ type FullNodeStruct struct {
type FullNodeStub struct { type FullNodeStub struct {
CommonStub CommonStub
NetStub
} }
type GatewayStruct struct { type GatewayStruct struct {

View File

@ -149,7 +149,7 @@ func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.
return addr, ainfo.AuthHeader(), nil return addr, ainfo.AuthHeader(), nil
} }
func GetAPI(ctx *cli.Context) (api.Common, jsonrpc.ClientCloser, error) { func GetAPI(ctx *cli.Context) (api.CommonNet, jsonrpc.ClientCloser, error) {
ti, ok := ctx.App.Metadata["repoType"] ti, ok := ctx.App.Metadata["repoType"]
if !ok { if !ok {
log.Errorf("unknown repo type, are you sure you want to use GetAPI?") log.Errorf("unknown repo type, are you sure you want to use GetAPI?")

View File

@ -635,7 +635,7 @@ func (n *Ensemble) InterconnectAll() *Ensemble {
} }
// Connect connects one full node to the provided full nodes. // Connect connects one full node to the provided full nodes.
func (n *Ensemble) Connect(from api.Common, to ...api.Common) *Ensemble { func (n *Ensemble) Connect(from api.Net, to ...api.Net) *Ensemble {
addr, err := from.NetAddrsListen(context.Background()) addr, err := from.NetAddrsListen(context.Background())
require.NoError(n.t, err) require.NoError(n.t, err)

View File

@ -11,54 +11,54 @@ import (
func MetricedStorMinerAPI(a api.StorageMiner) api.StorageMiner { func MetricedStorMinerAPI(a api.StorageMiner) api.StorageMiner {
var out api.StorageMinerStruct var out api.StorageMinerStruct
proxy(a, &out.Internal) proxy(a, &out)
proxy(a, &out.CommonStruct.Internal)
return &out return &out
} }
func MetricedFullAPI(a api.FullNode) api.FullNode { func MetricedFullAPI(a api.FullNode) api.FullNode {
var out api.FullNodeStruct var out api.FullNodeStruct
proxy(a, &out.Internal) proxy(a, &out)
proxy(a, &out.CommonStruct.Internal)
return &out return &out
} }
func MetricedWorkerAPI(a api.Worker) api.Worker { func MetricedWorkerAPI(a api.Worker) api.Worker {
var out api.WorkerStruct var out api.WorkerStruct
proxy(a, &out.Internal) proxy(a, &out)
return &out return &out
} }
func MetricedWalletAPI(a api.Wallet) api.Wallet { func MetricedWalletAPI(a api.Wallet) api.Wallet {
var out api.WalletStruct var out api.WalletStruct
proxy(a, &out.Internal) proxy(a, &out)
return &out return &out
} }
func MetricedGatewayAPI(a api.Gateway) api.Gateway { func MetricedGatewayAPI(a api.Gateway) api.Gateway {
var out api.GatewayStruct var out api.GatewayStruct
proxy(a, &out.Internal) proxy(a, &out)
return &out return &out
} }
func proxy(in interface{}, out interface{}) { func proxy(in interface{}, outstr interface{}) {
rint := reflect.ValueOf(out).Elem() outs := api.GetInternalStructs(outstr)
ra := reflect.ValueOf(in) for _, out := range outs {
rint := reflect.ValueOf(out).Elem()
ra := reflect.ValueOf(in)
for f := 0; f < rint.NumField(); f++ { for f := 0; f < rint.NumField(); f++ {
field := rint.Type().Field(f) field := rint.Type().Field(f)
fn := ra.MethodByName(field.Name) fn := ra.MethodByName(field.Name)
rint.Field(f).Set(reflect.MakeFunc(field.Type, func(args []reflect.Value) (results []reflect.Value) {
ctx := args[0].Interface().(context.Context)
// upsert function name into context
ctx, _ = tag.New(ctx, tag.Upsert(Endpoint, field.Name))
stop := Timer(ctx, APIRequestDuration)
defer stop()
// pass tagged ctx back into function call
args[0] = reflect.ValueOf(ctx)
return fn.Call(args)
}))
rint.Field(f).Set(reflect.MakeFunc(field.Type, func(args []reflect.Value) (results []reflect.Value) {
ctx := args[0].Interface().(context.Context)
// upsert function name into context
ctx, _ = tag.New(ctx, tag.Upsert(Endpoint, field.Name))
stop := Timer(ctx, APIRequestDuration)
defer stop()
// pass tagged ctx back into function call
args[0] = reflect.ValueOf(ctx)
return fn.Call(args)
}))
}
} }
} }

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"time" "time"
"github.com/filecoin-project/lotus/node/impl/net"
metricsi "github.com/ipfs/go-metrics-interface" metricsi "github.com/ipfs/go-metrics-interface"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
@ -36,7 +37,6 @@ import (
"github.com/filecoin-project/lotus/markets/storageadapter" "github.com/filecoin-project/lotus/markets/storageadapter"
"github.com/filecoin-project/lotus/node/config" "github.com/filecoin-project/lotus/node/config"
"github.com/filecoin-project/lotus/node/impl/common" "github.com/filecoin-project/lotus/node/impl/common"
"github.com/filecoin-project/lotus/node/impl/common/mock"
"github.com/filecoin-project/lotus/node/modules" "github.com/filecoin-project/lotus/node/modules"
"github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/node/modules/helpers"
@ -245,11 +245,11 @@ func ConfigCommon(cfg *config.Common, enableLibp2pNode bool) Option {
}), }),
ApplyIf(func(s *Settings) bool { return s.Base }), // apply only if Base has already been applied ApplyIf(func(s *Settings) bool { return s.Base }), // apply only if Base has already been applied
If(!enableLibp2pNode, If(!enableLibp2pNode,
Override(new(common.NetAPI), From(new(mock.MockNetAPI))), Override(new(api.Net), From(new(api.NetStub))),
Override(new(api.Common), From(new(common.CommonAPI))), Override(new(api.Common), From(new(common.CommonAPI))),
), ),
If(enableLibp2pNode, If(enableLibp2pNode,
Override(new(common.NetAPI), From(new(common.Libp2pNetAPI))), Override(new(api.Net), From(new(net.NetAPI))),
Override(new(api.Common), From(new(common.CommonAPI))), Override(new(api.Common), From(new(common.CommonAPI))),
Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)), Override(StartListeningKey, lp2p.StartListening(cfg.Libp2p.ListenAddresses)),
Override(ConnectionManagerKey, lp2p.ConnectionManager( Override(ConnectionManagerKey, lp2p.ConnectionManager(

View File

@ -10,8 +10,8 @@ import (
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/go-jsonrpc/auth" "github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
apitypes "github.com/filecoin-project/lotus/api/types"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/dtypes"
) )
@ -21,8 +21,6 @@ var session = uuid.New()
type CommonAPI struct { type CommonAPI struct {
fx.In fx.In
NetAPI
APISecret *dtypes.APIAlg APISecret *dtypes.APIAlg
ShutdownChan dtypes.ShutdownChan ShutdownChan dtypes.ShutdownChan
} }
@ -48,6 +46,24 @@ func (a *CommonAPI) AuthNew(ctx context.Context, perms []auth.Permission) ([]byt
return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret)) return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret))
} }
func (a *CommonAPI) Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) {
return build.OpenRPCDiscoverJSON_Full(), nil
}
func (a *CommonAPI) Version(context.Context) (api.APIVersion, error) {
v, err := api.VersionForType(api.RunningNodeType)
if err != nil {
return api.APIVersion{}, err
}
return api.APIVersion{
Version: build.UserVersion(),
APIVersion: v,
BlockDelay: build.BlockDelaySecs,
}, nil
}
func (a *CommonAPI) LogList(context.Context) ([]string, error) { func (a *CommonAPI) LogList(context.Context) ([]string, error) {
return logging.GetSubsystems(), nil return logging.GetSubsystems(), nil
} }
@ -68,17 +84,3 @@ func (a *CommonAPI) Session(ctx context.Context) (uuid.UUID, error) {
func (a *CommonAPI) Closing(ctx context.Context) (<-chan struct{}, error) { func (a *CommonAPI) Closing(ctx context.Context) (<-chan struct{}, error) {
return make(chan struct{}), nil // relies on jsonrpc closing return make(chan struct{}), nil // relies on jsonrpc closing
} }
func (a *CommonAPI) Version(context.Context) (api.APIVersion, error) {
v, err := api.VersionForType(api.RunningNodeType)
if err != nil {
return api.APIVersion{}, err
}
return api.APIVersion{
Version: build.UserVersion(),
APIVersion: v,
BlockDelay: build.BlockDelaySecs,
}, nil
}

View File

@ -1,101 +0,0 @@
package mock
import (
"context"
"errors"
"github.com/filecoin-project/lotus/api"
apitypes "github.com/filecoin-project/lotus/api/types"
"github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"go.uber.org/fx"
)
var (
errNotImplemented = errors.New("not implemented")
)
type MockNetAPI struct {
fx.In
}
func (a *MockNetAPI) NetAgentVersion(ctx context.Context, p peer.ID) (string, error) {
return "", errNotImplemented
}
func (a *MockNetAPI) NetConnectedness(ctx context.Context, pid peer.ID) (conn network.Connectedness, err error) {
err = errNotImplemented
return
}
func (a *MockNetAPI) NetPubsubScores(context.Context) ([]api.PubsubScore, error) {
return nil, errNotImplemented
}
func (a *MockNetAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) {
return nil, errNotImplemented
}
func (a *MockNetAPI) NetPeerInfo(_ context.Context, p peer.ID) (*api.ExtendedPeerInfo, error) {
return nil, errNotImplemented
}
func (a *MockNetAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return errNotImplemented
}
func (a *MockNetAPI) NetAddrsListen(context.Context) (ai peer.AddrInfo, err error) {
err = errNotImplemented
return
}
func (a *MockNetAPI) NetDisconnect(ctx context.Context, p peer.ID) error {
return errNotImplemented
}
func (a *MockNetAPI) NetFindPeer(ctx context.Context, p peer.ID) (ai peer.AddrInfo, err error) {
err = errNotImplemented
return
}
func (a *MockNetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error) {
err = errNotImplemented
return
}
func (a *MockNetAPI) NetBandwidthStats(ctx context.Context) (s metrics.Stats, err error) {
err = errNotImplemented
return
}
func (a *MockNetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) {
return nil, errNotImplemented
}
func (a *MockNetAPI) NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) {
return nil, errNotImplemented
}
func (a *MockNetAPI) Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) {
return nil, errNotImplemented
}
func (a *MockNetAPI) ID(context.Context) (p peer.ID, err error) {
err = errNotImplemented
return
}
func (a *MockNetAPI) NetBlockAdd(ctx context.Context, acl api.NetBlockList) error {
return errNotImplemented
}
func (a *MockNetAPI) NetBlockRemove(ctx context.Context, acl api.NetBlockList) error {
return errNotImplemented
}
func (a *MockNetAPI) NetBlockList(ctx context.Context) (result api.NetBlockList, err error) {
err = errNotImplemented
return
}

View File

@ -1,34 +0,0 @@
package common
import (
"context"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol"
"github.com/filecoin-project/lotus/api"
apitypes "github.com/filecoin-project/lotus/api/types"
)
type NetAPI interface {
NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error)
NetPubsubScores(context.Context) ([]api.PubsubScore, error)
NetPeers(context.Context) ([]peer.AddrInfo, error)
NetPeerInfo(_ context.Context, p peer.ID) (*api.ExtendedPeerInfo, error)
NetConnect(ctx context.Context, p peer.AddrInfo) error
NetAddrsListen(context.Context) (peer.AddrInfo, error)
NetDisconnect(ctx context.Context, p peer.ID) error
NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error)
NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error)
NetAgentVersion(ctx context.Context, p peer.ID) (string, error)
NetBandwidthStats(ctx context.Context) (metrics.Stats, error)
NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error)
NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error)
Discover(ctx context.Context) (apitypes.OpenRPCDocument, error)
ID(context.Context) (peer.ID, error)
NetBlockAdd(ctx context.Context, acl api.NetBlockList) error
NetBlockRemove(ctx context.Context, acl api.NetBlockList) error
NetBlockList(ctx context.Context) (api.NetBlockList, error)
}

View File

@ -14,6 +14,7 @@ import (
"github.com/filecoin-project/lotus/node/impl/common" "github.com/filecoin-project/lotus/node/impl/common"
"github.com/filecoin-project/lotus/node/impl/full" "github.com/filecoin-project/lotus/node/impl/full"
"github.com/filecoin-project/lotus/node/impl/market" "github.com/filecoin-project/lotus/node/impl/market"
"github.com/filecoin-project/lotus/node/impl/net"
"github.com/filecoin-project/lotus/node/impl/paych" "github.com/filecoin-project/lotus/node/impl/paych"
"github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/lp2p" "github.com/filecoin-project/lotus/node/modules/lp2p"
@ -23,6 +24,7 @@ var log = logging.Logger("node")
type FullNodeAPI struct { type FullNodeAPI struct {
common.CommonAPI common.CommonAPI
net.NetAPI
full.ChainAPI full.ChainAPI
client.API client.API
full.MpoolAPI full.MpoolAPI

View File

@ -1,4 +1,4 @@
package common package net
import ( import (
"context" "context"
@ -14,7 +14,7 @@ import (
var cLog = logging.Logger("conngater") var cLog = logging.Logger("conngater")
func (a *Libp2pNetAPI) NetBlockAdd(ctx context.Context, acl api.NetBlockList) error { func (a *NetAPI) NetBlockAdd(ctx context.Context, acl api.NetBlockList) error {
for _, p := range acl.Peers { for _, p := range acl.Peers {
err := a.ConnGater.BlockPeer(p) err := a.ConnGater.BlockPeer(p)
if err != nil { if err != nil {
@ -89,7 +89,7 @@ func (a *Libp2pNetAPI) NetBlockAdd(ctx context.Context, acl api.NetBlockList) er
return nil return nil
} }
func (a *Libp2pNetAPI) NetBlockRemove(ctx context.Context, acl api.NetBlockList) error { func (a *NetAPI) NetBlockRemove(ctx context.Context, acl api.NetBlockList) error {
for _, p := range acl.Peers { for _, p := range acl.Peers {
err := a.ConnGater.UnblockPeer(p) err := a.ConnGater.UnblockPeer(p)
if err != nil { if err != nil {
@ -124,7 +124,7 @@ func (a *Libp2pNetAPI) NetBlockRemove(ctx context.Context, acl api.NetBlockList)
return nil return nil
} }
func (a *Libp2pNetAPI) NetBlockList(ctx context.Context) (result api.NetBlockList, err error) { func (a *NetAPI) NetBlockList(ctx context.Context) (result api.NetBlockList, err error) {
result.Peers = a.ConnGater.ListBlockedPeers() result.Peers = a.ConnGater.ListBlockedPeers()
for _, ip := range a.ConnGater.ListBlockedAddrs() { for _, ip := range a.ConnGater.ListBlockedAddrs() {
result.IPAddrs = append(result.IPAddrs, ip.String()) result.IPAddrs = append(result.IPAddrs, ip.String())

View File

@ -1,29 +1,28 @@
package common package net
import ( import (
"context" "context"
"sort" "sort"
"strings" "strings"
"go.uber.org/fx"
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
metrics "github.com/libp2p/go-libp2p-core/metrics" "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol" "github.com/libp2p/go-libp2p-core/protocol"
swarm "github.com/libp2p/go-libp2p-swarm" swarm "github.com/libp2p/go-libp2p-swarm"
basichost "github.com/libp2p/go-libp2p/p2p/host/basic" basichost "github.com/libp2p/go-libp2p/p2p/host/basic"
"github.com/libp2p/go-libp2p/p2p/net/conngater" "github.com/libp2p/go-libp2p/p2p/net/conngater"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
"go.uber.org/fx"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
apitypes "github.com/filecoin-project/lotus/api/types"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/node/modules/lp2p" "github.com/filecoin-project/lotus/node/modules/lp2p"
) )
type Libp2pNetAPI struct { type NetAPI struct {
fx.In fx.In
RawHost lp2p.RawHost RawHost lp2p.RawHost
@ -34,11 +33,15 @@ type Libp2pNetAPI struct {
Sk *dtypes.ScoreKeeper Sk *dtypes.ScoreKeeper
} }
func (a *Libp2pNetAPI) NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error) { func (a *NetAPI) ID(context.Context) (peer.ID, error) {
return a.Host.ID(), nil
}
func (a *NetAPI) NetConnectedness(ctx context.Context, pid peer.ID) (network.Connectedness, error) {
return a.Host.Network().Connectedness(pid), nil return a.Host.Network().Connectedness(pid), nil
} }
func (a *Libp2pNetAPI) NetPubsubScores(context.Context) ([]api.PubsubScore, error) { func (a *NetAPI) NetPubsubScores(context.Context) ([]api.PubsubScore, error) {
scores := a.Sk.Get() scores := a.Sk.Get()
out := make([]api.PubsubScore, len(scores)) out := make([]api.PubsubScore, len(scores))
i := 0 i := 0
@ -54,7 +57,7 @@ func (a *Libp2pNetAPI) NetPubsubScores(context.Context) ([]api.PubsubScore, erro
return out, nil return out, nil
} }
func (a *Libp2pNetAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) { func (a *NetAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) {
conns := a.Host.Network().Conns() conns := a.Host.Network().Conns()
out := make([]peer.AddrInfo, len(conns)) out := make([]peer.AddrInfo, len(conns))
@ -70,7 +73,7 @@ func (a *Libp2pNetAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) {
return out, nil return out, nil
} }
func (a *Libp2pNetAPI) NetPeerInfo(_ context.Context, p peer.ID) (*api.ExtendedPeerInfo, error) { func (a *NetAPI) NetPeerInfo(_ context.Context, p peer.ID) (*api.ExtendedPeerInfo, error) {
info := &api.ExtendedPeerInfo{ID: p} info := &api.ExtendedPeerInfo{ID: p}
agent, err := a.Host.Peerstore().Get(p, "AgentVersion") agent, err := a.Host.Peerstore().Get(p, "AgentVersion")
@ -101,7 +104,7 @@ func (a *Libp2pNetAPI) NetPeerInfo(_ context.Context, p peer.ID) (*api.ExtendedP
return info, nil return info, nil
} }
func (a *Libp2pNetAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error { func (a *NetAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error {
if swrm, ok := a.Host.Network().(*swarm.Swarm); ok { if swrm, ok := a.Host.Network().(*swarm.Swarm); ok {
swrm.Backoff().Clear(p.ID) swrm.Backoff().Clear(p.ID)
} }
@ -109,22 +112,22 @@ func (a *Libp2pNetAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error {
return a.Host.Connect(ctx, p) return a.Host.Connect(ctx, p)
} }
func (a *Libp2pNetAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) { func (a *NetAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
return peer.AddrInfo{ return peer.AddrInfo{
ID: a.Host.ID(), ID: a.Host.ID(),
Addrs: a.Host.Addrs(), Addrs: a.Host.Addrs(),
}, nil }, nil
} }
func (a *Libp2pNetAPI) NetDisconnect(ctx context.Context, p peer.ID) error { func (a *NetAPI) NetDisconnect(ctx context.Context, p peer.ID) error {
return a.Host.Network().ClosePeer(p) return a.Host.Network().ClosePeer(p)
} }
func (a *Libp2pNetAPI) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) { func (a *NetAPI) NetFindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
return a.Router.FindPeer(ctx, p) return a.Router.FindPeer(ctx, p)
} }
func (a *Libp2pNetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error) { func (a *NetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err error) {
autonat := a.RawHost.(*basichost.BasicHost).GetAutoNat() autonat := a.RawHost.(*basichost.BasicHost).GetAutoNat()
if autonat == nil { if autonat == nil {
@ -148,7 +151,7 @@ func (a *Libp2pNetAPI) NetAutoNatStatus(ctx context.Context) (i api.NatInfo, err
}, nil }, nil
} }
func (a *Libp2pNetAPI) NetAgentVersion(ctx context.Context, p peer.ID) (string, error) { func (a *NetAPI) NetAgentVersion(ctx context.Context, p peer.ID) (string, error) {
ag, err := a.Host.Peerstore().Get(p, "AgentVersion") ag, err := a.Host.Peerstore().Get(p, "AgentVersion")
if err != nil { if err != nil {
return "", err return "", err
@ -161,11 +164,11 @@ func (a *Libp2pNetAPI) NetAgentVersion(ctx context.Context, p peer.ID) (string,
return ag.(string), nil return ag.(string), nil
} }
func (a *Libp2pNetAPI) NetBandwidthStats(ctx context.Context) (metrics.Stats, error) { func (a *NetAPI) NetBandwidthStats(ctx context.Context) (metrics.Stats, error) {
return a.Reporter.GetBandwidthTotals(), nil return a.Reporter.GetBandwidthTotals(), nil
} }
func (a *Libp2pNetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) { func (a *NetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) {
out := make(map[string]metrics.Stats) out := make(map[string]metrics.Stats)
for p, s := range a.Reporter.GetBandwidthByPeer() { for p, s := range a.Reporter.GetBandwidthByPeer() {
out[p.String()] = s out[p.String()] = s
@ -173,14 +176,8 @@ func (a *Libp2pNetAPI) NetBandwidthStatsByPeer(ctx context.Context) (map[string]
return out, nil return out, nil
} }
func (a *Libp2pNetAPI) NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) { func (a *NetAPI) NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) {
return a.Reporter.GetBandwidthByProtocol(), nil return a.Reporter.GetBandwidthByProtocol(), nil
} }
func (a *Libp2pNetAPI) Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) { var _ api.Net = &NetAPI{}
return build.OpenRPCDiscoverJSON_Full(), nil
}
func (a *Libp2pNetAPI) ID(context.Context) (peer.ID, error) {
return a.Host.ID(), nil
}

View File

@ -39,6 +39,7 @@ import (
"github.com/filecoin-project/lotus/markets/storageadapter" "github.com/filecoin-project/lotus/markets/storageadapter"
"github.com/filecoin-project/lotus/miner" "github.com/filecoin-project/lotus/miner"
"github.com/filecoin-project/lotus/node/impl/common" "github.com/filecoin-project/lotus/node/impl/common"
"github.com/filecoin-project/lotus/node/impl/net"
"github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/storage" "github.com/filecoin-project/lotus/storage"
"github.com/filecoin-project/lotus/storage/sectorblocks" "github.com/filecoin-project/lotus/storage/sectorblocks"
@ -47,6 +48,7 @@ import (
type StorageMinerAPI struct { type StorageMinerAPI struct {
common.CommonAPI common.CommonAPI
net.NetAPI
Full api.FullNode Full api.FullNode
LocalStore *stores.Local LocalStore *stores.Local