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.

72 lines
1.9 KiB

  1. package lite
  2. import (
  3. "bytes"
  4. cmn "github.com/tendermint/tendermint/libs/common"
  5. lerr "github.com/tendermint/tendermint/lite/errors"
  6. "github.com/tendermint/tendermint/types"
  7. )
  8. var _ Verifier = (*BaseVerifier)(nil)
  9. // BaseVerifier lets us check the validity of SignedHeaders at height or
  10. // later, requiring sufficient votes (> 2/3) from the given valset.
  11. // To verify blocks produced by a blockchain with mutable validator sets,
  12. // use the DynamicVerifier.
  13. // TODO: Handle unbonding time.
  14. type BaseVerifier struct {
  15. chainID string
  16. height int64
  17. valset *types.ValidatorSet
  18. }
  19. // NewBaseVerifier returns a new Verifier initialized with a validator set at
  20. // some height.
  21. func NewBaseVerifier(chainID string, height int64, valset *types.ValidatorSet) *BaseVerifier {
  22. if valset.IsNilOrEmpty() {
  23. panic("NewBaseVerifier requires a valid valset")
  24. }
  25. return &BaseVerifier{
  26. chainID: chainID,
  27. height: height,
  28. valset: valset,
  29. }
  30. }
  31. // Implements Verifier.
  32. func (bc *BaseVerifier) ChainID() string {
  33. return bc.chainID
  34. }
  35. // Implements Verifier.
  36. func (bc *BaseVerifier) Verify(signedHeader types.SignedHeader) error {
  37. // We can't verify commits older than bc.height.
  38. if signedHeader.Height < bc.height {
  39. return cmn.NewError("BaseVerifier height is %v, cannot verify height %v",
  40. bc.height, signedHeader.Height)
  41. }
  42. // We can't verify with the wrong validator set.
  43. if !bytes.Equal(signedHeader.ValidatorsHash,
  44. bc.valset.Hash()) {
  45. return lerr.ErrUnexpectedValidators(signedHeader.ValidatorsHash, bc.valset.Hash())
  46. }
  47. // Do basic sanity checks.
  48. err := signedHeader.ValidateBasic(bc.chainID)
  49. if err != nil {
  50. return cmn.ErrorWrap(err, "in verify")
  51. }
  52. // Check commit signatures.
  53. err = bc.valset.VerifyCommit(
  54. bc.chainID, signedHeader.Commit.BlockID,
  55. signedHeader.Height, signedHeader.Commit)
  56. if err != nil {
  57. return cmn.ErrorWrap(err, "in verify")
  58. }
  59. return nil
  60. }