Skip to content

Commit

Permalink
Merge pull request #128 from bro-n-bro/103-missed-pagination-in-valid…
Browse files Browse the repository at this point in the history
…ator_voting_power-table

103 fix pagination for validator voting power
  • Loading branch information
malekvictor authored Oct 18, 2023
2 parents c3147c3 + e944a2b commit 82eac15
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 82eac15

Please sign in to comment.