mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-03 03:35:19 +00:00
* p2p: ressurrect the p2p envelope and use to calculate message metric Co-authored-by: Callum Waters <cmwaters19@gmail.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package blocksync
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/cosmos/gogoproto/proto"
|
|
|
|
bcproto "github.com/tendermint/tendermint/proto/tendermint/blocksync"
|
|
"github.com/tendermint/tendermint/types"
|
|
)
|
|
|
|
const (
|
|
// NOTE: keep up to date with bcproto.BlockResponse
|
|
BlockResponseMessagePrefixSize = 4
|
|
BlockResponseMessageFieldKeySize = 1
|
|
MaxMsgSize = types.MaxBlockSizeBytes +
|
|
BlockResponseMessagePrefixSize +
|
|
BlockResponseMessageFieldKeySize
|
|
)
|
|
|
|
// ValidateMsg validates a message.
|
|
func ValidateMsg(pb proto.Message) error {
|
|
if pb == nil {
|
|
return errors.New("message cannot be nil")
|
|
}
|
|
|
|
switch msg := pb.(type) {
|
|
case *bcproto.BlockRequest:
|
|
if msg.Height < 0 {
|
|
return errors.New("negative Height")
|
|
}
|
|
case *bcproto.BlockResponse:
|
|
_, err := types.BlockFromProto(msg.Block)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case *bcproto.NoBlockResponse:
|
|
if msg.Height < 0 {
|
|
return errors.New("negative Height")
|
|
}
|
|
case *bcproto.StatusResponse:
|
|
if msg.Base < 0 {
|
|
return errors.New("negative Base")
|
|
}
|
|
if msg.Height < 0 {
|
|
return errors.New("negative Height")
|
|
}
|
|
if msg.Base > msg.Height {
|
|
return fmt.Errorf("base %v cannot be greater than height %v", msg.Base, msg.Height)
|
|
}
|
|
case *bcproto.StatusRequest:
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("unknown message type %T", msg)
|
|
}
|
|
return nil
|
|
}
|