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

     1  package statesync
     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 *ChunkRequest:
    14  		m.Sum = &Message_ChunkRequest{ChunkRequest: msg}
    15  
    16  	case *ChunkResponse:
    17  		m.Sum = &Message_ChunkResponse{ChunkResponse: msg}
    18  
    19  	case *SnapshotsRequest:
    20  		m.Sum = &Message_SnapshotsRequest{SnapshotsRequest: msg}
    21  
    22  	case *SnapshotsResponse:
    23  		m.Sum = &Message_SnapshotsResponse{SnapshotsResponse: 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_ChunkRequest:
    49  		return m.GetChunkRequest(), nil
    50  
    51  	case *Message_ChunkResponse:
    52  		return m.GetChunkResponse(), nil
    53  
    54  	case *Message_SnapshotsRequest:
    55  		return m.GetSnapshotsRequest(), nil
    56  
    57  	case *Message_SnapshotsResponse:
    58  		return m.GetSnapshotsResponse(), 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  	switch msg := m.Sum.(type) {
    84  	case *Message_ChunkRequest:
    85  		if m.GetChunkRequest().Height == 0 {
    86  			return errors.New("height cannot be 0")
    87  		}
    88  
    89  	case *Message_ChunkResponse:
    90  		if m.GetChunkResponse().Height == 0 {
    91  			return errors.New("height cannot be 0")
    92  		}
    93  		if m.GetChunkResponse().Missing && len(m.GetChunkResponse().Chunk) > 0 {
    94  			return errors.New("missing chunk cannot have contents")
    95  		}
    96  		if !m.GetChunkResponse().Missing && m.GetChunkResponse().Chunk == nil {
    97  			return errors.New("chunk cannot be nil")
    98  		}
    99  
   100  	case *Message_SnapshotsRequest:
   101  
   102  	case *Message_SnapshotsResponse:
   103  		if m.GetSnapshotsResponse().Height == 0 {
   104  			return errors.New("height cannot be 0")
   105  		}
   106  		if len(m.GetSnapshotsResponse().Hash) == 0 {
   107  			return errors.New("snapshot has no hash")
   108  		}
   109  		if m.GetSnapshotsResponse().Chunks == 0 {
   110  			return errors.New("snapshot has no chunks")
   111  		}
   112  
   113  	case *Message_LightBlockRequest:
   114  		if m.GetLightBlockRequest().Height == 0 {
   115  			return errors.New("height cannot be 0")
   116  		}
   117  
   118  	// light block validation handled by the backfill process
   119  	case *Message_LightBlockResponse:
   120  
   121  	case *Message_ParamsRequest:
   122  		if m.GetParamsRequest().Height == 0 {
   123  			return errors.New("height cannot be 0")
   124  		}
   125  
   126  	case *Message_ParamsResponse:
   127  		resp := m.GetParamsResponse()
   128  		if resp.Height == 0 {
   129  			return errors.New("height cannot be 0")
   130  		}
   131  
   132  	default:
   133  		return fmt.Errorf("unknown message type: %T", msg)
   134  	}
   135  
   136  	return nil
   137  }