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.

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