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.

97 lines
2.7 KiB

  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/tendermint/tendermint/libs/bytes"
  7. tmtime "github.com/tendermint/tendermint/types/time"
  8. )
  9. var (
  10. ErrInvalidBlockPartSignature = errors.New("error invalid block part signature")
  11. ErrInvalidBlockPartHash = errors.New("error invalid block part hash")
  12. )
  13. // Proposal defines a block proposal for the consensus.
  14. // It refers to the block by BlockID field.
  15. // It must be signed by the correct proposer for the given Height/Round
  16. // to be considered valid. It may depend on votes from a previous round,
  17. // a so-called Proof-of-Lock (POL) round, as noted in the POLRound.
  18. // If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound.
  19. type Proposal struct {
  20. Type SignedMsgType
  21. Height int64 `json:"height"`
  22. Round int `json:"round"`
  23. POLRound int `json:"pol_round"` // -1 if null.
  24. BlockID BlockID `json:"block_id"`
  25. Timestamp time.Time `json:"timestamp"`
  26. Signature []byte `json:"signature"`
  27. }
  28. // NewProposal returns a new Proposal.
  29. // If there is no POLRound, polRound should be -1.
  30. func NewProposal(height int64, round int, polRound int, blockID BlockID) *Proposal {
  31. return &Proposal{
  32. Type: ProposalType,
  33. Height: height,
  34. Round: round,
  35. BlockID: blockID,
  36. POLRound: polRound,
  37. Timestamp: tmtime.Now(),
  38. }
  39. }
  40. // ValidateBasic performs basic validation.
  41. func (p *Proposal) ValidateBasic() error {
  42. if p.Type != ProposalType {
  43. return errors.New("invalid Type")
  44. }
  45. if p.Height < 0 {
  46. return errors.New("negative Height")
  47. }
  48. if p.Round < 0 {
  49. return errors.New("negative Round")
  50. }
  51. if p.POLRound < -1 {
  52. return errors.New("negative POLRound (exception: -1)")
  53. }
  54. if err := p.BlockID.ValidateBasic(); err != nil {
  55. return fmt.Errorf("wrong BlockID: %v", err)
  56. }
  57. // ValidateBasic above would pass even if the BlockID was empty:
  58. if !p.BlockID.IsComplete() {
  59. return fmt.Errorf("expected a complete, non-empty BlockID, got: %v", p.BlockID)
  60. }
  61. // NOTE: Timestamp validation is subtle and handled elsewhere.
  62. if len(p.Signature) == 0 {
  63. return errors.New("signature is missing")
  64. }
  65. if len(p.Signature) > MaxSignatureSize {
  66. return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize)
  67. }
  68. return nil
  69. }
  70. // String returns a string representation of the Proposal.
  71. func (p *Proposal) String() string {
  72. return fmt.Sprintf("Proposal{%v/%v (%v, %v) %X @ %s}",
  73. p.Height,
  74. p.Round,
  75. p.BlockID,
  76. p.POLRound,
  77. bytes.Fingerprint(p.Signature),
  78. CanonicalTime(p.Timestamp))
  79. }
  80. // SignBytes returns the Proposal bytes for signing
  81. func (p *Proposal) SignBytes(chainID string) []byte {
  82. bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeProposal(chainID, p))
  83. if err != nil {
  84. panic(err)
  85. }
  86. return bz
  87. }