github.com/amazechain/amc@v0.1.3/internal/download/queue.go (about) 1 // Copyright 2022 The AmazeChain Authors 2 // This file is part of the AmazeChain library. 3 // 4 // The AmazeChain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The AmazeChain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package download 18 19 import ( 20 "github.com/amazechain/amc/api/protocol/types_pb" 21 "hash" 22 "sync" 23 "time" 24 ) 25 26 var ( 27 blockCacheMaxItems = 8192 // Maximum number of blocks to cache before throttling the download 28 blockCacheInitialItems = 2048 // Initial number of blocks to start fetching, before we know the sizes of the blocks 29 blockCacheMemory = 256 * 1024 * 1024 // Maximum amount of memory to use for block caching 30 blockCacheSizeWeight = 0.1 // Multiplier to approximate the average block size based on past ones 31 ) 32 33 // fetchRequest 34 type fetchRequest struct { 35 } 36 37 // fetchResult 38 type fetchResult struct { 39 } 40 41 // queue 42 type queue struct { 43 mode SyncMode 44 45 headerHead hash.Hash // Hash of the last queued header to verify order 46 headerTaskPool map[uint64]*types_pb.Header // Pending header retrieval tasks, mapping starting indexes to skeleton headers 47 headerPeerMiss map[string]map[uint64]struct{} // Set of per-peer header batches known to be unavailable 48 headerPendPool map[string]*fetchRequest // Currently pending header retrieval operations 49 headerResults []*types_pb.Header // Result cache accumulating the completed headers 50 headerHashes []hash.Hash // Result cache accumulating the completed header hashes 51 headerProced int // Number of headers already processed from the results 52 headerOffset uint64 // Number of the first header in the result cache 53 headerContCh chan bool // Channel to notify when header download finishes 54 55 resultCache *resultStore // Downloaded but not yet delivered fetch results 56 57 lock *sync.RWMutex 58 active *sync.Cond 59 closed bool 60 61 lastStatLog time.Time 62 } 63 64 func (q *queue) Prepare(offset uint64, mode SyncMode) { 65 q.lock.Lock() 66 defer q.lock.Unlock() 67 68 // Prepare the queue for sync results 69 q.resultCache.Prepare(offset) 70 q.mode = mode 71 }