/
msgs.go
56 lines (50 loc) · 1.49 KB
/
msgs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package blocksync
import (
"fmt"
"github.com/cosmos/gogoproto/proto"
bcproto "github.com/cometbft/cometbft/proto/tendermint/blocksync"
"github.com/cometbft/cometbft/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 ErrNilMessage
}
switch msg := pb.(type) {
case *bcproto.BlockRequest:
if msg.Height < 0 {
return ErrInvalidHeight{Height: msg.Height, Reason: "negative height"}
}
case *bcproto.BlockResponse:
// Avoid double-calling `types.BlockFromProto` for performance reasons.
// See https://github.com/cometbft/cometbft/issues/1964
return nil
case *bcproto.NoBlockResponse:
if msg.Height < 0 {
return ErrInvalidHeight{Height: msg.Height, Reason: "negative height"}
}
case *bcproto.StatusResponse:
if msg.Base < 0 {
return ErrInvalidBase{Base: msg.Base, Reason: "negative base"}
}
if msg.Height < 0 {
return ErrInvalidHeight{Height: msg.Height, Reason: "negative height"}
}
if msg.Base > msg.Height {
return ErrInvalidHeight{Height: msg.Height, Reason: fmt.Sprintf("base %v cannot be greater than height", msg.Base)}
}
case *bcproto.StatusRequest:
return nil
default:
return ErrUnknownMessageType{Msg: msg}
}
return nil
}