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

Master debug #585

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion x/payment/keeper/stream_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func (k Keeper) GetAllStreamRecord(ctx sdk.Context) (list []types.StreamRecord)
for ; iterator.Valid(); iterator.Next() {
var val types.StreamRecord
k.cdc.MustUnmarshal(iterator.Value(), &val)
val.Account = string(iterator.Key())
//start := len(types.StreamRecordKeyPrefix)
val.Account = sdk.AccAddress(iterator.Key()).String()
list = append(list, val)
}

Expand Down Expand Up @@ -221,11 +222,13 @@ func (k Keeper) UpdateStreamRecord(ctx sdk.Context, streamRecord *types.StreamRe

func (k Keeper) UpdateStreamRecordByAddr(ctx sdk.Context, change *types.StreamRecordChange) (ret *types.StreamRecord, err error) {
streamRecord, _ := k.GetStreamRecord(ctx, change.Addr)
fmt.Println("sr before", streamRecord.Account, streamRecord.LockBalance)
err = k.UpdateStreamRecord(ctx, streamRecord, change)
if err != nil {
return
}
k.SetStreamRecord(ctx, streamRecord)
fmt.Println("sr after", streamRecord.Account, streamRecord.LockBalance)
return streamRecord, nil
}

Expand Down
13 changes: 12 additions & 1 deletion x/storage/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"fmt"

paymenttypes "github.com/bnb-chain/greenfield/x/payment/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func BeginBlocker(ctx sdk.Context, keeper Keeper) {
Expand Down Expand Up @@ -37,6 +38,16 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) {
return
}

//bucketNames := []string{
// "u-100-sp111",
// "uploadimg",
// "testaccount",
// "pay1-1gb",
//}

fmt.Println("\n\n ############## checking in end block ##############", ctx.TxSize())
keeper.CheckLockBalance(ctx)

// delete buckets
_, err = keeper.DeleteDiscontinueBucketsUntil(ctx, blockTime, deletionMax-deleted)
if err != nil {
Expand Down
100 changes: 100 additions & 0 deletions x/storage/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2566,3 +2566,103 @@ func (k Keeper) CancelUpdateObjectContent(
ObjectId: objectInfo.Id,
})
}

func (k Keeper) CheckLockBalance(ctx sdk.Context) {
fmt.Println("\n\n-------------checking lock balance-------------")
type LockDetail struct {
locked uint64
expected uint64
}
paymentLocks := make(map[string]LockDetail, 0)

store := ctx.KVStore(k.storeKey)
bucketStore := prefix.NewStore(store, types.BucketByIDPrefix)
bucketIt := bucketStore.Iterator(nil, nil)
defer bucketIt.Close()

for ; bucketIt.Valid(); bucketIt.Next() {
var bucket types.BucketInfo
k.cdc.MustUnmarshal(bucketIt.Value(), &bucket)
if bucket.Id.Uint64() == 0 {
continue
}
streamRecord, _ := k.paymentKeeper.GetStreamRecord(ctx, sdk.MustAccAddressFromHex(bucket.PaymentAddress))

_, ok := paymentLocks[bucket.PaymentAddress]
if !ok {
paymentLocks[bucket.PaymentAddress] = LockDetail{
locked: 0,
expected: 0,
}
}

objectPrefixStore := prefix.NewStore(store, types.GetObjectKeyOnlyBucketPrefix(bucket.BucketName))
it := objectPrefixStore.Iterator(nil, nil)
defer it.Close()

expected := paymentLocks[bucket.PaymentAddress].expected
for ; it.Valid(); it.Next() {
u256Seq := sequence.Sequence[sdkmath.Uint]{}
objectInfo, found := k.GetObjectInfoById(ctx, u256Seq.DecodeSequence(it.Value()))
if found && (objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED || objectInfo.IsUpdating) {
if objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED {
if objectInfo.ObjectName == "0ksqn9u532" {
fmt.Println("checking", objectInfo.BucketName, objectInfo.ObjectName, objectInfo.PayloadSize, objectInfo.CreateAt)
}
toBeLocked, err := k.GetObjectLockFee(ctx, objectInfo.CreateAt, objectInfo.PayloadSize)
if err != nil {
fmt.Println(objectInfo.BucketName, objectInfo.ObjectName, objectInfo.ObjectStatus, objectInfo.IsUpdating)
panic(err)
}
expected = expected + toBeLocked.Uint64()
} else {
shadowObject, _ := k.GetShadowObjectInfo(ctx, bucket.BucketName, objectInfo.ObjectName)
toBeLocked, err := k.GetObjectLockFee(ctx, shadowObject.UpdatedAt, shadowObject.PayloadSize)
if err != nil {
fmt.Println(objectInfo.BucketName, objectInfo.ObjectName, objectInfo.ObjectStatus, objectInfo.IsUpdating)
panic(err)
}
expected = expected + toBeLocked.Uint64()
}
}
}
paymentLocks[bucket.PaymentAddress] = LockDetail{
locked: streamRecord.LockBalance.Uint64(),
expected: expected,
}
}

totalLocked, totalExpected := uint64(0), uint64(0)
for address, lock := range paymentLocks {
totalLocked = totalLocked + lock.locked
totalExpected = totalExpected + lock.expected

//if lock.locked != 0 || lock.expected != 0 {
// fmt.Println("not zero", address, lock.locked, lock.expected)
//}
if lock.locked != lock.expected {
fmt.Println("not equal", address, lock.locked, lock.expected)
//panic("not equal")
}
}
fmt.Println("totalLocked", totalLocked, "totalExpected", totalExpected)

allLocked := uint64(0)
allStreamRecord := k.paymentKeeper.GetAllStreamRecord(ctx)
for _, streamRecord := range allStreamRecord {
allLocked = allLocked + streamRecord.LockBalance.Uint64()
_, ok := paymentLocks[streamRecord.Account]
if streamRecord.LockBalance.Uint64() > 0 && !ok {
fmt.Println(streamRecord.Account, streamRecord.LockBalance.Uint64())
}
}
fmt.Println("allLocked", allLocked)

if totalLocked != totalExpected {
panic(fmt.Sprintf("not balanced1: %d", ctx.BlockHeight()))
}

if totalLocked != allLocked {
panic(fmt.Sprintf("not balanced2: %d", ctx.BlockHeight()))
}
}
10 changes: 9 additions & 1 deletion x/storage/keeper/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (k Keeper) lockObjectStoreFee(ctx sdk.Context, bucketInfo *storagetypes.Buc
Amount: amount,
})
}

