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

feat: enable querying bucket, object and group by id. #83

Merged
merged 16 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
1,465 changes: 1,340 additions & 125 deletions docs/static/swagger.yaml

Large diffs are not rendered by default.

33 changes: 31 additions & 2 deletions proto/greenfield/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";
package bnbchain.greenfield.storage;

import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "greenfield/storage/params.proto";
Expand All @@ -26,19 +27,34 @@ service Query {
option (google.api.http).get = "/greenfield/storage/head_bucket/{bucket_name}";
}

// Queries a bucket by its id
rpc HeadBucketById(QueryHeadBucketByIdRequest) returns (QueryHeadBucketResponse) {
option (google.api.http).get = "/greenfield/storage/head_bucket_by_id/{bucket_id}";
}

// Queries a object with specify name.
rpc HeadObject(QueryHeadObjectRequest) returns (QueryHeadObjectResponse) {
option (google.api.http).get = "/greenfield/storage/head_object/{bucket_name}/{object_name}";
}

// Queries a object by its id
rpc HeadObjectById(QueryHeadObjectByIdRequest) returns (QueryHeadObjectResponse) {
option (google.api.http).get = "/greenfield/storage/head_object_by_id/{object_id}";
}

// Queries a list of bucket items.
rpc ListBuckets(QueryListBucketsRequest) returns (QueryListBucketsResponse) {
option (google.api.http).get = "/greenfield/storage/list_buckets";
}

// Queries a list of object items under the bucket.
rpc ListObjects(QueryListObjectsRequest) returns (QueryListObjectsResponse) {
option (google.api.http).get = "/greenfield/storage/list_objects/{bucketName}";
option (google.api.http).get = "/greenfield/storage/list_objects/{bucket_name}";
}

// Queries a list of object items under the bucket.
rpc ListObjectsByBucketId(QueryListObjectsByBucketIdRequest) returns (QueryListObjectsResponse) {
option (google.api.http).get = "/greenfield/storage/list_objects_by_bucket_id/{bucket_id}";
}
}

Expand All @@ -56,6 +72,10 @@ message QueryHeadBucketRequest {
string bucket_name = 1;
}

message QueryHeadBucketByIdRequest {
string bucket_id = 1;
}

message QueryHeadBucketResponse {
BucketInfo bucket_info = 1;
}
Expand All @@ -65,6 +85,10 @@ message QueryHeadObjectRequest {
string object_name = 2;
}

message QueryHeadObjectByIdRequest {
string object_id = 1;
fynnss marked this conversation as resolved.
Show resolved Hide resolved
}

message QueryHeadObjectResponse {
ObjectInfo object_info = 1;
}
Expand All @@ -80,7 +104,12 @@ message QueryListBucketsResponse {

message QueryListObjectsRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string bucketName = 2;
string bucket_name = 2;
}

message QueryListObjectsByBucketIdRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
string bucket_id = 2;
}

