@@ -160,13 +160,15 @@ func (st *SnapshotTree) Update(blockRoot common.Hash, parentRoot common.Hash, ac
160160// are flattened downwards.
161161func (st * SnapshotTree ) Cap (blockRoot common.Hash , layers int , memory uint64 ) error {
162162 // Retrieve the head snapshot to cap from
163- snap := st . Snapshot ( blockRoot ).( snapshot )
164- if snap == nil {
163+ var snap snapshot
164+ if s := st . Snapshot ( blockRoot ); s == nil {
165165 return fmt .Errorf ("snapshot [%#x] missing" , blockRoot )
166+ } else {
167+ snap = s .(snapshot )
166168 }
167169 diff , ok := snap .(* diffLayer )
168170 if ! ok {
169- return fmt .Errorf ("snapshot [%#x] is base layer" , blockRoot )
171+ return fmt .Errorf ("snapshot [%#x] is disk layer" , blockRoot )
170172 }
171173 // Run the internal capping and discard all stale layers
172174 st .lock .Lock ()
@@ -228,13 +230,14 @@ func (st *SnapshotTree) Cap(blockRoot common.Hash, layers int, memory uint64) er
228230// block numbers for the disk layer and first diff layer are returned for GC.
229231func (st * SnapshotTree ) cap (diff * diffLayer , layers int , memory uint64 ) (uint64 , uint64 ) {
230232 // Dive until we run out of layers or reach the persistent database
231- if layers > 2 {
232- // If we still have diff layers below, recurse
233+ for ; layers > 2 ; layers -- {
234+ // If we still have diff layers below, continue down
233235 if parent , ok := diff .parent .(* diffLayer ); ok {
234- return st .cap (parent , layers - 1 , memory )
236+ diff = parent
237+ } else {
238+ // Diff stack too shallow, return block numbers without modifications
239+ return diff .parent .(* diskLayer ).number , diff .number
235240 }
236- // Diff stack too shallow, return block numbers without modifications
237- return diff .parent .(* diskLayer ).number , diff .number
238241 }
239242 // We're out of layers, flatten anything below, stopping if it's the disk or if
240243 // the memory limit is not yet exceeded.
@@ -356,9 +359,11 @@ func diffToDisk(bottom *diffLayer) *diskLayer {
356359// flattening everything down (bad for reorgs).
357360func (st * SnapshotTree ) Journal (blockRoot common.Hash ) error {
358361 // Retrieve the head snapshot to journal from
359- snap := st . Snapshot ( blockRoot ).( snapshot )
360- if snap == nil {
362+ var snap snapshot
363+ if s := st . Snapshot ( blockRoot ); s == nil {
361364 return fmt .Errorf ("snapshot [%#x] missing" , blockRoot )
365+ } else {
366+ snap = s .(snapshot )
362367 }
363368 // Run the journaling
364369 st .lock .Lock ()
0 commit comments