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.

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