From e944a2b3fccf0bbfb8b356dff975f997fda767fa Mon Sep 17 00:00:00 2001 From: Victor Neznaykin Date: Tue, 17 Oct 2023 21:08:15 +0400 Subject: [PATCH] use limit/offset pagination when get validatorset --- client/grpc/validator.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/client/grpc/validator.go b/client/grpc/validator.go index d69c06c..21b21ea 100644 --- a/client/grpc/validator.go +++ b/client/grpc/validator.go @@ -11,7 +11,7 @@ import ( ) const ( - defaultLimit = 150 + defaultLimit = 100 ) func (c *Client) Validators(ctx context.Context, height int64) (*cometbftcoretypes.ResultValidators, error) { @@ -19,15 +19,13 @@ func (c *Client) Validators(ctx context.Context, height int64) (*cometbftcoretyp 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, }, @@ -36,10 +34,7 @@ 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) } @@ -47,13 +42,17 @@ func (c *Client) Validators(ctx context.Context, height int64) (*cometbftcoretyp 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 }