Skip to content

Commit

Permalink
Merge pull request #5150 from filecoin-project/api/add_Version
Browse files Browse the repository at this point in the history
api: 各组件增加Version接口/add Version api for components
  • Loading branch information
hunjixin authored Aug 3, 2022
2 parents f5a4cc0 + 3bc3e6d commit 14d26a4
Show file tree
Hide file tree
Showing 49 changed files with 556 additions and 215 deletions.
7 changes: 6 additions & 1 deletion app/node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/filecoin-project/venus/app/submodule/blockstore"
"github.com/filecoin-project/venus/app/submodule/chain"
"github.com/filecoin-project/venus/app/submodule/common"
config2 "github.com/filecoin-project/venus/app/submodule/config"
"github.com/filecoin-project/venus/app/submodule/dagservice"
"github.com/filecoin-project/venus/app/submodule/discovery"
Expand Down Expand Up @@ -167,6 +168,8 @@ func (b *Builder) build(ctx context.Context) (*Node, error) {
}
nd.market = market.NewMarketModule(nd.chain.API(), nd.syncer.Stmgr)

commonModule := common.NewCommonModule()

apiBuilder := NewBuilder()
apiBuilder.NameSpace("Filecoin")

Expand All @@ -182,7 +185,9 @@ func (b *Builder) build(ctx context.Context) (*Node, error) {
nd.mining,
nd.mpool,
nd.paychan,
nd.market)
nd.market,
commonModule,
)

if err != nil {
return nil, errors.Wrap(err, "add service failed ")
Expand Down
35 changes: 35 additions & 0 deletions app/submodule/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package common

import (
"context"

apiwrapper "github.com/filecoin-project/venus/app/submodule/common/v0api"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/venus-shared/api/chain"
v0api "github.com/filecoin-project/venus/venus-shared/api/chain/v0"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
)

var _ v1api.ICommon = (*CommonModule)(nil)

type CommonModule struct{} // nolint

func NewCommonModule() *CommonModule {
return new(CommonModule)
}

func (cm *CommonModule) Version(ctx context.Context) (types.Version, error) {
return types.Version{
Version: constants.UserVersion(),
APIVersion: chain.FullAPIVersion1,
}, nil
}

func (cm *CommonModule) API() v1api.ICommon {
return cm
}

func (cm *CommonModule) V0API() v0api.ICommon {
return &apiwrapper.WrapperV1ICommon{ICommon: cm}
}
27 changes: 27 additions & 0 deletions app/submodule/common/v0api/common_v0api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package v0api

import (
"context"

"github.com/filecoin-project/venus/venus-shared/api/chain"
v0api "github.com/filecoin-project/venus/venus-shared/api/chain/v0"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
)

var _ v0api.ICommon = (*WrapperV1ICommon)(nil)

type WrapperV1ICommon struct { //nolint
v1api.ICommon
}

func (a *WrapperV1ICommon) Version(ctx context.Context) (types.Version, error) {
ver, err := a.ICommon.Version(ctx)
if err != nil {
return types.Version{}, err
}

ver.APIVersion = chain.FullAPIVersion0

return ver, nil
}
9 changes: 0 additions & 9 deletions app/submodule/network/network_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"

"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/venus-shared/api"
v1api "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
)

Expand Down Expand Up @@ -92,13 +90,6 @@ func (na *networkAPI) NetPing(ctx context.Context, p peer.ID) (time.Duration, er
return result.RTT, result.Error
}

func (na *networkAPI) Version(context.Context) (types.Version, error) {
return types.Version{
Version: constants.UserVersion(),
APIVersion: api.FullAPIVersion1,
}, nil
}

