github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/proto/tendermint/dbsync/message.go (about)

     1  package dbsync
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/gogo/protobuf/proto"
     8  )
     9  
    10  // Wrap implements the p2p Wrapper interface and wraps a state sync proto message.
    11  func (m *Message) Wrap(pb proto.Message) error {
    12  	switch msg := pb.(type) {
    13  	case *MetadataRequest:
    14  		m.Sum = &Message_MetadataRequest{MetadataRequest: msg}
    15  
    16  	case *MetadataResponse:
    17  		m.Sum = &Message_MetadataResponse{MetadataResponse: msg}
    18  
    19  	case *FileRequest:
    20  		m.Sum = &Message_FileRequest{FileRequest: msg}
    21  
    22  	case *FileResponse:
    23  		m.Sum = &Message_FileResponse{FileResponse: msg}
    24  
    25  	case *LightBlockRequest:
    26  		m.Sum = &Message_LightBlockRequest{LightBlockRequest: msg}
    27  
    28  	case *LightBlockResponse:
    29  		m.Sum = &Message_LightBlockResponse{LightBlockResponse: msg}
    30  
    31  	case *ParamsRequest:
    32  		m.Sum = &Message_ParamsRequest{ParamsRequest: msg}
    33  
    34  	case *ParamsResponse:
    35  		m.Sum = &Message_ParamsResponse{ParamsResponse: msg}
    36  
    37  	default:
    38  		return fmt.Errorf("unknown message: %T", msg)
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  // Unwrap implements the p2p Wrapper interface and unwraps a wrapped state sync
    45  // proto message.
    46  func (m *Message) Unwrap() (proto.Message, error) {
    47  	switch msg := m.Sum.(type) {
    48  	case *Message_MetadataRequest:
    49  		return m.GetMetadataRequest(), nil
    50  
    51  	case *Message_MetadataResponse:
    52  		return m.GetMetadataResponse(), nil
    53  
    54  	case *Message_FileRequest:
    55  		return m.GetFileRequest(), nil
    56  
    57  	case *Message_FileResponse:
    58  		return m.GetFileResponse(), nil
    59  
    60  	case *Message_LightBlockRequest:
    61  		return m.GetLightBlockRequest(), nil
    62  
    63  	case *Message_LightBlockResponse:
    64  		return m.GetLightBlockResponse(), nil
    65  
    66  	case *Message_ParamsRequest:
    67  		return m.GetParamsRequest(), nil
    68  
    69  	case *Message_ParamsResponse:
    70  		return m.GetParamsResponse(), nil
    71  
    72  	default:
    73  		return nil, fmt.Errorf("unknown message: %T", msg)
    74  	}
    75  }
    76  
    77  // Validate validates the message returning an error upon failure.
    78  func (m *Message) Validate() error {
    79  	if m == nil {
    80  		return errors.New("message cannot be nil")
    81  	}
    82  
    83  	return nil
    84  }