tendermint/lite/static_certifier.go

74 lines
1.8 KiB
Go
Raw Normal View History

2017-11-09 14:37:18 -08:00
package lite
2017-10-24 03:34:36 -07:00
import (
"bytes"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/types"
2017-11-09 14:37:18 -08:00
liteErr "github.com/tendermint/tendermint/lite/errors"
2017-10-24 03:34:36 -07:00
)
var _ Certifier = (*StaticCertifier)(nil)
2017-10-24 03:34:36 -07:00
// StaticCertifier assumes a static set of validators, set on
2017-10-24 03:34:36 -07:00
// initilization and checks against them.
// The signatures on every header is checked for > 2/3 votes
// against the known validator set upon Certify
//
// Good for testing or really simple chains. Building block
// to support real-world functionality.
type StaticCertifier struct {
2017-10-24 03:34:36 -07:00
chainID string
vSet *types.ValidatorSet
vhash []byte
}
// NewStaticCertifier returns a new certifier with a static validator set.
func NewStaticCertifier(chainID string, vals *types.ValidatorSet) *StaticCertifier {
return &StaticCertifier{
2017-10-24 03:34:36 -07:00
chainID: chainID,
vSet: vals,
}
}
// ChainID returns the chain id.
// Implements Certifier.
func (sc *StaticCertifier) ChainID() string {
return sc.chainID
2017-10-24 03:34:36 -07:00
}
// Validators returns the validator set.
func (sc *StaticCertifier) Validators() *types.ValidatorSet {
return sc.vSet
2017-10-24 03:34:36 -07:00
}
// Hash returns the hash of the validator set.
func (sc *StaticCertifier) Hash() []byte {
if len(sc.vhash) == 0 {
sc.vhash = sc.vSet.Hash()
2017-10-24 03:34:36 -07:00
}
return sc.vhash
2017-10-24 03:34:36 -07:00
}
// Certify makes sure that the commit is valid.
// Implements Certifier.
func (sc *StaticCertifier) Certify(commit Commit) error {
2017-10-24 03:34:36 -07:00
// do basic sanity checks
err := commit.ValidateBasic(sc.chainID)
2017-10-24 03:34:36 -07:00
if err != nil {
return err
}
// make sure it has the same validator set we have (static means static)
if !bytes.Equal(sc.Hash(), commit.Header.ValidatorsHash) {
2017-11-09 14:37:18 -08:00
return liteErr.ErrValidatorsChanged()
2017-10-24 03:34:36 -07:00
}
// then make sure we have the proper signatures for this
err = sc.vSet.VerifyCommit(sc.chainID, commit.Commit.BlockID,
2017-10-24 03:34:36 -07:00
commit.Header.Height, commit.Commit)
return errors.WithStack(err)
}