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.

76 lines
1.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package blocks
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. . "github.com/tendermint/tendermint/binary"
  7. )
  8. const (
  9. VoteTypePrevote = byte(0x00)
  10. VoteTypePrecommit = byte(0x01)
  11. VoteTypeCommit = byte(0x02)
  12. )
  13. var (
  14. ErrVoteUnexpectedPhase = errors.New("Unexpected phase")
  15. ErrVoteInvalidAccount = errors.New("Invalid round vote account")
  16. ErrVoteInvalidSignature = errors.New("Invalid round vote signature")
  17. ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
  18. ErrVoteConflictingSignature = errors.New("Conflicting round vote signature")
  19. )
  20. // Represents a prevote, precommit, or commit vote for proposals.
  21. type Vote struct {
  22. Height uint32
  23. Round uint16
  24. Type byte
  25. BlockHash []byte // empty if vote is nil.
  26. Signature
  27. }
  28. func ReadVote(r io.Reader, n *int64, err *error) *Vote {
  29. return &Vote{
  30. Height: ReadUInt32(r, n, err),
  31. Round: ReadUInt16(r, n, err),
  32. Type: ReadByte(r, n, err),
  33. BlockHash: ReadByteSlice(r, n, err),
  34. Signature: ReadSignature(r, n, err),
  35. }
  36. }
  37. func (v *Vote) WriteTo(w io.Writer) (n int64, err error) {
  38. WriteUInt32(w, v.Height, &n, &err)
  39. WriteUInt16(w, v.Round, &n, &err)
  40. WriteByte(w, v.Type, &n, &err)
  41. WriteByteSlice(w, v.BlockHash, &n, &err)
  42. WriteBinary(w, v.Signature, &n, &err)
  43. return
  44. }
  45. func (v *Vote) GetSignature() Signature {
  46. return v.Signature
  47. }
  48. func (v *Vote) SetSignature(sig Signature) {
  49. v.Signature = sig
  50. }
  51. func (v *Vote) String() string {
  52. blockHash := v.BlockHash
  53. if len(v.BlockHash) == 0 {
  54. blockHash = make([]byte, 6) // for printing
  55. }
  56. switch v.Type {
  57. case VoteTypePrevote:
  58. return fmt.Sprintf("Vote{%v/%v:%X:%v}", v.Height, v.Round, blockHash, v.SignerId)
  59. case VoteTypePrecommit:
  60. return fmt.Sprintf("Precommit{%v/%v:%X:%v}", v.Height, v.Round, blockHash, v.SignerId)
  61. case VoteTypeCommit:
  62. return fmt.Sprintf("Commit{%v/%v:%X:%v}", v.Height, v.Round, blockHash, v.SignerId)
  63. default:
  64. panic("Unknown vote type")
  65. }
  66. }