Skip to content

Commit

Permalink
Merge pull request #218 from bnb-chain/feat-migration-progress
Browse files Browse the repository at this point in the history
feat: add migration progress
  • Loading branch information
jingjunLi authored Jan 8, 2024
2 parents 5be80fe + 2727b16 commit da8108e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:

env:
GreenfieldTag: master
GreenfieldStorageProviderTag: fix-adapt-new-tag
GreenfieldStorageProviderTag: develop
GOPRIVATE: github.com/bnb-chain
GH_ACCESS_TOKEN: ${{ secrets.GH_TOKEN }}
MYSQL_USER: root
Expand Down
113 changes: 98 additions & 15 deletions client/api_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"strings"
"time"

ctypes "github.com/cometbft/cometbft/rpc/core/types"
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"
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
}
cancelBucketMsg := storageTypes.NewMsgCancelMigrateBucket(
govModuleAddress.GetAddress(), bucketName,
)

err = cancelBucketMsg.ValidateBasic()
// set the default txn broadcast mode as sync mode
if opts.TxOpts == nil {
broadcastMode := tx.BroadcastMode_BROADCAST_MODE_SYNC
opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode}
}

resp, err := c.BroadcastTx(ctx, []sdk.Msg{cancelMigrateBucketMsg}, opts.TxOpts)
if err != nil {
return 0, "", err
return "", err
}

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 c.SubmitProposal(ctx, []sdk.Msg{cancelBucketMsg}, opts.ProposalDepositAmount, opts.ProposalTitle, opts.ProposalSummary, types.SubmitProposalOptions{Metadata: opts.ProposalMetadata, TxOpts: opts.TxOpts})
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

0 comments on commit da8108e

Please sign in to comment.