github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/consensus/neatcon/types/proposal.go (about)

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