From 8e30d35a6c559e6e98cb19261e72b95e4ffe051b Mon Sep 17 00:00:00 2001 From: yutianwu Date: Mon, 11 Mar 2024 16:03:04 +0800 Subject: [PATCH] fix the test cases and rebase code --- e2e/tests/storage_rate_limit_test.go | 74 +++++++++++++++++---------- x/storage/keeper/bucket_rate_limit.go | 3 ++ 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/e2e/tests/storage_rate_limit_test.go b/e2e/tests/storage_rate_limit_test.go index 759a1177e..5dade0214 100644 --- a/e2e/tests/storage_rate_limit_test.go +++ b/e2e/tests/storage_rate_limit_test.go @@ -57,6 +57,15 @@ func (s *StorageTestSuite) TestSetBucketRateLimitToZero() { msgSetBucketRateLimit := storagetypes.NewMsgSetBucketFlowRateLimit(s.User.GetAddr(), s.User.GetAddr(), s.User.GetAddr(), bucketName, sdkmath.NewInt(0)) s.SendTxBlock(s.User, msgSetBucketRateLimit) + queryHeadBucketRequest = storagetypes.QueryHeadBucketRequest{ + BucketName: bucketName, + } + queryHeadBucketResponse, err = s.Client.HeadBucket(ctx, &queryHeadBucketRequest) + s.Require().NoError(err) + + s.Require().Equal(queryHeadBucketResponse.ExtraInfo.IsRateLimited, true) + s.Require().Equal(queryHeadBucketResponse.ExtraInfo.FlowRateLimit.String(), "0") + // CreateObject objectName := storageutils.GenRandomObjectName() // create test buffer @@ -122,10 +131,6 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Object() { s.Require().NoError(err) s.Require().Equal(queryHeadBucketResponse.BucketInfo.CreateAt, queryQuotaUpdateTimeResponse.UpdateAt) - fmt.Printf("User: %s\n", s.User.GetAddr().String()) - fmt.Printf("queryHeadBucketResponse.BucketInfo.Owner: %s\n", queryHeadBucketResponse.BucketInfo.Owner) - fmt.Printf("queryHeadBucketResponse.BucketInfo.PaymentAccount: %s\n", queryHeadBucketResponse.BucketInfo.PaymentAddress) - // SetBucketRateLimit msgSetBucketRateLimit := storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(100000000000000)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) @@ -155,17 +160,16 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Object() { objectName = storageutils.GenRandomObjectName() msgCreateObject = storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), storagetypes.VISIBILITY_TYPE_PRIVATE, expectChecksum, contextType, storagetypes.REDUNDANCY_EC_TYPE, math.MaxUint, nil) msgCreateObject.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateObject.GetApprovalBytes()) - s.Require().NoError(err) s.SendTxBlockWithExpectErrorString(msgCreateObject, user, "greater than the flow rate limit") // SetBucketRateLimit msgSetBucketRateLimit = storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(100000000000000)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) + // create object objectName = storageutils.GenRandomObjectName() msgCreateObject = storagetypes.NewMsgCreateObject(user.GetAddr(), bucketName, objectName, uint64(payloadSize), storagetypes.VISIBILITY_TYPE_PRIVATE, expectChecksum, contextType, storagetypes.REDUNDANCY_EC_TYPE, math.MaxUint, nil) msgCreateObject.PrimarySpApproval.Sig, err = sp.ApprovalKey.Sign(msgCreateObject.GetApprovalBytes()) - s.Require().NoError(err) s.SendTxBlock(user, msgCreateObject) } @@ -216,10 +220,6 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Bucket() { s.Require().NoError(err) s.Require().Equal(queryHeadBucketResponse.BucketInfo.CreateAt, queryQuotaUpdateTimeResponse.UpdateAt) - fmt.Printf("User: %s\n", s.User.GetAddr().String()) - fmt.Printf("queryHeadBucketResponse.BucketInfo.Owner: %s\n", queryHeadBucketResponse.BucketInfo.Owner) - fmt.Printf("queryHeadBucketResponse.BucketInfo.PaymentAccount: %s\n", queryHeadBucketResponse.BucketInfo.PaymentAddress) - // SetBucketRateLimit msgSetBucketRateLimit := storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(100000000000)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) @@ -228,32 +228,43 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Bucket() { var readQuota uint64 = 100 msgUpdateBucketInfo := storagetypes.NewMsgUpdateBucketInfo( user.GetAddr(), bucketName, &readQuota, nil, storagetypes.VISIBILITY_TYPE_PUBLIC_READ) - s.Require().NoError(err) s.SendTxBlock(user, msgUpdateBucketInfo) - s.Require().NoError(err) // SetBucketRateLimit msgSetBucketRateLimit = storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(0)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) - // CreateObject + queryHeadBucketRequest = storagetypes.QueryHeadBucketRequest{ + BucketName: bucketName, + } + queryHeadBucketResponse, err = s.Client.HeadBucket(ctx, &queryHeadBucketRequest) + s.Require().NoError(err) + + s.Require().Equal(queryHeadBucketResponse.ExtraInfo.IsRateLimited, true) + + // update bucket readQuota = 101 msgUpdateBucketInfo = storagetypes.NewMsgUpdateBucketInfo( user.GetAddr(), bucketName, &readQuota, nil, storagetypes.VISIBILITY_TYPE_PUBLIC_READ) - s.Require().NoError(err) - s.SendTxBlockWithExpectErrorString(msgUpdateBucketInfo, user, "greater than the flow rate limit") - s.Require().NoError(err) + s.SendTxBlockWithExpectErrorString(msgUpdateBucketInfo, user, "payment account is not changed but the bucket is limited") // SetBucketRateLimit msgSetBucketRateLimit = storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(100000000000)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) + queryHeadBucketRequest = storagetypes.QueryHeadBucketRequest{ + BucketName: bucketName, + } + queryHeadBucketResponse, err = s.Client.HeadBucket(ctx, &queryHeadBucketRequest) + s.Require().NoError(err) + + s.Require().Equal(queryHeadBucketResponse.ExtraInfo.IsRateLimited, false) + + // update bucket readQuota = 102 msgUpdateBucketInfo = storagetypes.NewMsgUpdateBucketInfo( user.GetAddr(), bucketName, &readQuota, nil, storagetypes.VISIBILITY_TYPE_PUBLIC_READ) - s.Require().NoError(err) s.SendTxBlock(user, msgUpdateBucketInfo) - s.Require().NoError(err) } // TestNotOwnerSetBucketRateLimit_BucketPaymentAccount @@ -303,10 +314,6 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_BucketPaymentAccount() s.Require().NoError(err) s.Require().Equal(queryHeadBucketResponse.BucketInfo.CreateAt, queryQuotaUpdateTimeResponse.UpdateAt) - fmt.Printf("User: %s\n", s.User.GetAddr().String()) - fmt.Printf("queryHeadBucketResponse.BucketInfo.Owner: %s\n", queryHeadBucketResponse.BucketInfo.Owner) - fmt.Printf("queryHeadBucketResponse.BucketInfo.PaymentAccount: %s\n", queryHeadBucketResponse.BucketInfo.PaymentAddress) - // SetBucketRateLimit msgSetBucketRateLimit := storagetypes.NewMsgSetBucketFlowRateLimit(s.User.GetAddr(), s.User.GetAddr(), s.User.GetAddr(), bucketName, sdkmath.NewInt(0)) s.SendTxBlock(s.User, msgSetBucketRateLimit) @@ -314,31 +321,41 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_BucketPaymentAccount() // SetBucketRateLimit msgUpdateBucketInfo := storagetypes.NewMsgUpdateBucketInfo( user.GetAddr(), bucketName, nil, paymentAcc.GetAddr(), storagetypes.VISIBILITY_TYPE_PUBLIC_READ) - s.Require().NoError(err) s.SendTxBlockWithExpectErrorString(msgUpdateBucketInfo, user, "the flow rate limit is not set") - s.Require().NoError(err) // SetBucketRateLimit msgSetBucketRateLimit = storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(0)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) + queryHeadBucketRequest = storagetypes.QueryHeadBucketRequest{ + BucketName: bucketName, + } + queryHeadBucketResponse, err = s.Client.HeadBucket(ctx, &queryHeadBucketRequest) + s.Require().NoError(err) + + s.Require().Equal(queryHeadBucketResponse.ExtraInfo.IsRateLimited, true) + // UpdateBucketInfo msgUpdateBucketInfo = storagetypes.NewMsgUpdateBucketInfo( user.GetAddr(), bucketName, nil, paymentAcc.GetAddr(), storagetypes.VISIBILITY_TYPE_PUBLIC_READ) - s.Require().NoError(err) s.SendTxBlockWithExpectErrorString(msgUpdateBucketInfo, user, "greater than the flow rate limit") - s.Require().NoError(err) // SetBucketRateLimit msgSetBucketRateLimit = storagetypes.NewMsgSetBucketFlowRateLimit(paymentAcc.GetAddr(), s.User.GetAddr(), paymentAcc.GetAddr(), bucketName, sdkmath.NewInt(100000000000)) s.SendTxBlock(paymentAcc, msgSetBucketRateLimit) + queryHeadBucketRequest = storagetypes.QueryHeadBucketRequest{ + BucketName: bucketName, + } + queryHeadBucketResponse, err = s.Client.HeadBucket(ctx, &queryHeadBucketRequest) + s.Require().NoError(err) + + s.Require().Equal(queryHeadBucketResponse.ExtraInfo.IsRateLimited, false) + // UpdateBucketInfo msgUpdateBucketInfo = storagetypes.NewMsgUpdateBucketInfo( user.GetAddr(), bucketName, nil, paymentAcc.GetAddr(), storagetypes.VISIBILITY_TYPE_PUBLIC_READ) - s.Require().NoError(err) s.SendTxBlock(user, msgUpdateBucketInfo) - s.Require().NoError(err) } func (s *StorageTestSuite) TestQueryBucketRateLimit() { @@ -377,6 +394,7 @@ func (s *StorageTestSuite) TestQueryBucketRateLimit() { queryBucketRateLimitRequest := storagetypes.QueryPaymentAccountBucketFlowRateLimitRequest{ PaymentAccount: paymentAcc.GetAddr().String(), BucketName: bucketName, + BucketOwner: user.GetAddr().String(), } queryBucketRateLimitResponse, err := s.Client.QueryPaymentAccountBucketFlowRateLimit(ctx, &queryBucketRateLimitRequest) s.Require().NoError(err) diff --git a/x/storage/keeper/bucket_rate_limit.go b/x/storage/keeper/bucket_rate_limit.go index cb5f19995..8aef69b25 100644 --- a/x/storage/keeper/bucket_rate_limit.go +++ b/x/storage/keeper/bucket_rate_limit.go @@ -281,6 +281,9 @@ func (k Keeper) GetBucketExtraInfo(ctx sdk.Context, bucketInfo *types.BucketInfo extraInfo.FlowRateLimit = rateLimit.FlowRateLimit } + isRateLimited := k.IsBucketRateLimited(ctx, bucketInfo.BucketName) + extraInfo.IsRateLimited = isRateLimited + internalBucketInfo := k.MustGetInternalBucketInfo(ctx, bucketInfo.Id) bill, err := k.GetBucketReadStoreBill(ctx, bucketInfo, internalBucketInfo)