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.

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