fix empty packet size calculation

Fixes #1762#issuecomment-398940107
This commit is contained in:
Anton Kaliaev 2018-06-27 18:07:26 +04:00
parent 297cd4cfe8
commit ab04201c3d
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
3 changed files with 9 additions and 10 deletions

View File

@ -5,8 +5,8 @@ import (
"reflect"
"time"
abci "github.com/tendermint/tendermint/abci/types"
amino "github.com/tendermint/go-amino"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tmlibs/clist"
"github.com/tendermint/tmlibs/log"

View File

@ -156,7 +156,7 @@ func NewMConnectionWithConfig(conn net.Conn, chDescs []*ChannelDescriptor, onRec
onReceive: onReceive,
onError: onError,
config: config,
emptyPacketMsgSize: emptyPacketMsgSize(),
emptyPacketMsgSize: emptyPacketMsgSize(config.MaxPacketMsgSize),
}
// Create channels
@ -631,7 +631,7 @@ func newChannel(conn *MConnection, desc ChannelDescriptor) *Channel {
desc: desc,
sendQueue: make(chan []byte, desc.SendQueueCapacity),
recving: make([]byte, 0, desc.RecvBufferCapacity),
maxPacketMsgPayloadSize: conn.config.MaxPacketMsgSize,
maxPacketMsgPayloadSize: conn.config.MaxPacketMsgSize - conn.emptyPacketMsgSize,
}
}
@ -694,7 +694,7 @@ func (ch *Channel) isSendPending() bool {
func (ch *Channel) nextPacketMsg() PacketMsg {
packet := PacketMsg{}
packet.ChannelID = byte(ch.desc.ID)
maxSize := ch.maxPacketMsgPayloadSize - ch.conn.emptyPacketMsgSize
maxSize := ch.maxPacketMsgPayloadSize
packet.Bytes = ch.sending[:cmn.MinInt(maxSize, len(ch.sending))]
if len(ch.sending) <= maxSize {
packet.EOF = byte(0x01)
@ -788,17 +788,16 @@ func (mp PacketMsg) String() string {
// - EOF field key + byte = 2 bytes
// - Bytes field key = 1 bytes
// - Uvarint length of MustMarshalBinary(bytes) = 1 or 2 bytes
// - Struct terminator = 1 byte
// = up to 14 bytes overhead for the packet.
// = up to 13 bytes overhead for the packet.
func emptyPacketMsgSize() int {
func emptyPacketMsgSize(maxPayloadSize int) int {
emptyPacketMsgSize := len(cdc.MustMarshalBinary(PacketMsg{
ChannelID: 0x01,
EOF: 1,
Bytes: make([]byte, 1),
Bytes: make([]byte, maxPayloadSize),
}))
// -1 byte of data
// +1 byte because uvarint length of MustMarshalBinary(bytes) will be 2 bytes for big packets
// +1 byte because uvarint length of MustMarshalBinary(packet) will be 2 bytes for big packets
return emptyPacketMsgSize - 1 + 1 + 1
return emptyPacketMsgSize - maxPayloadSize + 1 + 1
}

View File

@ -426,7 +426,7 @@ func TestMConnectionReadErrorLongMessage(t *testing.T) {
var packet = PacketMsg{
ChannelID: 0x01,
EOF: 1,
Bytes: make([]byte, mconnClient.config.MaxPacketMsgSize-emptyPacketMsgSize()),
Bytes: make([]byte, mconnClient.config.MaxPacketMsgSize-emptyPacketMsgSize(mconnClient.config.MaxPacketMsgSize)),
}
_, err = cdc.MarshalBinaryWriter(buf, packet)
assert.Nil(t, err)