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.

121 lines
3.1 KiB

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