Browse Source

fix empty packet size calculation

Fixes #1762#issuecomment-398940107
pull/1824/head
Anton Kaliaev 7 years ago
parent
commit
ab04201c3d
No known key found for this signature in database GPG Key ID: 7B6881D965918214
3 changed files with 9 additions and 10 deletions
  1. +1
    -1
      mempool/reactor.go
  2. +7
    -8
      p2p/conn/connection.go
  3. +1
    -1
      p2p/conn/connection_test.go

+ 1
- 1
mempool/reactor.go View File

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


+ 7
- 8
p2p/conn/connection.go View File

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

+ 1
- 1
p2p/conn/connection_test.go View File

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


Loading…
Cancel
Save