// NetAddrsListen return local p2p address info
func (na *networkAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) {
return peer.AddrInfo{
Expand Down
3 changes: 1 addition & 2 deletions app/submodule/network/network_submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
dtnet "github.com/filecoin-project/go-data-transfer/network"
dtgstransport "github.com/filecoin-project/go-data-transfer/transport/graphsync"

apiwrapper "github.com/filecoin-project/venus/app/submodule/network/v0api"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/discovery"
"github.com/filecoin-project/venus/pkg/net"
Expand Down Expand Up @@ -86,7 +85,7 @@ func (networkSubmodule *NetworkSubmodule) API() v1api.INetwork {
}

func (networkSubmodule *NetworkSubmodule) V0API() v0api.INetwork {
return &apiwrapper.WrapperV1INetwork{INetwork: &networkAPI{network: networkSubmodule}}
return &networkAPI{network: networkSubmodule}
}

func (networkSubmodule *NetworkSubmodule) Stop(ctx context.Context) {
Expand Down
28 changes: 0 additions & 28 deletions app/submodule/network/v0api/v1_wrapper.go

This file was deleted.

3 changes: 3 additions & 0 deletions venus-devtool/api-gen/doc_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var ctxElem = reflect.TypeOf((*context.Context)(nil)).Elem()
var docGenCmd = &cli.Command{
Name: "doc",
Action: func(cctx *cli.Context) error {
if err := util.LoadExtraInterfaceMeta(); err != nil {
return err
}
for _, t := range apiTargets {
if err := genDocForAPI(t); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions venus-devtool/api-gen/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/multiformats/go-multiaddr"

"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/venus-shared/api"
"github.com/filecoin-project/venus/venus-shared/api/chain"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/filecoin-project/venus/venus-shared/types/market/client"
"github.com/filecoin-project/venus/venus-shared/types/messager"
Expand Down Expand Up @@ -112,7 +112,7 @@ func init() {
addExample(network.Connected)
addExample(types.NetworkName("mainnet"))
addExample(types.SyncStateStage(1))
addExample(api.FullAPIVersion1)
addExample(chain.FullAPIVersion1)
addExample(types.PCHInbound)
addExample(time.Minute)
reqIDBytes, err := uuid.MarshalBinary()
Expand Down
14 changes: 10 additions & 4 deletions venus-devtool/api-gen/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var proxyCmd = &cli.Command{
Name: "proxy",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
if err := util.LoadExtraInterfaceMeta(); err != nil {
return err
}
for _, target := range apiTargets {
err := genProxyForAPI(target)
if err != nil {
Expand Down Expand Up @@ -169,17 +172,21 @@ func writeStruct(dst *bytes.Buffer, ifaceMeta *util.InterfaceMeta, astMeta *util
fmt.Fprintf(dst, "\t%s\n", structName(nested))
}

tmpBuf := &bytes.Buffer{}
if len(ifaceMeta.Defined) > 0 {
fmt.Fprint(dst, structInternalHead)

for _, meth := range ifaceMeta.Defined {
fmt.Fprintf(dst, "\t\t%s ", meth.Name)

err := printer.Fprint(dst, astMeta.FileSet, meth.FuncType)
err := printer.Fprint(tmpBuf, astMeta.FileSet, meth.FuncType)
if err != nil {
return fmt.Errorf("write func %s: %w", meth.Name, err)
}

dst.WriteString(strings.ReplaceAll(tmpBuf.String(), "\n\t", ""))
tmpBuf.Reset()

fmt.Fprintf(dst, " `perm:\"%s\"`\n", util.GetAPIMethodPerm(meth))
}

Expand Down Expand Up @@ -260,7 +267,6 @@ func resolveDep(typ ast.Expr, ifaceMeta *util.InterfaceMeta, deps map[string]uti
xident, ok := selector.X.(*ast.Ident)
if !ok || xident.Name == "" {
return nil

}

importMeta, has := ifaceMeta.File.Imports[xident.Name]
Expand Down Expand Up @@ -308,7 +314,7 @@ func writeMethodBody(dst *bytes.Buffer, typBuf *bytes.Buffer, ifaceMeta *util.In
}

callNames = append(callNames, names...)
params = append(params, strings.Join(names, ", ")+" "+typBuf.String())
params = append(params, strings.Join(names, ", ")+" "+strings.ReplaceAll(typBuf.String(), "\n\t", ""))
}

results := []string{}
Expand All @@ -329,7 +335,7 @@ func writeMethodBody(dst *bytes.Buffer, typBuf *bytes.Buffer, ifaceMeta *util.In
}

for i := 0; i < count; i++ {
results = append(results, typBuf.String())
results = append(results, strings.ReplaceAll(typBuf.String(), "\n\t", ""))
}
}

Expand Down
3 changes: 3 additions & 0 deletions venus-devtool/compatible/apis/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var permCmd = &cli.Command{
Name: "perm",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
if err := util.LoadExtraInterfaceMeta(); err != nil {
return err
}
for _, pair := range util.ChainAPIPairs {
originMetas, err := parsePermMetas(pair.Lotus.ParseOpt)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion venus-devtool/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ github.com/filecoin-project/go-hamt-ipld/v3 v3.1.0 h1:rVVNq0x6RGQIzCo1iiJlGFm9AG
github.com/filecoin-project/go-hamt-ipld/v3 v3.1.0/go.mod h1:bxmzgT8tmeVQA1/gvBwFmYdT8SOFUwB3ovSUfG1Ux0g=
github.com/filecoin-project/go-indexer-core v0.2.8/go.mod h1:IagNfTdFuX4057kla43PjRCn3yBuUiZgIxuA0hTUamY=
github.com/filecoin-project/go-indexer-core v0.2.16/go.mod h1:5kCKyhtT9k1vephr9l9SFGX8B/HowXIvOhGCkmbxwbY=
github.com/filecoin-project/go-leb128 v0.0.0-20190212224330-8d79a5489543/go.mod h1:mjrHv1cDGJWDlGmC0eDc1E5VJr8DmL9XMUcaFwiuKg8=
github.com/filecoin-project/go-legs v0.3.7/go.mod h1:pgekGm8/gKY5zCtQ/qGAoSjGP92wTLFqpO3GPHeu8YU=
github.com/filecoin-project/go-legs v0.4.4/go.mod h1:JQ3hA6xpJdbR8euZ2rO0jkxaMxeidXf0LDnVuqPAe9s=
github.com/filecoin-project/go-padreader v0.0.0-20200903213702-ed5fae088b20/go.mod h1:mPn+LRRd5gEKNAtc+r3ScpW2JRU/pj4NBKdADYWHiak=
Expand Down
36 changes: 36 additions & 0 deletions venus-devtool/util/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ func (iv *ifaceMetaVisitor) Visit(node ast.Node) ast.Visitor {
FuncType: meth,
Comments: iv.comments.Filter(m).Comments(),
})
case *ast.SelectorExpr:
methodName := meth.Sel.Name
methodMeta := extraInterfaceMethodMeta[methodName]
ifaceMeta.Defined = append(ifaceMeta.Defined, InterfaceMethodMeta{
Name: methodName,
Node: m,
FuncType: methodMeta.FuncType,
Comments: methodMeta.Comments,
})
for k, importMeta := range extraImports[methodName] {
ifaceMeta.File.Imports[k] = importMeta
}
}
}

Expand Down Expand Up @@ -209,3 +221,27 @@ func ParseInterfaceMetas(opt InterfaceParseOption) ([]*InterfaceMeta, *ASTMeta,
FileSet: fset,
}, nil
}

var extraInterfaceMethodMeta = make(map[string]InterfaceMethodMeta)
var extraImports = make(map[string]map[string]ImportMeta)

func LoadExtraInterfaceMeta() error {
opt := InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api",
Included: []string{"Version"},
ResolveImports: true,
}

ifaceMetas, _, err := ParseInterfaceMetas(opt)
if err != nil {
return err
}
for _, meta := range ifaceMetas {
for _, methodMeta := range meta.Defined {
extraInterfaceMethodMeta[methodMeta.Name] = methodMeta
extraImports[methodMeta.Name] = meta.File.Imports
}
}

return nil
}
12 changes: 12 additions & 0 deletions venus-shared/api/api_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package api

import (
"context"

"github.com/filecoin-project/venus/venus-shared/types"
)

type Version interface {
// Version provides information about API provider
Version(ctx context.Context) (types.Version, error) //perm:read
}
7 changes: 7 additions & 0 deletions venus-shared/api/chain/v0/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package v0

import "github.com/filecoin-project/venus/venus-shared/api"

type ICommon interface {
api.Version
}
1 change: 1 addition & 0 deletions venus-shared/api/chain/v0/fullnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type FullNode interface {
IPaychan
ISyncer
IWallet
ICommon
}
Loading

0 comments on commit 14d26a4

Please sign in to comment.