mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 22:44:24 +00:00
rpc/client: take context as first param (#5347)
Closes #5145 also applies to light/client
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
@@ -61,18 +62,18 @@ func (p *http) String() string {
|
||||
|
||||
// LightBlock fetches a LightBlock at the given height and checks the
|
||||
// chainID matches.
|
||||
func (p *http) LightBlock(height int64) (*types.LightBlock, error) {
|
||||
func (p *http) LightBlock(ctx context.Context, height int64) (*types.LightBlock, error) {
|
||||
h, err := validateHeight(height)
|
||||
if err != nil {
|
||||
return nil, provider.ErrBadLightBlock{Reason: err}
|
||||
}
|
||||
|
||||
sh, err := p.signedHeader(h)
|
||||
sh, err := p.signedHeader(ctx, h)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
vs, err := p.validatorSet(h)
|
||||
vs, err := p.validatorSet(ctx, h)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -91,12 +92,12 @@ func (p *http) LightBlock(height int64) (*types.LightBlock, error) {
|
||||
}
|
||||
|
||||
// ReportEvidence calls `/broadcast_evidence` endpoint.
|
||||
func (p *http) ReportEvidence(ev types.Evidence) error {
|
||||
_, err := p.client.BroadcastEvidence(ev)
|
||||
func (p *http) ReportEvidence(ctx context.Context, ev types.Evidence) error {
|
||||
_, err := p.client.BroadcastEvidence(ctx, ev)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *http) validatorSet(height *int64) (*types.ValidatorSet, error) {
|
||||
func (p *http) validatorSet(ctx context.Context, height *int64) (*types.ValidatorSet, error) {
|
||||
var (
|
||||
maxPerPage = 100
|
||||
vals = []*types.Validator{}
|
||||
@@ -105,7 +106,7 @@ func (p *http) validatorSet(height *int64) (*types.ValidatorSet, error) {
|
||||
|
||||
for len(vals)%maxPerPage == 0 {
|
||||
for attempt := 1; attempt <= maxRetryAttempts; attempt++ {
|
||||
res, err := p.client.Validators(height, &page, &maxPerPage)
|
||||
res, err := p.client.Validators(ctx, height, &page, &maxPerPage)
|
||||
if err != nil {
|
||||
// TODO: standardize errors on the RPC side
|
||||
if regexpMissingHeight.MatchString(err.Error()) {
|
||||
@@ -138,9 +139,9 @@ func (p *http) validatorSet(height *int64) (*types.ValidatorSet, error) {
|
||||
return valSet, nil
|
||||
}
|
||||
|
||||
func (p *http) signedHeader(height *int64) (*types.SignedHeader, error) {
|
||||
func (p *http) signedHeader(ctx context.Context, height *int64) (*types.SignedHeader, error) {
|
||||
for attempt := 1; attempt <= maxRetryAttempts; attempt++ {
|
||||
commit, err := p.client.Commit(height)
|
||||
commit, err := p.client.Commit(ctx, height)
|
||||
if err != nil {
|
||||
// TODO: standardize errors on the RPC side
|
||||
if regexpMissingHeight.MatchString(err.Error()) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package http_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -65,7 +66,7 @@ func TestProvider(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// let's get the highest block
|
||||
sh, err := p.LightBlock(0)
|
||||
sh, err := p.LightBlock(context.Background(), 0)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, sh.Height < 1000)
|
||||
|
||||
@@ -74,16 +75,16 @@ func TestProvider(t *testing.T) {
|
||||
|
||||
// historical queries now work :)
|
||||
lower := sh.Height - 3
|
||||
sh, err = p.LightBlock(lower)
|
||||
sh, err = p.LightBlock(context.Background(), lower)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, lower, sh.Height)
|
||||
|
||||
// fetching missing heights (both future and pruned) should return appropriate errors
|
||||
_, err = p.LightBlock(1000)
|
||||
_, err = p.LightBlock(context.Background(), 1000)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, provider.ErrLightBlockNotFound, err)
|
||||
|
||||
_, err = p.LightBlock(1)
|
||||
_, err = p.LightBlock(context.Background(), 1)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, provider.ErrLightBlockNotFound, err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/tendermint/tendermint/light/provider"
|
||||
@@ -22,10 +23,10 @@ func (p *deadMock) ChainID() string { return p.chainID }
|
||||
|
||||
func (p *deadMock) String() string { return "deadMock" }
|
||||
|
||||
func (p *deadMock) LightBlock(height int64) (*types.LightBlock, error) {
|
||||
func (p *deadMock) LightBlock(_ context.Context, height int64) (*types.LightBlock, error) {
|
||||
return nil, errNoResp
|
||||
}
|
||||
|
||||
func (p *deadMock) ReportEvidence(ev types.Evidence) error {
|
||||
func (p *deadMock) ReportEvidence(_ context.Context, ev types.Evidence) error {
|
||||
return errNoResp
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -48,7 +49,7 @@ func (p *Mock) String() string {
|
||||
return fmt.Sprintf("Mock{headers: %s, vals: %v}", headers.String(), vals.String())
|
||||
}
|
||||
|
||||
func (p *Mock) LightBlock(height int64) (*types.LightBlock, error) {
|
||||
func (p *Mock) LightBlock(_ context.Context, height int64) (*types.LightBlock, error) {
|
||||
var lb *types.LightBlock
|
||||
if height == 0 && len(p.headers) > 0 {
|
||||
sh := p.headers[int64(len(p.headers))]
|
||||
@@ -79,7 +80,7 @@ func (p *Mock) LightBlock(height int64) (*types.LightBlock, error) {
|
||||
return lb, nil
|
||||
}
|
||||
|
||||
func (p *Mock) ReportEvidence(ev types.Evidence) error {
|
||||
func (p *Mock) ReportEvidence(_ context.Context, ev types.Evidence) error {
|
||||
p.evidenceToReport[string(ev.Hash())] = ev
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
@@ -20,8 +22,8 @@ type Provider interface {
|
||||
// issues, an error will be returned.
|
||||
// If there's no LightBlock for the given height, ErrLightBlockNotFound
|
||||
// error is returned.
|
||||
LightBlock(height int64) (*types.LightBlock, error)
|
||||
LightBlock(ctx context.Context, height int64) (*types.LightBlock, error)
|
||||
|
||||
// ReportEvidence reports an evidence of misbehavior.
|
||||
ReportEvidence(ev types.Evidence) error
|
||||
ReportEvidence(context.Context, types.Evidence) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user