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.

229 lines
6.1 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/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 take the chainID & a vote, represented in protobuf, and creates a signature.
  73. // If any error arises this will panic
  74. func VoteSignBytes(chainID string, vote *tmproto.Vote) []byte {
  75. pb := CanonicalizeVote(chainID, vote)
  76. bz, err := protoio.MarshalDelimited(&pb)
  77. if err != nil {
  78. panic(err)
  79. }
  80. return bz
  81. }
  82. func (vote *Vote) Copy() *Vote {
  83. voteCopy := *vote
  84. return &voteCopy
  85. }
  86. func (vote *Vote) String() string {
  87. if vote == nil {
  88. return nilVoteStr
  89. }
  90. var typeString string
  91. switch vote.Type {
  92. case tmproto.PrevoteType:
  93. typeString = "Prevote"
  94. case tmproto.PrecommitType:
  95. typeString = "Precommit"
  96. default:
  97. panic("Unknown vote type")
  98. }
  99. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  100. vote.ValidatorIndex,
  101. tmbytes.Fingerprint(vote.ValidatorAddress),
  102. vote.Height,
  103. vote.Round,
  104. vote.Type,
  105. typeString,
  106. tmbytes.Fingerprint(vote.BlockID.Hash),
  107. tmbytes.Fingerprint(vote.Signature),
  108. CanonicalTime(vote.Timestamp),
  109. )
  110. }
  111. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  112. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  113. return ErrVoteInvalidValidatorAddress
  114. }
  115. v := vote.ToProto()
  116. if !pubKey.VerifyBytes(VoteSignBytes(chainID, v), vote.Signature) {
  117. return ErrVoteInvalidSignature
  118. }
  119. return nil
  120. }
  121. // ValidateBasic performs basic validation.
  122. func (vote *Vote) ValidateBasic() error {
  123. if !IsVoteTypeValid(vote.Type) {
  124. return errors.New("invalid Type")
  125. }
  126. if vote.Height < 0 {
  127. return errors.New("negative Height")
  128. }
  129. if vote.Round < 0 {
  130. return errors.New("negative Round")
  131. }
  132. // NOTE: Timestamp validation is subtle and handled elsewhere.
  133. if err := vote.BlockID.ValidateBasic(); err != nil {
  134. return fmt.Errorf("wrong BlockID: %v", err)
  135. }
  136. // BlockID.ValidateBasic would not err if we for instance have an empty hash but a
  137. // non-empty PartsSetHeader:
  138. if !vote.BlockID.IsZero() && !vote.BlockID.IsComplete() {
  139. return fmt.Errorf("blockID must be either empty or complete, got: %v", vote.BlockID)
  140. }
  141. if len(vote.ValidatorAddress) != crypto.AddressSize {
  142. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  143. crypto.AddressSize,
  144. len(vote.ValidatorAddress),
  145. )
  146. }
  147. if vote.ValidatorIndex < 0 {
  148. return errors.New("negative ValidatorIndex")
  149. }
  150. if len(vote.Signature) == 0 {
  151. return errors.New("signature is missing")
  152. }
  153. if len(vote.Signature) > MaxSignatureSize {
  154. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  155. }
  156. return nil
  157. }
  158. // ToProto converts the handwritten type to proto generated type
  159. // return type, nil if everything converts safely, otherwise nil, error
  160. func (vote *Vote) ToProto() *tmproto.Vote {
  161. if vote == nil {
  162. return nil
  163. }
  164. return &tmproto.Vote{
  165. Type: vote.Type,
  166. Height: vote.Height,
  167. Round: vote.Round,
  168. BlockID: vote.BlockID.ToProto(),
  169. Timestamp: vote.Timestamp,
  170. ValidatorAddress: vote.ValidatorAddress,
  171. ValidatorIndex: vote.ValidatorIndex,
  172. Signature: vote.Signature,
  173. }
  174. }
  175. //FromProto converts a proto generetad type to a handwritten type
  176. // return type, nil if everything converts safely, otherwise nil, error
  177. func VoteFromProto(pv *tmproto.Vote) (*Vote, error) {
  178. if pv == nil {
  179. return nil, errors.New("nil vote")
  180. }
  181. blockID, err := BlockIDFromProto(&pv.BlockID)
  182. if err != nil {
  183. return nil, err
  184. }
  185. vote := new(Vote)
  186. vote.Type = pv.Type
  187. vote.Height = pv.Height
  188. vote.Round = pv.Round
  189. vote.BlockID = *blockID
  190. vote.Timestamp = pv.Timestamp
  191. vote.ValidatorAddress = pv.ValidatorAddress
  192. vote.ValidatorIndex = pv.ValidatorIndex
  193. vote.Signature = pv.Signature
  194. return vote, vote.ValidateBasic()
  195. }