From 9bb5e1ce9a2610ed0cd8b284a1fb70f2ebd3ec91 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 11:47:21 +0800 Subject: [PATCH 01/10] chore: debug --- x/payment/keeper/stream_record.go | 3 +- x/storage/keeper/abci.go | 13 ++++- x/storage/keeper/keeper.go | 83 +++++++++++++++++++++++++++++ x/storage/types/expected_keepers.go | 1 + 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/x/payment/keeper/stream_record.go b/x/payment/keeper/stream_record.go index 2956f7c97..b6d30ebed 100644 --- a/x/payment/keeper/stream_record.go +++ b/x/payment/keeper/stream_record.go @@ -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) } diff --git a/x/storage/keeper/abci.go b/x/storage/keeper/abci.go index 01877b4b6..8dc6d0c0e 100644 --- a/x/storage/keeper/abci.go +++ b/x/storage/keeper/abci.go @@ -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) { @@ -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 { diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index d4e2fe06a..a823c86e9 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2566,3 +2566,86 @@ 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 { + toBeLocked, _ := k.GetObjectLockFee(ctx, objectInfo.CreateAt, objectInfo.PayloadSize) + 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())) + } +} diff --git a/x/storage/types/expected_keepers.go b/x/storage/types/expected_keepers.go index d56c545b6..fb6450a6a 100644 --- a/x/storage/types/expected_keepers.go +++ b/x/storage/types/expected_keepers.go @@ -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 { From 54911ef8f7b6b54a3a09af0035eb651943dbabcb Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 12:02:57 +0800 Subject: [PATCH 02/10] updating object --- x/storage/keeper/keeper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index a823c86e9..1b6eafa2c 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2604,8 +2604,8 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { 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 { - toBeLocked, _ := k.GetObjectLockFee(ctx, objectInfo.CreateAt, objectInfo.PayloadSize) + if found && (objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED || objectInfo.IsUpdating) { + toBeLocked, _ := k.GetObjectLockFee(ctx, objectInfo.GetUpdatedAt(), objectInfo.PayloadSize) expected = expected + toBeLocked.Uint64() } } From c39d5d614d874bd49cbdef940c163cebc39705ae Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 12:09:09 +0800 Subject: [PATCH 03/10] updating object --- x/storage/keeper/keeper.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index 1b6eafa2c..350d05e91 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2605,7 +2605,10 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { u256Seq := sequence.Sequence[sdkmath.Uint]{} objectInfo, found := k.GetObjectInfoById(ctx, u256Seq.DecodeSequence(it.Value())) if found && (objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED || objectInfo.IsUpdating) { - toBeLocked, _ := k.GetObjectLockFee(ctx, objectInfo.GetUpdatedAt(), objectInfo.PayloadSize) + toBeLocked, err := k.GetObjectLockFee(ctx, objectInfo.GetUpdatedAt(), objectInfo.PayloadSize) + if err != nil { + panic(err) + } expected = expected + toBeLocked.Uint64() } } From 0504cc7462988294d3d8a2fe668d349d8bbc9aa8 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 12:11:02 +0800 Subject: [PATCH 04/10] updating object --- x/storage/keeper/keeper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index 350d05e91..f710e60c4 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2607,6 +2607,7 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { if found && (objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED || objectInfo.IsUpdating) { toBeLocked, err := k.GetObjectLockFee(ctx, objectInfo.GetUpdatedAt(), objectInfo.PayloadSize) if err != nil { + fmt.Println(objectInfo.BucketName, objectInfo.ObjectName, objectInfo.ObjectStatus, objectInfo.IsUpdating) panic(err) } expected = expected + toBeLocked.Uint64() From 39135c02db3dc89c4a50bbf0f071756c3f75287c Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 12:13:29 +0800 Subject: [PATCH 05/10] updating object --- x/storage/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index f710e60c4..7da46e270 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2605,7 +2605,7 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { u256Seq := sequence.Sequence[sdkmath.Uint]{} objectInfo, found := k.GetObjectInfoById(ctx, u256Seq.DecodeSequence(it.Value())) if found && (objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED || objectInfo.IsUpdating) { - toBeLocked, err := k.GetObjectLockFee(ctx, objectInfo.GetUpdatedAt(), objectInfo.PayloadSize) + toBeLocked, err := k.GetObjectLockFee(ctx, objectInfo.GetLatestUpdatedTime(), objectInfo.PayloadSize) if err != nil { fmt.Println(objectInfo.BucketName, objectInfo.ObjectName, objectInfo.ObjectStatus, objectInfo.IsUpdating) panic(err) From 191137e4d17899715631f0a3ca2799af05abfb1e Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 12:15:31 +0800 Subject: [PATCH 06/10] updating object --- x/storage/keeper/keeper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index 7da46e270..022ca9101 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2629,7 +2629,7 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { //} if lock.locked != lock.expected { fmt.Println("not equal", address, lock.locked, lock.expected) - panic("not equal") + //panic("not equal") } } fmt.Println("totalLocked", totalLocked, "totalExpected", totalExpected) From 49bb582809bde3aca452464e53e04c46198f31bb Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 13:05:02 +0800 Subject: [PATCH 07/10] updating object --- x/storage/keeper/keeper.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index 022ca9101..162a0d2fa 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2605,12 +2605,22 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { u256Seq := sequence.Sequence[sdkmath.Uint]{} objectInfo, found := k.GetObjectInfoById(ctx, u256Seq.DecodeSequence(it.Value())) if found && (objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED || objectInfo.IsUpdating) { - toBeLocked, err := k.GetObjectLockFee(ctx, objectInfo.GetLatestUpdatedTime(), objectInfo.PayloadSize) - if err != nil { - fmt.Println(objectInfo.BucketName, objectInfo.ObjectName, objectInfo.ObjectStatus, objectInfo.IsUpdating) - panic(err) + if objectInfo.ObjectStatus == types.OBJECT_STATUS_CREATED { + 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() } - expected = expected + toBeLocked.Uint64() } } paymentLocks[bucket.PaymentAddress] = LockDetail{ From 020f469b675ffaacb3fa1c66969e71deedd527f9 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 14:22:39 +0800 Subject: [PATCH 08/10] updating object --- x/payment/keeper/stream_record.go | 2 ++ x/storage/keeper/payment.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/x/payment/keeper/stream_record.go b/x/payment/keeper/stream_record.go index b6d30ebed..f142bda03 100644 --- a/x/payment/keeper/stream_record.go +++ b/x/payment/keeper/stream_record.go @@ -222,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 } diff --git a/x/storage/keeper/payment.go b/x/storage/keeper/payment.go index fe23925c4..7cad1ab2e 100644 --- a/x/storage/keeper/payment.go +++ b/x/storage/keeper/payment.go @@ -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 { From 124714c33a9c0b369cdbfe62b758b1cd9462cc30 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 14:29:05 +0800 Subject: [PATCH 09/10] updating object --- x/storage/keeper/keeper.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/storage/keeper/keeper.go b/x/storage/keeper/keeper.go index 162a0d2fa..9fde2db15 100644 --- a/x/storage/keeper/keeper.go +++ b/x/storage/keeper/keeper.go @@ -2606,6 +2606,9 @@ func (k Keeper) CheckLockBalance(ctx sdk.Context) { 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) From 936cc6100f1a0579cc1f824e6ae9cd1e7bcd5282 Mon Sep 17 00:00:00 2001 From: forcodedancing Date: Fri, 8 Mar 2024 14:36:14 +0800 Subject: [PATCH 10/10] add logs --- x/storage/keeper/payment.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/x/storage/keeper/payment.go b/x/storage/keeper/payment.go index 7cad1ab2e..ca4bf759f 100644 --- a/x/storage/keeper/payment.go +++ b/x/storage/keeper/payment.go @@ -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 }