github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/proposal.go (about)

     1  package types
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  
     8  	//. "github.com/intfoundation/go-common"
     9  	"github.com/intfoundation/go-crypto"
    10  	"github.com/intfoundation/go-wire"
    11  	// crypto "github.com/intfoundation/go-crypto"
    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  type Proposal struct {
    20  	NodeID           string           `json:"node_id"`
    21  	Height           uint64           `json:"height"`
    22  	Round            int              `json:"round"`
    23  	Hash             []byte           `json:"hash"`
    24  	BlockPartsHeader PartSetHeader    `json:"block_parts_header"`
    25  	POLRound         int              `json:"pol_round"`    // -1 if null.
    26  	POLBlockID       BlockID          `json:"pol_block_id"` // zero if null.
    27  	ProposerNetAddr  string           `json:"proposer_net_addr"`
    28  	ProposerPeerKey  string           `json:"proposer_peer_key"`
    29  	Signature        crypto.Signature `json:"signature"`
    30  }
    31  
    32  // polRound: -1 if no polRound.
    33  func NewProposal(height uint64, round int, hash []byte, blockPartsHeader PartSetHeader, polRound int, polBlockID BlockID, peerKey string) *Proposal {
    34  	return &Proposal{
    35  		Height:           height,
    36  		Round:            round,
    37  		Hash:             hash,
    38  		BlockPartsHeader: blockPartsHeader,
    39  		POLRound:         polRound,
    40  		POLBlockID:       polBlockID,
    41  		ProposerPeerKey:  peerKey,
    42  	}
    43  }
    44  
    45  func (p *Proposal) String() string {
    46  	return fmt.Sprintf("Proposal{%v/%v %v %v (%v,%v) %s %s %v}", p.Height, p.Round, p.Hash,
    47  		p.BlockPartsHeader, p.POLRound, p.POLBlockID, p.ProposerNetAddr, p.ProposerPeerKey, p.Signature)
    48  }
    49  
    50  func (p *Proposal) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) {
    51  	wire.WriteJSON(CanonicalJSONOnceProposal{
    52  		ChainID:  chainID,
    53  		Proposal: CanonicalProposal(p),
    54  	}, w, n, err)
    55  }
    56  
    57  func (p *Proposal) BlockHash() []byte {
    58  	if p == nil {
    59  		return []byte{}
    60  	} else {
    61  		return p.BlockPartsHeader.Hash
    62  	}
    63  }
    64  
    65  func (p *Proposal) BlockHeaderHash() []byte {
    66  	if p == nil {
    67  		return []byte{}
    68  	} else {
    69  		return p.Hash
    70  	}
    71  }