|
@ -14,10 +14,12 @@ import ( |
|
|
"github.com/tendermint/tendermint/types" |
|
|
"github.com/tendermint/tendermint/types" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// This is very brittle, see: https://github.com/tendermint/tendermint/issues/4740
|
|
|
|
|
|
var ( |
|
|
var ( |
|
|
|
|
|
// This is very brittle, see: https://github.com/tendermint/tendermint/issues/4740
|
|
|
regexpMissingHeight = regexp.MustCompile(`height \d+ (must be less than or equal to|is not available)`) |
|
|
regexpMissingHeight = regexp.MustCompile(`height \d+ (must be less than or equal to|is not available)`) |
|
|
maxRetryAttempts = 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maxRetryAttempts = 10 |
|
|
|
|
|
timeout = 5 * time.Second |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// http provider uses an RPC client to obtain the necessary information.
|
|
|
// http provider uses an RPC client to obtain the necessary information.
|
|
@ -28,14 +30,14 @@ type http struct { |
|
|
|
|
|
|
|
|
// New creates a HTTP provider, which is using the rpchttp.HTTP client under
|
|
|
// New creates a HTTP provider, which is using the rpchttp.HTTP client under
|
|
|
// the hood. If no scheme is provided in the remote URL, http will be used by
|
|
|
// the hood. If no scheme is provided in the remote URL, http will be used by
|
|
|
// default.
|
|
|
|
|
|
|
|
|
// default. The 5s timeout is used for all requests.
|
|
|
func New(chainID, remote string) (provider.Provider, error) { |
|
|
func New(chainID, remote string) (provider.Provider, error) { |
|
|
// Ensure URL scheme is set (default HTTP) when not provided.
|
|
|
// Ensure URL scheme is set (default HTTP) when not provided.
|
|
|
if !strings.Contains(remote, "://") { |
|
|
if !strings.Contains(remote, "://") { |
|
|
remote = "http://" + remote |
|
|
remote = "http://" + remote |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
httpClient, err := rpchttp.New(remote, "/websocket") |
|
|
|
|
|
|
|
|
httpClient, err := rpchttp.NewWithTimeout(remote, "/websocket", timeout) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
@ -93,15 +95,21 @@ func (p *http) ReportEvidence(ctx context.Context, ev types.Evidence) error { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (p *http) validatorSet(ctx context.Context, height *int64) (*types.ValidatorSet, error) { |
|
|
func (p *http) validatorSet(ctx context.Context, height *int64) (*types.ValidatorSet, error) { |
|
|
|
|
|
// Since the malicious node could report a massive number of pages, making us
|
|
|
|
|
|
// spend a considerable time iterating, we restrict the number of pages here.
|
|
|
|
|
|
// => 10000 validators max
|
|
|
|
|
|
const maxPages = 100 |
|
|
|
|
|
|
|
|
var ( |
|
|
var ( |
|
|
maxPerPage = 100 |
|
|
|
|
|
vals = []*types.Validator{} |
|
|
|
|
|
page = 1 |
|
|
|
|
|
|
|
|
perPage = 100 |
|
|
|
|
|
vals = []*types.Validator{} |
|
|
|
|
|
page = 1 |
|
|
|
|
|
total = -1 |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
for len(vals)%maxPerPage == 0 { |
|
|
|
|
|
|
|
|
for len(vals) != total && page <= maxPages { |
|
|
for attempt := 1; attempt <= maxRetryAttempts; attempt++ { |
|
|
for attempt := 1; attempt <= maxRetryAttempts; attempt++ { |
|
|
res, err := p.client.Validators(ctx, height, &page, &maxPerPage) |
|
|
|
|
|
|
|
|
res, err := p.client.Validators(ctx, height, &page, &perPage) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
// TODO: standardize errors on the RPC side
|
|
|
// TODO: standardize errors on the RPC side
|
|
|
if regexpMissingHeight.MatchString(err.Error()) { |
|
|
if regexpMissingHeight.MatchString(err.Error()) { |
|
@ -115,18 +123,28 @@ func (p *http) validatorSet(ctx context.Context, height *int64) (*types.Validato |
|
|
time.Sleep(backoffTimeout(uint16(attempt))) |
|
|
time.Sleep(backoffTimeout(uint16(attempt))) |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
if len(res.Validators) == 0 { // no more validators left
|
|
|
|
|
|
valSet, err := types.ValidatorSetFromExistingValidators(vals) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, provider.ErrBadLightBlock{Reason: err} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Validate response.
|
|
|
|
|
|
if len(res.Validators) == 0 { |
|
|
|
|
|
return nil, provider.ErrBadLightBlock{ |
|
|
|
|
|
Reason: fmt.Errorf("validator set is empty (height: %d, page: %d, per_page: %d)", |
|
|
|
|
|
height, page, perPage), |
|
|
} |
|
|
} |
|
|
return valSet, nil |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
if res.Total <= 0 { |
|
|
|
|
|
return nil, provider.ErrBadLightBlock{ |
|
|
|
|
|
Reason: fmt.Errorf("total number of vals is <= 0: %d (height: %d, page: %d, per_page: %d)", |
|
|
|
|
|
res.Total, height, page, perPage), |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
total = res.Total |
|
|
vals = append(vals, res.Validators...) |
|
|
vals = append(vals, res.Validators...) |
|
|
page++ |
|
|
page++ |
|
|
break |
|
|
break |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
valSet, err := types.ValidatorSetFromExistingValidators(vals) |
|
|
valSet, err := types.ValidatorSetFromExistingValidators(vals) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, provider.ErrBadLightBlock{Reason: err} |
|
|
return nil, provider.ErrBadLightBlock{Reason: err} |
|
|