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.

118 lines
3.0 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "time"
  7. crypto "github.com/tendermint/tendermint/crypto"
  8. cmn "github.com/tendermint/tendermint/libs/common"
  9. )
  10. var (
  11. ErrVoteUnexpectedStep = errors.New("Unexpected step")
  12. ErrVoteInvalidValidatorIndex = errors.New("Invalid validator index")
  13. ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address")
  14. ErrVoteInvalidSignature = errors.New("Invalid signature")
  15. ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
  16. ErrVoteNonDeterministicSignature = errors.New("Non-deterministic signature")
  17. ErrVoteNil = errors.New("Nil vote")
  18. )
  19. type ErrVoteConflictingVotes struct {
  20. *DuplicateVoteEvidence
  21. }
  22. func (err *ErrVoteConflictingVotes) Error() string {
  23. return fmt.Sprintf("Conflicting votes from validator %v", err.PubKey.Address())
  24. }
  25. func NewConflictingVoteError(val *Validator, voteA, voteB *Vote) *ErrVoteConflictingVotes {
  26. return &ErrVoteConflictingVotes{
  27. &DuplicateVoteEvidence{
  28. PubKey: val.PubKey,
  29. VoteA: voteA,
  30. VoteB: voteB,
  31. },
  32. }
  33. }
  34. // Types of votes
  35. // TODO Make a new type "VoteType"
  36. const (
  37. VoteTypePrevote = byte(0x01)
  38. VoteTypePrecommit = byte(0x02)
  39. )
  40. func IsVoteTypeValid(type_ byte) bool {
  41. switch type_ {
  42. case VoteTypePrevote:
  43. return true
  44. case VoteTypePrecommit:
  45. return true
  46. default:
  47. return false
  48. }
  49. }
  50. // Address is hex bytes. TODO: crypto.Address
  51. type Address = cmn.HexBytes
  52. // Represents a prevote, precommit, or commit vote from validators for consensus.
  53. type Vote struct {
  54. ValidatorAddress Address `json:"validator_address"`
  55. ValidatorIndex int `json:"validator_index"`
  56. Height int64 `json:"height"`
  57. Round int `json:"round"`
  58. Timestamp time.Time `json:"timestamp"`
  59. Type byte `json:"type"`
  60. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  61. Signature []byte `json:"signature"`
  62. }
  63. func (vote *Vote) SignBytes(chainID string) []byte {
  64. bz, err := cdc.MarshalJSON(CanonicalVote(chainID, vote))
  65. if err != nil {
  66. panic(err)
  67. }
  68. return bz
  69. }
  70. func (vote *Vote) Copy() *Vote {
  71. voteCopy := *vote
  72. return &voteCopy
  73. }
  74. func (vote *Vote) String() string {
  75. if vote == nil {
  76. return "nil-Vote"
  77. }
  78. var typeString string
  79. switch vote.Type {
  80. case VoteTypePrevote:
  81. typeString = "Prevote"
  82. case VoteTypePrecommit:
  83. typeString = "Precommit"
  84. default:
  85. cmn.PanicSanity("Unknown vote type")
  86. }
  87. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  88. vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress),
  89. vote.Height, vote.Round, vote.Type, typeString,
  90. cmn.Fingerprint(vote.BlockID.Hash),
  91. cmn.Fingerprint(vote.Signature),
  92. CanonicalTime(vote.Timestamp))
  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. }