mirror of
https://github.com/tendermint/tendermint.git
synced 2026-05-31 19:36:20 +00:00
lite: fix HTTP provider error handling (#4882)
* lite: fix HTTP provider error handling Fixes #4739, kind of. See #4740 for the proper fix. --- For contributor use: - [x] Wrote tests - [x] Updated CHANGELOG_PENDING.md - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [x] Updated relevant documentation (`docs/`) and code comments - [x] Re-reviewed `Files changed` in the Github PR explorer - [x] Applied Appropriate Labels * adapt tests to missing pull request Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
This commit is contained in:
@@ -30,3 +30,5 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
|
||||
- race: pass -race to go build and enable data race detection.
|
||||
|
||||
### BUG FIXES:
|
||||
|
||||
- [light] [\#4741](https://github.com/tendermint/tendermint/pull/4741) Correctly return `ErrSignedHeaderNotFound` and `ErrValidatorSetNotFound` on corresponding RPC errors (@erikgrinaker)
|
||||
|
||||
@@ -3,6 +3,7 @@ package http
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/tendermint/tendermint/lite2/provider"
|
||||
@@ -11,6 +12,9 @@ import (
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// This is very brittle, see: https://github.com/tendermint/tendermint/issues/4740
|
||||
var regexpMissingHeight = regexp.MustCompile(`height (must be less than or equal to|\d+ is not available)`)
|
||||
|
||||
// SignStatusClient combines a SignClient and StatusClient.
|
||||
type SignStatusClient interface {
|
||||
rpcclient.SignClient
|
||||
@@ -70,7 +74,7 @@ func (p *http) SignedHeader(height int64) (*types.SignedHeader, error) {
|
||||
commit, err := p.SignStatusClient.Commit(h)
|
||||
if err != nil {
|
||||
// TODO: standartise errors on the RPC side
|
||||
if strings.Contains(err.Error(), "height must be less than or equal") {
|
||||
if regexpMissingHeight.MatchString(err.Error()) {
|
||||
return nil, provider.ErrSignedHeaderNotFound
|
||||
}
|
||||
return nil, err
|
||||
@@ -100,7 +104,7 @@ func (p *http) ValidatorSet(height int64) (*types.ValidatorSet, error) {
|
||||
res, err := p.SignStatusClient.Validators(h, 0, maxPerPage)
|
||||
if err != nil {
|
||||
// TODO: standartise errors on the RPC side
|
||||
if strings.Contains(err.Error(), "height must be less than or equal") {
|
||||
if regexpMissingHeight.MatchString(err.Error()) {
|
||||
return nil, provider.ErrValidatorSetNotFound
|
||||
}
|
||||
return nil, err
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/tendermint/tendermint/abci/example/kvstore"
|
||||
"github.com/tendermint/tendermint/lite2/provider"
|
||||
"github.com/tendermint/tendermint/lite2/provider/http"
|
||||
litehttp "github.com/tendermint/tendermint/lite2/provider/http"
|
||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||
@@ -32,6 +33,7 @@ func TestNewProvider(t *testing.T) {
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
app := kvstore.NewApplication()
|
||||
app.RetainBlocks = 5
|
||||
node := rpctest.StartTendermint(app)
|
||||
|
||||
code := m.Run()
|
||||
@@ -68,8 +70,25 @@ func TestProvider(t *testing.T) {
|
||||
assert.Nil(t, sh.ValidateBasic(chainID))
|
||||
|
||||
// historical queries now work :)
|
||||
lower := sh.Height - 5
|
||||
lower := sh.Height - 3
|
||||
sh, err = p.SignedHeader(lower)
|
||||
assert.Nil(t, err, "%+v", err)
|
||||
assert.Equal(t, lower, sh.Height)
|
||||
|
||||
// fetching missing heights (both future and pruned) should return appropriate errors
|
||||
_, err = p.SignedHeader(1000)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, provider.ErrSignedHeaderNotFound, err)
|
||||
|
||||
_, err = p.ValidatorSet(1000)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, provider.ErrValidatorSetNotFound, err)
|
||||
|
||||
_, err = p.SignedHeader(1)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, provider.ErrSignedHeaderNotFound, err)
|
||||
|
||||
_, err = p.ValidatorSet(1)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, provider.ErrValidatorSetNotFound, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user