From 6ac93500f0ca91d29f7903d260ef2f649c71cf6e Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 14 Apr 2025 13:38:25 +0800 Subject: [PATCH] eth/downloader: clean up PR change. Add some docs where appropriate and remove dead code/parameters/constants --- eth/downloader/downloader.go | 4 ++-- eth/downloader/queue.go | 13 ++++++------- eth/downloader/resultstore.go | 10 ++++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 3f3f9b7f0c..88784760d2 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -206,7 +206,7 @@ func New(stateDb ethdb.Database, mux *event.TypeMux, chain BlockChain, dropPeer dl := &Downloader{ stateDB: stateDb, mux: mux, - queue: newQueue(blockCacheMaxItems, blockCacheInitialItems), + queue: newQueue(), peers: newPeerSet(), blockchain: chain, dropPeer: dropPeer, @@ -352,7 +352,7 @@ func (d *Downloader) synchronise(mode SyncMode, beaconPing chan struct{}) error } } // Reset the queue, peer set and wake channels to clean any internal leftover state - d.queue.Reset(blockCacheMaxItems, blockCacheInitialItems) + d.queue.Reset() d.peers.Reset() for _, ch := range []chan bool{d.queue.blockWakeCh, d.queue.receiptWakeCh} { diff --git a/eth/downloader/queue.go b/eth/downloader/queue.go index f9b635b1d9..ce96cf5eeb 100644 --- a/eth/downloader/queue.go +++ b/eth/downloader/queue.go @@ -42,9 +42,8 @@ const ( ) var ( - blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download - blockCacheInitialItems = 32 // Initial number of blocks to start fetching, before we know the sizes of the blocks - blockCacheMemory = 1024 * 1024 * 1024 // Maximum amount of memory to use for block caching + blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download + blockCacheMemory = 1024 * 1024 * 1024 // Maximum amount of memory to use for block caching ) var ( @@ -159,7 +158,7 @@ type queue struct { } // newQueue creates a new download queue for scheduling block retrieval. -func newQueue(blockCacheLimit int, thresholdInitialSize int) *queue { +func newQueue() *queue { lock := new(sync.RWMutex) q := &queue{ headerContCh: make(chan bool, 1), @@ -170,12 +169,12 @@ func newQueue(blockCacheLimit int, thresholdInitialSize int) *queue { active: sync.NewCond(lock), lock: lock, } - q.Reset(blockCacheLimit, thresholdInitialSize) + q.Reset() return q } // Reset clears out the queue contents. -func (q *queue) Reset(blockCacheLimit int, thresholdInitialSize int) { +func (q *queue) Reset() { q.lock.Lock() defer q.lock.Unlock() @@ -193,7 +192,7 @@ func (q *queue) Reset(blockCacheLimit int, thresholdInitialSize int) { q.receiptTaskQueue.Reset() q.receiptPendPool = make(map[string]*fetchRequest) - q.resultCache = newResultStore(blockCacheLimit) + q.resultCache = newResultStore() } // Close marks the end of the sync, unblocking Results. diff --git a/eth/downloader/resultstore.go b/eth/downloader/resultstore.go index 352cc0e23a..0d1da6a8d8 100644 --- a/eth/downloader/resultstore.go +++ b/eth/downloader/resultstore.go @@ -43,10 +43,10 @@ type resultStore struct { lock sync.RWMutex } -func newResultStore(size int) *resultStore { +func newResultStore() *resultStore { return &resultStore{ resultOffset: 0, - items: make([]*fetchResult, size), + items: make([]*fetchResult, blockCacheMaxItems), } } @@ -99,8 +99,10 @@ func (r *resultStore) GetDeliverySlot(headerNumber uint64) (*fetchResult, bool, // blocks each contain a transaction filled with calldata that is all zeroes. // The size of a worst-case block is ~= gasUsed / 10 func (r *resultStore) throttleThreshold(index int) bool { - // estimate the average block size of all scheduled blocks - estBlockSize := max((r.itemsGasUsed/(uint64(index)+1))/10, 524) + // estimate the average size of a worst-case block, given what we have already queued: + // total gas used by in flight blocks / number of in-flight blocks / 10 + var headerSize uint64 = 524 + estBlockSize := max((r.itemsGasUsed/(uint64(index)+1))/10, headerSize) throttleThreshold := min(uint64(len(r.items)), uint64(blockCacheMemory)/estBlockSize+1) return index >= int(throttleThreshold)