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.

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