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.

127 lines
3.1 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 = 200
  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. // Types of votes
  39. // TODO Make a new type "VoteType"
  40. const (
  41. VoteTypePrevote = byte(0x01)
  42. VoteTypePrecommit = byte(0x02)
  43. )
  44. func IsVoteTypeValid(type_ byte) bool {
  45. switch type_ {
  46. case VoteTypePrevote:
  47. return true
  48. case VoteTypePrecommit:
  49. return true
  50. default:
  51. return false
  52. }
  53. }
  54. // Address is hex bytes.
  55. type Address = crypto.Address
  56. // Represents a prevote, precommit, or commit vote from validators for consensus.
  57. type Vote struct {
  58. ValidatorAddress Address `json:"validator_address"`
  59. ValidatorIndex int `json:"validator_index"`
  60. Height int64 `json:"height"`
  61. Round int `json:"round"`
  62. Timestamp time.Time `json:"timestamp"`
  63. Type byte `json:"type"`
  64. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  65. Signature []byte `json:"signature"`
  66. }
  67. func (vote *Vote) SignBytes(chainID string) []byte {
  68. bz, err := cdc.MarshalBinary(CanonicalizeVote(chainID, vote))
  69. if err != nil {
  70. panic(err)
  71. }
  72. return bz
  73. }
  74. func (vote *Vote) Copy() *Vote {
  75. voteCopy := *vote
  76. return &voteCopy
  77. }
  78. func (vote *Vote) String() string {
  79. if vote == nil {
  80. return "nil-Vote"
  81. }
  82. var typeString string
  83. switch vote.Type {
  84. case VoteTypePrevote:
  85. typeString = "Prevote"
  86. case VoteTypePrecommit:
  87. typeString = "Precommit"
  88. default:
  89. cmn.PanicSanity("Unknown vote type")
  90. }
  91. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  92. vote.ValidatorIndex,
  93. cmn.Fingerprint(vote.ValidatorAddress),
  94. vote.Height,
  95. vote.Round,
  96. vote.Type,
  97. typeString,
  98. cmn.Fingerprint(vote.BlockID.Hash),
  99. cmn.Fingerprint(vote.Signature),
  100. CanonicalTime(vote.Timestamp))
  101. }
  102. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  103. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  104. return ErrVoteInvalidValidatorAddress
  105. }
  106. if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) {
  107. return ErrVoteInvalidSignature
  108. }
  109. return nil
  110. }