@@ -180,35 +180,31 @@ func (n *cachedNode) obj(hash common.Hash) node {
180
180
return expandNode (hash [:], n .node )
181
181
}
182
182
183
- // childs returns all the tracked children of this node, both the implicit ones
184
- // from inside the node as well as the explicit ones from outside the node.
185
- func ( n * cachedNode ) childs () []common. Hash {
186
- children := make ([] common.Hash , 0 , 16 )
183
+ // forChilds invokes the callback for all the tracked children of this node,
184
+ // both the implicit ones from inside the node as well as the explicit ones
185
+ //from outside the node.
186
+ func ( n * cachedNode ) forChilds ( onChild func ( hash common.Hash )) {
187
187
for child := range n .children {
188
- children = append ( children , child )
188
+ onChild ( child )
189
189
}
190
190
if _ , ok := n .node .(rawNode ); ! ok {
191
- gatherChildren (n .node , & children )
191
+ forGatherChildren (n .node , onChild )
192
192
}
193
- return children
194
193
}
195
194
196
- // gatherChildren traverses the node hierarchy of a collapsed storage node and
197
- // retrieves all the hashnode children.
198
- func gatherChildren (n node , children * [] common.Hash ) {
195
+ // forGatherChildren traverses the node hierarchy of a collapsed storage node and
196
+ // invokes the callback for all the hashnode children.
197
+ func forGatherChildren (n node , onChild func ( hash common.Hash ) ) {
199
198
switch n := n .(type ) {
200
199
case * rawShortNode :
201
- gatherChildren (n .Val , children )
202
-
200
+ forGatherChildren (n .Val , onChild )
203
201
case rawFullNode :
204
202
for i := 0 ; i < 16 ; i ++ {
205
- gatherChildren (n [i ], children )
203
+ forGatherChildren (n [i ], onChild )
206
204
}
207
205
case hashNode :
208
- * children = append (* children , common .BytesToHash (n ))
209
-
206
+ onChild (common .BytesToHash (n ))
210
207
case valueNode , nil :
211
-
212
208
default :
213
209
panic (fmt .Sprintf ("unknown node type: %T" , n ))
214
210
}
@@ -334,11 +330,11 @@ func (db *Database) insert(hash common.Hash, blob []byte, node node) {
334
330
size : uint16 (len (blob )),
335
331
flushPrev : db .newest ,
336
332
}
337
- for _ , child := range entry .childs ( ) {
333
+ entry .forChilds ( func ( child common. Hash ) {
338
334
if c := db .dirties [child ]; c != nil {
339
335
c .parents ++
340
336
}
341
- }
337
+ })
342
338
db .dirties [hash ] = entry
343
339
344
340
// Update the flush-list endpoints
@@ -570,9 +566,9 @@ func (db *Database) dereference(child common.Hash, parent common.Hash) {
570
566
db .dirties [node .flushNext ].flushPrev = node .flushPrev
571
567
}
572
568
// Dereference all children and delete the node
573
- for _ , hash := range node .childs ( ) {
569
+ node .forChilds ( func ( hash common. Hash ) {
574
570
db .dereference (hash , child )
575
- }
571
+ })
576
572
delete (db .dirties , child )
577
573
db .dirtiesSize -= common .StorageSize (common .HashLength + int (node .size ))
578
574
if node .children != nil {
@@ -766,10 +762,14 @@ func (db *Database) commit(hash common.Hash, batch ethdb.Batch, uncacher *cleane
766
762
if ! ok {
767
763
return nil
768
764
}
769
- for _ , child := range node .childs () {
770
- if err := db .commit (child , batch , uncacher ); err != nil {
771
- return err
765
+ var err error
766
+ node .forChilds (func (child common.Hash ) {
767
+ if err == nil {
768
+ err = db .commit (child , batch , uncacher )
772
769
}
770
+ })
771
+ if err != nil {
772
+ return err
773
773
}
774
774
if err := batch .Put (hash [:], node .rlp ()); err != nil {
775
775
return err
0 commit comments