Skip to content

Commit

Permalink
fix: fix the SettleTimestamp calculation (#422)
Browse files Browse the repository at this point in the history
* totalRate is negative

* handle cases previous settlementTime exist or not

* fix test
  • Loading branch information
alexgao001 authored Aug 16, 2023
1 parent facbe3d commit 79ce538
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
11 changes: 4 additions & 7 deletions e2e/tests/payment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (s *PaymentTestSuite) TestVersionedParams_SealObjectAfterReserveTimeChange(
s.Require().True(found)

// create bucket, create object
user, bucketName, objectName, objectId, checksums := s.createBucketAndObject(sp)
user, bucketName, objectName, objectId, checksums := s.createBucketAndObject(sp, gvg)

// update params
params := s.queryParams()
Expand Down Expand Up @@ -180,7 +180,7 @@ func (s *PaymentTestSuite) TestVersionedParams_DeleteBucketAfterValidatorTaxRate
s.T().Logf("netflow, validatorTaxPoolRate: %s", validatorTaxPoolRate)

// create bucket, create object
user, bucketName, objectName, objectId, checksums := s.createBucketAndObject(sp)
user, bucketName, objectName, objectId, checksums := s.createBucketAndObject(sp, gvg)

// seal object
s.sealObject(sp, gvg, bucketName, objectName, objectId, checksums)
Expand Down Expand Up @@ -221,7 +221,7 @@ func (s *PaymentTestSuite) TestVersionedParams_DeleteObjectAfterReserveTimeChang
s.Require().True(found)

// create bucket, create object
user, bucketName, objectName, objectId, checksums := s.createBucketAndObject(sp)
user, bucketName, objectName, objectId, checksums := s.createBucketAndObject(sp, gvg)

// seal object
s.sealObject(sp, gvg, bucketName, objectName, objectId, checksums)
Expand Down Expand Up @@ -2279,11 +2279,8 @@ func (s *PaymentTestSuite) updateParams(params paymenttypes.Params) {
s.T().Log("params after", core.YamlString(queryParamsResponse.Params))
}

func (s *PaymentTestSuite) createBucketAndObject(sp *core.StorageProvider) (keys.KeyManager, string, string, storagetypes.Uint, [][]byte) {
func (s *PaymentTestSuite) createBucketAndObject(sp *core.StorageProvider, gvg *virtualgrouptypes.GlobalVirtualGroup) (keys.KeyManager, string, string, storagetypes.Uint, [][]byte) {
var err error
gvg, found := sp.GetFirstGlobalVirtualGroup()
s.Require().True(found)

// CreateBucket
user := s.GenAndChargeAccounts(1, 1000000)[0]
bucketName := "ch" + storagetestutils.GenRandomBucketName()
Expand Down
4 changes: 0 additions & 4 deletions x/payment/keeper/auto_settle_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ func (k Keeper) GetAllAutoSettleRecord(ctx sdk.Context) (list []types.AutoSettle
val := types.ParseAutoSettleRecordKey(iterator.Key())
list = append(list, val)
}

return
}

func (k Keeper) UpdateAutoSettleRecord(ctx sdk.Context, addr sdk.AccAddress, oldTime, newTime int64) {
if oldTime == newTime {
return
}
if oldTime != 0 {
k.RemoveAutoSettleRecord(ctx, oldTime, addr)
}
Expand Down
5 changes: 3 additions & 2 deletions x/payment/keeper/stream_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ func (k Keeper) TryResumeStreamRecord(ctx sdk.Context, streamRecord *types.Strea
}

now := ctx.BlockTime().Unix()
streamRecord.SettleTimestamp = now + streamRecord.StaticBalance.Quo(totalRate).Int64() - int64(forcedSettleTime)
prevSettleTime := streamRecord.SettleTimestamp
streamRecord.SettleTimestamp = now + streamRecord.StaticBalance.Quo(totalRate.Abs()).Int64() - int64(forcedSettleTime)
streamRecord.BufferBalance = expectedBalanceToResume
streamRecord.StaticBalance = streamRecord.StaticBalance.Sub(expectedBalanceToResume)
streamRecord.CrudTimestamp = now
Expand Down Expand Up @@ -416,7 +417,7 @@ func (k Keeper) TryResumeStreamRecord(ctx sdk.Context, streamRecord *types.Strea
}

k.SetStreamRecord(ctx, streamRecord)
k.UpdateAutoSettleRecord(ctx, sdk.MustAccAddressFromHex(streamRecord.Account), 0, streamRecord.SettleTimestamp)
k.UpdateAutoSettleRecord(ctx, sdk.MustAccAddressFromHex(streamRecord.Account), prevSettleTime, streamRecord.SettleTimestamp)
return nil
} else { //enqueue for resume in end block
k.SetStreamRecord(ctx, streamRecord)
Expand Down

0 comments on commit 79ce538

Please sign in to comment.