Skip to content

Commit

Permalink
feat: refactor historical validators pagination (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Oct 1, 2023
1 parent 3cfa20d commit a0304e4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
10 changes: 0 additions & 10 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ slashing-params = 300
slashing-params = 300
# Queries pagination params.
[[chains.pagination]]
# How many historical validators to query at once. Why you might need this:
# When querying for historical validators on chains with more than 100 validators, you need to do multiple queries,
# as Tendermint returns 100 validators at max. If your chain has exactly 100 validators, querying first batch
# would work, but second one won't with the following error: "page should be within [1, 1] range, given 2"
# meaning that asking for a second batch of validators of 100 entries on a chain with 100 validators
# won't work. If your chain has 100 validators, consider setting it to something like 95, so first batch
# would return 95 validators, and the second one would return 5 validators.
# Do not try setting it to more than 100, this won't work.
# Defaults to 100.
historical-validators = 95
# How many blocks to query at once.
# Defaults to 100.
# Decrease it if the node you're querying has rate limiting and doing too many requests at once
Expand Down
7 changes: 3 additions & 4 deletions pkg/config/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
)

type ChainPagination struct {
HistoricalValidators int `default:"100" toml:"historical-validators"`
BlocksSearch int `default:"100" toml:"blocks-search"`
ValidatorsList uint64 `default:"1000" toml:"validators-list"`
SigningInfos uint64 `default:"1000" toml:"signing-infos"`
BlocksSearch int `default:"100" toml:"blocks-search"`
ValidatorsList uint64 `default:"1000" toml:"validators-list"`
SigningInfos uint64 `default:"1000" toml:"signing-infos"`
}
type ChainConfig struct {
Name string `toml:"name"`
Expand Down
14 changes: 11 additions & 3 deletions pkg/tendermint/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"main/pkg/utils"
"net/http"
"net/url"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -239,9 +240,8 @@ func (rpc *RPC) GetActiveSetAtBlock(height int64) (map[string]bool, error) {

for {
queryURL := fmt.Sprintf(
"/validators?height=%d&per_page=%d&page=%d",
"/validators?height=%d&per_page=100&page=%d",
height,
rpc.config.Pagination.HistoricalValidators,
page,
)

Expand All @@ -265,11 +265,19 @@ func (rpc *RPC) GetActiveSetAtBlock(height int64) (map[string]bool, error) {
return nil, err
}

validatorsCount, err := strconv.Atoi(response.Result.Total)
if err != nil {
rpc.logger.Warn().
Err(err).
Msg("Error parsing validators count from response")
return nil, err
}

for _, validator := range response.Result.Validators {
activeSetMap[validator.Address] = true
}

if len(response.Result.Validators) < rpc.config.Pagination.HistoricalValidators {
if len(activeSetMap) >= validatorsCount {
break
}

Expand Down

0 comments on commit a0304e4

Please sign in to comment.