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>
This commit is contained in:
Anton Kaliaev
2020-02-11 17:30:26 +01:00
committed by GitHub
parent 9a9e8c5bb3
commit ab6ac6d435
5 changed files with 65 additions and 23 deletions

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) {

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")
}

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")
}

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).