Skip to content

Commit 347645a

Browse files
authored
Feat/dtynn/refactor parts of the api compatible checks (#4734)
1 parent 8f0a0cf commit 347645a

File tree

8 files changed

+394
-105
lines changed

8 files changed

+394
-105
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ api-docs:
5858

5959
compatible-all: compatible-api compatible-actor
6060

61-
compatible-api: api-checksum api-diff
61+
compatible-api: api-checksum api-diff api-perm
6262

6363
api-checksum:
6464
cd venus-devtool && go run ./compatible/apis/*.go checksum > ../venus-shared/compatible-checks/api-checksum.txt

venus-devtool/compatible/apis/checksum.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ import (
77
"reflect"
88
"strings"
99

10-
"github.com/filecoin-project/lotus/api/v0api"
11-
"github.com/filecoin-project/lotus/api/v1api"
1210
"github.com/urfave/cli/v2"
11+
12+
"github.com/filecoin-project/venus/venus-devtool/util"
1313
)
1414

1515
var checksumCmd = &cli.Command{
1616
Name: "checksum",
1717
Flags: []cli.Flag{},
1818
Action: func(cctx *cli.Context) error {
19-
wants := []reflect.Type{
20-
reflect.TypeOf((*v0api.FullNode)(nil)).Elem(),
21-
reflect.TypeOf((*v1api.FullNode)(nil)).Elem(),
22-
}
23-
2419
var buf bytes.Buffer
25-
for _, rt := range wants {
20+
for _, pair := range util.APIPairs {
21+
rt := pair.Lotus.Type
2622
fmt.Printf("%s:\n", rt)
2723
for mi := 0; mi < rt.NumMethod(); mi++ {
2824
buf.Reset()

venus-devtool/compatible/apis/diff.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,19 @@ import (
55
"reflect"
66
"sort"
77

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

11-
"github.com/filecoin-project/venus/venus-shared/api/chain/v1"
1210
"github.com/filecoin-project/venus/venus-shared/typeutil"
11+
12+
"github.com/filecoin-project/venus/venus-devtool/util"
1313
)
1414

1515
var diffCmd = &cli.Command{
1616
Name: "diff",
1717
Flags: []cli.Flag{},
1818
Action: func(cctx *cli.Context) error {
19-
pairs := [][2]reflect.Type{
20-
{
21-
reflect.TypeOf((*v1.FullNode)(nil)).Elem(),
22-
reflect.TypeOf((*v1api.FullNode)(nil)).Elem(),
23-
},
24-
}
25-
26-
for _, pair := range pairs {
27-
showDiff(pair[0], pair[1])
19+
for _, pair := range util.APIPairs {
20+
showDiff(pair.Venus.Type, pair.Lotus.Type)
2821
}
2922
return nil
3023
},

venus-devtool/compatible/apis/perm.go

+29-39
Original file line numberDiff line numberDiff line change
@@ -14,72 +14,62 @@ var permCmd = &cli.Command{
1414
Name: "perm",
1515
Flags: []cli.Flag{},
1616
Action: func(cctx *cli.Context) error {
17-
originMetas, err := parsePermMetas(permOption{
18-
importPath: "github.com/filecoin-project/lotus/api",
19-
})
20-
if err != nil {
21-
log.Fatalln("parse lotus api interfaces:", err)
22-
}
23-
24-
targetMetas, err := parsePermMetas(permOption{
25-
importPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v1",
26-
})
27-
if err != nil {
28-
log.Fatalln("parse venus chain api interfaces:", err)
29-
}
17+
for _, pair := range util.APIPairs {
18+
originMetas, err := parsePermMetas(pair.Lotus.ParseOpt)
19+
if err != nil {
20+
log.Fatalln("parse lotus api interfaces:", err)
21+
}
3022

31-
originMap := map[string]permMeta{}
32-
for _, om := range originMetas {
33-
if om.perm != "" {
34-
originMap[om.meth] = om
23+
targetMetas, err := parsePermMetas(pair.Venus.ParseOpt)
24+
if err != nil {
25+
log.Fatalln("parse venus chain api interfaces:", err)
3526
}
36-
}
3727

38-
for _, tm := range targetMetas {
39-
om, has := originMap[tm.meth]
40-
if !has {
41-
fmt.Printf("%s.%s: %s <> N/A\n", tm.iface, tm.meth, tm.perm)
42-
continue
28+
originMap := map[string]permMeta{}
29+
for _, om := range originMetas {
30+
if om.perm != "" {
31+
originMap[om.meth] = om
32+
}
4333
}
4434

45-
if tm.perm != om.perm {
46-
fmt.Printf("%s.%s: %s <> %s.%s: %s\n", tm.iface, tm.meth, tm.perm, om.iface, om.meth, om.perm)
35+
fmt.Printf("v%d: %s <> %s\n", pair.Ver, pair.Venus.ParseOpt.ImportPath, pair.Lotus.ParseOpt.ImportPath)
36+
for _, tm := range targetMetas {
37+
om, has := originMap[tm.meth]
38+
if !has {
39+
fmt.Printf("\t- %s.%s\n", tm.iface, tm.meth)
40+
continue
41+
}
42+
43+
if tm.perm != om.perm {
44+
fmt.Printf("\t> %s.%s: %s <> %s.%s: %s\n", tm.iface, tm.meth, tm.perm, om.iface, om.meth, om.perm)
45+
}
4746
}
48-
}
4947

50-
fmt.Println()
48+
fmt.Println()
49+
}
5150

5251
return nil
5352
},
5453
}
5554

56-
type permOption struct {
57-
importPath string
58-
excluded map[string]struct{}
59-
}
60-
6155
type permMeta struct {
6256
pkg string
6357
iface string
6458
meth string
6559
perm string
6660
}
6761

68-
func parsePermMetas(opt permOption) ([]permMeta, error) {
69-
ifaceMetas, err := util.ParseInterfaceMetas(opt.importPath)
62+
func parsePermMetas(opt util.InterfaceParseOption) ([]permMeta, error) {
63+
ifaceMetas, err := util.ParseInterfaceMetas(opt)
7064
if err != nil {
7165
return nil, err
7266
}
7367

7468
var permMetas []permMeta
7569
for _, iface := range ifaceMetas {
76-
if _, yes := opt.excluded[iface.Name]; yes {
77-
continue
78-
}
79-
8070
for _, ifMeth := range iface.Defined {
8171
permMetas = append(permMetas, permMeta{
82-
pkg: opt.importPath,
72+
pkg: opt.ImportPath,
8373
iface: iface.Name,
8474
meth: ifMeth.Name,
8575
perm: getPerms(ifMeth),

venus-devtool/util/api_meta.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package util
2+
3+
import (
4+
"reflect"
5+
6+
"github.com/filecoin-project/lotus/api/v0api"
7+
"github.com/filecoin-project/lotus/api/v1api"
8+
9+
"github.com/filecoin-project/venus/venus-shared/api/chain/v0"
10+
"github.com/filecoin-project/venus/venus-shared/api/chain/v1"
11+
)
12+
13+
var APIPairs = []struct {
14+
Ver int
15+
Lotus APIMeta
16+
Venus APIMeta
17+
}{
18+
{
19+
Ver: 0,
20+
Lotus: APIMeta{
21+
Type: reflect.TypeOf((*v0api.FullNode)(nil)).Elem(),
22+
ParseOpt: InterfaceParseOption{
23+
ImportPath: "github.com/filecoin-project/lotus/api/v0api",
24+
Included: []string{"FullNode", "Common", "Net"},
25+
},
26+
},
27+
Venus: APIMeta{
28+
Type: reflect.TypeOf((*v0.FullNode)(nil)).Elem(),
29+
ParseOpt: InterfaceParseOption{
30+
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v0",
31+
IncludeAll: true,
32+
},
33+
},
34+
},
35+
{
36+
Ver: 1,
37+
Lotus: APIMeta{
38+
Type: reflect.TypeOf((*v1api.FullNode)(nil)).Elem(),
39+
ParseOpt: InterfaceParseOption{
40+
ImportPath: "github.com/filecoin-project/lotus/api",
41+
Included: []string{"FullNode", "Common", "Net"},
42+
},
43+
},
44+
Venus: APIMeta{
45+
Type: reflect.TypeOf((*v1.FullNode)(nil)).Elem(),
46+
ParseOpt: InterfaceParseOption{
47+
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/chain/v1",
48+
IncludeAll: true,
49+
},
50+
},
51+
},
52+
}
53+
54+
var LatestAPIPair = APIPairs[len(APIPairs)-1]
55+
56+
type APIMeta struct {
57+
Type reflect.Type
58+
ParseOpt InterfaceParseOption
59+
}

venus-devtool/util/interface.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import (
88
"strings"
99
)
1010

11+
type InterfaceParseOption struct {
12+
ImportPath string
13+
IncludeAll bool
14+
Included []string
15+
}
16+
1117
type InterfaceMeta struct {
1218
Pkg string
1319
File string
@@ -26,6 +32,8 @@ type InterfaceMethodMeta struct {
2632
type ifaceMetaVisitor struct {
2733
pname string
2834
fname string
35+
included map[string]struct{}
36+
includAll bool
2937
comments ast.CommentMap
3038
ifaces []*InterfaceMeta
3139
ifaceIdxes map[string]int
@@ -42,6 +50,10 @@ func (iv *ifaceMetaVisitor) Visit(node ast.Node) ast.Visitor {
4250
return iv
4351
}
4452

53+
if _, yes := iv.included[st.Name.Name]; !yes && !iv.includAll {
54+
return iv
55+
}
56+
4557
ifaceIdx, ok := iv.ifaceIdxes[st.Name.Name]
4658
if !ok {
4759
ifaceIdx = len(iv.ifaces)
@@ -72,8 +84,8 @@ func (iv *ifaceMetaVisitor) Visit(node ast.Node) ast.Visitor {
7284
return iv
7385
}
7486

75-
func ParseInterfaceMetas(importPath string) ([]*InterfaceMeta, error) {
76-
location, err := FindLocationForImportPath(importPath)
87+
func ParseInterfaceMetas(opt InterfaceParseOption) ([]*InterfaceMeta, error) {
88+
location, err := FindLocationForImportPath(opt.ImportPath)
7789
if err != nil {
7890
return nil, err
7991
}
@@ -86,13 +98,20 @@ func ParseInterfaceMetas(importPath string) ([]*InterfaceMeta, error) {
8698

8799
var metas []*InterfaceMeta
88100

101+
included := map[string]struct{}{}
102+
for _, one := range opt.Included {
103+
included[one] = struct{}{}
104+
}
105+
89106
for pname, pkg := range pkgs {
90107
if strings.HasSuffix(pname, "_test") {
91108
continue
92109
}
93110

94111
visitor := &ifaceMetaVisitor{
95112
pname: pname,
113+
included: included,
114+
includAll: opt.IncludeAll,
96115
ifaceIdxes: map[string]int{},
97116
}
98117

0 commit comments

Comments
 (0)