-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add stm annotations/增加STM注解 (#103)
feat: STM annotations
- Loading branch information
Showing
15 changed files
with
528 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
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 "", err | ||
} | ||
|
||
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() | ||
if err != nil { | ||
return "", fmt.Errorf("get api endpoint failed:%w", err) | ||
} | ||
|
||
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("no signal from stopping channel, after 1 second waitting...") | ||
} | ||
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 | ||
} |
Oops, something went wrong.