Skip to content

Commit

Permalink
use limit/offset pagination when get validatorset
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Neznaykin committed Oct 17, 2023
1 parent c3147c3 commit e944a2b
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions client/grpc/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@ import (
)

const (
defaultLimit = 150
defaultLimit = 100
)

func (c *Client) Validators(ctx context.Context, height int64) (*cometbftcoretypes.ResultValidators, error) {
vals := &cometbftcoretypes.ResultValidators{
BlockHeight: height,
}

var (
nextKey []byte
)
var offset uint64

for {
respPb, err := c.TmsService.GetValidatorSetByHeight(ctx, &tmservice.GetValidatorSetByHeightRequest{
Height: height,
Pagination: &query.PageRequest{
Key: nextKey,
Offset: offset,
Limit: defaultLimit,
CountTotal: true,
},
Expand All @@ -36,24 +34,25 @@ func (c *Client) Validators(ctx context.Context, height int64) (*cometbftcoretyp
return nil, err
}

nextKey = respPb.Pagination.NextKey

vals.Total = int(respPb.Pagination.Total)
if len(nextKey) == 0 { // first iteration
if offset == 0 { // first iteration
vals.Validators = make([]*cometbfttypes.Validator, 0, vals.Total)
}

for _, val := range respPb.Validators {
vals.Validators = append(vals.Validators, convertValidator(val))
}

vals.Count += len(respPb.Validators)
vals.Total = int(respPb.Pagination.Total)

if len(respPb.Pagination.NextKey) == 0 {
if len(respPb.Validators) < defaultLimit {
break
}

offset += defaultLimit
}

vals.Count = len(vals.Validators)

return vals, nil
}

Expand Down

0 comments on commit e944a2b

Please sign in to comment.