github.com/adoriasoft/tendermint@v0.34.0-dev1.0.20200722151356-96d84601a75a/types/proposal.go (about) 1 package types 2 3 import ( 4 "errors" 5 "fmt" 6 "time" 7 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 tmtime "github.com/tendermint/tendermint/types/time" 12 ) 13 14 var ( 15 ErrInvalidBlockPartSignature = errors.New("error invalid block part signature") 16 ErrInvalidBlockPartHash = errors.New("error invalid block part hash") 17 ) 18 19 // Proposal defines a block proposal for the consensus. 20 // It refers to the block by BlockID field. 21 // It must be signed by the correct proposer for the given Height/Round 22 // to be considered valid. It may depend on votes from a previous round, 23 // a so-called Proof-of-Lock (POL) round, as noted in the POLRound. 24 // If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound. 25 type Proposal struct { 26 Type tmproto.SignedMsgType 27 Height int64 `json:"height"` 28 Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds 29 POLRound int32 `json:"pol_round"` // -1 if null. 30 BlockID BlockID `json:"block_id"` 31 Timestamp time.Time `json:"timestamp"` 32 Signature []byte `json:"signature"` 33 } 34 35 // NewProposal returns a new Proposal. 36 // If there is no POLRound, polRound should be -1. 37 func NewProposal(height int64, round int32, polRound int32, blockID BlockID) *Proposal { 38 return &Proposal{ 39 Type: tmproto.ProposalType, 40 Height: height, 41 Round: round, 42 BlockID: blockID, 43 POLRound: polRound, 44 Timestamp: tmtime.Now(), 45 } 46 } 47 48 // ValidateBasic performs basic validation. 49 func (p *Proposal) ValidateBasic() error { 50 if p.Type != tmproto.ProposalType { 51 return errors.New("invalid Type") 52 } 53 if p.Height < 0 { 54 return errors.New("negative Height") 55 } 56 if p.Round < 0 { 57 return errors.New("negative Round") 58 } 59 if p.POLRound < -1 { 60 return errors.New("negative POLRound (exception: -1)") 61 } 62 if err := p.BlockID.ValidateBasic(); err != nil { 63 return fmt.Errorf("wrong BlockID: %v", err) 64 } 65 // ValidateBasic above would pass even if the BlockID was empty: 66 if !p.BlockID.IsComplete() { 67 return fmt.Errorf("expected a complete, non-empty BlockID, got: %v", p.BlockID) 68 } 69 70 // NOTE: Timestamp validation is subtle and handled elsewhere. 71 72 if len(p.Signature) == 0 { 73 return errors.New("signature is missing") 74 } 75 76 if len(p.Signature) > MaxSignatureSize { 77 return fmt.Errorf("signature is too big (max: %d)", MaxSignatureSize) 78 } 79 return nil 80 } 81 82 // String returns a string representation of the Proposal. 83 func (p *Proposal) String() string { 84 return fmt.Sprintf("Proposal{%v/%v (%v, %v) %X @ %s}", 85 p.Height, 86 p.Round, 87 p.BlockID, 88 p.POLRound, 89 tmbytes.Fingerprint(p.Signature), 90 CanonicalTime(p.Timestamp)) 91 } 92 93 // ProposalSignBytes returns the proto-encoding of the canonicalized Proposal, 94 // for signing. 95 // 96 // Panics if the marshaling fails. 97 // 98 // See CanonicalizeProposal 99 func ProposalSignBytes(chainID string, p *tmproto.Proposal) []byte { 100 pb := CanonicalizeProposal(chainID, p) 101 bz, err := protoio.MarshalDelimited(&pb) 102 if err != nil { 103 panic(err) 104 } 105 106 return bz 107 } 108 109 // ToProto converts Proposal to protobuf 110 func (p *Proposal) ToProto() *tmproto.Proposal { 111 if p == nil { 112 return &tmproto.Proposal{} 113 } 114 pb := new(tmproto.Proposal) 115 116 pb.BlockID = p.BlockID.ToProto() 117 pb.Type = p.Type 118 pb.Height = p.Height 119 pb.Round = p.Round 120 pb.PolRound = p.POLRound 121 pb.Timestamp = p.Timestamp 122 pb.Signature = p.Signature 123 124 return pb 125 } 126 127 // FromProto sets a protobuf Proposal to the given pointer. 128 // It returns an error if the proposal is invalid. 129 func ProposalFromProto(pp *tmproto.Proposal) (*Proposal, error) { 130 if pp == nil { 131 return nil, errors.New("nil proposal") 132 } 133 134 p := new(Proposal) 135 136 blockID, err := BlockIDFromProto(&pp.BlockID) 137 if err != nil { 138 return nil, err 139 } 140 141 p.BlockID = *blockID 142 p.Type = pp.Type 143 p.Height = pp.Height 144 p.Round = pp.Round 145 p.POLRound = pp.PolRound 146 p.Timestamp = pp.Timestamp 147 p.Signature = pp.Signature 148 149 return p, p.ValidateBasic() 150 }