Skip to content

Commit

Permalink
add payment for bucket migration
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing committed Jun 19, 2023
1 parent bb4e6b5 commit 480346a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
13 changes: 13 additions & 0 deletions x/storage/keeper/msg_server_complete_migrate_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ func (k msgServer) CompleteMigrateBucket(goCtx context.Context, msg *types.MsgCo
return nil, virtualgroupmoduletypes.ErrGVGFamilyNotExist
}

oldBucketInfo := &types.BucketInfo{
PaymentAddress: bucketInfo.PaymentAddress,
PrimarySpId: bucketInfo.PrimarySpId,
GlobalVirtualGroupFamilyId: bucketInfo.GlobalVirtualGroupFamilyId,
ChargedReadQuota: bucketInfo.ChargedReadQuota,
BillingInfo: bucketInfo.BillingInfo,
}

bucketInfo.PrimarySpId = migrationBucketInfo.DstSpId
bucketInfo.GlobalVirtualGroupFamilyId = msg.GlobalVirtualGroupFamilyId
k.SetBucketInfo(ctx, bucketInfo)
k.DeleteMigrationBucketInfo(ctx, bucketInfo.Id)

err := k.ChargeBucketMigration(ctx, oldBucketInfo, bucketInfo)
if err != nil {
return nil, types.ErrMigtationBucketFailed.Wrapf("update payment info failed.")
}

return &types.MsgCompleteMigrateBucketResponse{}, nil
}
40 changes: 39 additions & 1 deletion x/storage/keeper/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func (k Keeper) ChargeViaBucketChange(ctx sdk.Context, bucketInfo *storagetypes.
return nil
}

func (k Keeper) GetBucketBill(ctx sdk.Context, bucketInfo *storagetypes.BucketInfo) (userFlows types.UserFlows, err error) {
func (k Keeper) GetBucketBill(ctx sdk.Context, bucketInfo *storagetypes.BucketInfo, settle ...bool) (userFlows types.UserFlows, err error) {
doSettle := settle != nil && settle[0]
userFlows.From = sdk.MustAccAddressFromHex(bucketInfo.PaymentAddress)
if bucketInfo.BillingInfo.TotalChargeSize == 0 && bucketInfo.ChargedReadQuota == 0 {
return userFlows, nil
Expand All @@ -197,6 +198,13 @@ func (k Keeper) GetBucketBill(ctx sdk.Context, bucketInfo *storagetypes.BucketIn
return userFlows, fmt.Errorf("get GVG family failed: %d", bucketInfo.GlobalVirtualGroupFamilyId)
}

if doSettle {
err := k.virtualGroupKeeper.SettleAndDistributeGVGFamily(ctx, primarySp.Id, gvgFamily)
if err != nil {
return userFlows, fmt.Errorf("settle GVG family failed: %d, err: %s", gvgFamily.Id, err.Error())
}
}

// primary sp total rate
primaryTotalFlowRate := price.ReadPrice.MulInt(sdkmath.NewIntFromUint64(bucketInfo.ChargedReadQuota)).TruncateInt()

Expand All @@ -220,6 +228,13 @@ func (k Keeper) GetBucketBill(ctx sdk.Context, bucketInfo *storagetypes.BucketIn
return userFlows, fmt.Errorf("get GVG failed: %d", lvg.GlobalVirtualGroupId)
}

if doSettle {
err := k.virtualGroupKeeper.SettleAndDistributeGVG(ctx, gvg)
if err != nil {
return userFlows, fmt.Errorf("settle GVG failed: %d, err: %s", gvg.Id, err.Error())
}
}

secondaryRate := price.SecondaryStorePrice.MulInt(sdkmath.NewIntFromUint64(lvgStoreSize.TotalChargeSize)).TruncateInt()
secondaryRate = secondaryRate.MulRaw(int64(len(gvg.SecondarySpIds)))
if secondaryRate.IsPositive() {
Expand Down Expand Up @@ -357,3 +372,26 @@ func (k Keeper) GetChargeSize(ctx sdk.Context, payloadSize uint64, ts int64) (si
return payloadSize, nil
}
}

func (k Keeper) ChargeBucketMigration(ctx sdk.Context, oldBucketInfo, newBucketInfo *storagetypes.BucketInfo) error {
// settle and get previous bill
prevBill, err := k.GetBucketBill(ctx, oldBucketInfo, true)
if err != nil {
return fmt.Errorf("settle and get bucket bill failed, bucket: %s, err: %s", oldBucketInfo.BucketName, err.Error())
}

newBucketInfo.BillingInfo.PriceTime = ctx.BlockTime().Unix()
// calculate new bill
newBill, err := k.GetBucketBill(ctx, newBucketInfo)
if err != nil {
return fmt.Errorf("get new bucket bill failed: %w", err)
}

// charge according to bill change
err = k.ChargeAccordingToBillChange(ctx, prevBill, newBill)
if err != nil {
ctx.Logger().Error("charge via bucket change failed", "err", err.Error())
return err
}
return nil
}

0 comments on commit 480346a

Please sign in to comment.