fmt.Println("bucket", bucketInfo.BucketName, "objectName", objectName, "payloadSize", payloadSize, "timestamp", timestamp, "amount", amount)
change := types.NewDefaultStreamRecordChangeWithAddr(paymentAddr).WithLockBalanceChange(amount)
streamRecord, err := k.paymentKeeper.UpdateStreamRecordByAddr(ctx, change)
if err != nil {
Expand Down Expand Up @@ -520,20 +520,28 @@ func (k Keeper) GetObjectLockFee(ctx sdk.Context, priceTime int64, payloadSize u
return amount, fmt.Errorf("get charge size failed: %d %w", priceTime, err)
}

fmt.Println("price", price.PrimaryStorePrice, price.SecondaryStorePrice)

primaryRate := price.PrimaryStorePrice.MulInt(sdkmath.NewIntFromUint64(chargeSize)).TruncateInt()

secondarySPNum := int64(k.GetExpectSecondarySPNumForECObject(ctx, priceTime))
secondaryRate := price.SecondaryStorePrice.MulInt(sdkmath.NewIntFromUint64(chargeSize)).TruncateInt()
secondaryRate = secondaryRate.MulRaw(int64(secondarySPNum))

fmt.Println("rate", primaryRate, secondarySPNum, secondaryRate)

versionedParams, err := k.paymentKeeper.GetVersionedParamsWithTs(ctx, priceTime)
if err != nil {
return amount, fmt.Errorf("get versioned reserve time error: %w", err)
}
validatorTaxRate := versionedParams.ValidatorTaxRate.MulInt(primaryRate.Add(secondaryRate)).TruncateInt()

fmt.Println("params", versionedParams.ValidatorTaxRate, versionedParams.ReserveTime)

rate := primaryRate.Add(secondaryRate).Add(validatorTaxRate) // should also lock for validator tax pool
amount = rate.Mul(sdkmath.NewIntFromUint64(versionedParams.ReserveTime))

fmt.Println("final amount", rate, amount)
return amount, nil
}

Expand Down
1 change: 1 addition & 0 deletions x/storage/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type PaymentKeeper interface {
UpdateStreamRecordByAddr(ctx sdk.Context, change *paymenttypes.StreamRecordChange) (ret *paymenttypes.StreamRecord, err error)
GetStreamRecord(ctx sdk.Context, account sdk.AccAddress) (ret *paymenttypes.StreamRecord, found bool)
MergeOutFlows(flows []paymenttypes.OutFlow) []paymenttypes.OutFlow
GetAllStreamRecord(ctx sdk.Context) (list []paymenttypes.StreamRecord)
}

type PermissionKeeper interface {
Expand Down
Loading