Skip to content

Commit 41b2008

Browse files
committed
eth: limit number of sent blocks based on message size
If blocks get larger, sending 256 at once can make messages large enough to exceed the low-level write timeout.
1 parent 7aefe12 commit 41b2008

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

eth/handler.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import (
1818
"github.com/ethereum/go-ethereum/rlp"
1919
)
2020

21+
// This is the target maximum size of returned blocks for the
22+
// getBlocks message. The reply message may exceed it
23+
// if a single block is larger than the limit.
24+
const maxBlockRespSize = 2 * 1024 * 1024
25+
2126
func errResp(code errCode, format string, v ...interface{}) error {
2227
return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
2328
}
@@ -246,7 +251,10 @@ func (self *ProtocolManager) handleMsg(p *peer) error {
246251
if _, err := msgStream.List(); err != nil {
247252
return err
248253
}
249-
var i int
254+
var (
255+
i int
256+
totalsize common.StorageSize
257+
)
250258
for {
251259
i++
252260
var hash common.Hash
@@ -260,8 +268,9 @@ func (self *ProtocolManager) handleMsg(p *peer) error {
260268
block := self.chainman.GetBlock(hash)
261269
if block != nil {
262270
blocks = append(blocks, block)
271+
totalsize += block.Size()
263272
}
264-
if i == downloader.MaxBlockFetch {
273+
if i == downloader.MaxBlockFetch || totalsize > maxBlockRespSize {
265274
break
266275
}
267276
}

0 commit comments

Comments
 (0)