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.

78 lines
2.1 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 (bv *BaseVerifier) ChainID() string {
  33. return bv.chainID
  34. }
  35. // Implements Verifier.
  36. func (bv *BaseVerifier) Verify(signedHeader types.SignedHeader) error {
  37. // We can't verify commits for a different chain.
  38. if signedHeader.ChainID != bv.chainID {
  39. return cmn.NewError("BaseVerifier chainID is %v, cannot verify chainID %v",
  40. bv.chainID, signedHeader.ChainID)
  41. }
  42. // We can't verify commits older than bv.height.
  43. if signedHeader.Height < bv.height {
  44. return cmn.NewError("BaseVerifier height is %v, cannot verify height %v",
  45. bv.height, signedHeader.Height)
  46. }
  47. // We can't verify with the wrong validator set.
  48. if !bytes.Equal(signedHeader.ValidatorsHash,
  49. bv.valset.Hash()) {
  50. return lerr.ErrUnexpectedValidators(signedHeader.ValidatorsHash, bv.valset.Hash())
  51. }
  52. // Do basic sanity checks.
  53. err := signedHeader.ValidateBasic(bv.chainID)
  54. if err != nil {
  55. return cmn.ErrorWrap(err, "in verify")
  56. }
  57. // Check commit signatures.
  58. err = bv.valset.VerifyCommit(
  59. bv.chainID, signedHeader.Commit.BlockID,
  60. signedHeader.Height, signedHeader.Commit)
  61. if err != nil {
  62. return cmn.ErrorWrap(err, "in verify")
  63. }
  64. return nil
  65. }