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.

71 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
  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. type Vote struct {
  25. Height int `json:"height"`
  26. Round int `json:"round"`
  27. Type byte `json:"type"`
  28. BlockHash []byte `json:"block_hash"` // empty if vote is nil.
  29. BlockParts PartSetHeader `json:"block_parts"` // zero if vote is nil.
  30. Signature account.SignatureEd25519 `json:"signature"`
  31. }
  32. // Types of votes
  33. const (
  34. VoteTypePrevote = byte(0x01)
  35. VoteTypePrecommit = byte(0x02)
  36. )
  37. func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int64, err *error) {
  38. binary.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err)
  39. binary.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err)
  40. binary.WriteTo([]byte(Fmt(`,"height":%v,"round":%v,"type":%v}}`, vote.Height, vote.Round, vote.Type)), w, n, err)
  41. }
  42. func (vote *Vote) Copy() *Vote {
  43. voteCopy := *vote
  44. return &voteCopy
  45. }
  46. func (vote *Vote) String() string {
  47. if vote == nil {
  48. return "nil-Vote"
  49. }
  50. var typeString string
  51. switch vote.Type {
  52. case VoteTypePrevote:
  53. typeString = "Prevote"
  54. case VoteTypePrecommit:
  55. typeString = "Precommit"
  56. default:
  57. panic("Unknown vote type")
  58. }
  59. return fmt.Sprintf("Vote{%v/%02d/%v(%v) %X#%v %v}", vote.Height, vote.Round, vote.Type, typeString, Fingerprint(vote.BlockHash), vote.BlockParts, vote.Signature)
  60. }