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

Fix lint #4

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
!feat: Add query server (realiotech#81)
* Add query server

* renaming for matching types

* remove unused file

* Renaming param func query

* rename for matching coding standard
  • Loading branch information
neitdung authored Jan 9, 2024
commit 5ab618d94ab4c7fdb6fedacc0cd20d3da06f480b
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/cosmos/cosmos-sdk v0.46.12
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/ibc-go/v6 v6.1.1
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/rakyll/statik v0.1.7
Expand All @@ -18,6 +19,7 @@ require (
github.com/stretchr/testify v1.8.1
github.com/tendermint/tendermint v0.34.27
github.com/tendermint/tm-db v0.6.7
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -74,7 +76,6 @@ require (
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand Down Expand Up @@ -154,7 +155,6 @@ require (
google.golang.org/api v0.126.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
106 changes: 106 additions & 0 deletions proto/multistaking/v1/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
syntax = "proto3";
package multistaking.v1;

import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";
import "multistaking/v1/multi_staking.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "google/api/annotations.proto";

option go_package = "github.com/realiotech/multi-staking/x/multi-staking/types";

// Query defines the gRPC querier service.
service Query {
rpc MultiStakingLocks(QueryMultiStakingLocksRequest)
returns (QueryMultiStakingLocksResponse) {
option (google.api.http).get = "/realiotech/multistaking/v1/locks";
}
rpc MultiStakingLock(QueryMultiStakingLockRequest)
returns (QueryMultiStakingLockResponse) {
option (google.api.http).get = "/realiotech/multistaking/v1/lock/"
"{validator_address}/{multi_staker_address}";
}
rpc MultiStakingUnlocks(QueryMultiStakingUnlocksRequest)
returns (QueryMultiStakingUnlocksResponse) {
option (google.api.http).get = "/realiotech/multistaking/v1/unlocks";
}
rpc MultiStakingUnlock(QueryMultiStakingUnlockRequest)
returns (QueryMultiStakingUnlockResponse) {
option (google.api.http).get = "/realiotech/multistaking/v1/unlock/"
"{validator_address}/{multi_staker_address}";
}
rpc ValidatorMultiStakingCoin(QueryValidatorMultiStakingCoinRequest)
returns (QueryValidatorMultiStakingCoinResponse) {
option (google.api.http).get =
"/realiotech/multistaking/v1/validator-coin/{validator_addr}";
}
rpc BondWeight(QueryBondWeightRequest) returns (QueryBondWeightResponse) {
option (google.api.http).get = "/realiotech/multistaking/v1/weight/{denom}";
}
}

message QueryMultiStakingLocksRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryMultiStakingLocksResponse {
repeated MultiStakingLock locks = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryMultiStakingLockRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
string multi_staker_address = 1
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string validator_address = 2
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message QueryMultiStakingLockResponse {
MultiStakingLock lock = 1;
bool found = 2;
}

message QueryMultiStakingUnlocksRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryMultiStakingUnlocksResponse {
repeated MultiStakingUnlock unlocks = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryMultiStakingUnlockRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
string multi_staker_address = 1
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string validator_address = 2
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message QueryMultiStakingUnlockResponse {
MultiStakingUnlock unlock = 1;
bool found = 2;
}

message QueryBondWeightRequest { string denom = 1; }

message QueryBondWeightResponse {
string weight = 1 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];

bool found = 2;
}

message QueryValidatorMultiStakingCoinRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;
string validator_addr = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

message QueryValidatorMultiStakingCoinResponse { string denom = 1; }
150 changes: 150 additions & 0 deletions x/multi-staking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package keeper

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/realio-tech/multi-staking-module/x/multi-staking/types"
)

type queryServer struct {
Keeper
}

// NewMsgServerImpl returns an implementation of the bank MsgServer interface
// for the provided Keeper.
func NewQueryServerImpl(keeper Keeper) types.QueryServer {
return &queryServer{
Keeper: keeper,
}
}

var _ types.QueryServer = queryServer{}

// BondWeight implements types.QueryServer.
func (k queryServer) BondWeight(c context.Context, req *types.QueryBondWeightRequest) (*types.QueryBondWeightResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

weight, found := k.Keeper.GetBondWeight(ctx, req.Denom)

return &types.QueryBondWeightResponse{
Weight: weight,
Found: found,
}, nil
}

// MultiStakingLock implements types.QueryServer.
func (k queryServer) MultiStakingLock(c context.Context, req *types.QueryMultiStakingLockRequest) (*types.QueryMultiStakingLockResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

lockId := types.MultiStakingLockID(req.MultiStakerAddress, req.ValidatorAddress)
lock, found := k.Keeper.GetMultiStakingLock(ctx, lockId)

return &types.QueryMultiStakingLockResponse{
Lock: &lock,
Found: found,
}, nil
}

// MultiStakingLocks implements types.QueryServer.
func (k queryServer) MultiStakingLocks(c context.Context, req *types.QueryMultiStakingLocksRequest) (*types.QueryMultiStakingLocksResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)
var locks []*types.MultiStakingLock

store := ctx.KVStore(k.storeKey)
lockStore := prefix.NewStore(store, types.MultiStakingLockPrefix)

pageRes, err := query.Paginate(lockStore, req.Pagination, func(key []byte, value []byte) error {
var lock types.MultiStakingLock
err := k.cdc.Unmarshal(value, &lock)

if err != nil {
return err
}
locks = append(locks, &lock)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryMultiStakingLocksResponse{Locks: locks, Pagination: pageRes}, nil
}

// MultiStakingUnlock implements types.QueryServer.
func (k queryServer) MultiStakingUnlock(c context.Context, req *types.QueryMultiStakingUnlockRequest) (*types.QueryMultiStakingUnlockResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

unlockId := types.MultiStakingUnlockID(req.MultiStakerAddress, req.ValidatorAddress)
unlock, found := k.Keeper.GetMultiStakingUnlock(ctx, unlockId)

return &types.QueryMultiStakingUnlockResponse{
Unlock: &unlock,
Found: found,
}, nil
}

// MultiStakingUnlocks implements types.QueryServer.
func (k queryServer) MultiStakingUnlocks(c context.Context, req *types.QueryMultiStakingUnlocksRequest) (*types.QueryMultiStakingUnlocksResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)
var unlocks []*types.MultiStakingUnlock

store := ctx.KVStore(k.storeKey)
unlockStore := prefix.NewStore(store, types.MultiStakingUnlockPrefix)

pageRes, err := query.Paginate(unlockStore, req.Pagination, func(key []byte, value []byte) error {
var unlock types.MultiStakingUnlock
err := k.cdc.Unmarshal(value, &unlock)

if err != nil {
return err
}
unlocks = append(unlocks, &unlock)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryMultiStakingUnlocksResponse{Unlocks: unlocks, Pagination: pageRes}, nil
}

// ValidatorMultiStakingCoin implements types.QueryServer.
func (k queryServer) ValidatorMultiStakingCoin(c context.Context, req *types.QueryValidatorMultiStakingCoinRequest) (*types.QueryValidatorMultiStakingCoinResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

ctx := sdk.UnwrapSDKContext(c)

denom := k.Keeper.GetValidatorMultiStakingCoin(ctx, sdk.ValAddress(req.ValidatorAddr))

return &types.QueryValidatorMultiStakingCoinResponse{
Denom: denom,
}, nil
}
3 changes: 1 addition & 2 deletions x/multi-staking/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
// TODO: add query server
// types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
}

// InitGenesis initial genesis state for feeabs module
Expand Down
Loading