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

available space query #474

Merged
merged 2 commits into from
Aug 27, 2024
Merged
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
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 @@

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 @@
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")

Check warning on line 33 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}

ctx := sdk.UnwrapSDKContext(c)

Check warning on line 36 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L36

Added line #L36 was not covered by tests

var s uint64

Check warning on line 38 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L38

Added line #L38 was not covered by tests

providers := k.GetAllActiveProviders(ctx)

Check warning on line 40 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L40

Added line #L40 was not covered by tests

for _, provider := range providers {
providerEntry, found := k.GetProviders(ctx, provider.Address)
if !found {
continue

Check warning on line 45 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L42-L45

Added lines #L42 - L45 were not covered by tests
}
space, err := strconv.ParseInt(providerEntry.Totalspace, 10, 64)
if err != nil {
continue

Check warning on line 49 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L47-L49

Added lines #L47 - L49 were not covered by tests
}

s += uint64(space)

Check warning on line 52 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L52

Added line #L52 was not covered by tests
}

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

Check warning on line 55 in x/storage/keeper/grpc_query_storage_stats.go

View check run for this annotation

Codecov / codecov/patch

x/storage/keeper/grpc_query_storage_stats.go#L55

Added line #L55 was not covered by tests
}

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
Loading