Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add migration progress #218

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 98 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"
jingjunLi marked this conversation as resolved.
Show resolved Hide resolved

"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)
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
jingjunLi marked this conversation as resolved.
Show resolved Hide resolved
if opts.TxOpts == nil {
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}
cancelBucketMsg := storageTypes.NewMsgCancelMigrateBucket(
govModuleAddress.GetAddress(), bucketName,
)

err = cancelBucketMsg.ValidateBasic()
resp, err := c.chainClient.BroadcastTx(ctx, []sdk.Msg{cancelMigrateBucketMsg}, opts.TxOpts)
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
jingjunLi marked this conversation as resolved.
Show resolved Hide resolved
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,65 @@ func (c *Client) ListBucketsByPaymentAccount(ctx context.Context, paymentAccount

return buckets, nil
}

// GetBucketMigrationProgress - Query the migration progress info of the specific bucket.
//
// - ctx: Context variables for the current API call.
//
// - bucketName: The bucket name identifies the bucket.
//
// - ret1: The info of migration progress which contains the progress info of the bucket
//
// - ret2: Return error when the request failed, otherwise return 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) {
_, err := c.HeadBucket(ctx, bucketName)
if err != nil {
return types.MigrationProgress{}, err
}

// 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())
}
return migrationProgress, nil
}

func (c *Client) getMigrationStateFromSP(ctx context.Context, bucketName string, destSP uint32) (types.MigrationProgress, error) {
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
}

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
}
10 changes: 10 additions & 0 deletions types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ type UploadOffset struct {
Offset uint64 `xml:"Offset"` // Offset defines the offset info of resumable uploading object
}

// MigrationProgress indicates the progress info of bucket migration
type MigrationProgress struct {
XMLName xml.Name `xml:"QueryMigrationProgress"`
Version string `xml:"version,attr"` // Version defines version info
ProgressDescription string `xml:"ProgressDescription"` // ProgressDescription defines a string message representing the upload progress.
ErrorDescription string `xml:"ErrorDescription"` // ErrorDescription defines a string message representing an upload error exception.
MigratedBytes uint64 `xml:"MigratedBytes"`
MigrationState uint64 `xml:"MigrationState"`
}

// ChallengeV2Result indicates the response info of challenge v2 API
type ChallengeV2Result struct {
XMLName xml.Name `xml:"GetChallengeInfo"`
Expand Down
8 changes: 2 additions & 6 deletions types/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ type MigrateBucketOptions struct {

// CancelMigrateBucketOptions indicates the metadata to construct `CancelMigrateBucket` msg of storage module.
type CancelMigrateBucketOptions struct {
ProposalDepositAmount math.Int // ProposalDepositAmount defines the amount in wei BNB.
ProposalTitle string // ProposalTitle defines the title for proposal.
ProposalSummary string // ProposalSummary defines the summary for proposal.
ProposalMetadata string // ProposalMetadata defines the metadata for proposal.
TxOpts gnfdsdktypes.TxOption // TxOpts defines the options to customize a transaction.
IsAsyncMode bool // IsAsyncMode indicates whether to create the bucket in asynchronous mode.
TxOpts *gnfdsdktypes.TxOption
IsAsyncMode bool // indicate whether to create the bucket in asynchronous mode
}

// VoteProposalOptions indicates the metadata to construct `VoteProposal` msg.
Expand Down
Loading