From 92e9bc456b42bb6812d718290d05af278efee299 Mon Sep 17 00:00:00 2001 From: marston Date: Wed, 2 Oct 2024 16:51:11 -0400 Subject: [PATCH 1/3] fixing post key emitting events twice --- x/filetree/keeper/msg_server_postkey.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/x/filetree/keeper/msg_server_postkey.go b/x/filetree/keeper/msg_server_postkey.go index 79088600..8ef13f28 100644 --- a/x/filetree/keeper/msg_server_postkey.go +++ b/x/filetree/keeper/msg_server_postkey.go @@ -22,13 +22,6 @@ func (k msgServer) PostKey(goCtx context.Context, msg *types.MsgPostKey) (*types ), ) - ctx.EventManager().EmitEvent( - sdk.NewEvent( - types.EventTypePostKey, - sdk.NewAttribute(types.AttributeKeySigner, msg.Creator), - ), - ) - ctx.EventManager().EmitEvent( sdk.NewEvent( types.EventTypeJackalMessage, From 25229fe2daeb60d9bdb5180f39112dc11691376d Mon Sep 17 00:00:00 2001 From: marston Date: Wed, 2 Oct 2024 20:08:26 -0400 Subject: [PATCH 2/3] improving storage stats query --- x/storage/keeper/grpc_query_storage_stats.go | 30 +++-- .../keeper/grpc_query_storage_stats_test.go | 106 ++++++++++++++++++ x/storage/keeper/msg_server_post_file_test.go | 1 - 3 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 x/storage/keeper/grpc_query_storage_stats_test.go diff --git a/x/storage/keeper/grpc_query_storage_stats.go b/x/storage/keeper/grpc_query_storage_stats.go index 91fe2878..2b3defe5 100644 --- a/x/storage/keeper/grpc_query_storage_stats.go +++ b/x/storage/keeper/grpc_query_storage_stats.go @@ -66,22 +66,38 @@ func (k Keeper) StorageStats(c context.Context, req *types.QueryStorageStats) (* var spacePurchased int64 var spaceUsed int64 - var activeUsers uint64 - var totalUsers uint64 + + activeUsers := make(map[string]bool) + allUsers := make(map[string]bool) usersByPlan := make(map[int64]int64) for _, info := range payment { - totalUsers++ // counting in total users + allUsers[info.Address] = true + if info.End.Before(ctx.BlockTime()) { continue } usersByPlan[info.SpaceAvailable]++ + activeUsers[info.Address] = true spacePurchased += info.SpaceAvailable - spaceUsed += info.SpaceUsed - activeUsers++ } + k.IterateAndParseFilesByMerkle(ctx, false, func(_ []byte, val types.UnifiedFile) bool { + allUsers[val.Owner] = true + activeUsers[val.Owner] = true + + m := val.FileSize * val.MaxProofs + + if val.Expires > 0 { + spacePurchased += m + } + + spaceUsed += m + + return false + }) + decSpent := sdk.NewDec(spacePurchased) decUsed := sdk.NewDec(spaceUsed) @@ -91,8 +107,8 @@ func (k Keeper) StorageStats(c context.Context, req *types.QueryStorageStats) (* Purchased: uint64(spacePurchased), Used: uint64(spaceUsed), UsedRatio: ratio, - ActiveUsers: activeUsers, - UniqueUsers: totalUsers, + ActiveUsers: uint64(len(activeUsers)), + UniqueUsers: uint64(len(allUsers)), UsersByPlan: usersByPlan, }, nil } diff --git a/x/storage/keeper/grpc_query_storage_stats_test.go b/x/storage/keeper/grpc_query_storage_stats_test.go new file mode 100644 index 00000000..d87ade21 --- /dev/null +++ b/x/storage/keeper/grpc_query_storage_stats_test.go @@ -0,0 +1,106 @@ +package keeper_test + +import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/jackalLabs/canine-chain/v4/testutil" + "github.com/jackalLabs/canine-chain/v4/x/storage/types" + "math/rand" + "time" +) + +func (suite *KeeperTestSuite) TestStorageStats() { + + for i := 0; i < 100; i++ { + + var mm = rand.Int63n(50_000_000_000) + var fs = rand.Int63n(1_000_000_000_000) + + suite.SetupSuite() + + testAddresses, err := testutil.CreateTestAddresses("cosmos", 4) + suite.Require().NoError(err) + + testAccount := testAddresses[0] + otherAccount := testAddresses[2] + deadAccount := testAddresses[3] + + depoAccount := testAddresses[1] + + coins := sdk.NewCoins(sdk.NewCoin("ujkl", sdk.NewInt(100000000000))) // Send some coins to their account + testAcc, _ := sdk.AccAddressFromBech32(testAccount) + err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, testAcc, coins) + suite.Require().NoError(err) + + suite.storageKeeper.SetParams(suite.ctx, types.Params{ + DepositAccount: depoAccount, + ProofWindow: 50, + ChunkSize: 1024, + PriceFeed: "jklprice", + MissesToBurn: 3, + MaxContractAgeInBlocks: 100, + PricePerTbPerMonth: 8, + CollateralPrice: 2, + CheckWindow: 11, + ReferralCommission: 25, + PolRatio: 40, + }) + + suite.storageKeeper.SetStoragePaymentInfo(suite.ctx, types.StoragePaymentInfo{ + Start: time.Date(2023, 10, 10, 0, 0, 0, 0, time.UTC), + End: time.Date(2024, 10, 10, 0, 0, 0, 0, time.UTC), + SpaceAvailable: mm, + SpaceUsed: 0, + Address: testAccount, + Coins: nil, + }) + + suite.storageKeeper.SetStoragePaymentInfo(suite.ctx, types.StoragePaymentInfo{ // dead account plan (counts for unique but not active) + Start: time.Date(2023, 10, 10, 0, 0, 0, 0, time.UTC), + End: time.Date(2023, 12, 10, 0, 0, 0, 0, time.UTC), + SpaceAvailable: 5_000_000_000, + SpaceUsed: 0, + Address: deadAccount, + Coins: nil, + }) + + merkle := []byte("merkle") + + suite.storageKeeper.SetFile(suite.ctx, types.UnifiedFile{ + Merkle: merkle, + Owner: testAccount, + Start: 0, + Expires: 0, + FileSize: fs, + ProofInterval: 400, + ProofType: 0, + Proofs: make([]string, 0), + MaxProofs: 3, + Note: "test", + }) + + suite.storageKeeper.SetFile(suite.ctx, types.UnifiedFile{ + Merkle: merkle, + Owner: otherAccount, + Start: 10, + Expires: 100, + FileSize: fs, + ProofInterval: 400, + ProofType: 0, + Proofs: make([]string, 0), + MaxProofs: 3, + Note: "test", + }) + + res, err := suite.queryClient.StorageStats(context.Background(), &types.QueryStorageStats{}) + suite.Require().NoError(err) + + suite.Require().Equal(uint64(2), res.ActiveUsers) + suite.Require().Equal(uint64(3), res.UniqueUsers) + suite.Require().Equal(uint64(fs*6), res.Used) + suite.Require().Equal(uint64(fs*3+mm), res.Purchased) + + suite.reset() + } + +} diff --git a/x/storage/keeper/msg_server_post_file_test.go b/x/storage/keeper/msg_server_post_file_test.go index eba7e37f..1fb85ef5 100644 --- a/x/storage/keeper/msg_server_post_file_test.go +++ b/x/storage/keeper/msg_server_post_file_test.go @@ -82,7 +82,6 @@ func (suite *KeeperTestSuite) TestPostFile() { tc := tcs suite.Run(tc.testName, func() { res, err := msgSrvr.PostFile(ctx, &tc.msg) - if tc.expErr { suite.Require().ErrorContains(err, tc.expErrMsg) } else { From fbabbfeecce5639d2d7cc17c20d2949dc940e929 Mon Sep 17 00:00:00 2001 From: marston Date: Wed, 2 Oct 2024 20:10:52 -0400 Subject: [PATCH 3/3] lint --- x/storage/keeper/grpc_query_storage_stats_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/x/storage/keeper/grpc_query_storage_stats_test.go b/x/storage/keeper/grpc_query_storage_stats_test.go index d87ade21..35461ee7 100644 --- a/x/storage/keeper/grpc_query_storage_stats_test.go +++ b/x/storage/keeper/grpc_query_storage_stats_test.go @@ -2,19 +2,19 @@ package keeper_test import ( "context" + "math/rand" + "time" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/jackalLabs/canine-chain/v4/testutil" "github.com/jackalLabs/canine-chain/v4/x/storage/types" - "math/rand" - "time" ) func (suite *KeeperTestSuite) TestStorageStats() { - for i := 0; i < 100; i++ { - var mm = rand.Int63n(50_000_000_000) - var fs = rand.Int63n(1_000_000_000_000) + mm := rand.Int63n(50_000_000_000) + fs := rand.Int63n(1_000_000_000_000) suite.SetupSuite() @@ -102,5 +102,4 @@ func (suite *KeeperTestSuite) TestStorageStats() { suite.reset() } - }