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.

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