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.

73 lines
2.2 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
9 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 `json:"height"`
  28. Round uint `json:"round"`
  29. Type byte `json:"type"`
  30. BlockHash []byte `json:"block_hash"` // empty if vote is nil.
  31. BlockParts PartSetHeader `json:"block_parts"` // zero if vote is nil.
  32. Signature account.SignatureEd25519 `json:"signature"`
  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(chainID string, w io.Writer, n *int64, err *error) {
  41. binary.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err)
  42. binary.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err)
  43. binary.WriteTo([]byte(Fmt(`,"height":%v,"round":%v,"type":%v}}`, vote.Height, vote.Round, vote.Type)), w, n, err)
  44. }
  45. func (vote *Vote) Copy() *Vote {
  46. voteCopy := *vote
  47. return &voteCopy
  48. }
  49. func (vote *Vote) String() string {
  50. var typeString string
  51. switch vote.Type {
  52. case VoteTypePrevote:
  53. typeString = "Prevote"
  54. case VoteTypePrecommit:
  55. typeString = "Precommit"
  56. case VoteTypeCommit:
  57. typeString = "Commit"
  58. default:
  59. panic("Unknown vote type")
  60. }
  61. 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)
  62. }