@@ -34,14 +34,19 @@ var ErrNotRequested = errors.New("not requested")
3434// node it already processed previously.
3535var ErrAlreadyProcessed = errors .New ("already processed" )
3636
37+ // maxFetchesPerDepth is the maximum number of pending trie nodes per depth. The
38+ // role of this value is to limit the number of trie nodes that get expanded in
39+ // memory if the node was configured with a significant number of peers.
40+ const maxFetchesPerDepth = 16384
41+
3742// request represents a scheduled or already in-flight state retrieval request.
3843type request struct {
44+ path []byte // Merkle path leading to this node for prioritization
3945 hash common.Hash // Hash of the node data content to retrieve
4046 data []byte // Data content of the node, cached until all subtrees complete
4147 code bool // Whether this is a code entry
4248
4349 parents []* request // Parent state nodes referencing this entry (notify all upon completion)
44- depth int // Depth level within the trie the node is located to prioritise DFS
4550 deps int // Number of dependencies before allowed to commit this node
4651
4752 callback LeafCallback // Callback to invoke if a leaf node it reached on this branch
@@ -89,6 +94,7 @@ type Sync struct {
8994 nodeReqs map [common.Hash ]* request // Pending requests pertaining to a trie node hash
9095 codeReqs map [common.Hash ]* request // Pending requests pertaining to a code hash
9196 queue * prque.Prque // Priority queue with the pending requests
97+ fetches map [int ]int // Number of active fetches per trie node depth
9298 bloom * SyncBloom // Bloom filter for fast state existence checks
9399}
94100
@@ -100,14 +106,15 @@ func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallb
100106 nodeReqs : make (map [common.Hash ]* request ),
101107 codeReqs : make (map [common.Hash ]* request ),
102108 queue : prque .New (nil ),
109+ fetches : make (map [int ]int ),
103110 bloom : bloom ,
104111 }
105- ts .AddSubTrie (root , 0 , common.Hash {}, callback )
112+ ts .AddSubTrie (root , nil , common.Hash {}, callback )
106113 return ts
107114}
108115
109116// AddSubTrie registers a new trie to the sync code, rooted at the designated parent.
110- func (s * Sync ) AddSubTrie (root common.Hash , depth int , parent common.Hash , callback LeafCallback ) {
117+ func (s * Sync ) AddSubTrie (root common.Hash , path [] byte , parent common.Hash , callback LeafCallback ) {
111118 // Short circuit if the trie is empty or already known
112119 if root == emptyRoot {
113120 return
@@ -128,8 +135,8 @@ func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callb
128135 }
129136 // Assemble the new sub-trie sync request
130137 req := & request {
138+ path : path ,
131139 hash : root ,
132- depth : depth ,
133140 callback : callback ,
134141 }
135142 // If this sub-trie has a designated parent, link them together
@@ -147,7 +154,7 @@ func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callb
147154// AddCodeEntry schedules the direct retrieval of a contract code that should not
148155// be interpreted as a trie node, but rather accepted and stored into the database
149156// as is.
150- func (s * Sync ) AddCodeEntry (hash common.Hash , depth int , parent common.Hash ) {
157+ func (s * Sync ) AddCodeEntry (hash common.Hash , path [] byte , parent common.Hash ) {
151158 // Short circuit if the entry is empty or already known
152159 if hash == emptyState {
153160 return
@@ -170,9 +177,9 @@ func (s *Sync) AddCodeEntry(hash common.Hash, depth int, parent common.Hash) {
170177 }
171178 // Assemble the new sub-trie sync request
172179 req := & request {
173- hash : hash ,
174- code : true ,
175- depth : depth ,
180+ path : path ,
181+ hash : hash ,
182+ code : true ,
176183 }
177184 // If this sub-trie has a designated parent, link them together
178185 if parent != (common.Hash {}) {
@@ -190,7 +197,18 @@ func (s *Sync) AddCodeEntry(hash common.Hash, depth int, parent common.Hash) {
190197func (s * Sync ) Missing (max int ) []common.Hash {
191198 var requests []common.Hash
192199 for ! s .queue .Empty () && (max == 0 || len (requests ) < max ) {
193- requests = append (requests , s .queue .PopItem ().(common.Hash ))
200+ // Retrieve th enext item in line
201+ item , prio := s .queue .Peek ()
202+
203+ // If we have too many already-pending tasks for this depth, throttle
204+ depth := int (prio >> 56 )
205+ if s .fetches [depth ] > maxFetchesPerDepth {
206+ break
207+ }
208+ // Item is allowed to be scheduled, add it to the task list
209+ s .queue .Pop ()
210+ s .fetches [depth ]++
211+ requests = append (requests , item .(common.Hash ))
194212 }
195213 return requests
196214}
@@ -285,31 +303,35 @@ func (s *Sync) schedule(req *request) {
285303 // is a trie node and code has same hash. In this case two elements
286304 // with same hash and same or different depth will be pushed. But it's
287305 // ok the worst case is the second response will be treated as duplicated.
288- s .queue .Push (req .hash , int64 (req .depth ))
306+ prio := int64 (len (req .path )) << 56 // depth >= 128 will never happen, storage leaves will be included in their parents
307+ for i := 0 ; i < 14 && i < len (req .path ); i ++ {
308+ prio |= int64 (15 - req .path [i ]) << (52 - i * 4 ) // 15-nibble => lexicographic order
309+ }
310+ s .queue .Push (req .hash , prio )
289311}
290312
291313// children retrieves all the missing children of a state trie entry for future
292314// retrieval scheduling.
293315func (s * Sync ) children (req * request , object node ) ([]* request , error ) {
294316 // Gather all the children of the node, irrelevant whether known or not
295317 type child struct {
296- node node
297- depth int
318+ path [] byte
319+ node node
298320 }
299321 var children []child
300322
301323 switch node := (object ).(type ) {
302324 case * shortNode :
303325 children = []child {{
304- node : node .Val ,
305- depth : req .depth + len ( node .Key ),
326+ node : node .Val ,
327+ path : append ( append ([] byte ( nil ), req .path ... ), node .Key ... ),
306328 }}
307329 case * fullNode :
308330 for i := 0 ; i < 17 ; i ++ {
309331 if node .Children [i ] != nil {
310332 children = append (children , child {
311- node : node .Children [i ],
312- depth : req .depth + 1 ,
333+ node : node .Children [i ],
334+ path : append ( append ([] byte ( nil ), req .path ... ), byte ( i )) ,
313335 })
314336 }
315337 }
@@ -322,7 +344,7 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
322344 // Notify any external watcher of a new key/value node
323345 if req .callback != nil {
324346 if node , ok := (child .node ).(valueNode ); ok {
325- if err := req .callback (node , req .hash ); err != nil {
347+ if err := req .callback (req . path , node , req .hash ); err != nil {
326348 return nil , err
327349 }
328350 }
@@ -346,9 +368,9 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
346368 }
347369 // Locally unknown node, schedule for retrieval
348370 requests = append (requests , & request {
371+ path : child .path ,
349372 hash : hash ,
350373 parents : []* request {req },
351- depth : child .depth ,
352374 callback : req .callback ,
353375 })
354376 }
@@ -364,9 +386,11 @@ func (s *Sync) commit(req *request) (err error) {
364386 if req .code {
365387 s .membatch .codes [req .hash ] = req .data
366388 delete (s .codeReqs , req .hash )
389+ s .fetches [len (req .path )]--
367390 } else {
368391 s .membatch .nodes [req .hash ] = req .data
369392 delete (s .nodeReqs , req .hash )
393+ s .fetches [len (req .path )]--
370394 }
371395 // Check all parents for completion
372396 for _ , parent := range req .parents {
0 commit comments