Skip to content

Commit

Permalink
feat: STM annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
zl03jsj committed Sep 24, 2022
1 parent 794a32f commit c5c98d0
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ var RunCmd = &cli.Command{
}

// TODO: properly parse api endpoint (or make it a URL)
return ServeRPC(fullAPI, stop, endpoint)
return ServeRPC(fullAPI, stop, endpoint, nil)
},
}
2 changes: 1 addition & 1 deletion cmd/mock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func main() {
log.Println("Pre-preparation completed")
// TODO: properly parse api endpoint (or make it a URL)
// Use serveRPC method to perform local CLI debugging
err = cmd.ServeRPC(fullAPI, stop, endpoint)
err = cmd.ServeRPC(fullAPI, stop, endpoint, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
19 changes: 16 additions & 3 deletions cmd/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func CorsMiddleWare(next http.Handler) http.Handler {
})
}

// Start the interface service and bind the address
func ServeRPC(a api.IFullAPI, stop build.StopFunc, addr string) error {
// ServeRPC Start the interface service and bind the address
func ServeRPC(a api.IFullAPI, stop build.StopFunc, addr string, sigChan chan os.Signal) error {
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", permissionedFullAPI(a))
ah := &Handler{
Expand All @@ -58,7 +58,9 @@ func ServeRPC(a api.IFullAPI, stop build.StopFunc, addr string) error {
return fmt.Errorf("could not listen: %w", err)
}
srv := &http.Server{Handler: http.DefaultServeMux}
sigChan := make(chan os.Signal, 2)
if sigChan == nil {
sigChan = make(chan os.Signal, 2)
}
go func() {
<-sigChan
if err := srv.Shutdown(context.TODO()); err != nil {
Expand All @@ -74,6 +76,17 @@ func ServeRPC(a api.IFullAPI, stop build.StopFunc, addr string) error {
return srv.Serve(manet.NetListener(lst))
}

func NewService(a api.IFullAPI, stop build.StopFunc, addr string) (*http.Server, error) {
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", permissionedFullAPI(a))
ah := &Handler{
Verify: a.AuthVerify,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", CorsMiddleWare(ah))
return &http.Server{Handler: http.DefaultServeMux}, nil
}

func permissionedFullAPI(a api.IFullAPI) api.IFullAPI {
var out shared.IFullAPIStruct
permission.PermissionProxy(a, &out)
Expand Down
71 changes: 71 additions & 0 deletions common/api_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//stm: #unit
package common

import (
"context"
"encoding/hex"
"reflect"
"testing"

"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/venus-wallet/filemgr"
"github.com/gbrlsnchs/jwt/v3"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)

func TestCommon_AuthVerify(t *testing.T) {
//stm: @VENUSWALLET_API_COMMON_AUTH_VERIFY_001, @VENUSWALLET_API_COMMON_AUTH_NEW_001
t.Parallel()
var c Common

cng, err := filemgr.RandJWTConfig()
require.NoError(t, err)
sec, err := hex.DecodeString(cng.Secret)
require.NoError(t, err)

app := fxtest.New(t,
fx.Provide(func() *jwt.HMACSHA { return jwt.NewHS256(sec) }),
fx.Populate(&c),
)
defer app.RequireStart().RequireStop()

type args struct {
token string
}

type testCase struct {
args args
want []auth.Permission
wantErr bool
}

tests := map[string]*testCase{
"invalid-token-verify": {args: args{"invalid-token"}, want: nil, wantErr: true},
}

ctx := context.Background()

validTokenCase := &testCase{want: []auth.Permission{"admin", "sign", "write"}, wantErr: false}
token, err := c.AuthNew(ctx, validTokenCase.want)
require.NoError(t, err)
validTokenCase.args.token = string(token)

tests["valid-token-verify"] = validTokenCase

for tName, tt := range tests {
t.Run(tName, func(t *testing.T) {
got, err := c.AuthVerify(ctx, tt.args.token)

if (err != nil) != tt.wantErr {
t.Errorf("AuthVerify() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AuthVerify() got = %v, want %v", got, tt.want)
}
})
}
}
19 changes: 16 additions & 3 deletions filemgr/fs_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
//stm: #unit
package filemgr

import (
"io/ioutil"
"os"
"testing"

"github.com/stretchr/testify/require"
"gotest.tools/assert"
)

func TestNewFS(t *testing.T) {
//stm: @VENUSWALLET_FILEMGR_FS_NEW_001
fsPath, err := ioutil.TempDir("", "venus-repo-")
defer os.RemoveAll(fsPath)
if err != nil {
Expand All @@ -18,9 +21,19 @@ func TestNewFS(t *testing.T) {
fs, err := NewFS(fsPath, &OverrideParams{
API: targetAPI,
})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
require.NotNil(t, fs)

//stm: @VENUSWALLET_FILEMGR_FS_API_SECRET_001,
secr, err := fs.APISecret()
require.NoError(t, err)
require.NotNil(t, secr)

//stm: @VENUSWALLET_FILEMGR_FS_API_STRATEGY_TOKEN_001
token, err := fs.APIStrategyToken("password")
require.NoError(t, err)
require.NotEqual(t, token, "")

curAPI, err := fs.APIEndpoint()
if err != nil {
t.Fatal()
Expand Down
120 changes: 120 additions & 0 deletions integration_test/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package integration

import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"syscall"
"time"

"github.com/filecoin-project/venus-wallet/api"
"github.com/filecoin-project/venus-wallet/build"
"github.com/filecoin-project/venus-wallet/cmd"
"github.com/filecoin-project/venus-wallet/core"
"github.com/filecoin-project/venus-wallet/filemgr"
"github.com/filecoin-project/venus-wallet/middleware"
"github.com/filecoin-project/venus-wallet/version"
logging "github.com/ipfs/go-log/v2"
"github.com/multiformats/go-multiaddr"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)

var log = logging.Logger("wallet_instance")

type WalletInst struct {
repo filemgr.Repo
repoDir string

stopChan chan error
sigChan chan os.Signal
}

func (inst *WalletInst) Start() (string, error) {
secret, err := inst.repo.APISecret()
if err != nil {
return "", nil
}

var fullAPI api.IFullAPI
var appStopFn build.StopFunc

ctx, _ := tag.New(context.Background(), tag.Insert(middleware.Version, version.BuildVersion))

if appStopFn, err = build.New(ctx, build.FullAPIOpt(&fullAPI),
build.WalletOpt(inst.repo, ""),
build.CommonOpt(secret)); err != nil {
return "", err
}

// Register all metric views
if err = view.Register(
middleware.DefaultViews...,
); err != nil {
return "", fmt.Errorf("can't register the view: %v", err)
}
stats.Record(ctx, middleware.VenusInfo.M(1))

endPoint, err := inst.repo.APIEndpoint()

ma, err := multiaddr.NewMultiaddr(endPoint)
if err != nil {
return "", fmt.Errorf("new multi-address failed:%w", err)
}

url, err := ToURL(ma)
if err != nil {
return "", fmt.Errorf("convert multi-addr:%s to url failed:%w", endPoint, err)
}

go func() {
err := cmd.ServeRPC(fullAPI, appStopFn, endPoint, inst.sigChan)
inst.stopChan <- err
}()

return url.String(), inst.checkService()
}

func (inst *WalletInst) checkService() error {
select {
case err := <-inst.stopChan:
return err
case <-time.After(time.Second):
log.Info("waiting for service shutdown for 1 seconds")
}
return nil
}

func (inst *WalletInst) StopAndWait() error {
inst.sigChan <- syscall.SIGINT
for {
if err := inst.checkService(); err != nil {
// server close is not an error
if strings.ContainsAny(err.Error(), "server closed") {
return nil
}
return err
}
}
}

func NewWalletInst() (*WalletInst, error) {
dir, err := ioutil.TempDir("", "venus_wallet_")
if err != nil {
return nil, err

}
repo, err := filemgr.NewFS(dir, nil)
if err != nil {
return nil, err
}
core.WalletStrategyLevel = repo.Config().Strategy.Level
return &WalletInst{
repo: repo,
sigChan: make(chan os.Signal, 1),
stopChan: make(chan error, 1),
repoDir: dir}, nil
}
Loading

0 comments on commit c5c98d0

Please sign in to comment.