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.

244 lines
6.4 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. // String returns a string representation of Vote.
  91. //
  92. // 1. validator index
  93. // 2. first 6 bytes of validator address
  94. // 3. height
  95. // 4. round,
  96. // 5. type byte
  97. // 6. type string
  98. // 7. first 6 bytes of block hash
  99. // 8. first 6 bytes of signature
  100. // 9. timestamp
  101. func (vote *Vote) String() string {
  102. if vote == nil {
  103. return nilVoteStr
  104. }
  105. var typeString string
  106. switch vote.Type {
  107. case tmproto.PrevoteType:
  108. typeString = "Prevote"
  109. case tmproto.PrecommitType:
  110. typeString = "Precommit"
  111. default:
  112. panic("Unknown vote type")
  113. }
  114. return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X @ %s}",
  115. vote.ValidatorIndex,
  116. tmbytes.Fingerprint(vote.ValidatorAddress),
  117. vote.Height,
  118. vote.Round,
  119. vote.Type,
  120. typeString,
  121. tmbytes.Fingerprint(vote.BlockID.Hash),
  122. tmbytes.Fingerprint(vote.Signature),
  123. CanonicalTime(vote.Timestamp),
  124. )
  125. }
  126. func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error {
  127. if !bytes.Equal(pubKey.Address(), vote.ValidatorAddress) {
  128. return ErrVoteInvalidValidatorAddress
  129. }
  130. v := vote.ToProto()
  131. if !pubKey.VerifyBytes(VoteSignBytes(chainID, v), vote.Signature) {
  132. return ErrVoteInvalidSignature
  133. }
  134. return nil
  135. }
  136. // ValidateBasic performs basic validation.
  137. func (vote *Vote) ValidateBasic() error {
  138. if !IsVoteTypeValid(vote.Type) {
  139. return errors.New("invalid Type")
  140. }
  141. if vote.Height < 0 {
  142. return errors.New("negative Height")
  143. }
  144. if vote.Round < 0 {
  145. return errors.New("negative Round")
  146. }
  147. // NOTE: Timestamp validation is subtle and handled elsewhere.
  148. if err := vote.BlockID.ValidateBasic(); err != nil {
  149. return fmt.Errorf("wrong BlockID: %v", err)
  150. }
  151. // BlockID.ValidateBasic would not err if we for instance have an empty hash but a
  152. // non-empty PartsSetHeader:
  153. if !vote.BlockID.IsZero() && !vote.BlockID.IsComplete() {
  154. return fmt.Errorf("blockID must be either empty or complete, got: %v", vote.BlockID)
  155. }
  156. if len(vote.ValidatorAddress) != crypto.AddressSize {
  157. return fmt.Errorf("expected ValidatorAddress size to be %d bytes, got %d bytes",
  158. crypto.AddressSize,
  159. len(vote.ValidatorAddress),
  160. )
  161. }
  162. if vote.ValidatorIndex < 0 {
  163. return errors.New("negative ValidatorIndex")
  164. }
  165. if len(vote.Signature) == 0 {
  166. return errors.New("signature is missing")
  167. }
  168. if len(vote.Signature) > MaxSignatureSize {
  169. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  170. }
  171. return nil
  172. }
  173. // ToProto converts the handwritten type to proto generated type
  174. // return type, nil if everything converts safely, otherwise nil, error
  175. func (vote *Vote) ToProto() *tmproto.Vote {
  176. if vote == nil {
  177. return nil
  178. }
  179. return &tmproto.Vote{
  180. Type: vote.Type,
  181. Height: vote.Height,
  182. Round: vote.Round,
  183. BlockID: vote.BlockID.ToProto(),
  184. Timestamp: vote.Timestamp,
  185. ValidatorAddress: vote.ValidatorAddress,
  186. ValidatorIndex: vote.ValidatorIndex,
  187. Signature: vote.Signature,
  188. }
  189. }
  190. //FromProto converts a proto generetad type to a handwritten type
  191. // return type, nil if everything converts safely, otherwise nil, error
  192. func VoteFromProto(pv *tmproto.Vote) (*Vote, error) {
  193. if pv == nil {
  194. return nil, errors.New("nil vote")
  195. }
  196. blockID, err := BlockIDFromProto(&pv.BlockID)
  197. if err != nil {
  198. return nil, err
  199. }
  200. vote := new(Vote)
  201. vote.Type = pv.Type
  202. vote.Height = pv.Height
  203. vote.Round = pv.Round
  204. vote.BlockID = *blockID
  205. vote.Timestamp = pv.Timestamp
  206. vote.ValidatorAddress = pv.ValidatorAddress
  207. vote.ValidatorIndex = pv.ValidatorIndex
  208. vote.Signature = pv.Signature
  209. return vote, vote.ValidateBasic()
  210. }