Skip to content

Commit

Permalink
add migration progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjunLi committed Dec 25, 2023
1 parent 5be80fe commit 5499a43
Showing 1 changed file with 88 additions and 15 deletions.
103 changes: 88 additions & 15 deletions client/api_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
govTypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/rs/zerolog/log"

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"
ctypes "github.com/cometbft/cometbft/rpc/core/types"

"github.com/bnb-chain/greenfield-go-sdk/pkg/utils"
"github.com/bnb-chain/greenfield-go-sdk/types"
Expand Down Expand Up @@ -52,7 +52,8 @@ type IBucketClient interface {
ListBucketsByBucketID(ctx context.Context, bucketIds []uint64, opts types.EndPointOptions) (types.ListBucketsByBucketIDResponse, error)
GetMigrateBucketApproval(ctx context.Context, migrateBucketMsg *storageTypes.MsgMigrateBucket) (*storageTypes.MsgMigrateBucket, error)
MigrateBucket(ctx context.Context, bucketName string, dstPrimarySPID uint32, opts types.MigrateBucketOptions) (string, error)
CancelMigrateBucket(ctx context.Context, bucketName string, opts types.CancelMigrateBucketOptions) (uint64, string, error)
CancelMigrateBucket(ctx context.Context, bucketName string, opts types.CancelMigrateBucketOptions) (string, error)
GetBucketMigrationProgress(ctx context.Context, bucketName string, destSP uint32) (types.MigrationProgress, error)

Check failure on line 56 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

undefined: types.MigrationProgress
ListBucketsByPaymentAccount(ctx context.Context, paymentAccount string, opts types.ListBucketsByPaymentAccountOptions) (types.ListBucketsByPaymentAccountResult, error)
}

Expand Down Expand Up @@ -976,26 +977,46 @@ func (c *Client) MigrateBucket(ctx context.Context, bucketName string, dstPrimar
//
// - opt: The options of the proposal meta and transaction.
//
// - ret1: The proposal ID of canceling migration.
//
// - ret2: Transaction hash return from blockchain.
// - ret1: Transaction hash return from blockchain.
//
// - ret3: Return error when the request of cancel migration failed, otherwise return nil.
func (c *Client) CancelMigrateBucket(ctx context.Context, bucketName string, opts types.CancelMigrateBucketOptions) (uint64, string, error) {
govModuleAddress, err := c.GetModuleAccountByName(ctx, govTypes.ModuleName)
// - ret2: Return error when the request of cancel migration failed, otherwise return nil.
func (c *Client) CancelMigrateBucket(ctx context.Context, bucketName string, opts types.CancelMigrateBucketOptions) (string, error) {

cancelMigrateBucketMsg := storageTypes.NewMsgCancelMigrateBucket(c.MustGetDefaultAccount().GetAddress(), bucketName)

err := cancelMigrateBucketMsg.ValidateBasic()
if err != nil {
return 0, "", err
return "", err
}

// set the default txn broadcast mode as block mode
if opts.TxOpts == nil {

Check failure on line 993 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

invalid operation: opts.TxOpts == nil (mismatched types "github.com/bnb-chain/greenfield/sdk/types".TxOption and untyped nil)
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}

Check failure on line 995 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

cannot use &gnfdsdk.TxOption{…} (value of type *"github.com/bnb-chain/greenfield/sdk/types".TxOption) as "github.com/bnb-chain/greenfield/sdk/types".TxOption value in assignment
}
cancelBucketMsg := storageTypes.NewMsgCancelMigrateBucket(
govModuleAddress.GetAddress(), bucketName,
)

err = cancelBucketMsg.ValidateBasic()
resp, err := c.chainClient.BroadcastTx(ctx, []sdk.Msg{cancelMigrateBucketMsg}, opts.TxOpts)

Check failure on line 998 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

cannot use opts.TxOpts (variable of type "github.com/bnb-chain/greenfield/sdk/types".TxOption) as *"github.com/bnb-chain/greenfield/sdk/types".TxOption value in argument to c.chainClient.BroadcastTx
if err != nil {
return 0, "", err
return "", err
}

return c.SubmitProposal(ctx, []sdk.Msg{cancelBucketMsg}, opts.ProposalDepositAmount, opts.ProposalTitle, opts.ProposalSummary, types.SubmitProposalOptions{Metadata: opts.ProposalMetadata, TxOpts: opts.TxOpts})
var txnResponse *ctypes.ResultTx
txnHash := resp.TxResponse.TxHash
if !opts.IsAsyncMode {
ctxTimeout, cancel := context.WithTimeout(ctx, types.ContextTimeout)
defer cancel()

txnResponse, err = c.WaitForTx(ctxTimeout, txnHash)
if err != nil {
return txnHash, fmt.Errorf("the transaction has been submitted, please check it later:%v", err)
}

if txnResponse.TxResult.Code != 0 {
return txnHash, fmt.Errorf("the createBucket txn has failed with response code: %d", txnResponse.TxResult.Code)
}
}

return txnHash, nil
}

// ListBucketsByPaymentAccount - List bucket info by payment account.
Expand Down Expand Up @@ -1063,3 +1084,55 @@ func (c *Client) ListBucketsByPaymentAccount(ctx context.Context, paymentAccount

return buckets, nil
}

// GetBucketMigrationProgress return the status of object including the uploading progress
func (c *Client) GetBucketMigrationProgress(ctx context.Context, bucketName string, destSP uint32) (types.MigrationProgress, error) {

Check failure on line 1089 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

undefined: types.MigrationProgress
_, err := c.HeadBucket(ctx, bucketName)
if err != nil {
return types.MigrationProgress{}, err

Check failure on line 1092 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

undefined: types.MigrationProgress
}

// get object status from sp
migrationProgress, err := c.getMigrationStateFromSP(ctx, bucketName, destSP)
if err != nil {
return types.MigrationProgress{}, errors.New("fail to fetch bucket migration progress from sp" + err.Error())

Check failure on line 1098 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

undefined: types.MigrationProgress
}
return migrationProgress, nil
}

func (c *Client) getMigrationStateFromSP(ctx context.Context, bucketName string, destSP uint32) (types.MigrationProgress, error) {

Check failure on line 1103 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

undefined: types.MigrationProgress
params := url.Values{}
params.Set("bucket-migration-progress", "")

reqMeta := requestMeta{
urlValues: params,
bucketName: bucketName,
contentSHA256: types.EmptyStringSHA256,
}

sendOpt := sendOptions{
method: http.MethodGet,
disableCloseBody: true,
}

endpoint, err := c.getSPUrlByID(destSP)
if err != nil {
return types.MigrationProgress{}, err

Check failure on line 1120 in client/api_bucket.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.20.x, ubuntu-20.04)

undefined: types.MigrationProgress
}

resp, err := c.sendReq(ctx, reqMeta, &sendOpt, endpoint)
if err != nil {
return types.MigrationProgress{}, err
}

defer utils.CloseResponse(resp)

migrationProgress := types.MigrationProgress{}
// decode the xml content from response body
err = xml.NewDecoder(resp.Body).Decode(&migrationProgress)
if err != nil {
return types.MigrationProgress{}, err
}

return migrationProgress, nil
}

0 comments on commit 5499a43

Please sign in to comment.