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.

158 lines
4.2 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. nilVoteStr string = "nil-Vote"
  14. )
  15. var (
  16. ErrVoteUnexpectedStep = errors.New("unexpected step")
  17. ErrVoteInvalidValidatorIndex = errors.New("invalid validator index")
  18. ErrVoteInvalidValidatorAddress = errors.New("invalid validator address")
  19. ErrVoteInvalidSignature = errors.New("invalid signature")
  20. ErrVoteInvalidBlockHash = errors.New("invalid block hash")
  21. ErrVoteNonDeterministicSignature = errors.New("non-deterministic signature")
  22. ErrVoteNil = errors.New("nil vote")
  23. )
  24. type ErrVoteConflictingVotes struct {
  25. *DuplicateVoteEvidence
  26. }
  27. func (err *ErrVoteConflictingVotes) Error() string {
  28. return fmt.Sprintf("Conflicting votes from validator %v", err.PubKey.Address())
  29. }
  30. func NewConflictingVoteError(val *Validator, vote1, vote2 *Vote) *ErrVoteConflictingVotes {
  31. return &ErrVoteConflictingVotes{
  32. NewDuplicateVoteEvidence(val.PubKey, vote1, vote2),
  33. }
  34. }
  35. // Address is hex bytes.
  36. type Address = crypto.Address
  37. // Vote represents a prevote, precommit, or commit vote from validators for
  38. // consensus.
  39. type Vote struct {
  40. Type SignedMsgType `json:"type"`
  41. Height int64 `json:"height"`
  42. Round int `json:"round"`
  43. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  44. Timestamp time.Time `json:"timestamp"`
  45. ValidatorAddress Address `json:"validator_address"`
  46. ValidatorIndex int `json:"validator_index"`
  47. Signature []byte `json:"signature"`
  48. }
  49. // CommitSig converts the Vote to a CommitSig.
  50. // If the Vote is nil, the CommitSig will be nil.
  51. func (vote *Vote) CommitSig() *CommitSig {
  52. if vote == nil {
  53. return nil
  54. }
  55. cs := CommitSig(*vote)
  56. return &cs
  57. }
  58. func (vote *Vote) SignBytes(chainID string) []byte {
  59. bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote(chainID, vote))
  60. if err != nil {
  61. panic(err)
  62. }
  63. return bz
  64. }
  65. func (vote *Vote) Copy() *Vote {
  66. voteCopy := *vote
  67. return &voteCopy
  68. }
  69. func (vote *Vote) String() string {
  70. if vote == nil {
  71. return nilVoteStr
  72. }
  73. var typeString string
  74. switch vote.Type {
  75. case PrevoteType:
  76. typeString = "Prevote"
  77. case PrecommitType:
  78. typeString = "Precommit"
  79. default:
  80. panic("Unknown vote type")
  81. }
  82. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  83. vote.ValidatorIndex,
  84. cmn.Fingerprint(vote.ValidatorAddress),
  85. vote.Height,
  86. vote.Round,
  87. vote.Type,
  88. typeString,
  89. cmn.Fingerprint(vote.BlockID.Hash),
  90. cmn.Fingerprint(vote.Signature),
  91. CanonicalTime(vote.Timestamp),
  92. )
  93. }
  94. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  95. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  96. return ErrVoteInvalidValidatorAddress
  97. }
  98. if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
  99. return ErrVoteInvalidSignature
  100. }
  101. return nil
  102. }
  103. // ValidateBasic performs basic validation.
  104. func (vote *Vote) ValidateBasic() error {
  105. if !IsVoteTypeValid(vote.Type) {
  106. return errors.New("invalid Type")
  107. }
  108. if vote.Height < 0 {
  109. return errors.New("negative Height")
  110. }
  111. if vote.Round < 0 {
  112. return errors.New("negative Round")
  113. }
  114. // NOTE: Timestamp validation is subtle and handled elsewhere.
  115. if err := vote.BlockID.ValidateBasic(); err != nil {
  116. return fmt.Errorf("wrong BlockID: %v", err)
  117. }
  118. // BlockID.ValidateBasic would not err if we for instance have an empty hash but a
  119. // non-empty PartsSetHeader:
  120. if !vote.BlockID.IsZero() && !vote.BlockID.IsComplete() {
  121. return fmt.Errorf("blockID must be either empty or complete, got: %v", vote.BlockID)
  122. }
  123. if len(vote.ValidatorAddress) != crypto.AddressSize {
  124. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  125. crypto.AddressSize,
  126. len(vote.ValidatorAddress),
  127. )
  128. }
  129. if vote.ValidatorIndex < 0 {
  130. return errors.New("negative ValidatorIndex")
  131. }
  132. if len(vote.Signature) == 0 {
  133. return errors.New("signature is missing")
  134. }
  135. if len(vote.Signature) > MaxSignatureSize {
  136. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  137. }
  138. return nil
  139. }