Skip to content

Commit

Permalink
feat: support delete bucket with non-zero charged read quota (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-reorg authored Mar 24, 2023
1 parent d7237bf commit e4879c0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
28 changes: 28 additions & 0 deletions e2e/tests/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,34 @@ func (s *StorageTestSuite) TestPayment_Smoke() {
// delete bucket
}

func (s *StorageTestSuite) TestPayment_DeleteBucketWithReadQuota() {
var err error
sp := s.StorageProviders[0]
user := s.User
// CreateBucket
chargedReadQuota := uint64(100)
bucketName := storageutils.GenRandomBucketName()
msgCreateBucket := storagetypes.NewMsgCreateBucket(
user.GetAddr(), bucketName, storagetypes.VISIBILITY_TYPE_PUBLIC_READ, sp.OperatorKey.GetAddr(),
nil, math.MaxUint, nil, chargedReadQuota)
msgCreateBucket.PrimarySpApproval.Sig, err = sp.ApprovalKey.GetPrivKey().Sign(msgCreateBucket.GetApprovalBytes())
s.Require().NoError(err)
s.SendTxBlock(msgCreateBucket, user)

streamRecordsBeforeDelete := s.GetStreamRecords()
s.T().Logf("streamRecordsBeforeDelete: %s", core.YamlString(streamRecordsBeforeDelete))
s.Require().NotEqual(streamRecordsBeforeDelete.User.NetflowRate.String(), "0")

// DeleteBucket
msgDeleteBucket := storagetypes.NewMsgDeleteBucket(user.GetAddr(), bucketName)
s.SendTxBlock(msgDeleteBucket, user)

// check the billing change
streamRecordsAfterDelete := s.GetStreamRecords()
s.T().Logf("streamRecordsBeforeDelete: %s", core.YamlString(streamRecordsAfterDelete))
s.Require().Equal(streamRecordsAfterDelete.User.NetflowRate.String(), "0")
}

func (s *StorageTestSuite) TestPayment_AutoSettle() {
ctx := context.Background()
sp := s.StorageProviders[0]
Expand Down
9 changes: 3 additions & 6 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,10 @@ func (k Keeper) DeleteBucket(ctx sdk.Context, operator sdk.AccAddress, bucketNam
return types.ErrBucketNotEmpty
}

// check bill is empty
bill, err := k.GetBucketBill(ctx, bucketInfo)
// change the bill
err := k.ChargeDeleteBucket(ctx, bucketInfo)
if err != nil {
return errors.Wrapf(err, "Get bucket bill failed.")
}
if len(bill.Flows) != 0 {
return types.ErrBucketBillNotEmpty
return types.ErrCharge.Wrapf("ChargeDeleteBucket error: %s", err)
}

store.Delete(bucketKey)
Expand Down
17 changes: 17 additions & 0 deletions x/storage/keeper/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ func (k Keeper) ChargeInitialReadFee(ctx sdk.Context, bucketInfo *storagetypes.B
return k.paymentKeeper.ApplyUserFlowsList(ctx, []types.UserFlows{bill})
}

func (k Keeper) ChargeDeleteBucket(ctx sdk.Context, bucketInfo *storagetypes.BucketInfo) error {
bill, err := k.GetBucketBill(ctx, bucketInfo)
if err != nil {
return err
}
if len(bill.Flows) == 0 {
return nil
}
// should only remain at most 2 flows: charged_read_quota fee and tax
if len(bill.Flows) > 2 {
return fmt.Errorf("unexpected left flow number: %d", len(bill.Flows))
}
bill.Flows = GetNegFlows(bill.Flows)
err = k.paymentKeeper.ApplyUserFlowsList(ctx, []types.UserFlows{bill})
return err
}

func (k Keeper) UpdateBucketInfoAndCharge(ctx sdk.Context, bucketInfo *storagetypes.BucketInfo, newPaymentAddr string, newReadQuota uint64) error {
if bucketInfo.PaymentAddress != newPaymentAddr && bucketInfo.ChargedReadQuota != newReadQuota {
return fmt.Errorf("payment address and read quota can not be changed at the same time")
Expand Down
2 changes: 1 addition & 1 deletion x/storage/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (
ErrSourceTypeMismatch = errors.Register(ModuleName, 1114, "Object source type mismatch")
ErrTooLargeObject = errors.Register(ModuleName, 1115, "Object payload size is too large")
ErrInvalidApproval = errors.Register(ModuleName, 1116, "Invalid approval of sp")
ErrBucketBillNotEmpty = errors.Register(ModuleName, 1117, "bucket bill is not empty")
ErrCharge = errors.Register(ModuleName, 1117, "charge error")

ErrNoSuchPolicy = errors.Register(ModuleName, 1120, "No such Policy")
ErrInvalidParameter = errors.Register(ModuleName, 1121, "Invalid parameter")
Expand Down

0 comments on commit e4879c0

Please sign in to comment.