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

fix: fix uncharge issue when force-deleting objects #601

Merged
merged 8 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (app *App) registerSerengetiUpgradeHandler() {
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas(sdk.MsgTypeURL(&storagemoduletypes.MsgDelegateCreateObject{}), 1.2e3))
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas(sdk.MsgTypeURL(&storagemoduletypes.MsgDelegateUpdateObjectContent{}), 1.2e3))
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas(sdk.MsgTypeURL(&storagemoduletypes.MsgSealObjectV2{}), 1.2e2))
app.GashubKeeper.SetMsgGasParams(ctx, *gashubtypes.NewMsgGasParamsWithFixedGas(sdk.MsgTypeURL(&storagemoduletypes.MsgSetBucketFlowRateLimit{}), 1.2e3))
return app.mm.RunMigrations(ctx, app.configurator, fromVM)
})

Expand Down
111 changes: 111 additions & 0 deletions e2e/tests/storage_rate_limit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,85 @@ import (
"context"
"fmt"
"math"
"strconv"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
gashubtypes "github.com/cosmos/cosmos-sdk/x/gashub/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

types2 "github.com/bnb-chain/greenfield/sdk/types"

storageutils "github.com/bnb-chain/greenfield/testutil/storage"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)

func (s *StorageTestSuite) enableMessage() {
msgSetBucketFlowRateLimit := sdk.MsgTypeURL(&storagetypes.MsgSetBucketFlowRateLimit{})
msgMigrateBuketGasParams := gashubtypes.NewMsgGasParamsWithFixedGas(msgSetBucketFlowRateLimit, 1.2e3)

msgUpdateGasParams := gashubtypes.NewMsgSetMsgGasParams(authtypes.NewModuleAddress(govtypes.ModuleName).String(), []*gashubtypes.MsgGasParams{msgMigrateBuketGasParams}, nil)

var err error
validator := s.Validator.GetAddr()

ctx := context.Background()

msgProposal, err := govtypesv1.NewMsgSubmitProposal(
[]sdk.Msg{msgUpdateGasParams},
sdk.Coins{sdk.NewCoin(s.BaseSuite.Config.Denom, types2.NewIntFromInt64WithDecimal(100, types2.DecimalBNB))},
validator.String(),
"test", "test", "test",
)
s.Require().NoError(err)

txRes := s.SendTxBlock(s.Validator, msgProposal)
s.Require().Equal(txRes.Code, uint32(0))

// 3. query proposal and get proposal ID
var proposalId uint64
for _, event := range txRes.Logs[0].Events {
if event.Type == "submit_proposal" {
for _, attr := range event.Attributes {
if attr.Key == "proposal_id" {
proposalId, err = strconv.ParseUint(attr.Value, 10, 0)
s.Require().NoError(err)
break
}
}
break
}
}
s.Require().True(proposalId != 0)

queryProposal := &govtypesv1.QueryProposalRequest{ProposalId: proposalId}
_, err = s.Client.GovQueryClientV1.Proposal(ctx, queryProposal)
s.Require().NoError(err)

// 4. submit MsgVote and wait the proposal exec
msgVote := govtypesv1.NewMsgVote(validator, proposalId, govtypesv1.OptionYes, "test")
txRes = s.SendTxBlock(s.Validator, msgVote)
s.Require().Equal(txRes.Code, uint32(0))

queryVoteParamsReq := govtypesv1.QueryParamsRequest{ParamsType: "voting"}
queryVoteParamsResp, err := s.Client.GovQueryClientV1.Params(ctx, &queryVoteParamsReq)
s.Require().NoError(err)

// 5. wait a voting period and confirm that the proposal success.
s.T().Logf("voting period %s", *queryVoteParamsResp.Params.VotingPeriod)
time.Sleep(*queryVoteParamsResp.Params.VotingPeriod)
time.Sleep(1 * time.Second)
proposalRes, err := s.Client.GovQueryClientV1.Proposal(ctx, queryProposal)
s.Require().NoError(err)
s.Require().Equal(proposalRes.Proposal.Status, govtypesv1.ProposalStatus_PROPOSAL_STATUS_PASSED)
}

