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.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. ErrVoteInvalidValidatorIndex = errors.New("Invalid round vote validator index")
  13. ErrVoteInvalidValidatorAddress = errors.New("Invalid round vote validator address")
  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. type Vote struct {
  26. ValidatorAddress []byte `json:"validator_address"`
  27. ValidatorIndex int `json:"validator_index"`
  28. Height int `json:"height"`
  29. Round int `json:"round"`
  30. Type byte `json:"type"`
  31. BlockHash []byte `json:"block_hash"` // empty if vote is nil.
  32. BlockPartsHeader PartSetHeader `json:"block_parts_header"` // zero if vote is nil.
  33. Signature crypto.SignatureEd25519 `json:"signature"`
  34. }
  35. // Types of votes
  36. const (
  37. VoteTypePrevote = byte(0x01)
  38. VoteTypePrecommit = byte(0x02)
  39. )
  40. func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
  41. wire.WriteTo([]byte(Fmt(`{"chain_id":"%s"`, chainID)), w, n, err)
  42. wire.WriteTo([]byte(Fmt(`,"vote":{"block_hash":"%X","block_parts_header":%v`, vote.BlockHash, vote.BlockPartsHeader)), w, n, err)
  43. wire.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. if vote == nil {
  51. return "nil-Vote"
  52. }
  53. var typeString string
  54. switch vote.Type {
  55. case VoteTypePrevote:
  56. typeString = "Prevote"
  57. case VoteTypePrecommit:
  58. typeString = "Precommit"
  59. default:
  60. PanicSanity("Unknown vote type")
  61. }
  62. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v}",
  63. vote.ValidatorIndex, Fingerprint(vote.ValidatorAddress),
  64. vote.Height, vote.Round, vote.Type, typeString,
  65. Fingerprint(vote.BlockHash), vote.Signature)
  66. }