refactor multiprovider

This commit is contained in:
Anton Kaliaev 2019-08-15 12:09:38 +04:00
parent 46c3d0baec
commit f9061f11e6
No known key found for this signature in database
GPG Key ID: 7B6881D965918214

View File

@ -6,17 +6,15 @@ import (
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
) )
var _ PersistentProvider = (*multiProvider)(nil)
// multiProvider allows you to place one or more caches in front of a source // multiProvider allows you to place one or more caches in front of a source
// Provider. It runs through them in order until a match is found. // Provider. It runs through them in order until a match is found.
type multiProvider struct { type multiProvider struct {
logger log.Logger logger log.Logger
providers []PersistentProvider providers []PersistentProvider
} }
// NewMultiProvider returns a new provider which wraps multiple other providers. // NewMultiProvider returns a new provider which wraps multiple other providers.
func NewMultiProvider(providers ...PersistentProvider) *multiProvider { func NewMultiProvider(providers ...PersistentProvider) PersistentProvider {
return &multiProvider{ return &multiProvider{
logger: log.NewNopLogger(), logger: log.NewNopLogger(),
providers: providers, providers: providers,
@ -47,8 +45,8 @@ func (mc *multiProvider) SaveFullCommit(fc FullCommit) (err error) {
// Returns the first error encountered. // Returns the first error encountered.
func (mc *multiProvider) LatestFullCommit(chainID string, minHeight, maxHeight int64) (fc FullCommit, err error) { func (mc *multiProvider) LatestFullCommit(chainID string, minHeight, maxHeight int64) (fc FullCommit, err error) {
for _, p := range mc.providers { for _, p := range mc.providers {
var fc_ FullCommit var pfc FullCommit
fc_, err = p.LatestFullCommit(chainID, minHeight, maxHeight) pfc, err = p.LatestFullCommit(chainID, minHeight, maxHeight)
if lerr.IsErrCommitNotFound(err) { if lerr.IsErrCommitNotFound(err) {
err = nil err = nil
continue continue
@ -56,18 +54,20 @@ func (mc *multiProvider) LatestFullCommit(chainID string, minHeight, maxHeight i
return return
} }
if fc == (FullCommit{}) { if fc == (FullCommit{}) {
fc = fc_ fc = pfc
} else if fc_.Height() > fc.Height() { } else if pfc.Height() > fc.Height() {
fc = fc_ fc = pfc
} }
if fc.Height() == maxHeight { if fc.Height() == maxHeight {
return return
} }
} }
if fc == (FullCommit{}) { if fc == (FullCommit{}) {
err = lerr.ErrCommitNotFound() err = lerr.ErrCommitNotFound()
return return
} }
return return
} }
@ -76,10 +76,11 @@ func (mc *multiProvider) LatestFullCommit(chainID string, minHeight, maxHeight i
func (mc *multiProvider) ValidatorSet(chainID string, height int64) (valset *types.ValidatorSet, err error) { func (mc *multiProvider) ValidatorSet(chainID string, height int64) (valset *types.ValidatorSet, err error) {
for _, p := range mc.providers { for _, p := range mc.providers {
valset, err = p.ValidatorSet(chainID, height) valset, err = p.ValidatorSet(chainID, height)
if err == nil { if lerr.IsErrUnknownValidators(err) {
// TODO Log unexpected types of errors. err = nil
return valset, nil continue
} }
return
} }
return nil, lerr.ErrUnknownValidators(chainID, height) return nil, lerr.ErrUnknownValidators(chainID, height)
} }