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.

75 lines
1.9 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
10 years ago
  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "github.com/tendermint/tendermint/account"
  7. "github.com/tendermint/tendermint/binary"
  8. . "github.com/tendermint/tendermint/common"
  9. )
  10. var (
  11. ErrVoteUnexpectedStep = errors.New("Unexpected step")
  12. ErrVoteInvalidAccount = errors.New("Invalid round vote account")
  13. ErrVoteInvalidSignature = errors.New("Invalid round vote signature")
  14. ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
  15. )
  16. type ErrVoteConflictingSignature struct {
  17. VoteA *Vote
  18. VoteB *Vote
  19. }
  20. func (err *ErrVoteConflictingSignature) Error() string {
  21. return "Conflicting round vote signature"
  22. }
  23. // Represents a prevote, precommit, or commit vote from validators for consensus.
  24. // Commit votes get aggregated into the next block's Validaiton.
  25. // See the whitepaper for details.
  26. type Vote struct {
  27. Height uint
  28. Round uint
  29. Type byte
  30. BlockHash []byte // empty if vote is nil.
  31. BlockParts PartSetHeader // zero if vote is nil.
  32. Signature account.SignatureEd25519
  33. }
  34. // Types of votes
  35. const (
  36. VoteTypePrevote = byte(0x01)
  37. VoteTypePrecommit = byte(0x02)
  38. VoteTypeCommit = byte(0x03)
  39. )
  40. func (vote *Vote) WriteSignBytes(w io.Writer, n *int64, err *error) {
  41. binary.WriteUvarint(vote.Height, w, n, err)
  42. binary.WriteUvarint(vote.Round, w, n, err)
  43. binary.WriteByte(vote.Type, w, n, err)
  44. binary.WriteByteSlice(vote.BlockHash, w, n, err)
  45. binary.WriteBinary(vote.BlockParts, w, n, err)
  46. }
  47. func (vote *Vote) Copy() *Vote {
  48. voteCopy := *vote
  49. return &voteCopy
  50. }
  51. func (vote *Vote) String() string {
  52. var typeString string
  53. switch vote.Type {
  54. case VoteTypePrevote:
  55. typeString = "Prevote"
  56. case VoteTypePrecommit:
  57. typeString = "Precommit"
  58. case VoteTypeCommit:
  59. typeString = "Commit"
  60. default:
  61. panic("Unknown vote type")
  62. }
  63. return fmt.Sprintf("%v{%v/%v %X#%v %v}", typeString, vote.Height, vote.Round, Fingerprint(vote.BlockHash), vote.BlockParts, vote.Signature)
  64. }