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
2.3 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 `json:"height"`
  29. Round uint `json:"round"`
  30. Type byte `json:"type"`
  31. BlockHash []byte `json:"block_hash"` // empty if vote is nil.
  32. BlockParts PartSetHeader `json:"block_parts"` // zero if vote is nil.
  33. Signature account.SignatureEd25519 `json:"signature"`
  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. // We hex encode the network name so we don't deal with escaping issues.
  43. binary.WriteTo([]byte(Fmt(`{"network":"%X"`, config.App().GetString("network"))), w, n, err)
  44. binary.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts":%v`, vote.BlockHash, vote.BlockParts)), w, n, err)
  45. binary.WriteTo([]byte(Fmt(`,"height":%v,"round":%v,"type":%v}}`, vote.Height, vote.Round, vote.Type)), 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("Vote{%v/%02d/%v(%v) %X#%v %v}", vote.Height, vote.Round, vote.Type, typeString, Fingerprint(vote.BlockHash), vote.BlockParts, vote.Signature)
  64. }