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.

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