Skip to content

Commit

Permalink
Storage stats query cleaning (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Oct 3, 2024
2 parents a813d77 + fbabbfe commit 52cdab2
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 15 deletions.
7 changes: 0 additions & 7 deletions x/filetree/keeper/msg_server_postkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 23 additions & 7 deletions x/storage/keeper/grpc_query_storage_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
105 changes: 105 additions & 0 deletions x/storage/keeper/grpc_query_storage_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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"
)

func (suite *KeeperTestSuite) TestStorageStats() {
for i := 0; i < 100; i++ {

mm := rand.Int63n(50_000_000_000)
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()
}
}
1 change: 0 additions & 1 deletion x/storage/keeper/msg_server_post_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 52cdab2

Please sign in to comment.