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.

85 lines
2.4 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
  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-crypto"
  8. "github.com/tendermint/go-wire"
  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. BlockPartsHeader PartSetHeader `json:"block_parts_header"` // zero if vote is nil.
  30. Signature crypto.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 *int, err *error) {
  38. wire.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err)
  39. wire.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts_header":%v`, vote.BlockHash, vote.BlockPartsHeader)), w, n, err)
  40. wire.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. PanicSanity("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.BlockPartsHeader, vote.Signature)
  60. }
  61. //--------------------------------------------------------------------------------
  62. // TODO: Move blocks/Validation to here?
  63. // Common interface between *consensus.VoteSet and types.Validation
  64. type VoteSetReader interface {
  65. Height() int
  66. Round() int
  67. Type() byte
  68. Size() int
  69. BitArray() *BitArray
  70. GetByIndex(int) *Vote
  71. IsCommit() bool
  72. }