Skip to content

Commit

Permalink
update Validators func
Browse files Browse the repository at this point in the history
  • Loading branch information
trestinlsd committed Nov 1, 2024
1 parent a8e8b13 commit d9f397c
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 150 deletions.
6 changes: 4 additions & 2 deletions proto/exocore/operator/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,10 @@ service Query {
}
// QueryValidatorsRequest is request type for Query/Validators RPC method.
message QueryValidatorsRequest {
// status enables to query for validators matching a given status.
string status = 1;
// chain for which the cons keys are being queried. here chain_id is not used since the
// Linter complains about capitalization, which can be set with a gogoproto.custom_name but
// that is not compatible with google.api.http.get in the Query service below.
string chain = 1;

// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
Expand Down
56 changes: 27 additions & 29 deletions x/operator/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package keeper
import (
"context"
"errors"
"google.golang.org/grpc/codes"
"strings"

assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"
"google.golang.org/grpc/codes"

keytypes "github.com/ExocoreNetwork/exocore/types/keys"
avstypes "github.com/ExocoreNetwork/exocore/x/avs/types"
Expand All @@ -17,7 +16,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/status"

cosmostypes "github.com/cosmos/cosmos-sdk/x/staking/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

var _ types.QueryServer = &Keeper{}
Expand Down Expand Up @@ -259,40 +258,39 @@ func (k *Keeper) QueryOptInfo(goCtx context.Context, req *types.QueryOptInfoRequ
return k.GetOptedInfo(ctx, req.OperatorAddr, req.AvsAddress)
}

// Validators queries all validators that match the given status
func (k *Keeper) Validators(c context.Context, req *types.QueryValidatorsRequest) (*types.QueryValidatorsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

// validate the provided status, return all the validators if the status is empty
if req.Status != "" && !(req.Status == cosmostypes.Bonded.String() || req.Status == cosmostypes.Unbonded.String() || req.Status == cosmostypes.Unbonding.String()) {
return nil, status.Errorf(codes.InvalidArgument, "invalid validator status %s", req.Status)
}

ctx := sdk.UnwrapSDKContext(c)
vals := make([]stakingtypes.Validator, 0)
var chainIDWithoutRevision string

store := ctx.KVStore(k.storeKey)
valStore := prefix.NewStore(store, cosmostypes.ValidatorsKey)

validators, pageRes, err := query.GenericFilteredPaginate(k.cdc, valStore, req.Pagination, func(key []byte, val *cosmostypes.Validator) (*cosmostypes.Validator, error) {
if req.Status != "" && !strings.EqualFold(val.GetStatus().String(), req.Status) {
return nil, nil
if len(req.Chain) == 0 {
chainIDWithoutRevision = avstypes.ChainIDWithoutRevision(ctx.ChainID())
} else {
chainIDWithoutRevision = avstypes.ChainIDWithoutRevision(req.Chain)
}
chainPrefix := types.ChainIDAndAddrKey(
types.BytePrefixForChainIDAndOperatorToConsKey,
chainIDWithoutRevision, nil,
)
store := prefix.NewStore(ctx.KVStore(k.storeKey), chainPrefix)
pageRes, err := query.Paginate(store, req.Pagination, func(_ []byte, value []byte) error {
ret := &tmprotocrypto.PublicKey{}
// don't use MustUnmarshal to not panic for queries
if err := ret.Unmarshal(value); err != nil {
return err
}
val, found := k.ValidatorByConsAddrForChainID(
ctx, keytypes.NewWrappedConsKeyFromTmProtoKey(ret).ToConsAddr(), avstypes.ChainIDWithoutRevision(ctx.ChainID()),
)
if found {
vals = append(vals, val)
}

return val, nil
}, func() *cosmostypes.Validator {
return &cosmostypes.Validator{}
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

vals := cosmostypes.Validators{}
for _, val := range validators {
vals = append(vals, *val)
return nil, err
}

return &types.QueryValidatorsResponse{Validators: vals, Pagination: pageRes}, nil
}

Expand Down
Loading

0 comments on commit d9f397c

Please sign in to comment.