From 5e513fbb4fef8ff5908f07869997b766d64a8603 Mon Sep 17 00:00:00 2001 From: William Banfield Date: Thu, 20 Oct 2022 11:33:42 -0400 Subject: [PATCH] clean up blocksync proto wrap func --- blocksync/msgs.go | 21 +++------------------ blocksync/reactor.go | 36 ++++++------------------------------ 2 files changed, 9 insertions(+), 48 deletions(-) diff --git a/blocksync/msgs.go b/blocksync/msgs.go index 2d1792fe5..680380110 100644 --- a/blocksync/msgs.go +++ b/blocksync/msgs.go @@ -19,22 +19,7 @@ const ( BlockResponseMessageFieldKeySize ) -// EncodeMsg encodes a Protobuf message -func EncodeMsg(pb proto.Message) ([]byte, error) { - msg, err := toWrappedMessage(pb) - if err != nil { - return nil, err - } - - bz, err := proto.Marshal(msg) - if err != nil { - return nil, fmt.Errorf("unable to marshal %T: %w", pb, err) - } - - return bz, nil -} - -func toWrappedMessage(pb proto.Message) (proto.Message, error) { +func MustWrapMessage(pb proto.Message) proto.Message { msg := bcproto.Message{} switch pb := pb.(type) { @@ -49,10 +34,10 @@ func toWrappedMessage(pb proto.Message) (proto.Message, error) { case *bcproto.StatusResponse: msg.Sum = &bcproto.Message_StatusResponse{StatusResponse: pb} default: - return nil, fmt.Errorf("unknown message type %T", pb) + panic(fmt.Errorf("unknown message type %T", pb)) } - return &msg, nil + return &msg } // DecodeMsg decodes a Protobuf message. diff --git a/blocksync/reactor.go b/blocksync/reactor.go index 3e4bd3795..693f6b9cc 100644 --- a/blocksync/reactor.go +++ b/blocksync/reactor.go @@ -150,14 +150,10 @@ func (bcR *Reactor) GetChannels() []*p2p.ChannelDescriptor { // AddPeer implements Reactor by sending our state to peer. func (bcR *Reactor) AddPeer(peer p2p.Peer) { - msg, err := toWrappedMessage(&bcproto.StatusResponse{ + msg := MustWrapMessage(&bcproto.StatusResponse{ Base: bcR.store.Base(), Height: bcR.store.Height(), }) - if err != nil { - bcR.Logger.Error("could not convert msg to proto", "err", err) - return - } e := p2p.Envelope{ ChannelID: BlocksyncChannel, Message: msg, @@ -187,11 +183,7 @@ func (bcR *Reactor) respondToPeer(msg *bcproto.BlockRequest, return false } - wm, err := toWrappedMessage(&bcproto.BlockResponse{Block: bl}) - if err != nil { - bcR.Logger.Error("could not marshal msg", "err", err) - return false - } + wm := MustWrapMessage(&bcproto.BlockResponse{Block: bl}) e := p2p.Envelope{ ChannelID: BlocksyncChannel, Message: wm, @@ -202,11 +194,7 @@ func (bcR *Reactor) respondToPeer(msg *bcproto.BlockRequest, bcR.Logger.Info("Peer asking for a block we don't have", "src", src, "height", msg.Height) - wm, err := toWrappedMessage(&bcproto.NoBlockResponse{Height: msg.Height}) - if err != nil { - bcR.Logger.Error("could not convert msg to protobuf", "err", err) - return false - } + wm := MustWrapMessage(&bcproto.NoBlockResponse{Height: msg.Height}) e := p2p.Envelope{ ChannelID: BlocksyncChannel, Message: wm, @@ -244,14 +232,10 @@ func (bcR *Reactor) Receive(chID byte, src p2p.Peer, msgBytes []byte) { bcR.pool.AddBlock(src.ID(), bi, len(msgBytes)) case *bcproto.StatusRequest: // Send peer our state. - wm, err := toWrappedMessage(&bcproto.StatusResponse{ + wm := MustWrapMessage(&bcproto.StatusResponse{ Height: bcR.store.Height(), Base: bcR.store.Base(), }) - if err != nil { - bcR.Logger.Error("could not convert msg to wrapped protobuf", "err", err) - return - } e := p2p.Envelope{ ChannelID: BlocksyncChannel, Message: wm, @@ -302,11 +286,7 @@ func (bcR *Reactor) poolRoutine(stateSynced bool) { if peer == nil { continue } - wm, err := toWrappedMessage(&bcproto.BlockRequest{Height: request.Height}) - if err != nil { - bcR.Logger.Error("could not convert msg to wrapped proto", "err", err) - continue - } + wm := MustWrapMessage(&bcproto.BlockRequest{Height: request.Height}) e := p2p.Envelope{ ChannelID: BlocksyncChannel, Message: wm, @@ -450,11 +430,7 @@ FOR_LOOP: // BroadcastStatusRequest broadcasts `BlockStore` base and height. func (bcR *Reactor) BroadcastStatusRequest() error { - wm, err := toWrappedMessage(&bcproto.StatusRequest{}) - if err != nil { - bcR.Logger.Error("could not convert msg to proto", "err", err) - return fmt.Errorf("could not convert msg to proto: %w", err) - } + wm := MustWrapMessage(&bcproto.StatusRequest{}) e := p2p.Envelope{ ChannelID: BlocksyncChannel, Message: wm,