Skip to content

Commit dc2c5d0

Browse files
Remove bitmaskCodec (#2792)
1 parent 5793120 commit dc2c5d0

File tree

2 files changed

+14
-94
lines changed

2 files changed

+14
-94
lines changed

network/peer/msg_length.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,20 @@ import (
1212
)
1313

1414
var (
15-
errInvalidMaxMessageLength = errors.New("invalid maximum message length")
1615
errInvalidMessageLength = errors.New("invalid message length")
1716
errMaxMessageLengthExceeded = errors.New("maximum message length exceeded")
1817
)
1918

20-
// Used to mask the most significant bit that was used to indicate that the
21-
// message format uses protocol buffers.
22-
//
23-
// TODO: Once the v1.11 is activated, this mask should be removed.
24-
const bitmaskCodec = uint32(1 << 31)
25-
2619
// Assumes the specified [msgLen] will never >= 1<<31.
2720
func writeMsgLen(msgLen uint32, maxMsgLen uint32) ([wrappers.IntLen]byte, error) {
28-
if maxMsgLen >= bitmaskCodec {
21+
if msgLen > maxMsgLen {
2922
return [wrappers.IntLen]byte{}, fmt.Errorf(
30-
"%w; maximum message length must be <%d to be able to embed codec information at most significant bit",
31-
errInvalidMaxMessageLength,
32-
bitmaskCodec,
23+
"%w; the message length %d exceeds the specified limit %d",
24+
errMaxMessageLengthExceeded,
25+
msgLen,
26+
maxMsgLen,
3327
)
3428
}
35-
if msgLen > maxMsgLen {
36-
return [wrappers.IntLen]byte{}, fmt.Errorf("%w; the message length %d exceeds the specified limit %d", errMaxMessageLengthExceeded, msgLen, maxMsgLen)
37-
}
3829

3930
b := [wrappers.IntLen]byte{}
4031
binary.BigEndian.PutUint32(b[:], msgLen)
@@ -44,13 +35,6 @@ func writeMsgLen(msgLen uint32, maxMsgLen uint32) ([wrappers.IntLen]byte, error)
4435

4536
// Assumes the read [msgLen] will never >= 1<<31.
4637
func readMsgLen(b []byte, maxMsgLen uint32) (uint32, error) {
47-
if maxMsgLen >= bitmaskCodec {
48-
return 0, fmt.Errorf(
49-
"%w; maximum message length must be <%d to be able to embed codec information at most significant bit",
50-
errInvalidMaxMessageLength,
51-
bitmaskCodec,
52-
)
53-
}
5438
if len(b) != wrappers.IntLen {
5539
return 0, fmt.Errorf(
5640
"%w; readMsgLen only supports 4 bytes (got %d bytes)",
@@ -61,12 +45,6 @@ func readMsgLen(b []byte, maxMsgLen uint32) (uint32, error) {
6145

6246
// parse the message length
6347
msgLen := binary.BigEndian.Uint32(b)
64-
65-
// Because we always use proto messages, there's no need to check the most
66-
// significant bit to inspect the message format. So, we just zero the proto
67-
// flag.
68-
msgLen &^= bitmaskCodec
69-
7048
if msgLen > maxMsgLen {
7149
return 0, fmt.Errorf(
7250
"%w; the message length %d exceeds the specified limit %d",

network/peer/msg_length_test.go

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,9 @@ func TestWriteMsgLen(t *testing.T) {
2121
expectedErr error
2222
}{
2323
{
24-
msgLen: math.MaxUint32,
25-
msgLimit: math.MaxUint32,
26-
expectedErr: errInvalidMaxMessageLength,
27-
},
28-
{
29-
msgLen: bitmaskCodec,
30-
msgLimit: bitmaskCodec,
31-
expectedErr: errInvalidMaxMessageLength,
32-
},
33-
{
34-
msgLen: bitmaskCodec - 1,
35-
msgLimit: bitmaskCodec - 1,
36-
expectedErr: nil,
24+
msgLen: constants.DefaultMaxMessageSize,
25+
msgLimit: 1,
26+
expectedErr: errMaxMessageLengthExceeded,
3727
},
3828
{
3929
msgLen: constants.DefaultMaxMessageSize,
@@ -45,11 +35,6 @@ func TestWriteMsgLen(t *testing.T) {
4535
msgLimit: constants.DefaultMaxMessageSize,
4636
expectedErr: nil,
4737
},
48-
{
49-
msgLen: constants.DefaultMaxMessageSize,
50-
msgLimit: 1,
51-
expectedErr: errMaxMessageLengthExceeded,
52-
},
5338
}
5439
for _, tv := range tt {
5540
msgLenBytes, err := writeMsgLen(tv.msgLen, tv.msgLimit)
@@ -73,39 +58,27 @@ func TestReadMsgLen(t *testing.T) {
7358
expectedErr error
7459
expectedMsgLen uint32
7560
}{
76-
{
77-
msgLenBytes: []byte{0xFF, 0xFF, 0xFF, 0xFF},
78-
msgLimit: math.MaxUint32,
79-
expectedErr: errInvalidMaxMessageLength,
80-
expectedMsgLen: 0,
81-
},
8261
{
8362
msgLenBytes: []byte{0b11111111, 0xFF},
8463
msgLimit: math.MaxInt32,
8564
expectedErr: errInvalidMessageLength,
8665
expectedMsgLen: 0,
8766
},
8867
{
89-
msgLenBytes: []byte{0b11111111, 0xFF, 0xFF, 0xFF},
68+
msgLenBytes: []byte{0xFF, 0xFF, 0xFF, 0xFF},
9069
msgLimit: constants.DefaultMaxMessageSize,
9170
expectedErr: errMaxMessageLengthExceeded,
9271
expectedMsgLen: 0,
9372
},
9473
{
95-
msgLenBytes: []byte{0b11111111, 0xFF, 0xFF, 0xFF},
96-
msgLimit: math.MaxInt32,
97-
expectedErr: nil,
98-
expectedMsgLen: math.MaxInt32,
99-
},
100-
{
101-
msgLenBytes: []byte{0b10000000, 0x00, 0x00, 0x01},
102-
msgLimit: math.MaxInt32,
74+
msgLenBytes: []byte{0xFF, 0xFF, 0xFF, 0xFF},
75+
msgLimit: math.MaxUint32,
10376
expectedErr: nil,
104-
expectedMsgLen: 1,
77+
expectedMsgLen: math.MaxUint32,
10578
},
10679
{
107-
msgLenBytes: []byte{0b10000000, 0x00, 0x00, 0x01},
108-
msgLimit: 1,
80+
msgLenBytes: []byte{0x00, 0x00, 0x00, 0x01},
81+
msgLimit: 10,
10982
expectedErr: nil,
11083
expectedMsgLen: 1,
11184
},
@@ -126,34 +99,3 @@ func TestReadMsgLen(t *testing.T) {
12699
require.Equal(tv.expectedMsgLen, msgLenAfterWrite)
127100
}
128101
}
129-
130-
func TestBackwardsCompatibleReadMsgLen(t *testing.T) {
131-
require := require.New(t)
132-
133-
tt := []struct {
134-
msgLenBytes []byte
135-
msgLimit uint32
136-
expectedMsgLen uint32
137-
}{
138-
{
139-
msgLenBytes: []byte{0b01111111, 0xFF, 0xFF, 0xFF},
140-
msgLimit: math.MaxInt32,
141-
expectedMsgLen: math.MaxInt32,
142-
},
143-
{
144-
msgLenBytes: []byte{0b00000000, 0x00, 0x00, 0x01},
145-
msgLimit: math.MaxInt32,
146-
expectedMsgLen: 1,
147-
},
148-
{
149-
msgLenBytes: []byte{0b00000000, 0x00, 0x00, 0x01},
150-
msgLimit: 1,
151-
expectedMsgLen: 1,
152-
},
153-
}
154-
for _, tv := range tt {
155-
msgLen, err := readMsgLen(tv.msgLenBytes, tv.msgLimit)
156-
require.NoError(err)
157-
require.Equal(tv.expectedMsgLen, msgLen)
158-
}
159-
}

0 commit comments

Comments
 (0)