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.

233 lines
6.2 KiB

  1. package types
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "github.com/tendermint/tendermint/crypto"
  8. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  9. "github.com/tendermint/tendermint/libs/protoio"
  10. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  11. )
  12. const (
  13. // MaxVoteBytes is a maximum vote size (including amino overhead).
  14. MaxVoteBytes int64 = 209
  15. nilVoteStr string = "nil-Vote"
  16. )
  17. var (
  18. ErrVoteUnexpectedStep = errors.New("unexpected step")
  19. ErrVoteInvalidValidatorIndex = errors.New("invalid validator index")
  20. ErrVoteInvalidValidatorAddress = errors.New("invalid validator address")
  21. ErrVoteInvalidSignature = errors.New("invalid signature")
  22. ErrVoteInvalidBlockHash = errors.New("invalid block hash")
  23. ErrVoteNonDeterministicSignature = errors.New("non-deterministic signature")
  24. ErrVoteNil = errors.New("nil vote")
  25. )
  26. type ErrVoteConflictingVotes struct {
  27. *DuplicateVoteEvidence
  28. }
  29. func (err *ErrVoteConflictingVotes) Error() string {
  30. return fmt.Sprintf("conflicting votes from validator %X", err.VoteA.ValidatorAddress)
  31. }
  32. func NewConflictingVoteError(val *Validator, vote1, vote2 *Vote) *ErrVoteConflictingVotes {
  33. return &ErrVoteConflictingVotes{
  34. NewDuplicateVoteEvidence(vote1, vote2),
  35. }
  36. }
  37. // Address is hex bytes.
  38. type Address = crypto.Address
  39. // Vote represents a prevote, precommit, or commit vote from validators for
  40. // consensus.
  41. type Vote struct {
  42. Type tmproto.SignedMsgType `json:"type"`
  43. Height int64 `json:"height"`
  44. Round int32 `json:"round"` // assume there will not be greater than 2_147_483_647 rounds
  45. BlockID BlockID `json:"block_id"` // zero if vote is nil.
  46. Timestamp time.Time `json:"timestamp"`
  47. ValidatorAddress Address `json:"validator_address"`
  48. ValidatorIndex int32 `json:"validator_index"`
  49. Signature []byte `json:"signature"`
  50. }
  51. // CommitSig converts the Vote to a CommitSig.
  52. func (vote *Vote) CommitSig() CommitSig {
  53. if vote == nil {
  54. return NewCommitSigAbsent()
  55. }
  56. var blockIDFlag BlockIDFlag
  57. switch {
  58. case vote.BlockID.IsComplete():
  59. blockIDFlag = BlockIDFlagCommit
  60. case vote.BlockID.IsZero():
  61. blockIDFlag = BlockIDFlagNil
  62. default:
  63. panic(fmt.Sprintf("Invalid vote %v - expected BlockID to be either empty or complete", vote))
  64. }
  65. return CommitSig{
  66. BlockIDFlag: blockIDFlag,
  67. ValidatorAddress: vote.ValidatorAddress,
  68. Timestamp: vote.Timestamp,
  69. Signature: vote.Signature,
  70. }
  71. }
  72. // VoteSignBytes returns the proto-encoding of the canonicalized Vote, for
  73. // signing.
  74. //
  75. // Panics if the marshaling fails.
  76. //
  77. // See CanonicalizeVote
  78. func VoteSignBytes(chainID string, vote *tmproto.Vote) []byte {
  79. pb := CanonicalizeVote(chainID, vote)
  80. bz, err := protoio.MarshalDelimited(&pb)
  81. if err != nil {
  82. panic(err)
  83. }
  84. return bz
  85. }
  86. func (vote *Vote) Copy() *Vote {
  87. voteCopy := *vote
  88. return &voteCopy
  89. }
  90. func (vote *Vote) String() string {
  91. if vote == nil {
  92. return nilVoteStr
  93. }
  94. var typeString string
  95. switch vote.Type {
  96. case tmproto.PrevoteType:
  97. typeString = "Prevote"
  98. case tmproto.PrecommitType:
  99. typeString = "Precommit"
  100. default:
  101. panic("Unknown vote type")
  102. }
  103. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  104. vote.ValidatorIndex,
  105. tmbytes.Fingerprint(vote.ValidatorAddress),
  106. vote.Height,
  107. vote.Round,
  108. vote.Type,
  109. typeString,
  110. tmbytes.Fingerprint(vote.BlockID.Hash),
  111. tmbytes.Fingerprint(vote.Signature),
  112. CanonicalTime(vote.Timestamp),
  113. )
  114. }
  115. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  116. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  117. return ErrVoteInvalidValidatorAddress
  118. }
  119. v := vote.ToProto()
  120. if !pubKey.VerifyBytes(VoteSignBytes(chainID, v), vote.Signature) {
  121. return ErrVoteInvalidSignature
  122. }
  123. return nil
  124. }
  125. // ValidateBasic performs basic validation.
  126. func (vote *Vote) ValidateBasic() error {
  127. if !IsVoteTypeValid(vote.Type) {
  128. return errors.New("invalid Type")
  129. }
  130. if vote.Height < 0 {
  131. return errors.New("negative Height")
  132. }
  133. if vote.Round < 0 {
  134. return errors.New("negative Round")
  135. }
  136. // NOTE: Timestamp validation is subtle and handled elsewhere.
  137. if err := vote.BlockID.ValidateBasic(); err != nil {
  138. return fmt.Errorf("wrong BlockID: %v", err)
  139. }
  140. // BlockID.ValidateBasic would not err if we for instance have an empty hash but a
  141. // non-empty PartsSetHeader:
  142. if !vote.BlockID.IsZero() && !vote.BlockID.IsComplete() {
  143. return fmt.Errorf("blockID must be either empty or complete, got: %v", vote.BlockID)
  144. }
  145. if len(vote.ValidatorAddress) != crypto.AddressSize {
  146. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  147. crypto.AddressSize,
  148. len(vote.ValidatorAddress),
  149. )
  150. }
  151. if vote.ValidatorIndex < 0 {
  152. return errors.New("negative ValidatorIndex")
  153. }
  154. if len(vote.Signature) == 0 {
  155. return errors.New("signature is missing")
  156. }
  157. if len(vote.Signature) > MaxSignatureSize {
  158. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  159. }
  160. return nil
  161. }
  162. // ToProto converts the handwritten type to proto generated type
  163. // return type, nil if everything converts safely, otherwise nil, error
  164. func (vote *Vote) ToProto() *tmproto.Vote {
  165. if vote == nil {
  166. return nil
  167. }
  168. return &tmproto.Vote{
  169. Type: vote.Type,
  170. Height: vote.Height,
  171. Round: vote.Round,
  172. BlockID: vote.BlockID.ToProto(),
  173. Timestamp: vote.Timestamp,
  174. ValidatorAddress: vote.ValidatorAddress,
  175. ValidatorIndex: vote.ValidatorIndex,
  176. Signature: vote.Signature,
  177. }
  178. }
  179. //FromProto converts a proto generetad type to a handwritten type
  180. // return type, nil if everything converts safely, otherwise nil, error
  181. func VoteFromProto(pv *tmproto.Vote) (*Vote, error) {
  182. if pv == nil {
  183. return nil, errors.New("nil vote")
  184. }
  185. blockID, err := BlockIDFromProto(&pv.BlockID)
  186. if err != nil {
  187. return nil, err
  188. }
  189. vote := new(Vote)
  190. vote.Type = pv.Type
  191. vote.Height = pv.Height
  192. vote.Round = pv.Round
  193. vote.BlockID = *blockID
  194. vote.Timestamp = pv.Timestamp
  195. vote.ValidatorAddress = pv.ValidatorAddress
  196. vote.ValidatorIndex = pv.ValidatorIndex
  197. vote.Signature = pv.Signature
  198. return vote, vote.ValidateBasic()
  199. }