You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

  1. package lite
  2. import (
  3. "bytes"
  4. "github.com/pkg/errors"
  5. "github.com/tendermint/tendermint/types"
  6. liteErr "github.com/tendermint/tendermint/lite/errors"
  7. )
  8. var _ Certifier = (*StaticCertifier)(nil)
  9. // StaticCertifier assumes a static set of validators, set on
  10. // initilization and checks against them.
  11. // The signatures on every header is checked for > 2/3 votes
  12. // against the known validator set upon Certify
  13. //
  14. // Good for testing or really simple chains. Building block
  15. // to support real-world functionality.
  16. type StaticCertifier struct {
  17. chainID string
  18. vSet *types.ValidatorSet
  19. vhash []byte
  20. }
  21. // NewStaticCertifier returns a new certifier with a static validator set.
  22. func NewStaticCertifier(chainID string, vals *types.ValidatorSet) *StaticCertifier {
  23. return &StaticCertifier{
  24. chainID: chainID,
  25. vSet: vals,
  26. }
  27. }
  28. // ChainID returns the chain id.
  29. // Implements Certifier.
  30. func (sc *StaticCertifier) ChainID() string {
  31. return sc.chainID
  32. }
  33. // Validators returns the validator set.
  34. func (sc *StaticCertifier) Validators() *types.ValidatorSet {
  35. return sc.vSet
  36. }
  37. // Hash returns the hash of the validator set.
  38. func (sc *StaticCertifier) Hash() []byte {
  39. if len(sc.vhash) == 0 {
  40. sc.vhash = sc.vSet.Hash()
  41. }
  42. return sc.vhash
  43. }
  44. // Certify makes sure that the commit is valid.
  45. // Implements Certifier.
  46. func (sc *StaticCertifier) Certify(commit Commit) error {
  47. // do basic sanity checks
  48. err := commit.ValidateBasic(sc.chainID)
  49. if err != nil {
  50. return err
  51. }
  52. // make sure it has the same validator set we have (static means static)
  53. if !bytes.Equal(sc.Hash(), commit.Header.ValidatorsHash) {
  54. return liteErr.ErrValidatorsChanged()
  55. }
  56. // then make sure we have the proper signatures for this
  57. err = sc.vSet.VerifyCommit(sc.chainID, commit.Commit.BlockID,
  58. commit.Header.Height, commit.Commit)
  59. return errors.WithStack(err)
  60. }