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.

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