func (s *StorageTestSuite) TestSetBucketRateLimitToZero() {
s.enableMessage()

var err error
sp := s.BaseSuite.PickStorageProvider()
gvg, found := sp.GetFirstGlobalVirtualGroup()
Expand Down Expand Up @@ -93,6 +163,8 @@ func (s *StorageTestSuite) TestSetBucketRateLimitToZero() {
// 6. the payment account set the rate limit to a positive number
// 7. user create an object in the bucket and it should pass
func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Object() {
s.enableMessage()

var err error
sp := s.BaseSuite.PickStorageProvider()
gvg, found := sp.GetFirstGlobalVirtualGroup()
Expand Down Expand Up @@ -185,6 +257,8 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Object() {
// 6. the payment account set the rate limit to a positive number
// 7. user update the read quota to a positive number and it should pass
func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Bucket() {
s.enableMessage()

var err error
sp := s.BaseSuite.PickStorageProvider()
gvg, found := sp.GetFirstGlobalVirtualGroup()
Expand Down Expand Up @@ -279,6 +353,8 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_Bucket() {
// 6. the payment account set the rate limit to a positive number
// 7. user update the payment account to another payment account, it should pass
func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_BucketPaymentAccount() {
s.enableMessage()

var err error
sp := s.BaseSuite.PickStorageProvider()
gvg, found := sp.GetFirstGlobalVirtualGroup()
Expand Down Expand Up @@ -362,6 +438,8 @@ func (s *StorageTestSuite) TestNotOwnerSetBucketRateLimit_BucketPaymentAccount()
}

func (s *StorageTestSuite) TestQueryBucketRateLimit() {
s.enableMessage()

var err error
sp := s.BaseSuite.PickStorageProvider()
gvg, found := sp.GetFirstGlobalVirtualGroup()
Expand Down Expand Up @@ -404,3 +482,36 @@ func (s *StorageTestSuite) TestQueryBucketRateLimit() {
s.Require().Equal(queryBucketRateLimitResponse.IsSet, true)
s.Require().Equal(queryBucketRateLimitResponse.FlowRateLimit, sdkmath.NewInt(100000000000000))
}

func (s *StorageTestSuite) TestSetBucketFlowRateLimit_Discontinue() {
sp, user, bucketName, _, _, _ := s.createObject()
yutianwu marked this conversation as resolved.
Show resolved Hide resolved

// SetBucketRateLimit
msgSetBucketRateLimit := storagetypes.NewMsgSetBucketFlowRateLimit(user.GetAddr(), user.GetAddr(), user.GetAddr(), bucketName, sdkmath.NewInt(0))
s.SendTxBlock(user, msgSetBucketRateLimit)

queryHeadBucketRequest := storagetypes.QueryHeadBucketRequest{
BucketName: bucketName,
}
queryHeadBucketResponse, err := s.Client.HeadBucket(context.Background(), &queryHeadBucketRequest)
s.Require().NoError(err)

s.Require().Equal(queryHeadBucketResponse.ExtraInfo.IsRateLimited, true)

msgDiscontinueBucket := storagetypes.NewMsgDiscontinueBucket(sp.GcKey.GetAddr(), bucketName, "test")
txRes1 := s.SendTxBlock(sp.GcKey, msgDiscontinueBucket)
deleteAt1 := filterDiscontinueBucketEventFromTx(txRes1).DeleteAt

for {
time.Sleep(200 * time.Millisecond)
statusRes, err := s.TmClient.TmClient.Status(context.Background())
s.Require().NoError(err)
blockTime := statusRes.SyncInfo.LatestBlockTime.Unix()

s.T().Logf("current blockTime: %d, delete blockTime: %d", blockTime, deleteAt1)

if blockTime >= deleteAt1 {
break
}
}
}
42 changes: 32 additions & 10 deletions x/storage/keeper/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,23 +435,45 @@ func (k Keeper) ChargeViaObjectChange(ctx sdk.Context, bucketInfo *storagetypes.
userFlows.Flows = append(userFlows.Flows, newOutFlows...)

if ctx.IsUpgraded(upgradetypes.Serengeti) {
currentBill, err := k.GetBucketReadStoreBill(ctx, bucketInfo, internalBucketInfo)
if err != nil {
return nil, fmt.Errorf("get bucket bill failed: %s %w", bucketInfo.BucketName, err)
var shouldApplyFlowRate = true
forced, _ := ctx.Value(types.ForceUpdateStreamRecordKey).(bool)
if forced {
isRateLimited := k.IsBucketRateLimited(ctx, bucketInfo.BucketName)
if isRateLimited {
// if the bucket is rate limited, no need to apply the flow rate since
// the bucket is already uncharged
shouldApplyFlowRate = false
}
} else {
// we should only check the flow rate limit when is not forced
currentBill, err := k.GetBucketReadStoreBill(ctx, bucketInfo, internalBucketInfo)
if err != nil {
return nil, fmt.Errorf("get bucket bill failed: %s %w", bucketInfo.BucketName, err)
}

err = k.isBucketFlowRateUnderLimit(ctx, sdk.MustAccAddressFromHex(bucketInfo.PaymentAddress), sdk.MustAccAddressFromHex(bucketInfo.Owner), bucketInfo.BucketName, currentBill)
if err != nil {
return nil, err
}
}

err = k.isBucketFlowRateUnderLimit(ctx, sdk.MustAccAddressFromHex(bucketInfo.PaymentAddress), sdk.MustAccAddressFromHex(bucketInfo.Owner), bucketInfo.BucketName, currentBill)
if shouldApplyFlowRate {
err = k.paymentKeeper.ApplyUserFlowsList(ctx, []types.UserFlows{userFlows})
if err != nil {
ctx.Logger().Error("charge object store fee failed", "bucket", bucketInfo.BucketName,
"object", objectInfo.ObjectName, "err", err.Error())
return nil, err
}
}
} else {
forcodedancing marked this conversation as resolved.
Show resolved Hide resolved
err = k.paymentKeeper.ApplyUserFlowsList(ctx, []types.UserFlows{userFlows})
if err != nil {
ctx.Logger().Error("charge object store fee failed", "bucket", bucketInfo.BucketName,
"object", objectInfo.ObjectName, "err", err.Error())
return nil, err
}
}

err = k.paymentKeeper.ApplyUserFlowsList(ctx, []types.UserFlows{userFlows})
if err != nil {
ctx.Logger().Error("charge object store fee failed", "bucket", bucketInfo.BucketName,
"object", objectInfo.ObjectName, "err", err.Error())
return nil, err
}
// merge outflows for early deletion usage
return k.paymentKeeper.MergeOutFlows(userFlows.Flows), nil
}
Expand Down
Loading