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.

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