From bfeffc573c728dbd75c47e2ac79bacc86cd3b063 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Thu, 27 Mar 2025 18:26:00 +0100 Subject: [PATCH] eth/downloader: make block body download throttle heuristic based on worst-case block size at the current gas limit --- eth/downloader/queue.go | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index a1c114f057..39c74f36a0 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -373,26 +373,16 @@ func (q *queue) Results(block bool) []*fetchResult { q.lock.Unlock() } // Regardless if closed or not, we can still deliver whatever we have + results := q.resultCache.GetCompleted(maxResultsProcess) - for _, result := range results { - // Recalculate the result item weights to prevent memory exhaustion - size := result.Header.Size() - for _, uncle := range result.Uncles { - size += uncle.Size() - } - for _, receipt := range result.Receipts { - size += receipt.Size() - } - for _, tx := range result.Transactions { - size += common.StorageSize(tx.Size()) - } - size += common.StorageSize(result.Withdrawals.Size()) - q.resultSize = common.StorageSize(blockCacheSizeWeight)*size + - (1-common.StorageSize(blockCacheSizeWeight))*q.resultSize + + // set a throttle threshhold based on estimated worst-sized block that can be created for a given gas limit. + targetSize := uint64(256_000_000) + gasLimit := results[0].Header.GasLimit + if gasLimit == 0 { + gasLimit = 1 } - // Using the newly calibrated resultsize, figure out the new throttle limit - // on the result cache - throttleThreshold := uint64((common.StorageSize(blockCacheMemory) + q.resultSize - 1) / q.resultSize) + throttleThreshold := min(targetSize/(gasLimit/10), 2048) throttleThreshold = q.resultCache.SetThrottleThreshold(throttleThreshold) // With results removed from the cache, wake throttled fetchers