Skip to content

Commit

Permalink
api to get virtual group family id from SP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Apr 23, 2024
1 parent 7e89adb commit 5c44f5a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
45 changes: 39 additions & 6 deletions client/api_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ import (
"time"

sdkmath "cosmossdk.io/math"

ctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/rs/zerolog/log"

"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
gnfdsdk "github.com/bnb-chain/greenfield/sdk/types"
gnfdTypes "github.com/bnb-chain/greenfield/types"
"github.com/bnb-chain/greenfield/types/s3util"
permTypes "github.com/bnb-chain/greenfield/x/permission/types"
storageTypes "github.com/bnb-chain/greenfield/x/storage/types"
virtualgroupTypes "github.com/bnb-chain/greenfield/x/virtualgroup/types"

"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
)

// IBucketClient interface defines functions related to bucket.
Expand Down Expand Up @@ -60,6 +59,7 @@ type IBucketClient interface {
ListBucketsByPaymentAccount(ctx context.Context, paymentAccount string, opts types.ListBucketsByPaymentAccountOptions) (types.ListBucketsByPaymentAccountResult, error)
SetBucketFlowRateLimit(ctx context.Context, bucketName string, paymentAddr, bucketOwner sdk.AccAddress, flowRateLimit sdkmath.Int, opt types.SetBucketFlowRateLimitOption) (string, error)
GetPaymentAccountFlowRateLimit(ctx context.Context, paymentAddr, bucketOwner sdk.AccAddress, bucketName string) (*storageTypes.QueryPaymentAccountBucketFlowRateLimitResponse, error)
GetRecommendedVirtualGroupFamilyIDBySPID(ctx context.Context, spID uint32) (uint32, error)
}

// GetCreateBucketApproval - Send create bucket approval request to SP and returns the signature info for the approval of preCreating resources.
Expand Down Expand Up @@ -175,9 +175,9 @@ func (c *Client) CreateBucket(ctx context.Context, bucketName string, primaryAdd
return "", err
}

familyID, err := c.QuerySpOptimalGlobalVirtualGroupFamily(ctx, sp.Id, virtualgroupTypes.Strategy_Oldest_Create_Time)
familyID, err := c.GetRecommendedVirtualGroupFamilyIDBySPID(ctx, sp.Id)
if err != nil {
log.Error().Msg(fmt.Sprintf("failed to query sp ptimal vgf: %s", err.Error()))
log.Error().Msg(fmt.Sprintf("failed to query sp vgf: %s", err.Error()))
var signedMsg *storageTypes.MsgCreateBucket
signedMsg, err = c.GetCreateBucketApproval(ctx, createBucketMsg)
if err != nil {
Expand Down Expand Up @@ -1229,3 +1229,36 @@ func (c *Client) getMigrationStateFromSP(ctx context.Context, bucketName string,

return migrationProgress, nil
}

func (c *Client) GetRecommendedVirtualGroupFamilyIDBySPID(ctx context.Context, spID uint32) (uint32, error) {
endpoint, err := c.getSPUrlByID(spID)
if err != nil {
log.Error().Msg(fmt.Sprintf("route endpoint by sp ID: %d failed, err: %s", spID, err.Error()))
return 0, err
}
return c.getRecommendedVirtualGroupFamilyIDBySPEndpoint(ctx, endpoint)
}

func (c *Client) getRecommendedVirtualGroupFamilyIDBySPEndpoint(ctx context.Context, endpoint *url.URL) (uint32, error) {
reqMeta := requestMeta{
urlRelPath: "get-recommended-vgf",
}
sendOpt := sendOptions{
method: http.MethodGet,
adminInfo: AdminAPIInfo{
isAdminAPI: true,
adminVersion: types.AdminV1Version,
},
disableCloseBody: true,
}
resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return 0, err
}
vgf := types.VirtualGroupFamily{}
err = xml.NewDecoder(resp.Body).Decode(&vgf)
if err != nil {
return 0, err
}
return vgf.Id, nil
}
8 changes: 8 additions & 0 deletions e2e/e2e_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package e2e

import (
"bytes"
"context"
"fmt"
"io"
"os"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/bnb-chain/greenfield/types/resource"

"cosmossdk.io/math"

"github.com/stretchr/testify/suite"

"github.com/bnb-chain/greenfield-go-sdk/client"
Expand Down Expand Up @@ -993,3 +995,9 @@ func (s *StorageTestSuite) PutObjectWithRetry(bucketName, objectName string, obj
}
return err
}

func (s *StorageTestSuite) Test_quota() {
vgfID, err := s.Client.GetRecommendedVirtualGroupFamilyIDBySPID(context.Background(), 1)
s.Require().NoError(err)
fmt.Println(vgfID)
}
5 changes: 5 additions & 0 deletions types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,8 @@ type PolicyMeta struct {
// ExpirationTime defines the expiration time of permission
ExpirationTime int64 `xml:"ExpirationTime"`
}

type VirtualGroupFamily struct {
XMLName xml.Name `xml:"VirtualGroupFamily"`
Id uint32 `xml:"Id"`
}

0 comments on commit 5c44f5a

Please sign in to comment.