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.

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