buildable

This commit is contained in:
William Banfield
2022-10-20 15:39:24 -04:00
parent 9b2c2ee3af
commit 2b7b2f2012
2 changed files with 8 additions and 95 deletions

View File

@@ -7,12 +7,12 @@ import (
)
// Wrap implements the p2p Wrapper interface and wraps a PEX message.
func (m *PexMessage) Wrap(pb proto.Message) error {
func (m *Message) Wrap(pb proto.Message) error {
switch msg := pb.(type) {
case *PexRequest:
m.Sum = &PexMessage_PexRequest{PexRequest: msg}
case *PexResponse:
m.Sum = &PexMessage_PexResponse{PexResponse: msg}
m.Sum = &Message_PexRequest{PexRequest: msg}
case *PexAddrs:
m.Sum = &Message_PexAddrs{PexAddrs: msg}
default:
return fmt.Errorf("unknown pex message: %T", msg)
}
@@ -21,12 +21,12 @@ func (m *PexMessage) Wrap(pb proto.Message) error {
// Unwrap implements the p2p Wrapper interface and unwraps a wrapped PEX
// message.
func (m *PexMessage) Unwrap() (proto.Message, error) {
func (m *Message) Unwrap() (proto.Message, error) {
switch msg := m.Sum.(type) {
case *PexMessage_PexRequest:
case *Message_PexRequest:
return msg.PexRequest, nil
case *PexMessage_PexResponse:
return msg.PexResponse, nil
case *Message_PexAddrs:
return msg.PexAddrs, nil
default:
return nil, fmt.Errorf("unknown pex message: %T", msg)
}

View File

@@ -1,7 +1,6 @@
package statesync
import (
"errors"
"fmt"
"github.com/gogo/protobuf/proto"
@@ -22,18 +21,6 @@ func (m *Message) Wrap(pb proto.Message) error {
case *SnapshotsResponse:
m.Sum = &Message_SnapshotsResponse{SnapshotsResponse: msg}
case *LightBlockRequest:
m.Sum = &Message_LightBlockRequest{LightBlockRequest: msg}
case *LightBlockResponse:
m.Sum = &Message_LightBlockResponse{LightBlockResponse: msg}
case *ParamsRequest:
m.Sum = &Message_ParamsRequest{ParamsRequest: msg}
case *ParamsResponse:
m.Sum = &Message_ParamsResponse{ParamsResponse: msg}
default:
return fmt.Errorf("unknown message: %T", msg)
}
@@ -57,81 +44,7 @@ func (m *Message) Unwrap() (proto.Message, error) {
case *Message_SnapshotsResponse:
return m.GetSnapshotsResponse(), nil
case *Message_LightBlockRequest:
return m.GetLightBlockRequest(), nil
case *Message_LightBlockResponse:
return m.GetLightBlockResponse(), nil
case *Message_ParamsRequest:
return m.GetParamsRequest(), nil
case *Message_ParamsResponse:
return m.GetParamsResponse(), nil
default:
return nil, fmt.Errorf("unknown message: %T", msg)
}
}
// Validate validates the message returning an error upon failure.
func (m *Message) Validate() error {
if m == nil {
return errors.New("message cannot be nil")
}
switch msg := m.Sum.(type) {
case *Message_ChunkRequest:
if m.GetChunkRequest().Height == 0 {
return errors.New("height cannot be 0")
}
case *Message_ChunkResponse:
if m.GetChunkResponse().Height == 0 {
return errors.New("height cannot be 0")
}
if m.GetChunkResponse().Missing && len(m.GetChunkResponse().Chunk) > 0 {
return errors.New("missing chunk cannot have contents")
}
if !m.GetChunkResponse().Missing && m.GetChunkResponse().Chunk == nil {
return errors.New("chunk cannot be nil")
}
case *Message_SnapshotsRequest:
case *Message_SnapshotsResponse:
if m.GetSnapshotsResponse().Height == 0 {
return errors.New("height cannot be 0")
}
if len(m.GetSnapshotsResponse().Hash) == 0 {
return errors.New("snapshot has no hash")
}
if m.GetSnapshotsResponse().Chunks == 0 {
return errors.New("snapshot has no chunks")
}
case *Message_LightBlockRequest:
if m.GetLightBlockRequest().Height == 0 {
return errors.New("height cannot be 0")
}
// light block validation handled by the backfill process
case *Message_LightBlockResponse:
case *Message_ParamsRequest:
if m.GetParamsRequest().Height == 0 {
return errors.New("height cannot be 0")
}
case *Message_ParamsResponse:
resp := m.GetParamsResponse()
if resp.Height == 0 {
return errors.New("height cannot be 0")
}
default:
return fmt.Errorf("unknown message type: %T", msg)
}
return nil
}