github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/common/rpc/convert/headers.go (about)

     1  package convert
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"google.golang.org/protobuf/types/known/timestamppb"
     7  
     8  	"github.com/onflow/flow/protobuf/go/flow/entities"
     9  
    10  	"github.com/onflow/flow-go/model/flow"
    11  )
    12  
    13  // BlockHeaderToMessage converts a flow.Header to a protobuf message
    14  func BlockHeaderToMessage(
    15  	h *flow.Header,
    16  	signerIDs flow.IdentifierList,
    17  ) (*entities.BlockHeader, error) {
    18  	id := h.ID()
    19  
    20  	t := timestamppb.New(h.Timestamp)
    21  	var lastViewTC *entities.TimeoutCertificate
    22  	if h.LastViewTC != nil {
    23  		newestQC := h.LastViewTC.NewestQC
    24  		lastViewTC = &entities.TimeoutCertificate{
    25  			View:          h.LastViewTC.View,
    26  			HighQcViews:   h.LastViewTC.NewestQCViews,
    27  			SignerIndices: h.LastViewTC.SignerIndices,
    28  			SigData:       h.LastViewTC.SigData,
    29  			HighestQc: &entities.QuorumCertificate{
    30  				View:          newestQC.View,
    31  				BlockId:       newestQC.BlockID[:],
    32  				SignerIndices: newestQC.SignerIndices,
    33  				SigData:       newestQC.SigData,
    34  			},
    35  		}
    36  	}
    37  	parentVoterIds := IdentifiersToMessages(signerIDs)
    38  
    39  	return &entities.BlockHeader{
    40  		Id:                 id[:],
    41  		ParentId:           h.ParentID[:],
    42  		Height:             h.Height,
    43  		PayloadHash:        h.PayloadHash[:],
    44  		Timestamp:          t,
    45  		View:               h.View,
    46  		ParentView:         h.ParentView,
    47  		ParentVoterIndices: h.ParentVoterIndices,
    48  		ParentVoterIds:     parentVoterIds,
    49  		ParentVoterSigData: h.ParentVoterSigData,
    50  		ProposerId:         h.ProposerID[:],
    51  		ProposerSigData:    h.ProposerSigData,
    52  		ChainId:            h.ChainID.String(),
    53  		LastViewTc:         lastViewTC,
    54  	}, nil
    55  }
    56  
    57  // MessageToBlockHeader converts a protobuf message to a flow.Header
    58  func MessageToBlockHeader(m *entities.BlockHeader) (*flow.Header, error) {
    59  	chainId, err := MessageToChainId(m.ChainId)
    60  	if err != nil {
    61  		return nil, fmt.Errorf("failed to convert ChainId: %w", err)
    62  	}
    63  	var lastViewTC *flow.TimeoutCertificate
    64  	if m.LastViewTc != nil {
    65  		newestQC := m.LastViewTc.HighestQc
    66  		if newestQC == nil {
    67  			return nil, fmt.Errorf("invalid structure newest QC should be present")
    68  		}
    69  		lastViewTC = &flow.TimeoutCertificate{
    70  			View:          m.LastViewTc.View,
    71  			NewestQCViews: m.LastViewTc.HighQcViews,
    72  			SignerIndices: m.LastViewTc.SignerIndices,
    73  			SigData:       m.LastViewTc.SigData,
    74  			NewestQC: &flow.QuorumCertificate{
    75  				View:          newestQC.View,
    76  				BlockID:       MessageToIdentifier(newestQC.BlockId),
    77  				SignerIndices: newestQC.SignerIndices,
    78  				SigData:       newestQC.SigData,
    79  			},
    80  		}
    81  	}
    82  
    83  	return &flow.Header{
    84  		ParentID:           MessageToIdentifier(m.ParentId),
    85  		Height:             m.Height,
    86  		PayloadHash:        MessageToIdentifier(m.PayloadHash),
    87  		Timestamp:          m.Timestamp.AsTime(),
    88  		View:               m.View,
    89  		ParentView:         m.ParentView,
    90  		ParentVoterIndices: m.ParentVoterIndices,
    91  		ParentVoterSigData: m.ParentVoterSigData,
    92  		ProposerID:         MessageToIdentifier(m.ProposerId),
    93  		ProposerSigData:    m.ProposerSigData,
    94  		ChainID:            *chainId,
    95  		LastViewTC:         lastViewTC,
    96  	}, nil
    97  }