question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to paginate queries in StakingExtension/validatorDelegations

See original GitHub issue

Since fetching all the validator delegations is a very intensive method, trying to get all values directly usually results in an error, as already noted here https://github.com/cosmos/cosmjs/issues/1049.

I’m trying to get a paginated result with Stargate, StakingExtension, and the method validatorDelegations but I don’t know what should I pass as the paginationKey parameter.

According to the docs, paginationKey should be an Uint8Array, but what should I set as my first paginationKey, when I have none? I have been trying by setting paginationKey as an object with pagination information, null, ‘’, etc. without result. All the time it tries to get all the results directly which means that the pagination is not working and the node is not able to answer this request.

This is my code:

const { StargateClient } = require('@cosmjs/stargate')
const provider = 'https://cosmoshub-rpc.stakely.io/'
const client = await StargateClient.connect(provider)
const validatorAdress = 'cosmosvaloper16yupepagywvlk7uhpfchtwa0stu5f8cyhh54f2'

...
// Test 1
const pagination = '' // Timeout, pagination not working
// Test 2
const pagination = null // Timeout, pagination not working
// Test 3 
const pagination = {
      countTotal: false,
      key: null,
      limit: 50,
      reverse: false,
} // Timeout, pagination not working
// Test 4
const pagination = new Uint8Array() // Timeout, pagination not working

const queryData = await client.queryClient.staking.validatorDelegations(validatorAdress, pagination)
console.log(queryData )

This would be the request using the LCD, which works fine: https://cosmoshub-lcd.stakely.io/cosmos/staking/v1beta1/validators/cosmosvaloper16yupepagywvlk7uhpfchtwa0stu5f8cyhh54f2/delegations?pagination.limit=50

Thanks in advance.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
iicc1commented, Aug 17, 2022

I have tried the query against one of our nodes that is deployed on a really powerful dedicated server and it times out.

0reactions
webmaster128commented, Aug 17, 2022

This is strange. There is this code in Cosmos SDK v0.45.6:

// DefaultLimit is the default `limit` for queries
// if the `limit` is not supplied, paginate will use `DefaultLimit`
const DefaultLimit = 100

and

func FilteredPaginate(
	prefixStore types.KVStore,
	pageRequest *PageRequest,
	onResult func(key []byte, value []byte, accumulate bool) (bool, error),
) (*PageResponse, error) {

	// if the PageRequest is nil, use default PageRequest
	if pageRequest == nil {
		pageRequest = &PageRequest{}
	}

	offset := pageRequest.Offset
	key := pageRequest.Key
	limit := pageRequest.Limit
	countTotal := pageRequest.CountTotal
	reverse := pageRequest.Reverse

	if offset > 0 && key != nil {
		return nil, fmt.Errorf("invalid request, either offset or key is expected, got both")
	}

	if limit == 0 {
		limit = DefaultLimit

		// count total results when the limit is zero/not supplied
		countTotal = true
	}

which is used in

// ValidatorDelegations queries delegate info for given validator
func (k Querier) ValidatorDelegations(c context.Context, req *types.QueryValidatorDelegationsRequest) (*types.QueryValidatorDelegationsResponse, error) {
	if req == nil {
		return nil, status.Error(codes.InvalidArgument, "empty request")
	}

	if req.ValidatorAddr == "" {
		return nil, status.Error(codes.InvalidArgument, "validator address cannot be empty")
	}
	var delegations []types.Delegation
	ctx := sdk.UnwrapSDKContext(c)

	store := ctx.KVStore(k.storeKey)
	valStore := prefix.NewStore(store, types.DelegationKey)
	pageRes, err := query.FilteredPaginate(valStore, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
		delegation, err := types.UnmarshalDelegation(k.cdc, value)
		if err != nil {
			return false, err
		}

		valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddr)
		if err != nil {
			return false, err
		}

		if !delegation.GetValidatorAddr().Equals(valAddr) {
			return false, nil
		}

		if accumulate {
			delegations = append(delegations, delegation)
		}
		return true, nil
	})
	if err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}

	delResponses, err := DelegationsToDelegationResponses(ctx, k.Keeper, delegations)
	if err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}

	return &types.QueryValidatorDelegationsResponse{
		DelegationResponses: delResponses, Pagination: pageRes}, nil
}

But maybe thos 100 is even too much because the query implementation is inefficient?

Can you test this on your own node?

See also https://github.com/cosmos/cosmjs/issues/1209 and https://github.com/cosmos/cosmos-sdk/issues/12756

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't use paginate on a query result - cakephp - Stack Overflow
It means that you are passing the wrong type of object. Pagination on result sets is not supported, only tables (either objects or...
Read more >
Paginate Results | CockroachDB Docs
Paginate results from queries against your cluster. ... The general pattern for keyset pagination queries is: SELECT * FROM t AS OF SYSTEM...
Read more >
Paginating table query results - Amazon DynamoDB
DynamoDB paginates the results from Query operations. With pagination, the Query results are divided into "pages" of data that are 1 MB in...
Read more >
Paginate data with query cursors | Firestore - Firebase
Your browser can't play this video. ... With query cursors in Cloud Firestore, you can split data returned by a query into ......
Read more >
Data retrieval guidance for paginated reports - Power BI
It's done by passing the UserID built-in field to a dataset query parameter. The data source will need to store User Principal Name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found