Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove bitmask from writeMsgLen #1342

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions network/peer/msg_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ var (
errMaxMessageLengthExceeded = errors.New("maximum message length exceeded")
)

// Used to mask the most significant bit to indicate that the message format
// uses protocol buffers.
// Used to mask the most significant bit that was used to indicate that the
// message format uses protocol buffers.
//
// TODO: Once the v1.11 is activated, this mask should be removed.
const bitmaskCodec = uint32(1 << 31)

// Assumes the specified [msgLen] will never >= 1<<31.
Expand All @@ -34,16 +36,8 @@ func writeMsgLen(msgLen uint32, maxMsgLen uint32) ([wrappers.IntLen]byte, error)
return [wrappers.IntLen]byte{}, fmt.Errorf("%w; the message length %d exceeds the specified limit %d", errMaxMessageLengthExceeded, msgLen, maxMsgLen)
}

x := msgLen

// Mask the most significant bit to denote it's using proto. This bit isn't
// read anymore, because all the messages use proto. However, it is set for
// backwards compatibility.
// TODO: Once the v1.10 is activated, this mask should be removed.
x |= bitmaskCodec

b := [wrappers.IntLen]byte{}
binary.BigEndian.PutUint32(b[:], x)
binary.BigEndian.PutUint32(b[:], msgLen)

return b, nil
}
Expand Down
5 changes: 4 additions & 1 deletion network/peer/msg_length_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func TestReadMsgLen(t *testing.T) {

msgLenBytes, err := writeMsgLen(msgLen, tv.msgLimit)
require.NoError(err)
require.Equal(tv.msgLenBytes, msgLenBytes[:])

msgLenAfterWrite, err := readMsgLen(msgLenBytes[:], tv.msgLimit)
require.NoError(err)
require.Equal(tv.expectedMsgLen, msgLenAfterWrite)
}
}

Expand Down