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.

159 lines
4.0 KiB

  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. tmbytes "github.com/tendermint/tendermint/libs/bytes"
  7. "github.com/tendermint/tendermint/libs/protoio"
  8. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  9. tmtime "github.com/tendermint/tendermint/types/time"
  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"`
  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) *Proposal {
  33. return &Proposal{
  34. Type: tmproto.ProposalType,
  35. Height: height,
  36. Round: round,
  37. BlockID: blockID,
  38. POLRound: polRound,
  39. Timestamp: tmtime.Now(),
  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: %v", 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. // String returns a string representation of the Proposal.
  73. //
  74. // 1. height
  75. // 2. round
  76. // 3. block ID
  77. // 4. POL round
  78. // 5. first 6 bytes of signature
  79. // 6. timestamp
  80. //
  81. // See BlockID#String.
  82. func (p *Proposal) String() string {
  83. return fmt.Sprintf("Proposal{%v/%v (%v, %v) %X @ %s}",
  84. p.Height,
  85. p.Round,
  86. p.BlockID,
  87. p.POLRound,
  88. tmbytes.Fingerprint(p.Signature),
  89. CanonicalTime(p.Timestamp))
  90. }
  91. // ProposalSignBytes returns the proto-encoding of the canonicalized Proposal,
  92. // for signing.
  93. //
  94. // Panics if the marshaling fails.
  95. //
  96. // See CanonicalizeProposal
  97. func ProposalSignBytes(chainID string, p *tmproto.Proposal) []byte {
  98. pb := CanonicalizeProposal(chainID, p)
  99. bz, err := protoio.MarshalDelimited(&pb)
  100. if err != nil {
  101. panic(err)
  102. }
  103. return bz
  104. }
  105. // ToProto converts Proposal to protobuf
  106. func (p *Proposal) ToProto() *tmproto.Proposal {
  107. if p == nil {
  108. return &tmproto.Proposal{}
  109. }
  110. pb := new(tmproto.Proposal)
  111. pb.BlockID = p.BlockID.ToProto()
  112. pb.Type = p.Type
  113. pb.Height = p.Height
  114. pb.Round = p.Round
  115. pb.PolRound = p.POLRound
  116. pb.Timestamp = p.Timestamp
  117. pb.Signature = p.Signature
  118. return pb
  119. }
  120. // FromProto sets a protobuf Proposal to the given pointer.
  121. // It returns an error if the proposal is invalid.
  122. func ProposalFromProto(pp *tmproto.Proposal) (*Proposal, error) {
  123. if pp == nil {
  124. return nil, errors.New("nil proposal")
  125. }
  126. p := new(Proposal)
  127. blockID, err := BlockIDFromProto(&pp.BlockID)
  128. if err != nil {
  129. return nil, err
  130. }
  131. p.BlockID = *blockID
  132. p.Type = pp.Type
  133. p.Height = pp.Height
  134. p.Round = pp.Round
  135. p.POLRound = pp.PolRound
  136. p.Timestamp = pp.Timestamp
  137. p.Signature = pp.Signature
  138. return p, p.ValidateBasic()
  139. }