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.

146 lines
3.7 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "github.com/tendermint/tendermint/crypto"
  8. cmn "github.com/tendermint/tendermint/libs/common"
  9. )
  10. const (
  11. // MaxVoteBytes is a maximum vote size (including amino overhead).
  12. MaxVoteBytes int64 = 223
  13. )
  14. var (
  15. ErrVoteUnexpectedStep = errors.New("Unexpected step")
  16. ErrVoteInvalidValidatorIndex = errors.New("Invalid validator index")
  17. ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address")
  18. ErrVoteInvalidSignature = errors.New("Invalid signature")
  19. ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
  20. ErrVoteNonDeterministicSignature = errors.New("Non-deterministic signature")
  21. ErrVoteNil = errors.New("Nil vote")
  22. )
  23. type ErrVoteConflictingVotes struct {
  24. *DuplicateVoteEvidence
  25. }
  26. func (err *ErrVoteConflictingVotes) Error() string {
  27. return fmt.Sprintf("Conflicting votes from validator %v", err.PubKey.Address())
  28. }
  29. func NewConflictingVoteError(val *Validator, voteA, voteB *Vote) *ErrVoteConflictingVotes {
  30. return &ErrVoteConflictingVotes{
  31. &DuplicateVoteEvidence{
  32. PubKey: val.PubKey,
  33. VoteA: voteA,
  34. VoteB: voteB,
  35. },
  36. }
  37. }
  38. // Address is hex bytes.
  39. type Address = crypto.Address
  40. // Vote represents a prevote, precommit, or commit vote from validators for
  41. // consensus.
  42. type Vote struct {
  43. Type SignedMsgType `json:"type"`
  44. Height int64 `json:"height"`
  45. Round int `json:"round"`
  46. Timestamp time.Time `json:"timestamp"`
  47. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  48. ValidatorAddress Address `json:"validator_address"`
  49. ValidatorIndex int `json:"validator_index"`
  50. Signature []byte `json:"signature"`
  51. }
  52. func (vote *Vote) SignBytes(chainID string) []byte {
  53. bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote(chainID, vote))
  54. if err != nil {
  55. panic(err)
  56. }
  57. return bz
  58. }
  59. func (vote *Vote) Copy() *Vote {
  60. voteCopy := *vote
  61. return &voteCopy
  62. }
  63. func (vote *Vote) String() string {
  64. if vote == nil {
  65. return "nil-Vote"
  66. }
  67. var typeString string
  68. switch vote.Type {
  69. case PrevoteType:
  70. typeString = "Prevote"
  71. case PrecommitType:
  72. typeString = "Precommit"
  73. default:
  74. cmn.PanicSanity("Unknown vote type")
  75. }
  76. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  77. vote.ValidatorIndex,
  78. cmn.Fingerprint(vote.ValidatorAddress),
  79. vote.Height,
  80. vote.Round,
  81. vote.Type,
  82. typeString,
  83. cmn.Fingerprint(vote.BlockID.Hash),
  84. cmn.Fingerprint(vote.Signature),
  85. CanonicalTime(vote.Timestamp),
  86. )
  87. }
  88. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  89. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  90. return ErrVoteInvalidValidatorAddress
  91. }
  92. if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
  93. return ErrVoteInvalidSignature
  94. }
  95. return nil
  96. }
  97. // ValidateBasic performs basic validation.
  98. func (vote *Vote) ValidateBasic() error {
  99. if !IsVoteTypeValid(vote.Type) {
  100. return errors.New("Invalid Type")
  101. }
  102. if vote.Height < 0 {
  103. return errors.New("Negative Height")
  104. }
  105. if vote.Round < 0 {
  106. return errors.New("Negative Round")
  107. }
  108. // NOTE: Timestamp validation is subtle and handled elsewhere.
  109. if err := vote.BlockID.ValidateBasic(); err != nil {
  110. return fmt.Errorf("Wrong BlockID: %v", err)
  111. }
  112. if len(vote.ValidatorAddress) != crypto.AddressSize {
  113. return fmt.Errorf("Expected ValidatorAddress size to be %d bytes, got %d bytes",
  114. crypto.AddressSize,
  115. len(vote.ValidatorAddress),
  116. )
  117. }
  118. if vote.ValidatorIndex < 0 {
  119. return errors.New("Negative ValidatorIndex")
  120. }
  121. if len(vote.Signature) == 0 {
  122. return errors.New("Signature is missing")
  123. }
  124. if len(vote.Signature) > MaxSignatureSize {
  125. return fmt.Errorf("Signature is too big (max: %d)", MaxSignatureSize)
  126. }
  127. return nil
  128. }