clean up blocksync proto wrap func

This commit is contained in:
William Banfield
2022-10-20 11:33:42 -04:00
parent d677121ae0
commit 5e513fbb4f
2 changed files with 9 additions and 48 deletions

View File

@@ -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.

View File

@@ -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,