Skip to content

Commit

Permalink
Feat/dtynn/refactor parts of the api compatible checks (#4734)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtynn authored Jan 20, 2022
1 parent 8f0a0cf commit 347645a
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ api-docs:

compatible-all: compatible-api compatible-actor

compatible-api: api-checksum api-diff
compatible-api: api-checksum api-diff api-perm

api-checksum:
cd venus-devtool && go run ./compatible/apis/*.go checksum > ../venus-shared/compatible-checks/api-checksum.txt
Expand Down
12 changes: 4 additions & 8 deletions venus-devtool/compatible/apis/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@ import (
"reflect"
"strings"

"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/api/v1api"
"github.com/urfave/cli/v2"

"github.com/filecoin-project/venus/venus-devtool/util"
)

var checksumCmd = &cli.Command{
Name: "checksum",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
wants := []reflect.Type{
reflect.TypeOf((*v0api.FullNode)(nil)).Elem(),
reflect.TypeOf((*v1api.FullNode)(nil)).Elem(),
}

var buf bytes.Buffer
for _, rt := range wants {
for _, pair := range util.APIPairs {
rt := pair.Lotus.Type
fmt.Printf("%s:\n", rt)
for mi := 0; mi < rt.NumMethod(); mi++ {
buf.Reset()
Expand Down
15 changes: 4 additions & 11 deletions venus-devtool/compatible/apis/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,19 @@ import (
"reflect"
"sort"

"github.com/filecoin-project/lotus/api/v1api"
"github.com/urfave/cli/v2"

"github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/typeutil"

"github.com/filecoin-project/venus/venus-devtool/util"
)

var diffCmd = &cli.Command{
Name: "diff",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
pairs := [][2]reflect.Type{
{
reflect.TypeOf((*v1.FullNode)(nil)).Elem(),
reflect.TypeOf((*v1api.FullNode)(nil)).Elem(),
},
}

for _, pair := range pairs {
showDiff(pair[0], pair[1])
for _, pair := range util.APIPairs {
showDiff(pair.Venus.Type, pair.Lotus.Type)
}
return nil
},
Expand Down
68 changes: 29 additions & 39 deletions venus-devtool/compatible/apis/perm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,62 @@ var permCmd = &cli.Command{
Name: "perm",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
originMetas, err := parsePermMetas(permOption{
importPath: "github.com/filecoin-project/lotus/api",
})
if err != nil {
log.Fatalln("parse lotus api interfaces:", err)
}

targetMetas, err := parsePermMetas(permOption{
importPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v1",
})
if err != nil {
log.Fatalln("parse venus chain api interfaces:", err)
}
for _, pair := range util.APIPairs {
originMetas, err := parsePermMetas(pair.Lotus.ParseOpt)
if err != nil {
log.Fatalln("parse lotus api interfaces:", err)
}

originMap := map[string]permMeta{}
for _, om := range originMetas {
if om.perm != "" {
originMap[om.meth] = om
targetMetas, err := parsePermMetas(pair.Venus.ParseOpt)
if err != nil {
log.Fatalln("parse venus chain api interfaces:", err)
}
}

for _, tm := range targetMetas {
om, has := originMap[tm.meth]
if !has {
fmt.Printf("%s.%s: %s <> N/A\n", tm.iface, tm.meth, tm.perm)
continue
originMap := map[string]permMeta{}
for _, om := range originMetas {
if om.perm != "" {
originMap[om.meth] = om
}
}

if tm.perm != om.perm {
fmt.Printf("%s.%s: %s <> %s.%s: %s\n", tm.iface, tm.meth, tm.perm, om.iface, om.meth, om.perm)
fmt.Printf("v%d: %s <> %s\n", pair.Ver, pair.Venus.ParseOpt.ImportPath, pair.Lotus.ParseOpt.ImportPath)
for _, tm := range targetMetas {
om, has := originMap[tm.meth]
if !has {
fmt.Printf("\t- %s.%s\n", tm.iface, tm.meth)
continue
}

if tm.perm != om.perm {
fmt.Printf("\t> %s.%s: %s <> %s.%s: %s\n", tm.iface, tm.meth, tm.perm, om.iface, om.meth, om.perm)
}
}
}

fmt.Println()
fmt.Println()
}

return nil
},
}

type permOption struct {
importPath string
excluded map[string]struct{}
}

type permMeta struct {
pkg string
iface string
meth string
perm string
}

func parsePermMetas(opt permOption) ([]permMeta, error) {
ifaceMetas, err := util.ParseInterfaceMetas(opt.importPath)
func parsePermMetas(opt util.InterfaceParseOption) ([]permMeta, error) {
ifaceMetas, err := util.ParseInterfaceMetas(opt)
if err != nil {
return nil, err
}

var permMetas []permMeta
for _, iface := range ifaceMetas {
if _, yes := opt.excluded[iface.Name]; yes {
continue
}

for _, ifMeth := range iface.Defined {
permMetas = append(permMetas, permMeta{
pkg: opt.importPath,
pkg: opt.ImportPath,
iface: iface.Name,
meth: ifMeth.Name,
perm: getPerms(ifMeth),
Expand Down
59 changes: 59 additions & 0 deletions venus-devtool/util/api_meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package util

import (
"reflect"

"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/api/v1api"

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

var APIPairs = []struct {
Ver int
Lotus APIMeta
Venus APIMeta
}{
{
Ver: 0,
Lotus: APIMeta{
Type: reflect.TypeOf((*v0api.FullNode)(nil)).Elem(),
ParseOpt: InterfaceParseOption{
ImportPath: "github.com/filecoin-project/lotus/api/v0api",
Included: []string{"FullNode", "Common", "Net"},
},
},
Venus: APIMeta{
Type: reflect.TypeOf((*v0.FullNode)(nil)).Elem(),
ParseOpt: InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v0",
IncludeAll: true,
},
},
},
{
Ver: 1,
Lotus: APIMeta{
Type: reflect.TypeOf((*v1api.FullNode)(nil)).Elem(),
ParseOpt: InterfaceParseOption{
ImportPath: "github.com/filecoin-project/lotus/api",
Included: []string{"FullNode", "Common", "Net"},
},
},
Venus: APIMeta{
Type: reflect.TypeOf((*v1.FullNode)(nil)).Elem(),
ParseOpt: InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v1",
IncludeAll: true,
},
},
},
}

var LatestAPIPair = APIPairs[len(APIPairs)-1]

type APIMeta struct {
Type reflect.Type
ParseOpt InterfaceParseOption
}
23 changes: 21 additions & 2 deletions venus-devtool/util/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"strings"
)

type InterfaceParseOption struct {
ImportPath string
IncludeAll bool
Included []string
}

type InterfaceMeta struct {
Pkg string
File string
Expand All @@ -26,6 +32,8 @@ type InterfaceMethodMeta struct {
type ifaceMetaVisitor struct {
pname string
fname string
included map[string]struct{}
includAll bool
comments ast.CommentMap
ifaces []*InterfaceMeta
ifaceIdxes map[string]int
Expand All @@ -42,6 +50,10 @@ func (iv *ifaceMetaVisitor) Visit(node ast.Node) ast.Visitor {
return iv
}

if _, yes := iv.included[st.Name.Name]; !yes && !iv.includAll {
return iv
}

ifaceIdx, ok := iv.ifaceIdxes[st.Name.Name]
if !ok {
ifaceIdx = len(iv.ifaces)
Expand Down Expand Up @@ -72,8 +84,8 @@ func (iv *ifaceMetaVisitor) Visit(node ast.Node) ast.Visitor {
return iv
}

func ParseInterfaceMetas(importPath string) ([]*InterfaceMeta, error) {
location, err := FindLocationForImportPath(importPath)
func ParseInterfaceMetas(opt InterfaceParseOption) ([]*InterfaceMeta, error) {
location, err := FindLocationForImportPath(opt.ImportPath)
if err != nil {
return nil, err
}
Expand All @@ -86,13 +98,20 @@ func ParseInterfaceMetas(importPath string) ([]*InterfaceMeta, error) {

var metas []*InterfaceMeta

included := map[string]struct{}{}
for _, one := range opt.Included {
included[one] = struct{}{}
}

for pname, pkg := range pkgs {
if strings.HasSuffix(pname, "_test") {
continue
}

visitor := &ifaceMetaVisitor{
pname: pname,
included: included,
includAll: opt.IncludeAll,
ifaceIdxes: map[string]int{},
}

Expand Down
Loading

0 comments on commit 347645a

Please sign in to comment.