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

     1  package blocksync
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/gogo/protobuf/proto"
     8  )
     9  
    10  const (
    11  	BlockResponseMessagePrefixSize   = 4
    12  	BlockResponseMessageFieldKeySize = 1
    13  )
    14  
    15  // Wrap implements the p2p Wrapper interface and wraps a blockchain message.
    16  func (m *Message) Wrap(pb proto.Message) error {
    17  	switch msg := pb.(type) {
    18  	case *BlockRequest:
    19  		m.Sum = &Message_BlockRequest{BlockRequest: msg}
    20  
    21  	case *BlockResponse:
    22  		m.Sum = &Message_BlockResponse{BlockResponse: msg}
    23  
    24  	case *NoBlockResponse:
    25  		m.Sum = &Message_NoBlockResponse{NoBlockResponse: msg}
    26  
    27  	case *StatusRequest:
    28  		m.Sum = &Message_StatusRequest{StatusRequest: msg}
    29  
    30  	case *StatusResponse:
    31  		m.Sum = &Message_StatusResponse{StatusResponse: msg}
    32  
    33  	default:
    34  		return fmt.Errorf("unknown message: %T", msg)
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  // Unwrap implements the p2p Wrapper interface and unwraps a wrapped blockchain
    41  // message.
    42  func (m *Message) Unwrap() (proto.Message, error) {
    43  	switch msg := m.Sum.(type) {
    44  	case *Message_BlockRequest:
    45  		return m.GetBlockRequest(), nil
    46  
    47  	case *Message_BlockResponse:
    48  		return m.GetBlockResponse(), nil
    49  
    50  	case *Message_NoBlockResponse:
    51  		return m.GetNoBlockResponse(), nil
    52  
    53  	case *Message_StatusRequest:
    54  		return m.GetStatusRequest(), nil
    55  
    56  	case *Message_StatusResponse:
    57  		return m.GetStatusResponse(), nil
    58  
    59  	default:
    60  		return nil, fmt.Errorf("unknown message: %T", msg)
    61  	}
    62  }
    63  
    64  // Validate validates the message returning an error upon failure.
    65  func (m *Message) Validate() error {
    66  	if m == nil {
    67  		return errors.New("message cannot be nil")
    68  	}
    69  
    70  	switch msg := m.Sum.(type) {
    71  	case *Message_BlockRequest:
    72  		if m.GetBlockRequest().Height < 0 {
    73  			return errors.New("negative Height")
    74  		}
    75  
    76  	case *Message_BlockResponse:
    77  		// validate basic is called later when converting from proto
    78  		return nil
    79  
    80  	case *Message_NoBlockResponse:
    81  		if m.GetNoBlockResponse().Height < 0 {
    82  			return errors.New("negative Height")
    83  		}
    84  
    85  	case *Message_StatusResponse:
    86  		if m.GetStatusResponse().Base < 0 {
    87  			return errors.New("negative Base")
    88  		}
    89  		if m.GetStatusResponse().Height < 0 {
    90  			return errors.New("negative Height")
    91  		}
    92  		if m.GetStatusResponse().Base > m.GetStatusResponse().Height {
    93  			return fmt.Errorf(
    94  				"base %v cannot be greater than height %v",
    95  				m.GetStatusResponse().Base, m.GetStatusResponse().Height,
    96  			)
    97  		}
    98  
    99  	case *Message_StatusRequest:
   100  		return nil
   101  
   102  	default:
   103  		return fmt.Errorf("unknown message type: %T", msg)
   104  	}
   105  
   106  	return nil
   107  }