Skip to content

Commit 2d4eff2

Browse files
authored
eth/downloader: increase downloader block body allowance (ethereum#23074)
This change increases the cache size from 64 to 256 Mb for block bodies. Benchmarks have shown this to be one bottleneck when trying to achieve higher download speeds. The commit also includes a minor optimization for header inserts in package core: previously, the presence of headers in the database was checked for every header before writing it. With the change, if one header fails the presence check, all subsequent headers are also assumed to be missing. This is an improvement because in practice, the headers are almost always missing during sync.
1 parent bca8c03 commit 2d4eff2

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

core/headerchain.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func (hc *HeaderChain) writeHeaders(headers []*types.Header) (result *headerWrit
165165
)
166166

167167
batch := hc.chainDb.NewBatch()
168+
parentKnown := true // Set to true to force hc.HasHeader check the first iteration
168169
for i, header := range headers {
169170
var hash common.Hash
170171
// The headers have already been validated at this point, so we already
@@ -178,8 +179,10 @@ func (hc *HeaderChain) writeHeaders(headers []*types.Header) (result *headerWrit
178179
number := header.Number.Uint64()
179180
newTD.Add(newTD, header.Difficulty)
180181

182+
// If the parent was not present, store it
181183
// If the header is already known, skip it, otherwise store
182-
if !hc.HasHeader(hash, number) {
184+
alreadyKnown := parentKnown && hc.HasHeader(hash, number)
185+
if !alreadyKnown {
183186
// Irrelevant of the canonical status, write the TD and header to the database.
184187
rawdb.WriteTd(batch, hash, number, newTD)
185188
hc.tdCache.Add(hash, new(big.Int).Set(newTD))
@@ -192,6 +195,7 @@ func (hc *HeaderChain) writeHeaders(headers []*types.Header) (result *headerWrit
192195
firstInserted = i
193196
}
194197
}
198+
parentKnown = alreadyKnown
195199
lastHeader, lastHash, lastNumber = header, hash, number
196200
}
197201

eth/downloader/queue.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ const (
4040
)
4141

4242
var (
43-
blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download
44-
blockCacheInitialItems = 2048 // Initial number of blocks to start fetching, before we know the sizes of the blocks
45-
blockCacheMemory = 64 * 1024 * 1024 // Maximum amount of memory to use for block caching
46-
blockCacheSizeWeight = 0.1 // Multiplier to approximate the average block size based on past ones
43+
blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download
44+
blockCacheInitialItems = 2048 // Initial number of blocks to start fetching, before we know the sizes of the blocks
45+
blockCacheMemory = 256 * 1024 * 1024 // Maximum amount of memory to use for block caching
46+
blockCacheSizeWeight = 0.1 // Multiplier to approximate the average block size based on past ones
4747
)
4848

4949
var (
@@ -783,8 +783,9 @@ func (q *queue) DeliverHeaders(id string, headers []*types.Header, headerProcCh
783783
func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, uncleLists [][]*types.Header) (int, error) {
784784
q.lock.Lock()
785785
defer q.lock.Unlock()
786+
trieHasher := trie.NewStackTrie(nil)
786787
validate := func(index int, header *types.Header) error {
787-
if types.DeriveSha(types.Transactions(txLists[index]), trie.NewStackTrie(nil)) != header.TxHash {
788+
if types.DeriveSha(types.Transactions(txLists[index]), trieHasher) != header.TxHash {
788789
return errInvalidBody
789790
}
790791
if types.CalcUncleHash(uncleLists[index]) != header.UncleHash {
@@ -808,8 +809,9 @@ func (q *queue) DeliverBodies(id string, txLists [][]*types.Transaction, uncleLi
808809
func (q *queue) DeliverReceipts(id string, receiptList [][]*types.Receipt) (int, error) {
809810
q.lock.Lock()
810811
defer q.lock.Unlock()
812+
trieHasher := trie.NewStackTrie(nil)
811813
validate := func(index int, header *types.Header) error {
812-
if types.DeriveSha(types.Receipts(receiptList[index]), trie.NewStackTrie(nil)) != header.ReceiptHash {
814+
if types.DeriveSha(types.Receipts(receiptList[index]), trieHasher) != header.ReceiptHash {
813815
return errInvalidReceipt
814816
}
815817
return nil

0 commit comments

Comments
 (0)