Browse Source

lite2: improve string output of all existing providers (#4387)

before:
&http{AFBSD743A...}

after:
http{https://127.0.0.1:26657}

Co-authored-by: Marko <marbar3778@yahoo.com>
pull/4391/head
Anton Kaliaev 4 years ago
committed by GitHub
parent
commit
ab6ac6d435
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 23 deletions
  1. +6
    -0
      lite2/provider/http/http.go
  2. +33
    -0
      lite2/provider/mock/deadmock.go
  3. +17
    -22
      lite2/provider/mock/mock.go
  4. +3
    -1
      lite2/provider/provider.go
  5. +6
    -0
      rpc/client/httpclient.go

+ 6
- 0
lite2/provider/http/http.go View File

@ -13,6 +13,8 @@ import (
type SignStatusClient interface {
rpcclient.SignClient
rpcclient.StatusClient
// Remote returns the remote network address in a string form.
Remote() string
}
// http provider uses an RPC client (or SignStatusClient more generally) to
@ -45,6 +47,10 @@ func (p *http) ChainID() string {
return p.chainID
}
func (p *http) String() string {
return fmt.Sprintf("http{%s}", p.client.Remote())
}
// SignedHeader fetches a SignedHeader at the given height and checks the
// chainID matches.
func (p *http) SignedHeader(height int64) (*types.SignedHeader, error) {


+ 33
- 0
lite2/provider/mock/deadmock.go View File

@ -0,0 +1,33 @@
package mock
import (
"errors"
"github.com/tendermint/tendermint/lite2/provider"
"github.com/tendermint/tendermint/types"
)
type deadMock struct {
chainID string
}
// NewDeadMock creates a mock provider that always errors.
func NewDeadMock(chainID string) provider.Provider {
return &deadMock{chainID: chainID}
}
func (p *deadMock) ChainID() string {
return p.chainID
}
func (p *deadMock) String() string {
return "deadMock"
}
func (p *deadMock) SignedHeader(height int64) (*types.SignedHeader, error) {
return nil, errors.New("no response from provider")
}
func (p *deadMock) ValidatorSet(height int64) (*types.ValidatorSet, error) {
return nil, errors.New("no response from provider")
}

+ 17
- 22
lite2/provider/mock/mock.go View File

@ -1,7 +1,8 @@
package mock
import (
"github.com/pkg/errors"
"fmt"
"strings"
"github.com/tendermint/tendermint/lite2/provider"
"github.com/tendermint/tendermint/types"
@ -23,10 +24,25 @@ func New(chainID string, headers map[int64]*types.SignedHeader, vals map[int64]*
}
}
// ChainID returns the blockchain ID.
func (p *mock) ChainID() string {
return p.chainID
}
func (p *mock) String() string {
var headers strings.Builder
for _, h := range p.headers {
fmt.Fprintf(&headers, " %d:%X", h.Height, h.Hash())
}
var vals strings.Builder
for _, v := range p.vals {
fmt.Fprintf(&vals, " %X", v.Hash())
}
return fmt.Sprintf("mock{headers: %s, vals: %v}", headers.String(), vals.String())
}
func (p *mock) SignedHeader(height int64) (*types.SignedHeader, error) {
if height == 0 && len(p.headers) > 0 {
return p.headers[int64(len(p.headers))], nil
@ -46,24 +62,3 @@ func (p *mock) ValidatorSet(height int64) (*types.ValidatorSet, error) {
}
return nil, provider.ErrValidatorSetNotFound
}
type deadMock struct {
chainID string
}
// NewDeadMock creates a mock provider that always errors.
func NewDeadMock(chainID string) provider.Provider {
return &deadMock{chainID: chainID}
}
func (p *deadMock) ChainID() string {
return p.chainID
}
func (p *deadMock) SignedHeader(height int64) (*types.SignedHeader, error) {
return nil, errors.New("no response from provider")
}
func (p *deadMock) ValidatorSet(height int64) (*types.ValidatorSet, error) {
return nil, errors.New("no response from provider")
}

+ 3
- 1
lite2/provider/provider.go View File

@ -1,6 +1,8 @@
package provider
import "github.com/tendermint/tendermint/types"
import (
"github.com/tendermint/tendermint/types"
)
// Provider provides information for the lite client to sync (verification
// happens in the client).


+ 6
- 0
rpc/client/httpclient.go View File

@ -123,10 +123,16 @@ func NewHTTPWithClient(remote, wsEndpoint string, client *http.Client) (*HTTP, e
var _ Client = (*HTTP)(nil)
// SetLogger sets a logger.
func (c *HTTP) SetLogger(l log.Logger) {
c.WSEvents.SetLogger(l)
}
// Remote returns the remote network address in a string form.
func (c *HTTP) Remote() string {
return c.remote
}
// NewBatch creates a new batch client for this HTTP client.
func (c *HTTP) NewBatch() *BatchHTTP {
rpcBatch := c.rpc.NewRequestBatch()


Loading…
Cancel
Save