From 5499a430157f741d13855106edc3e81ec81c6307 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 25 Dec 2023 14:12:35 +0800 Subject: [PATCH 1/6] add migration progress --- client/api_bucket.go | 103 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 15 deletions(-) diff --git a/client/api_bucket.go b/client/api_bucket.go index 39e04ec2..0a27700d 100644 --- a/client/api_bucket.go +++ b/client/api_bucket.go @@ -16,7 +16,6 @@ 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" @@ -24,6 +23,7 @@ import ( "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" @@ -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) } @@ -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 { + 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 + 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. @@ -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) { + _, 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 +} From bb8472b08dbef54f1936992322b7d66e3ad4d3c0 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 25 Dec 2023 14:16:11 +0800 Subject: [PATCH 2/6] add migration progress --- client/api_bucket.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/client/api_bucket.go b/client/api_bucket.go index 0a27700d..e423f7eb 100644 --- a/client/api_bucket.go +++ b/client/api_bucket.go @@ -1085,6 +1085,16 @@ 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) From 47f9d6625d7e58601229a4756056f2ed7f0fafae Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 25 Dec 2023 14:20:50 +0800 Subject: [PATCH 3/6] fix cancel migrate bucket --- types/list.go | 10 ++++++++++ types/option.go | 8 ++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/types/list.go b/types/list.go index 76d97614..3e78571c 100644 --- a/types/list.go +++ b/types/list.go @@ -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"` diff --git a/types/option.go b/types/option.go index e498f80d..a959fe84 100644 --- a/types/option.go +++ b/types/option.go @@ -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. From ea3f9a3cbfdfd10d10c2e71741b1c9a16ecb3a33 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Wed, 27 Dec 2023 18:10:29 +0800 Subject: [PATCH 4/6] use Client's BroadcastTx function --- client/api_bucket.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/api_bucket.go b/client/api_bucket.go index e423f7eb..8dcf0308 100644 --- a/client/api_bucket.go +++ b/client/api_bucket.go @@ -995,7 +995,7 @@ func (c *Client) CancelMigrateBucket(ctx context.Context, bucketName string, opt opts.TxOpts = &gnfdsdk.TxOption{Mode: &broadcastMode} } - resp, err := c.chainClient.BroadcastTx(ctx, []sdk.Msg{cancelMigrateBucketMsg}, opts.TxOpts) + resp, err := c.BroadcastTx(ctx, []sdk.Msg{cancelMigrateBucketMsg}, opts.TxOpts) if err != nil { return "", err } From abac1a5aeebe4d75ccc9d50f36afffa5bf5322d9 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 8 Jan 2024 14:26:29 +0800 Subject: [PATCH 5/6] format code --- client/api_bucket.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/api_bucket.go b/client/api_bucket.go index 8dcf0308..b39e2b87 100644 --- a/client/api_bucket.go +++ b/client/api_bucket.go @@ -14,6 +14,7 @@ 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" "github.com/rs/zerolog/log" @@ -23,7 +24,6 @@ import ( "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" @@ -989,7 +989,7 @@ func (c *Client) CancelMigrateBucket(ctx context.Context, bucketName string, opt return "", err } - // set the default txn broadcast mode as block mode + // 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} From 2727b167c43ba6ddfc7f3ceb1e08df61120e7644 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 8 Jan 2024 14:43:59 +0800 Subject: [PATCH 6/6] fix e2e test branch to develop --- .github/workflows/e2e.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 3c41e3d2..b8b73d5a 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -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