diff --git a/api/blockdaoservice.go b/api/blockdaoservice.go index 3a1b62fa83..4a7da209c0 100644 --- a/api/blockdaoservice.go +++ b/api/blockdaoservice.go @@ -152,12 +152,7 @@ func (service *blockDAOService) FooterByHeight(_ context.Context, request *block if err != nil { return nil, err } - footerpb, err := footer.ConvertToBlockFooterPb() - if err != nil { - return nil, err - } - return &blockdaopb.FooterResponse{ - Footer: footerpb, + Footer: footer.Proto(), }, nil } diff --git a/blockchain/block/block.go b/blockchain/block/block.go index 36d281d4de..edf1ab91fb 100644 --- a/blockchain/block/block.go +++ b/blockchain/block/block.go @@ -32,14 +32,10 @@ type Block struct { // ConvertToBlockPb converts Block to Block func (b *Block) ConvertToBlockPb() *iotextypes.Block { - footer, err := b.ConvertToBlockFooterPb() - if err != nil { - log.L().Panic("failed to convert block footer to protobuf message") - } return &iotextypes.Block{ Header: b.Header.Proto(), Body: b.Body.Proto(), - Footer: footer, + Footer: b.Footer.Proto(), } } diff --git a/blockchain/block/footer.go b/blockchain/block/footer.go index a79028760a..9922ba5f2a 100644 --- a/blockchain/block/footer.go +++ b/blockchain/block/footer.go @@ -21,20 +21,16 @@ type Footer struct { commitTime time.Time } -// ConvertToBlockFooterPb converts BlockFooter -func (f *Footer) ConvertToBlockFooterPb() (*iotextypes.BlockFooter, error) { +// Proto converts BlockFooter +func (f *Footer) Proto() *iotextypes.BlockFooter { pb := iotextypes.BlockFooter{} commitTime := timestamppb.New(f.commitTime) pb.Timestamp = commitTime pb.Endorsements = []*iotextypes.Endorsement{} for _, en := range f.endorsements { - ePb, err := en.Proto() - if err != nil { - return nil, err - } - pb.Endorsements = append(pb.Endorsements, ePb) + pb.Endorsements = append(pb.Endorsements, en.Proto()) } - return &pb, nil + return &pb } // ConvertFromBlockFooterPb converts BlockFooter to BlockFooter @@ -75,11 +71,7 @@ func (f *Footer) Endorsements() []*endorsement.Endorsement { // Serialize returns the serialized byte stream of the block footer func (f *Footer) Serialize() ([]byte, error) { - pb, err := f.ConvertToBlockFooterPb() - if err != nil { - return nil, err - } - return proto.Marshal(pb) + return proto.Marshal(f.Proto()) } // Deserialize loads from the serialized byte stream diff --git a/blockchain/block/footer_test.go b/blockchain/block/footer_test.go index 8aad685f06..9a8c946dd5 100644 --- a/blockchain/block/footer_test.go +++ b/blockchain/block/footer_test.go @@ -20,14 +20,12 @@ import ( func TestConvertToBlockFooterPb(t *testing.T) { require := require.New(t) footer := &Footer{nil, time.Now()} - blockFooter, err := footer.ConvertToBlockFooterPb() - require.NoError(err) + blockFooter := footer.Proto() require.NotNil(blockFooter) require.Equal(0, len(blockFooter.Endorsements)) footer = makeFooter() - blockFooter, err = footer.ConvertToBlockFooterPb() - require.NoError(err) + blockFooter = footer.Proto() require.NotNil(blockFooter) require.Equal(1, len(blockFooter.Endorsements)) } diff --git a/consensus/scheme/rolldpos/blockproposal.go b/consensus/scheme/rolldpos/blockproposal.go index 76cf87308d..e98fd8c0d6 100644 --- a/consensus/scheme/rolldpos/blockproposal.go +++ b/consensus/scheme/rolldpos/blockproposal.go @@ -34,11 +34,7 @@ func (bp *blockProposal) Proto() (*iotextypes.BlockProposal, error) { bPb := bp.block.ConvertToBlockPb() endorsements := []*iotextypes.Endorsement{} for _, en := range bp.proofOfLock { - ePb, err := en.Proto() - if err != nil { - return nil, err - } - endorsements = append(endorsements, ePb) + endorsements = append(endorsements, en.Proto()) } return &iotextypes.BlockProposal{ Block: bPb, diff --git a/consensus/scheme/rolldpos/endorsedconsensusmessage.go b/consensus/scheme/rolldpos/endorsedconsensusmessage.go index fd018c4b53..42c3f4b112 100644 --- a/consensus/scheme/rolldpos/endorsedconsensusmessage.go +++ b/consensus/scheme/rolldpos/endorsedconsensusmessage.go @@ -50,13 +50,9 @@ func (ecm *EndorsedConsensusMessage) Height() uint64 { // Proto converts an endorsement to endorse proto func (ecm *EndorsedConsensusMessage) Proto() (*iotextypes.ConsensusMessage, error) { - ebp, err := ecm.endorsement.Proto() - if err != nil { - return nil, err - } cmsg := &iotextypes.ConsensusMessage{ Height: ecm.height, - Endorsement: ebp, + Endorsement: ecm.endorsement.Proto(), } switch message := ecm.message.(type) { case *ConsensusVote: diff --git a/consensus/scheme/rolldpos/endorsementmanager.go b/consensus/scheme/rolldpos/endorsementmanager.go index de7819bde4..f977c038d3 100644 --- a/consensus/scheme/rolldpos/endorsementmanager.go +++ b/consensus/scheme/rolldpos/endorsementmanager.go @@ -60,11 +60,7 @@ func (ee *endorserEndorsementCollection) toProto(endorser string) (*endorsementp eeProto.Endorser = endorser for topic, endorse := range ee.endorsements { eeProto.Topics = append(eeProto.Topics, uint32(topic)) - ioEndorsement, err := endorse.Proto() - if err != nil { - return nil, err - } - eeProto.Endorsements = append(eeProto.Endorsements, ioEndorsement) + eeProto.Endorsements = append(eeProto.Endorsements, endorse.Proto()) } return eeProto, nil } diff --git a/consensus/scheme/rolldpos/endorsementmanager_test.go b/consensus/scheme/rolldpos/endorsementmanager_test.go index 423f450ca1..2c49090ea6 100644 --- a/consensus/scheme/rolldpos/endorsementmanager_test.go +++ b/consensus/scheme/rolldpos/endorsementmanager_test.go @@ -195,10 +195,8 @@ func TestEndorsementManagerProto(t *testing.T) { require.NoError(em.SetMintedBlock(&b)) //test converting endorsement pb - endProto, err := end.Proto() - require.NoError(err) end2 := &endorsement.Endorsement{} - require.NoError(end2.LoadProto(endProto)) + require.NoError(end2.LoadProto(end.Proto())) require.Equal(end, end2) //test converting emanager pb diff --git a/consensus/scheme/rolldpos/rolldpos_test.go b/consensus/scheme/rolldpos/rolldpos_test.go index 1301a230f6..cbcb3ccd42 100644 --- a/consensus/scheme/rolldpos/rolldpos_test.go +++ b/consensus/scheme/rolldpos/rolldpos_test.go @@ -179,9 +179,7 @@ func makeBlock(t *testing.T, accountIndex, numOfEndosements int, makeInvalidEndo } en, err := endorsement.Endorse(identityset.PrivateKey(i), consensusVote, timeTime) require.NoError(t, err) - enProto, err := en.Proto() - require.NoError(t, err) - typesFooter.Endorsements = append(typesFooter.Endorsements, enProto) + typesFooter.Endorsements = append(typesFooter.Endorsements, en.Proto()) } ts := timestamppb.New(time.Unix(int64(unixTime), 0)) typesFooter.Timestamp = ts diff --git a/endorsement/endorsement.go b/endorsement/endorsement.go index fcc5f7bdc2..6884a4ccd0 100644 --- a/endorsement/endorsement.go +++ b/endorsement/endorsement.go @@ -113,13 +113,13 @@ func (en *Endorsement) Signature() []byte { } // Proto converts an endorsement to protobuf message -func (en *Endorsement) Proto() (*iotextypes.Endorsement, error) { +func (en *Endorsement) Proto() *iotextypes.Endorsement { ts := timestamppb.New(en.ts) return &iotextypes.Endorsement{ Timestamp: ts, Endorser: en.endorser.Bytes(), Signature: en.Signature(), - }, nil + } } // LoadProto converts a protobuf message to endorsement