Skip to content

Commit

Permalink
fix: fix error code judgment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
will-2012 committed May 25, 2023
1 parent cf31063 commit 863c86f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
12 changes: 6 additions & 6 deletions base/gnfd/gnfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type GreenfieldClient struct {
Provider string
}

// GnfdClient return the greenfield chain client.
// GnfdClient returns the greenfield chain client.
func (client *GreenfieldClient) GnfdClient() *chainClient.GreenfieldClient {
return client.chainClient
}
Expand All @@ -55,7 +55,7 @@ type Gnfd struct {
mutex sync.RWMutex
}

// NewGnfd return the Greenfield instance.
// NewGnfd returns the Greenfield instance.
func NewGnfd(cfg *GnfdChainConfig) (*Gnfd, error) {
if len(cfg.ChainAddress) == 0 {
return nil, errors.New("greenfield nodes missing")
Expand Down Expand Up @@ -88,21 +88,21 @@ func (g *Gnfd) Close() error {
return nil
}

// getCurrentClient return the current client to use.
// getCurrentClient returns the current client to use.
func (g *Gnfd) getCurrentClient() *GreenfieldClient {
g.mutex.RLock()
defer g.mutex.RUnlock()
return g.client
}

// setCurrentClient set client to current client for using.
// setCurrentClient sets client to current client for using.
func (g *Gnfd) setCurrentClient(client *GreenfieldClient) {
g.mutex.Lock()
defer g.mutex.Unlock()
g.client = client
}

// updateClient select the client that block height is the largest and set to current client.
// updateClient selects the client that block height is the largest and set to current client.
func (g *Gnfd) updateClient() {
ticker := time.NewTicker(UpdateClientInternal * time.Second)
for {
Expand All @@ -117,7 +117,7 @@ func (g *Gnfd) updateClient() {
context.Background(),
&tmservice.GetLatestBlockRequest{})
if err != nil {
log.Errorw("get latest block height failed", "node_addr", client.Provider, "error", err)
log.Errorw("failed to get latest block height", "node_addr", client.Provider, "error", err)
continue
}
currentHeight := (uint64)(resp.SdkBlock.Header.Height)
Expand Down
37 changes: 10 additions & 27 deletions base/gnfd/gnfd_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gnfd

import (
"context"
"errors"
"math"
"strconv"
"time"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

merrors "github.com/bnb-chain/greenfield-storage-provider/model/errors"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"
permissiontypes "github.com/bnb-chain/greenfield/x/permission/types"
Expand Down Expand Up @@ -78,24 +76,21 @@ func (g *Gnfd) QueryStorageParams(
return &resp.Params, nil
}

// QueryBucketInfo return the bucket info by name.
// QueryBucketInfo returns the bucket info by name.
func (g *Gnfd) QueryBucketInfo(
ctx context.Context,
bucket string) (
*storagetypes.BucketInfo, error) {
client := g.getCurrentClient().GnfdClient()
resp, err := client.HeadBucket(ctx, &storagetypes.QueryHeadBucketRequest{BucketName: bucket})
if errors.Is(err, storagetypes.ErrNoSuchBucket) {
return nil, ErrNoSuchBucket
}
if err != nil {
log.CtxErrorw(ctx, "failed to query bucket", "bucket_name", bucket, "error", err)
return nil, err
}
return resp.GetBucketInfo(), nil
}

// QueryObjectInfo return the object info by name.
// QueryObjectInfo returns the object info by name.
func (g *Gnfd) QueryObjectInfo(
ctx context.Context,
bucket, object string) (
Expand All @@ -112,7 +107,7 @@ func (g *Gnfd) QueryObjectInfo(
return resp.GetObjectInfo(), nil
}

// QueryObjectInfoByID return the object info by name.
// QueryObjectInfoByID returns the object info by name.
func (g *Gnfd) QueryObjectInfoByID(
ctx context.Context,
objectID string) (
Expand All @@ -121,54 +116,42 @@ func (g *Gnfd) QueryObjectInfoByID(
resp, err := client.HeadObjectById(ctx, &storagetypes.QueryHeadObjectByIdRequest{
ObjectId: objectID,
})
// TODO: refine it
if errors.Is(err, storagetypes.ErrNoSuchObject) {
return nil, merrors.ErrNoSuchObject
}
if err != nil {
log.CtxErrorw(ctx, "failed to query object", "object_id", objectID, "error", err)
return nil, err
}
return resp.GetObjectInfo(), nil
}

// QueryBucketInfoAndObjectInfo return bucket info and object info, if not found, return the corresponding error code
// QueryBucketInfoAndObjectInfo returns bucket info and object info, if not found, return the corresponding error code
func (g *Gnfd) QueryBucketInfoAndObjectInfo(
ctx context.Context,
bucket, object string) (
*storagetypes.BucketInfo,
*storagetypes.ObjectInfo,
error) {
bucketInfo, err := g.QueryBucketInfo(ctx, bucket)
// TODO: refine it
if errors.Is(err, storagetypes.ErrNoSuchBucket) {
return nil, nil, merrors.ErrNoSuchBucket
}
if err != nil {
return nil, nil, err
}
objectInfo, err := g.QueryObjectInfo(ctx, bucket, object)
// TODO: refine it
if errors.Is(err, storagetypes.ErrNoSuchObject) {
return bucketInfo, nil, merrors.ErrNoSuchObject
}
if err != nil {
return bucketInfo, nil, err
}
return bucketInfo, objectInfo, nil
}

// ListenObjectSeal return an indication of the object is sealed.
// ListenObjectSeal returns an indication of the object is sealed.
// TODO:: retrieve service support seal event subscription
func (g *Gnfd) ListenObjectSeal(
ctx context.Context,
objectID uint64,
timeOutHeight int) (bool, error) {
timeoutHeight int) (bool, error) {
var (
objectInfo *storagetypes.ObjectInfo
err error
)
for i := 0; i < timeOutHeight; i++ {
for i := 0; i < timeoutHeight; i++ {
time.Sleep(ExpectedOutputBlockInternal * time.Second)
objectInfo, err = g.QueryObjectInfoByID(ctx, strconv.FormatUint(objectID, 10))
if err != nil {
Expand All @@ -187,7 +170,7 @@ func (g *Gnfd) ListenObjectSeal(
return false, err
}

// QueryPaymentStreamRecord return the steam record info by account.
// QueryPaymentStreamRecord returns the steam record info by account.
func (g *Gnfd) QueryPaymentStreamRecord(
ctx context.Context,
account string) (
Expand All @@ -204,7 +187,7 @@ func (g *Gnfd) QueryPaymentStreamRecord(
return &resp.StreamRecord, nil
}

// VerifyGetObjectPermission verify get object permission.
// VerifyGetObjectPermission verifies get object permission.
func (g *Gnfd) VerifyGetObjectPermission(
ctx context.Context,
account, bucket, object string) (
Expand All @@ -226,7 +209,7 @@ func (g *Gnfd) VerifyGetObjectPermission(
return false, err
}

// VerifyPutObjectPermission verify put object permission.
// VerifyPutObjectPermission verifies put object permission.
func (g *Gnfd) VerifyPutObjectPermission(
ctx context.Context,
account, bucket, object string) (
Expand Down
4 changes: 2 additions & 2 deletions base/types/gfsptask/task_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
KeyPrefixGfSpDownloadObjectTask = "DownloadObject"
KeyPrefixGfSpChallengeTask = "ChallengePiece"
KeyPrefixGfSpUploadObjectTask = "Uploading"
KeyPrefixGfSpReplicatePieceTask = "Uploading"
KeyPrefixGfSpSealObjectTask = "Uploading"
KeyPrefixGfSpReplicatePieceTask = "Replicating"
KeyPrefixGfSpSealObjectTask = "Sealing"
KeyPrefixGfSpReceivePieceTask = "ReceivePiece"
)

Expand Down
46 changes: 40 additions & 6 deletions modular/authorizer/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package authorizer
import (
"context"
"net/http"
"strings"

"github.com/bnb-chain/greenfield-storage-provider/base/gfspapp"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfsperrors"
Expand All @@ -18,13 +19,14 @@ var (
ErrUnsupportedAuthType = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20001, "unsupported auth op type")
ErrMismatchSp = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20002, "mismatched primary sp")
ErrNotCreatedState = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20003, "object has not been created state")
ErrNotSealedState = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20004, "object has not been sealed")
ErrNotSealedState = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20004, "object has not been sealed state")
ErrPaymentState = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20005, "payment account is not active")
ErrNoSuchAccount = gfsperrors.Register(module.AuthorizationModularName, http.StatusNotFound, 20006, "no such account")
ErrNoSuchBucket = gfsperrors.Register(module.AuthorizationModularName, http.StatusNotFound, 20007, "no such bucket")
ErrRepeatedBucket = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20008, "repeated bucket")
ErrRepeatedObject = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20009, "repeated object")
ErrNoPermission = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20010, "no permission")
ErrNoSuchObject = gfsperrors.Register(module.AuthorizationModularName, http.StatusNotFound, 20008, "no such object")
ErrRepeatedBucket = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20009, "repeated bucket")
ErrRepeatedObject = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20010, "repeated object")
ErrNoPermission = gfsperrors.Register(module.AuthorizationModularName, http.StatusBadRequest, 20011, "no permission")
ErrConsensus = gfsperrors.Register(module.AuthorizationModularName, http.StatusInternalServerError, 25002, "server slipped away, try again later")
)

Expand Down Expand Up @@ -118,6 +120,13 @@ func (a *AuthorizeModular) VerifyAuthorize(
bucketInfo, objectInfo, err := a.baseApp.Consensus().QueryBucketInfoAndObjectInfo(ctx, bucket, object)
if err != nil {
log.CtxErrorw(ctx, "failed to get bucket and object info from consensus", "error", err)
// refer to https://github.com/bnb-chain/greenfield/blob/master/x/storage/types/errors.go
if strings.Contains(err.Error(), "No such bucket") {
return false, ErrNoSuchBucket
}
if strings.Contains(err.Error(), "No such object") {
return false, ErrNoSuchObject
}
return false, ErrConsensus
}
if bucketInfo.GetPrimarySpAddress() != a.baseApp.OperateAddress() {
Expand All @@ -126,7 +135,7 @@ func (a *AuthorizeModular) VerifyAuthorize(
return false, ErrMismatchSp
}
if objectInfo.GetObjectStatus() != storagetypes.OBJECT_STATUS_CREATED {
log.CtxErrorw(ctx, "object state is noe create", "state", objectInfo.GetObjectStatus())
log.CtxErrorw(ctx, "object state is not sealed", "state", objectInfo.GetObjectStatus())
return false, ErrNotCreatedState
}
allow, err := a.baseApp.Consensus().VerifyPutObjectPermission(ctx, account, bucket, object)
Expand All @@ -139,6 +148,13 @@ func (a *AuthorizeModular) VerifyAuthorize(
bucketInfo, objectInfo, err := a.baseApp.Consensus().QueryBucketInfoAndObjectInfo(ctx, bucket, object)
if err != nil {
log.CtxErrorw(ctx, "failed to get bucket and object info from consensus", "error", err)
// refer to https://github.com/bnb-chain/greenfield/blob/master/x/storage/types/errors.go
if strings.Contains(err.Error(), "No such bucket") {
return false, ErrNoSuchBucket
}
if strings.Contains(err.Error(), "No such object") {
return false, ErrNoSuchObject
}
return false, ErrConsensus
}
if bucketInfo.GetPrimarySpAddress() != a.baseApp.OperateAddress() {
Expand All @@ -147,7 +163,7 @@ func (a *AuthorizeModular) VerifyAuthorize(
return false, ErrMismatchSp
}
if objectInfo.GetObjectStatus() != storagetypes.OBJECT_STATUS_CREATED {
log.CtxErrorw(ctx, "object state is not create", "state", objectInfo.GetObjectStatus())
log.CtxErrorw(ctx, "object state is not created", "state", objectInfo.GetObjectStatus())
return false, ErrNotCreatedState
}
allow, err := a.baseApp.Consensus().VerifyPutObjectPermission(ctx, account, bucket, object)
Expand All @@ -160,6 +176,13 @@ func (a *AuthorizeModular) VerifyAuthorize(
bucketInfo, objectInfo, err := a.baseApp.Consensus().QueryBucketInfoAndObjectInfo(ctx, bucket, object)
if err != nil {
log.CtxErrorw(ctx, "failed to get bucket and object info from consensus", "error", err)
// refer to https://github.com/bnb-chain/greenfield/blob/master/x/storage/types/errors.go
if strings.Contains(err.Error(), "No such bucket") {
return false, ErrNoSuchBucket
}
if strings.Contains(err.Error(), "No such object") {
return false, ErrNoSuchObject
}
return false, ErrConsensus
}
if bucketInfo.GetPrimarySpAddress() != a.baseApp.OperateAddress() {
Expand Down Expand Up @@ -190,6 +213,10 @@ func (a *AuthorizeModular) VerifyAuthorize(
bucketInfo, err := a.baseApp.Consensus().QueryBucketInfo(ctx, bucket)
if err != nil {
log.CtxErrorw(ctx, "failed to get bucket info from consensus", "error", err)
// refer to https://github.com/bnb-chain/greenfield/blob/master/x/storage/types/errors.go
if strings.Contains(err.Error(), "No such bucket") {
return false, ErrNoSuchBucket
}
return false, ErrConsensus
}
if bucketInfo.GetPrimarySpAddress() != a.baseApp.OperateAddress() {
Expand All @@ -208,6 +235,13 @@ func (a *AuthorizeModular) VerifyAuthorize(
bucketInfo, objectInfo, err := a.baseApp.Consensus().QueryBucketInfoAndObjectInfo(ctx, bucket, object)
if err != nil {
log.CtxErrorw(ctx, "failed to get object info from consensus", "error", err)
// refer to https://github.com/bnb-chain/greenfield/blob/master/x/storage/types/errors.go
if strings.Contains(err.Error(), "No such bucket") {
return false, ErrNoSuchBucket
}
if strings.Contains(err.Error(), "No such object") {
return false, ErrNoSuchObject
}
return false, ErrConsensus
}
if bucketInfo.GetPrimarySpAddress() == a.baseApp.OperateAddress() {
Expand Down

0 comments on commit 863c86f

Please sign in to comment.