github.com/vipernet-xyz/tm@v0.34.24/statesync/messages.go (about)

     1  package statesync
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/gogo/protobuf/proto"
     8  
     9  	ssproto "github.com/vipernet-xyz/tm/proto/tendermint/statesync"
    10  )
    11  
    12  const (
    13  	// snapshotMsgSize is the maximum size of a snapshotResponseMessage
    14  	snapshotMsgSize = int(4e6)
    15  	// chunkMsgSize is the maximum size of a chunkResponseMessage
    16  	chunkMsgSize = int(16e6)
    17  )
    18  
    19  // validateMsg validates a message.
    20  func validateMsg(pb proto.Message) error {
    21  	if pb == nil {
    22  		return errors.New("message cannot be nil")
    23  	}
    24  	switch msg := pb.(type) {
    25  	case *ssproto.ChunkRequest:
    26  		if msg.Height == 0 {
    27  			return errors.New("height cannot be 0")
    28  		}
    29  	case *ssproto.ChunkResponse:
    30  		if msg.Height == 0 {
    31  			return errors.New("height cannot be 0")
    32  		}
    33  		if msg.Missing && len(msg.Chunk) > 0 {
    34  			return errors.New("missing chunk cannot have contents")
    35  		}
    36  		if !msg.Missing && msg.Chunk == nil {
    37  			return errors.New("chunk cannot be nil")
    38  		}
    39  	case *ssproto.SnapshotsRequest:
    40  	case *ssproto.SnapshotsResponse:
    41  		if msg.Height == 0 {
    42  			return errors.New("height cannot be 0")
    43  		}
    44  		if len(msg.Hash) == 0 {
    45  			return errors.New("snapshot has no hash")
    46  		}
    47  		if msg.Chunks == 0 {
    48  			return errors.New("snapshot has no chunks")
    49  		}
    50  	default:
    51  		return fmt.Errorf("unknown message type %T", msg)
    52  	}
    53  	return nil
    54  }