Skip to content

Commit

Permalink
Feat/simple api perm comment (#4655)
Browse files Browse the repository at this point in the history
* simplified permission comment

* fix replica cmd

* add proxy util
  • Loading branch information
simlecode authored Dec 29, 2021
1 parent ae10362 commit f7328ce
Show file tree
Hide file tree
Showing 35 changed files with 485 additions and 741 deletions.
55 changes: 6 additions & 49 deletions venus-devtool/api-gen/proxygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"
"unicode"

"github.com/filecoin-project/go-jsonrpc/auth"
_ "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/venus/app/client/funcrule"
"golang.org/x/xerrors"
)

Expand All @@ -36,49 +32,12 @@ var (
VenusV1ApiFile = path.Join(VenusAPIPath, "chain/v1", GenAPIFileName)
)

// Rule[perm:read,ignore:true]
var rulePattern = `Rule\[(?P<rule>.*)\]`

type ruleKey = string

const (
rkPerm ruleKey = "perm"
rkIgnore ruleKey = "ignore"
rkPerm ruleKey = "perm"
)

var defaultPerm = []string{"perm", "read"}
var regRule, _ = regexp.Compile(rulePattern)

func parseRule(comment string) (*funcrule.Rule, map[string][]string) {
rule := new(funcrule.Rule)
match := regRule.FindStringSubmatch(comment)
tags := map[string][]string{}
if len(match) == 2 {
pairs := strings.Split(match[1], ",")
for _, v := range pairs {
pair := strings.Split(v, ":")
if len(pair) != 2 {
continue
}
switch pair[0] {
case rkPerm:
tags[rkPerm] = pair
rule.Perm = auth.Permission(pair[1])
case rkIgnore:
ig, err := strconv.ParseBool(pair[1])
if err != nil {
panic("the rule tag is invalid format")
}
rule.Ignore = ig
}
}
} else {
rule.Perm = "read"
tags[rkPerm] = defaultPerm
}
return rule, tags
}

type methodMeta struct {
node ast.Node
ftype *ast.FuncType
Expand Down Expand Up @@ -372,13 +331,11 @@ func methodMetaFromInterface(rootPath string, pkg, outpkg string) (*meta, error)

// try to parse tag info
if len(filteredComments) > 0 {
cmt := filteredComments[0].List[len(filteredComments[0].List)-1].Text
rule, tags := parseRule(cmt)
info.Methods[mname].Tags[rkPerm] = tags[rkPerm]
// remove ignore method
if rule.Ignore {
ignoreMethods[ifname] = append(ignoreMethods[ifname], mname)
}
lastComment := filteredComments[len(filteredComments)-1]
// eg. cmt = `//perm:read`
cmt := lastComment.List[len(lastComment.List)-1].Text
cmt = strings.Replace(cmt, "//", "", 1)
info.Methods[mname].Tags[rkPerm] = strings.Split(cmt, ":")
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions venus-devtool/compatible/actors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -145,18 +146,35 @@ var replicaCmd = &cli.Command{
return fmt.Errorf("find chain/actors: %w", err)
}

reg := regexp.MustCompile(`v[0-9]+.go`)

files, err := listFilesInDir(srcDir, func(path string, d fs.DirEntry) bool {
if d.IsDir() {
return true
}

// need adt.go diff_adt.go
if strings.Contains(path, "adt.go") {
return false
}

// skip test file
if strings.HasSuffix(path, "test.go") {
return true
}

// diff.go diff_deadlines.go version.go params.go utils.go util.go
if !strings.Contains(path, "diff") && !strings.HasSuffix(path, "version.go") &&
!strings.HasSuffix(path, "params.go") && !strings.Contains(path, "util") {
if strings.HasSuffix(path, "main.go") || strings.Contains(path, "template") ||
strings.Contains(path, "message") {
return true
}

dir := filepath.Dir(path)
arr := strings.Split(dir, "/")
if strings.HasSuffix(path, fmt.Sprintf("%s.go", arr[len(arr)-1])) {
return true
}

if reg.MatchString(d.Name()) {
return true
}

Expand Down
2 changes: 2 additions & 0 deletions venus-shared/actors/adt/adt.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FETCHED FROM LOTUS: adt/adt.go

package adt

import (
Expand Down
2 changes: 2 additions & 0 deletions venus-shared/actors/adt/store.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FETCHED FROM LOTUS: adt/store.go

package adt

import (
Expand Down
2 changes: 2 additions & 0 deletions venus-shared/actors/aerrors/error.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FETCHED FROM LOTUS: aerrors/error.go

package aerrors

import (
Expand Down
2 changes: 2 additions & 0 deletions venus-shared/actors/aerrors/wrap.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// FETCHED FROM LOTUS: aerrors/wrap.go

package aerrors

import (
Expand Down
31 changes: 31 additions & 0 deletions venus-shared/actors/builtin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// FETCHED FROM LOTUS: builtin/README.md

# Actors

This package contains shims for abstracting over different actor versions.

## Design

Shims in this package follow a few common design principles.

### Structure Agnostic

Shims interfaces defined in this package should (ideally) not change even if the
structure of the underlying data changes. For example:

* All shims store an internal "store" object. That way, state can be moved into
a separate object without needing to add a store to the function signature.
* All functions must return an error, even if unused for now.

### Minimal

These interfaces should be expanded only as necessary to reduce maintenance burden.

### Queries, not field assessors.

When possible, functions should query the state instead of simply acting as
field assessors. These queries are more likely to remain stable across
specs-actor upgrades than specific state fields.

Note: there is a trade-off here. Avoid implementing _complicated_ query logic
inside these shims, as it will need to be replicated in every shim.
12 changes: 4 additions & 8 deletions venus-shared/api/chain/v0/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import (
)

type IBlockStore interface {
// Rule[perm:read]
ChainReadObj(ctx context.Context, ocid cid.Cid) ([]byte, error)
// Rule[perm:admin]
ChainDeleteObj(ctx context.Context, obj cid.Cid) error
// Rule[perm:read]
ChainHasObj(ctx context.Context, obj cid.Cid) (bool, error)
// Rule[perm:read]
ChainStatObj(ctx context.Context, obj cid.Cid, base cid.Cid) (chain2.ObjStat, error)
ChainReadObj(ctx context.Context, cid cid.Cid) ([]byte, error) //perm:read
ChainDeleteObj(ctx context.Context, obj cid.Cid) error //perm:admin
ChainHasObj(ctx context.Context, obj cid.Cid) (bool, error) //perm:read
ChainStatObj(ctx context.Context, obj cid.Cid, base cid.Cid) (chain2.ObjStat, error) //perm:read
}
Loading

0 comments on commit f7328ce

Please sign in to comment.