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.

93 lines
2.5 KiB

  1. package types
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. cmn "github.com/tendermint/tendermint/libs/common"
  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. // NOTE: Timestamp validation is subtle and handled elsewhere.
  58. if len(p.Signature) == 0 {
  59. return errors.New("Signature is missing")
  60. }
  61. if len(p.Signature) > MaxSignatureSize {
  62. return fmt.Errorf("Signature is too big (max: %d)", MaxSignatureSize)
  63. }
  64. return nil
  65. }
  66. // String returns a string representation of the Proposal.
  67. func (p *Proposal) String() string {
  68. return fmt.Sprintf("Proposal{%v/%v (%v, %v) %X @ %s}",
  69. p.Height,
  70. p.Round,
  71. p.BlockID,
  72. p.POLRound,
  73. cmn.Fingerprint(p.Signature),
  74. CanonicalTime(p.Timestamp))
  75. }
  76. // SignBytes returns the Proposal bytes for signing
  77. func (p *Proposal) SignBytes(chainID string) []byte {
  78. bz, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeProposal(chainID, p))
  79. if err != nil {
  80. panic(err)
  81. }
  82. return bz
  83. }