message QueryListObjectsResponse {
Expand Down
33 changes: 29 additions & 4 deletions proto/greenfield/storage/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package bnbchain.greenfield.storage;
import "cosmos/msg/v1/msg.proto";
// this line is used by starport scaffolding # proto/tx/import
import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "greenfield/storage/common.proto";

option go_package = "github.com/bnb-chain/greenfield/x/storage/types";
Expand Down Expand Up @@ -48,7 +49,13 @@ message MsgCreateBucket {
ReadQuota read_quota = 8;
}

message MsgCreateBucketResponse {}
message MsgCreateBucketResponse {
string bucket_id = 1 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
}

message MsgDeleteBucket {
option (cosmos.msg.v1.signer) = "operator";
Expand Down Expand Up @@ -88,7 +95,13 @@ message MsgCreateObject {
repeated string expect_secondary_sp_addresses = 10 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message MsgCreateObjectResponse {}
message MsgCreateObjectResponse {
string object_id = 1 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
}

message MsgSealObject {
option (cosmos.msg.v1.signer) = "operator";
Expand Down Expand Up @@ -137,7 +150,13 @@ message MsgCopyObject {
Approval dst_primary_sp_approval = 6;
}

message MsgCopyObjectResponse {}
message MsgCopyObjectResponse {
string object_id = 1 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
}

message MsgDeleteObject {
option (cosmos.msg.v1.signer) = "operator";
Expand All @@ -163,7 +182,13 @@ message MsgCreateGroup {
repeated string members = 3 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message MsgCreateGroupResponse {}
message MsgCreateGroupResponse {
string group_id = 1 [
(cosmos_proto.scalar) = "cosmos.Uint",
(gogoproto.customtype) = "Uint",
(gogoproto.nullable) = false
];
}

message MsgDeleteGroup {
option (cosmos.msg.v1.signer) = "operator";
Expand Down
26 changes: 13 additions & 13 deletions x/storage/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (k msgServer) CreateBucket(goCtx context.Context, msg *types.MsgCreateBucke
return nil, err
}

// TODO(alex): add id to response
_ = id
return &types.MsgCreateBucketResponse{}, nil
return &types.MsgCreateBucketResponse{
BucketId: id,
}, nil
}

func (k msgServer) DeleteBucket(goCtx context.Context, msg *types.MsgDeleteBucket) (*types.MsgDeleteBucketResponse, error) {
Expand Down Expand Up @@ -104,10 +104,9 @@ func (k msgServer) CreateObject(goCtx context.Context, msg *types.MsgCreateObjec
return nil, err
}

// TODO(alex): add id to response
_ = id

return &types.MsgCreateObjectResponse{}, nil
return &types.MsgCreateObjectResponse{
ObjectId: id,
}, nil
}

func (k msgServer) SealObject(goCtx context.Context, msg *types.MsgSealObject) (*types.MsgSealObjectResponse, error) {
Expand Down Expand Up @@ -165,9 +164,9 @@ func (k msgServer) CopyObject(goCtx context.Context, msg *types.MsgCopyObject) (
return nil, err
}

// TODO(alex): add id to response
_ = id
return &types.MsgCopyObjectResponse{}, nil
return &types.MsgCopyObjectResponse{
ObjectId: id,
}, nil
}

func (k msgServer) DeleteObject(goCtx context.Context, msg *types.MsgDeleteObject) (*types.MsgDeleteObjectResponse, error) {
Expand Down Expand Up @@ -215,9 +214,10 @@ func (k msgServer) CreateGroup(goCtx context.Context, msg *types.MsgCreateGroup)
if err != nil {
return nil, err
}
// TODO(alex): add id to response
_ = id
return &types.MsgCreateGroupResponse{}, nil

return &types.MsgCreateGroupResponse{
GroupId: id,
}, nil
}

func (k msgServer) DeleteGroup(goCtx context.Context, msg *types.MsgDeleteGroup) (*types.MsgDeleteGroupResponse, error) {
Expand Down
75 changes: 75 additions & 0 deletions x/storage/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"cosmossdk.io/math"
"github.com/bnb-chain/greenfield/x/storage/types"
)

Expand All @@ -29,6 +30,26 @@ func (k Keeper) HeadBucket(goCtx context.Context, req *types.QueryHeadBucketRequ
return nil, types.ErrNoSuchBucket
}

func (k Keeper) HeadBucketById(goCtx context.Context, req *types.QueryHeadBucketByIdRequest) (*types.QueryHeadBucketResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
id, err := math.ParseUint(req.BucketId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid bucket id")
}

bucketInfo, found := k.GetBucketInfoById(ctx, id)
if found {
return &types.QueryHeadBucketResponse{
BucketInfo: &bucketInfo,
}, nil

}
return nil, types.ErrNoSuchBucket
}

func (k Keeper) HeadObject(goCtx context.Context, req *types.QueryHeadObjectRequest) (*types.QueryHeadObjectResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand All @@ -45,6 +66,27 @@ func (k Keeper) HeadObject(goCtx context.Context, req *types.QueryHeadObjectRequ
return nil, types.ErrNoSuchObject
}

func (k Keeper) HeadObjectById(goCtx context.Context, req *types.QueryHeadObjectByIdRequest) (*types.QueryHeadObjectResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

id, err := math.ParseUint(req.ObjectId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid object id")
}

objectInfo, found := k.GetObjectInfoById(ctx, id)
if found {
return &types.QueryHeadObjectResponse{
ObjectInfo: &objectInfo,
}, nil
}
return nil, types.ErrNoSuchObject
}

func (k Keeper) ListBuckets(goCtx context.Context, req *types.QueryListBucketsRequest) (*types.QueryListBucketsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand Down Expand Up @@ -93,3 +135,36 @@ func (k Keeper) ListObjects(goCtx context.Context, req *types.QueryListObjectsRe
}
return &types.QueryListObjectsResponse{ObjectInfos: objectInfos, Pagination: pageRes}, nil
}

func (k Keeper) ListObjectsByBucketId(goCtx context.Context, req *types.QueryListObjectsByBucketIdRequest) (*types.QueryListObjectsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

var objectInfos []types.ObjectInfo
store := ctx.KVStore(k.storeKey)
id, err := math.ParseUint(req.BucketId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid bucket id")
}
bucketInfo, found := k.GetBucketInfoById(ctx, id)
if !found {
return nil, types.ErrNoSuchBucket
}
objectPrefixStore := prefix.NewStore(store, types.GetObjectKeyOnlyBucketPrefix(bucketInfo.BucketName))

pageRes, err := query.Paginate(objectPrefixStore, req.Pagination, func(key []byte, value []byte) error {
objectInfo, found := k.GetObjectInfoById(ctx, types.DecodeSequence(value))
if found {
objectInfos = append(objectInfos, objectInfo)
}
return nil
})

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryListObjectsResponse{ObjectInfos: objectInfos, Pagination: pageRes}, nil
}
Loading