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.

115 lines
2.9 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. 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. // Represents a prevote, precommit, or commit vote from validators for consensus.
  53. type Vote struct {
  54. ValidatorAddress data.Bytes `json:"validator_address"`
  55. ValidatorIndex int `json:"validator_index"`
  56. Height int64 `json:"height"`
  57. Round int `json:"round"`
  58. Timestamp time.Time `json:"timestamp"`
  59. Type byte `json:"type"`
  60. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  61. Signature crypto.Signature `json:"signature"`
  62. }
  63. func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
  64. wire.WriteJSON(CanonicalJSONOnceVote{
  65. chainID,
  66. CanonicalVote(vote),
  67. }, w, n, err)
  68. }
  69. func (vote *Vote) Copy() *Vote {
  70. voteCopy := *vote
  71. return &voteCopy
  72. }
  73. func (vote *Vote) String() string {
  74. if vote == nil {
  75. return "nil-Vote"
  76. }
  77. var typeString string
  78. switch vote.Type {
  79. case VoteTypePrevote:
  80. typeString = "Prevote"
  81. case VoteTypePrecommit:
  82. typeString = "Precommit"
  83. default:
  84. cmn.PanicSanity("Unknown vote type")
  85. }
  86. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %v @ %s}",
  87. vote.ValidatorIndex, cmn.Fingerprint(vote.ValidatorAddress),
  88. vote.Height, vote.Round, vote.Type, typeString,
  89. cmn.Fingerprint(vote.BlockID.Hash), vote.Signature,
  90. CanonicalTime(vote.Timestamp))
  91. }
  92. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  93. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  94. return ErrVoteInvalidValidatorAddress
  95. }
  96. if !pubKey.VerifyBytes(SignBytes(chainID, vote), vote.Signature) {
  97. return ErrVoteInvalidSignature
  98. }
  99. return nil
  100. }