Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[block] simplify ConvertToBlockFooterPb() #4465

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions api/blockdaoservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 1 addition & 5 deletions blockchain/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

Expand Down
18 changes: 5 additions & 13 deletions blockchain/block/footer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

@dustinxie dustinxie Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it always return nil, so remove from return value

}

// ConvertFromBlockFooterPb converts BlockFooter to BlockFooter
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions blockchain/block/footer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
6 changes: 1 addition & 5 deletions consensus/scheme/rolldpos/blockproposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions consensus/scheme/rolldpos/endorsedconsensusmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions consensus/scheme/rolldpos/endorsementmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions consensus/scheme/rolldpos/endorsementmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions consensus/scheme/rolldpos/rolldpos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions endorsement/endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member Author

@dustinxie dustinxie Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always returns nil, so remove error from return value

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
Expand Down