Skip to content

Commit

Permalink
available space query
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell committed Aug 22, 2024
1 parent 36c7ecc commit 4d65639
Show file tree
Hide file tree
Showing 4 changed files with 574 additions and 155 deletions.
14 changes: 13 additions & 1 deletion proto/canine_chain/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,18 @@ service Query {
"/jackal/canine-chain/storage/storage_stats";
}

// Queries protocol storage space used and purchased.
// Queries how much storage space is being used on the network at this time.
rpc NetworkSize(QueryNetworkSize) returns (QueryNetworkSizeResponse) {
option (google.api.http).get =
"/jackal/canine-chain/storage/network_size";
}

// Queries the amount of offered storage by active providers
rpc AvailableSpace(QueryAvailableSpace) returns (QueryAvailableSpaceResponse) {
option (google.api.http).get =
"/jackal/canine-chain/storage/available_space";
}

// Queries protocol storage space used and purchased.
rpc Gauges(QueryAllGauges) returns (QueryAllGaugesResponse) {
option (google.api.http).get =
Expand Down Expand Up @@ -451,4 +457,10 @@ message QueryNetworkSize {}

message QueryNetworkSizeResponse {
uint64 size = 1;
}

message QueryAvailableSpace {}

message QueryAvailableSpaceResponse {
uint64 size = 1;
}
28 changes: 28 additions & 0 deletions x/storage/keeper/grpc_query_storage_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v4/x/storage/types"
Expand All @@ -27,6 +28,33 @@ func (k Keeper) NetworkSize(c context.Context, req *types.QueryNetworkSize) (*ty
return &types.QueryNetworkSizeResponse{Size_: s}, nil
}

func (k Keeper) AvailableSpace(c context.Context, req *types.QueryAvailableSpace) (*types.QueryAvailableSpaceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)

var s uint64

providers := k.GetAllActiveProviders(ctx)

for _, provider := range providers {
providerEntry, found := k.GetProviders(ctx, provider.Address)
if !found {
continue
}
space, err := strconv.ParseInt(providerEntry.Totalspace, 10, 64)
if err != nil {
continue
}

s += uint64(space)
}

return &types.QueryAvailableSpaceResponse{Size_: s}, nil
}

func (k Keeper) StorageStats(c context.Context, req *types.QueryStorageStats) (*types.QueryStorageStatsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand Down
Loading

0 comments on commit 4d65639

Please sign in to comment.