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.

91 lines
2.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/go-crypto"
  7. "github.com/tendermint/go-wire"
  8. "github.com/tendermint/go-wire/data"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. var (
  12. ErrVoteUnexpectedStep = errors.New("Unexpected step")
  13. ErrVoteInvalidValidatorIndex = errors.New("Invalid validator index")
  14. ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address")
  15. ErrVoteInvalidSignature = errors.New("Invalid signature")
  16. ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
  17. ErrVoteNil = errors.New("Nil vote")
  18. )
  19. type ErrVoteConflictingVotes struct {
  20. VoteA *Vote
  21. VoteB *Vote
  22. }
  23. func (err *ErrVoteConflictingVotes) Error() string {
  24. return "Conflicting votes"
  25. }
  26. // Types of votes
  27. // TODO Make a new type "VoteType"
  28. const (
  29. VoteTypePrevote = byte(0x01)
  30. VoteTypePrecommit = byte(0x02)
  31. )
  32. func IsVoteTypeValid(type_ byte) bool {
  33. switch type_ {
  34. case VoteTypePrevote:
  35. return true
  36. case VoteTypePrecommit:
  37. return true
  38. default:
  39. return false
  40. }
  41. }
  42. // Represents a prevote, precommit, or commit vote from validators for consensus.
  43. type Vote struct {
  44. ValidatorAddress data.Bytes `json:"validator_address"`
  45. ValidatorIndex int `json:"validator_index"`
  46. Height int `json:"height"`
  47. Round int `json:"round"`
  48. Type byte `json:"type"`
  49. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  50. Signature crypto.Signature `json:"signature"`
  51. }
  52. func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
  53. wire.WriteJSON(CanonicalJSONOnceVote{
  54. chainID,
  55. CanonicalVote(vote),
  56. }, w, n, err)
  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 VoteTypePrevote:
  69. typeString = "Prevote"
  70. case VoteTypePrecommit:
  71. typeString = "Precommit"
  72. default:
  73. cmn.PanicSanity("Unknown vote type")
  74. }
  75. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v}",
  76. vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress),
  77. vote.Height, vote.Round, vote.Type, typeString,
  78. cmn.Fingerprint(vote.BlockID.Hash), vote.Signature)
  79. }