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.

186 lines
5.6 KiB

  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/tendermint/tendermint/internal/libs/protoio"
  7. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  8. tmtime "github.com/tendermint/tendermint/libs/time"
  9. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  10. )
  11. var (
  12. ErrInvalidBlockPartSignature = errors.New("error invalid block part signature")
  13. ErrInvalidBlockPartHash = errors.New("error invalid block part hash")
  14. )
  15. // Proposal defines a block proposal for the consensus.
  16. // It refers to the block by BlockID field.
  17. // It must be signed by the correct proposer for the given Height/Round
  18. // to be considered valid. It may depend on votes from a previous round,
  19. // a so-called Proof-of-Lock (POL) round, as noted in the POLRound.
  20. // If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound.
  21. type Proposal struct {
  22. Type tmproto.SignedMsgType
  23. Height int64 `json:"height,string"`
  24. Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds
  25. POLRound int32 `json:"pol_round"` // -1 if null.
  26. BlockID BlockID `json:"block_id"`
  27. Timestamp time.Time `json:"timestamp"`
  28. Signature []byte `json:"signature"`
  29. }
  30. // NewProposal returns a new Proposal.
  31. // If there is no POLRound, polRound should be -1.
  32. func NewProposal(height int64, round int32, polRound int32, blockID BlockID, ts time.Time) *Proposal {
  33. return &Proposal{
  34. Type: tmproto.ProposalType,
  35. Height: height,
  36. Round: round,
  37. BlockID: blockID,
  38. POLRound: polRound,
  39. Timestamp: tmtime.Canonical(ts),
  40. }
  41. }
  42. // ValidateBasic performs basic validation.
  43. func (p *Proposal) ValidateBasic() error {
  44. if p.Type != tmproto.ProposalType {
  45. return errors.New("invalid Type")
  46. }
  47. if p.Height < 0 {
  48. return errors.New("negative Height")
  49. }
  50. if p.Round < 0 {
  51. return errors.New("negative Round")
  52. }
  53. if p.POLRound < -1 {
  54. return errors.New("negative POLRound (exception: -1)")
  55. }
  56. if err := p.BlockID.ValidateBasic(); err != nil {
  57. return fmt.Errorf("wrong BlockID: %w", err)
  58. }
  59. // ValidateBasic above would pass even if the BlockID was empty:
  60. if !p.BlockID.IsComplete() {
  61. return fmt.Errorf("expected a complete, non-empty BlockID, got: %v", p.BlockID)
  62. }
  63. // NOTE: Timestamp validation is subtle and handled elsewhere.
  64. if len(p.Signature) == 0 {
  65. return errors.New("signature is missing")
  66. }
  67. if len(p.Signature) > MaxSignatureSize {
  68. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  69. }
  70. return nil
  71. }
  72. // IsTimely validates that the block timestamp is 'timely' according to the proposer-based timestamp algorithm.
  73. // To evaluate if a block is timely, its timestamp is compared to the local time of the validator along with the
  74. // configured Precision and MsgDelay parameters.
  75. // Specifically, a proposed block timestamp is considered timely if it is satisfies the following inequalities:
  76. //
  77. // localtime >= proposedBlockTime - Precision
  78. // localtime <= proposedBlockTime + MsgDelay + Precision
  79. //
  80. // Note: If the proposal is for the `initialHeight` the second inequality is not checked. This is because
  81. // the timestamp in this case is set to the preconfigured genesis time.
  82. // For more information on the meaning of 'timely', see the proposer-based timestamp specification:
  83. // https://github.com/tendermint/spec/tree/master/spec/consensus/proposer-based-timestamp
  84. func (p *Proposal) IsTimely(recvTime time.Time, sp SynchronyParams, initialHeight int64) bool {
  85. // lhs is `proposedBlockTime - Precision` in the first inequality
  86. lhs := p.Timestamp.Add(-sp.Precision)
  87. // rhs is `proposedBlockTime + MsgDelay + Precision` in the second inequality
  88. rhs := p.Timestamp.Add(sp.MessageDelay).Add(sp.Precision)
  89. if recvTime.Before(lhs) || (p.Height != initialHeight && recvTime.After(rhs)) {
  90. return false
  91. }
  92. return true
  93. }
  94. // String returns a string representation of the Proposal.
  95. //
  96. // 1. height
  97. // 2. round
  98. // 3. block ID
  99. // 4. POL round
  100. // 5. first 6 bytes of signature
  101. // 6. timestamp
  102. //
  103. // See BlockID#String.
  104. func (p *Proposal) String() string {
  105. return fmt.Sprintf("Proposal{%v/%v (%v, %v) %X @ %s}",
  106. p.Height,
  107. p.Round,
  108. p.BlockID,
  109. p.POLRound,
  110. tmbytes.Fingerprint(p.Signature),
  111. CanonicalTime(p.Timestamp))
  112. }
  113. // ProposalSignBytes returns the proto-encoding of the canonicalized Proposal,
  114. // for signing. Panics if the marshaling fails.
  115. //
  116. // The encoded Protobuf message is varint length-prefixed (using MarshalDelimited)
  117. // for backwards-compatibility with the Amino encoding, due to e.g. hardware
  118. // devices that rely on this encoding.
  119. //
  120. // See CanonicalizeProposal
  121. func ProposalSignBytes(chainID string, p *tmproto.Proposal) []byte {
  122. pb := CanonicalizeProposal(chainID, p)
  123. bz, err := protoio.MarshalDelimited(&pb)
  124. if err != nil {
  125. panic(err)
  126. }
  127. return bz
  128. }
  129. // ToProto converts Proposal to protobuf
  130. func (p *Proposal) ToProto() *tmproto.Proposal {
  131. if p == nil {
  132. return &tmproto.Proposal{}
  133. }
  134. pb := new(tmproto.Proposal)
  135. pb.BlockID = p.BlockID.ToProto()
  136. pb.Type = p.Type
  137. pb.Height = p.Height
  138. pb.Round = p.Round
  139. pb.PolRound = p.POLRound
  140. pb.Timestamp = p.Timestamp
  141. pb.Signature = p.Signature
  142. return pb
  143. }
  144. // FromProto sets a protobuf Proposal to the given pointer.
  145. // It returns an error if the proposal is invalid.
  146. func ProposalFromProto(pp *tmproto.Proposal) (*Proposal, error) {
  147. if pp == nil {
  148. return nil, errors.New("nil proposal")
  149. }
  150. p := new(Proposal)
  151. blockID, err := BlockIDFromProto(&pp.BlockID)
  152. if err != nil {
  153. return nil, err
  154. }
  155. p.BlockID = *blockID
  156. p.Type = pp.Type
  157. p.Height = pp.Height
  158. p.Round = pp.Round
  159. p.POLRound = pp.PolRound
  160. p.Timestamp = pp.Timestamp
  161. p.Signature = pp.Signature
  162. return p, p.ValidateBasic()
  163. }