tendermint/lite/client/provider.go

129 lines
3.7 KiB
Go
Raw Normal View History

2017-10-24 03:34:36 -07:00
/*
Package client defines a provider that uses a rpcclient
to get information, which is used to get new headers
and validators directly from a Tendermint client.
2017-10-24 03:34:36 -07:00
*/
package client
import (
"fmt"
2017-10-24 03:34:36 -07:00
rpcclient "github.com/tendermint/tendermint/rpc/client"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/types"
2017-11-09 14:37:18 -08:00
"github.com/tendermint/tendermint/lite"
lerr "github.com/tendermint/tendermint/lite/errors"
2017-10-24 03:34:36 -07:00
)
// SignStatusClient combines a SignClient and StatusClient.
2017-10-24 03:34:36 -07:00
type SignStatusClient interface {
rpcclient.SignClient
rpcclient.StatusClient
}
type provider struct {
chainID string
client SignStatusClient
2017-10-24 03:34:36 -07:00
}
// NewProvider implements Provider (but not PersistentProvider).
func NewProvider(chainID string, client SignStatusClient) lite.Provider {
return &provider{chainID: chainID, client: client}
2017-10-24 03:34:36 -07:00
}
2017-11-09 14:37:18 -08:00
// NewHTTPProvider can connect to a tendermint json-rpc endpoint
2017-10-24 03:34:36 -07:00
// at the given url, and uses that as a read-only provider.
func NewHTTPProvider(chainID, remote string) lite.Provider {
2017-10-24 03:34:36 -07:00
return &provider{
chainID: chainID,
client: rpcclient.NewHTTP(remote, "/websocket"),
2017-10-24 03:34:36 -07:00
}
}
// StatusClient returns the internal client as a StatusClient
2017-11-11 16:43:16 -08:00
func (p *provider) StatusClient() rpcclient.StatusClient {
return p.client
2017-11-11 16:43:16 -08:00
}
// LatestFullCommit implements Provider.
func (p *provider) LatestFullCommit(chainID string, minHeight, maxHeight int64) (fc lite.FullCommit, err error) {
if chainID != p.chainID {
err = fmt.Errorf("expected chainID %s, got %s", p.chainID, chainID)
return
2017-10-24 03:34:36 -07:00
}
if maxHeight != 0 && maxHeight < minHeight {
2018-06-19 23:55:15 -07:00
err = fmt.Errorf("need maxHeight == 0 or minHeight <= maxHeight, got min %v and max %v",
minHeight, maxHeight)
return
2017-10-24 03:34:36 -07:00
}
commit, err := p.fetchLatestCommit(minHeight, maxHeight)
2017-10-24 03:34:36 -07:00
if err != nil {
return
2017-10-24 03:34:36 -07:00
}
fc, err = p.fillFullCommit(commit.SignedHeader)
return
2017-10-24 03:34:36 -07:00
}
// fetchLatestCommit fetches the latest commit from the client.
func (p *provider) fetchLatestCommit(minHeight int64, maxHeight int64) (*ctypes.ResultCommit, error) {
status, err := p.client.Status()
2017-10-24 03:34:36 -07:00
if err != nil {
return nil, err
2017-10-24 03:34:36 -07:00
}
if status.SyncInfo.LatestBlockHeight < minHeight {
err = fmt.Errorf("provider is at %v but require minHeight=%v",
status.SyncInfo.LatestBlockHeight, minHeight)
2017-10-24 03:34:36 -07:00
return nil, err
}
if maxHeight == 0 {
maxHeight = status.SyncInfo.LatestBlockHeight
} else if status.SyncInfo.LatestBlockHeight < maxHeight {
maxHeight = status.SyncInfo.LatestBlockHeight
}
return p.client.Commit(&maxHeight)
2017-10-24 03:34:36 -07:00
}
// Implements Provider.
func (p *provider) ValidatorSet(chainID string, height int64) (valset *types.ValidatorSet, err error) {
return p.getValidatorSet(chainID, height)
}
func (p *provider) getValidatorSet(chainID string, height int64) (valset *types.ValidatorSet, err error) {
if chainID != p.chainID {
err = fmt.Errorf("expected chainID %s, got %s", p.chainID, chainID)
return
}
if height < 1 {
2018-06-19 23:55:15 -07:00
err = fmt.Errorf("expected height >= 1, got height %v", height)
return
}
heightPtr := new(int64)
*heightPtr = height
res, err := p.client.Validators(heightPtr)
2017-10-24 03:34:36 -07:00
if err != nil {
// TODO pass through other types of errors.
return nil, lerr.ErrMissingValidators(chainID, height)
2017-10-24 03:34:36 -07:00
}
valset = types.NewValidatorSet(res.Validators)
return
2017-10-24 03:34:36 -07:00
}
// This does no validation.
func (p *provider) fillFullCommit(signedHeader types.SignedHeader) (fc lite.FullCommit, err error) {
2017-10-24 03:34:36 -07:00
// Get the validators.
valset, err := p.getValidatorSet(signedHeader.ChainID, signedHeader.Height)
2017-10-24 03:34:36 -07:00
if err != nil {
return lite.FullCommit{}, err
2017-10-24 03:34:36 -07:00
}
// Get the next validators.
2018-06-19 23:55:15 -07:00
nextValset, err := p.getValidatorSet(signedHeader.ChainID, signedHeader.Height+1)
if err != nil {
return lite.FullCommit{}, err
2017-10-24 03:34:36 -07:00
}
2018-06-25 16:31:59 -07:00
return lite.NewFullCommit(signedHeader, valset, nextValset), nil
2017-10-24 03:34:36 -07:00
}