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.

71 lines
1.7 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 = &Static{}
  9. // Static 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 Static struct {
  17. chainID string
  18. vSet *types.ValidatorSet
  19. vhash []byte
  20. }
  21. // NewStatic returns a new certifier with a static validator set.
  22. func NewStatic(chainID string, vals *types.ValidatorSet) *Static {
  23. return &Static{
  24. chainID: chainID,
  25. vSet: vals,
  26. }
  27. }
  28. // ChainID returns the chain id.
  29. func (c *Static) ChainID() string {
  30. return c.chainID
  31. }
  32. // Validators returns the validator set.
  33. func (c *Static) Validators() *types.ValidatorSet {
  34. return c.vSet
  35. }
  36. // Hash returns the hash of the validator set.
  37. func (c *Static) Hash() []byte {
  38. if len(c.vhash) == 0 {
  39. c.vhash = c.vSet.Hash()
  40. }
  41. return c.vhash
  42. }
  43. // Certify makes sure that the commit is valid.
  44. func (c *Static) Certify(commit Commit) error {
  45. // do basic sanity checks
  46. err := commit.ValidateBasic(c.chainID)
  47. if err != nil {
  48. return err
  49. }
  50. // make sure it has the same validator set we have (static means static)
  51. if !bytes.Equal(c.Hash(), commit.Header.ValidatorsHash) {
  52. return liteErr.ErrValidatorsChanged()
  53. }
  54. // then make sure we have the proper signatures for this
  55. err = c.vSet.VerifyCommit(c.chainID, commit.Commit.BlockID,
  56. commit.Header.Height, commit.Commit)
  57. return errors.WithStack(err)
  58. }