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.

173 lines
4.6 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. func (vote *Vote) CommitSig() CommitSig {
  51. if vote == nil {
  52. return NewCommitSigAbsent()
  53. }
  54. var blockIDFlag BlockIDFlag
  55. switch {
  56. case vote.BlockID.IsComplete():
  57. blockIDFlag = BlockIDFlagCommit
  58. case vote.BlockID.IsZero():
  59. blockIDFlag = BlockIDFlagNil
  60. default:
  61. panic(fmt.Sprintf("Invalid vote %v - expected BlockID to be either empty or complete", vote))
  62. }
  63. return CommitSig{
  64. BlockIDFlag: blockIDFlag,
  65. ValidatorAddress: vote.ValidatorAddress,
  66. Timestamp: vote.Timestamp,
  67. Signature: vote.Signature,
  68. }
  69. }
  70. func (vote *Vote) SignBytes(chainID string) []byte {
  71. bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote(chainID, vote))
  72. if err != nil {
  73. panic(err)
  74. }
  75. return bz
  76. }
  77. func (vote *Vote) Copy() *Vote {
  78. voteCopy := *vote
  79. return &voteCopy
  80. }
  81. func (vote *Vote) String() string {
  82. if vote == nil {
  83. return nilVoteStr
  84. }
  85. var typeString string
  86. switch vote.Type {
  87. case PrevoteType:
  88. typeString = "Prevote"
  89. case PrecommitType:
  90. typeString = "Precommit"
  91. default:
  92. panic("Unknown vote type")
  93. }
  94. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  95. vote.ValidatorIndex,
  96. cmn.Fingerprint(vote.ValidatorAddress),
  97. vote.Height,
  98. vote.Round,
  99. vote.Type,
  100. typeString,
  101. cmn.Fingerprint(vote.BlockID.Hash),
  102. cmn.Fingerprint(vote.Signature),
  103. CanonicalTime(vote.Timestamp),
  104. )
  105. }
  106. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  107. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  108. return ErrVoteInvalidValidatorAddress
  109. }
  110. if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
  111. return ErrVoteInvalidSignature
  112. }
  113. return nil
  114. }
  115. // ValidateBasic performs basic validation.
  116. func (vote *Vote) ValidateBasic() error {
  117. if !IsVoteTypeValid(vote.Type) {
  118. return errors.New("invalid Type")
  119. }
  120. if vote.Height < 0 {
  121. return errors.New("negative Height")
  122. }
  123. if vote.Round < 0 {
  124. return errors.New("negative Round")
  125. }
  126. // NOTE: Timestamp validation is subtle and handled elsewhere.
  127. if err := vote.BlockID.ValidateBasic(); err != nil {
  128. return fmt.Errorf("wrong BlockID: %v", err)
  129. }
  130. // BlockID.ValidateBasic would not err if we for instance have an empty hash but a
  131. // non-empty PartsSetHeader:
  132. if !vote.BlockID.IsZero() && !vote.BlockID.IsComplete() {
  133. return fmt.Errorf("blockID must be either empty or complete, got: %v", vote.BlockID)
  134. }
  135. if len(vote.ValidatorAddress) != crypto.AddressSize {
  136. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  137. crypto.AddressSize,
  138. len(vote.ValidatorAddress),
  139. )
  140. }
  141. if vote.ValidatorIndex < 0 {
  142. return errors.New("negative ValidatorIndex")
  143. }
  144. if len(vote.Signature) == 0 {
  145. return errors.New("signature is missing")
  146. }
  147. if len(vote.Signature) > MaxSignatureSize {
  148. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  149. }
  150. return nil
  151. }