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.

118 lines
3.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
10 years ago
  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "time"
  8. "github.com/tendermint/go-crypto"
  9. "github.com/tendermint/go-wire"
  10. cmn "github.com/tendermint/tmlibs/common"
  11. )
  12. var (
  13. ErrVoteUnexpectedStep = errors.New("Unexpected step")
  14. ErrVoteInvalidValidatorIndex = errors.New("Invalid validator index")
  15. ErrVoteInvalidValidatorAddress = errors.New("Invalid validator address")
  16. ErrVoteInvalidSignature = errors.New("Invalid signature")
  17. ErrVoteInvalidBlockHash = errors.New("Invalid block hash")
  18. ErrVoteNonDeterministicSignature = errors.New("Non-deterministic signature")
  19. ErrVoteNil = errors.New("Nil vote")
  20. )
  21. type ErrVoteConflictingVotes struct {
  22. *DuplicateVoteEvidence
  23. }
  24. func (err *ErrVoteConflictingVotes) Error() string {
  25. return fmt.Sprintf("Conflicting votes from validator %v", err.PubKey.Address())
  26. }
  27. func NewConflictingVoteError(val *Validator, voteA, voteB *Vote) *ErrVoteConflictingVotes {
  28. return &ErrVoteConflictingVotes{
  29. &DuplicateVoteEvidence{
  30. PubKey: val.PubKey,
  31. VoteA: voteA,
  32. VoteB: voteB,
  33. },
  34. }
  35. }
  36. // Types of votes
  37. // TODO Make a new type "VoteType"
  38. const (
  39. VoteTypePrevote = byte(0x01)
  40. VoteTypePrecommit = byte(0x02)
  41. )
  42. func IsVoteTypeValid(type_ byte) bool {
  43. switch type_ {
  44. case VoteTypePrevote:
  45. return true
  46. case VoteTypePrecommit:
  47. return true
  48. default:
  49. return false
  50. }
  51. }
  52. // Address is hex bytes. TODO: crypto.Address
  53. type Address = cmn.HexBytes
  54. // Represents a prevote, precommit, or commit vote from validators for consensus.
  55. type Vote struct {
  56. ValidatorAddress Address `json:"validator_address"`
  57. ValidatorIndex int `json:"validator_index"`
  58. Height int64 `json:"height"`
  59. Round int `json:"round"`
  60. Timestamp time.Time `json:"timestamp"`
  61. Type byte `json:"type"`
  62. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  63. Signature crypto.Signature `json:"signature"`
  64. }
  65. func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
  66. wire.WriteJSON(CanonicalJSONOnceVote{
  67. chainID,
  68. CanonicalVote(vote),
  69. }, w, n, err)
  70. }
  71. func (vote *Vote) Copy() *Vote {
  72. voteCopy := *vote
  73. return &voteCopy
  74. }
  75. func (vote *Vote) String() string {
  76. if vote == nil {
  77. return "nil-Vote"
  78. }
  79. var typeString string
  80. switch vote.Type {
  81. case VoteTypePrevote:
  82. typeString = "Prevote"
  83. case VoteTypePrecommit:
  84. typeString = "Precommit"
  85. default:
  86. cmn.PanicSanity("Unknown vote type")
  87. }
  88. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v @ %s}",
  89. vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress),
  90. vote.Height, vote.Round, vote.Type, typeString,
  91. cmn.Fingerprint(vote.BlockID.Hash), vote.Signature,
  92. CanonicalTime(vote.Timestamp))
  93. }
  94. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  95. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  96. return ErrVoteInvalidValidatorAddress
  97. }
  98. if !pubKey.VerifyBytes(SignBytes(chainID, vote), vote.Signature) {
  99. return ErrVoteInvalidSignature
  100. }
  101. return nil
  102. }