Skip to content

Commit

Permalink
chore: added staker status
Browse files Browse the repository at this point in the history
  • Loading branch information
troykessler committed Dec 18, 2024
1 parent 405b806 commit 513fadd
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 68 deletions.
12 changes: 8 additions & 4 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8063,15 +8063,19 @@ paths:
status looks whether a staker is participating in pools or not

- STAKER_STATUS_UNSPECIFIED: STAKER_STATUS_UNSPECIFIED ...
- STAKER_STATUS_ACTIVE: STAKER_STATUS_ACTIVE ...
- STAKER_STATUS_INACTIVE: STAKER_STATUS_INACTIVE ...
- STAKER_STATUS_PROTOCOL_ACTIVE: STAKER_STATUS_PROTOCOL_ACTIVE ...
- STAKER_STATUS_PROTOCOL_INACTIVE: STAKER_STATUS_PROTOCOL_INACTIVE ...
- STAKER_STATUS_CHAIN_ACTIVE: STAKER_STATUS_CHAIN_ACTIVE ...
- STAKER_STATUS_CHAIN_INACTIVE: STAKER_STATUS_CHAIN_INACTIVE ...
in: query
required: false
type: string
enum:
- STAKER_STATUS_UNSPECIFIED
- STAKER_STATUS_ACTIVE
- STAKER_STATUS_INACTIVE
- STAKER_STATUS_PROTOCOL_ACTIVE
- STAKER_STATUS_PROTOCOL_INACTIVE
- STAKER_STATUS_CHAIN_ACTIVE
- STAKER_STATUS_CHAIN_INACTIVE
default: STAKER_STATUS_UNSPECIFIED
- name: search
description: search searches for moniker OR address
Expand Down
12 changes: 8 additions & 4 deletions proto/kyve/query/v1beta1/stakers.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ enum StakerStatus {

// STAKER_STATUS_UNSPECIFIED ...
STAKER_STATUS_UNSPECIFIED = 0;
// STAKER_STATUS_ACTIVE ...
STAKER_STATUS_ACTIVE = 1;
// STAKER_STATUS_INACTIVE ...
STAKER_STATUS_INACTIVE = 2;
// STAKER_STATUS_PROTOCOL_ACTIVE ...
STAKER_STATUS_PROTOCOL_ACTIVE = 1;
// STAKER_STATUS_PROTOCOL_INACTIVE ...
STAKER_STATUS_PROTOCOL_INACTIVE = 2;
// STAKER_STATUS_CHAIN_ACTIVE ...
STAKER_STATUS_CHAIN_ACTIVE = 3;
// STAKER_STATUS_CHAIN_INACTIVE ...
STAKER_STATUS_CHAIN_INACTIVE = 4;
}

// QueryStakersResponse is the response type for the Query/Stakers RPC method.
Expand Down
2 changes: 1 addition & 1 deletion x/query/keeper/grpc_query_staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k Keeper) Stakers(c context.Context, req *types.QueryStakersRequest) (*typ
var pageRes *query.PageResponse
var err error

pageRes, err = k.stakerKeeper.GetPaginatedStakersByPoolStake(ctx, req.Pagination, accumulator)
pageRes, err = k.stakerKeeper.GetPaginatedStakersByPoolStake(ctx, req.Pagination, req.Status, accumulator)

if err != nil {
return nil, err
Expand Down
118 changes: 64 additions & 54 deletions x/query/types/stakers.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 26 additions & 5 deletions x/stakers/keeper/exported_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
querytypes "github.com/KYVENetwork/chain/x/query/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"sort"
Expand Down Expand Up @@ -46,7 +47,7 @@ func (k Keeper) GetAllStakerAddressesOfPool(ctx sdk.Context, poolId uint64) (sta
return stakers
}

func (k Keeper) GetPaginatedStakersByPoolStake(ctx sdk.Context, pagination *query.PageRequest, accumulator func(validator string, accumulate bool) bool) (*query.PageResponse, error) {
func (k Keeper) GetPaginatedStakersByPoolStake(ctx sdk.Context, pagination *query.PageRequest, stakerStatus querytypes.StakerStatus, accumulator func(validator string, accumulate bool) bool) (*query.PageResponse, error) {
validators, err := k.stakingKeeper.GetBondedValidatorsByPower(ctx)
if err != nil {
return nil, err
Expand All @@ -55,13 +56,33 @@ func (k Keeper) GetPaginatedStakersByPoolStake(ctx sdk.Context, pagination *quer
addresses := make([]string, 0)

for _, validator := range validators {
addresses = append(addresses, util.MustAccountAddressFromValAddress(validator.OperatorAddress))
address := util.MustAccountAddressFromValAddress(validator.OperatorAddress)

if stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_ACTIVE && len(k.GetValaccountsFromStaker(ctx, address)) == 0 {
continue
}

if stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_INACTIVE && len(k.GetValaccountsFromStaker(ctx, address)) > 0 {
continue
}

if stakerStatus == querytypes.STAKER_STATUS_CHAIN_ACTIVE && !validator.IsBonded() {
continue
}

if stakerStatus == querytypes.STAKER_STATUS_CHAIN_INACTIVE && validator.IsBonded() {
continue
}

addresses = append(addresses, address)
}

// TODO: is this too expensive?
sort.Slice(addresses, func(i, j int) bool {
return k.GetValidatorTotalPoolStake(ctx, addresses[i]) > k.GetValidatorTotalPoolStake(ctx, addresses[j])
})
if stakerStatus == querytypes.STAKER_STATUS_UNSPECIFIED || stakerStatus == querytypes.STAKER_STATUS_PROTOCOL_ACTIVE {
sort.Slice(addresses, func(i, j int) bool {
return k.GetValidatorTotalPoolStake(ctx, addresses[i]) > k.GetValidatorTotalPoolStake(ctx, addresses[j])
})
}

pageRes, err := arrayPaginationAccumulator(addresses, pagination, accumulator)
if err != nil {
Expand Down

0 comments on commit 513fadd

Please sign in to comment.