@@ -22,8 +22,6 @@ import (
2222 "sync"
2323
2424 "github.com/ethereum/go-ethereum/common"
25- "github.com/ethereum/go-ethereum/core/rawdb"
26- "github.com/ethereum/go-ethereum/ethdb"
2725 "github.com/ethereum/go-ethereum/log"
2826 "github.com/ethereum/go-ethereum/rlp"
2927)
@@ -169,125 +167,6 @@ func (dl *diffLayer) Update(blockRoot common.Hash, accounts map[common.Hash][]by
169167 return newDiffLayer (dl , dl .number + 1 , blockRoot , accounts , storage )
170168}
171169
172- // Cap traverses downwards the diff tree until the number of allowed layers are
173- // crossed. All diffs beyond the permitted number are flattened downwards. If
174- // the layer limit is reached, memory cap is also enforced (but not before). The
175- // block numbers for the disk layer and first diff layer are returned for GC.
176- func (dl * diffLayer ) Cap (layers int , memory uint64 ) (uint64 , uint64 ) {
177- // Dive until we run out of layers or reach the persistent database
178- if layers > 2 {
179- // If we still have diff layers below, recurse
180- if parent , ok := dl .parent .(* diffLayer ); ok {
181- return parent .Cap (layers - 1 , memory )
182- }
183- // Diff stack too shallow, return block numbers without modifications
184- return dl .parent .(* diskLayer ).number , dl .number
185- }
186- // We're out of layers, flatten anything below, stopping if it's the disk or if
187- // the memory limit is not yet exceeded.
188- switch parent := dl .parent .(type ) {
189- case * diskLayer :
190- return parent .number , dl .number
191- case * diffLayer :
192- // Flatten the parent into the grandparent. The flattening internally obtains a
193- // write lock on grandparent.
194- flattened := parent .flatten ().(* diffLayer )
195-
196- dl .lock .Lock ()
197- defer dl .lock .Unlock ()
198-
199- dl .parent = flattened
200- if flattened .memory < memory {
201- diskNumber , _ := flattened .parent .Info ()
202- return diskNumber , flattened .number
203- }
204- default :
205- panic (fmt .Sprintf ("unknown data layer: %T" , parent ))
206- }
207- // If the bottommost layer is larger than our memory cap, persist to disk
208- var (
209- parent = dl .parent .(* diffLayer )
210- base = parent .parent .(* diskLayer )
211- batch = base .db .NewBatch ()
212- )
213- parent .lock .RLock ()
214- defer parent .lock .RUnlock ()
215-
216- // Start by temporarily deleting the current snapshot block marker. This
217- // ensures that in the case of a crash, the entire snapshot is invalidated.
218- rawdb .DeleteSnapshotBlock (batch )
219-
220- // Mark the original base as stale as we're going to create a new wrapper
221- base .lock .Lock ()
222- if base .stale {
223- panic ("parent disk layer is stale" ) // we've committed into the same base from two children, boo
224- }
225- base .stale = true
226- base .lock .Unlock ()
227-
228- // Push all the accounts into the database
229- for hash , data := range parent .accountData {
230- if len (data ) > 0 {
231- // Account was updated, push to disk
232- rawdb .WriteAccountSnapshot (batch , hash , data )
233- base .cache .Set (string (hash [:]), data )
234-
235- if batch .ValueSize () > ethdb .IdealBatchSize {
236- if err := batch .Write (); err != nil {
237- log .Crit ("Failed to write account snapshot" , "err" , err )
238- }
239- batch .Reset ()
240- }
241- } else {
242- // Account was deleted, remove all storage slots too
243- rawdb .DeleteAccountSnapshot (batch , hash )
244- base .cache .Set (string (hash [:]), nil )
245-
246- it := rawdb .IterateStorageSnapshots (base .db , hash )
247- for it .Next () {
248- if key := it .Key (); len (key ) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator
249- batch .Delete (key )
250- base .cache .Delete (string (key [1 :]))
251- }
252- }
253- it .Release ()
254- }
255- }
256- // Push all the storage slots into the database
257- for accountHash , storage := range parent .storageData {
258- for storageHash , data := range storage {
259- if len (data ) > 0 {
260- rawdb .WriteStorageSnapshot (batch , accountHash , storageHash , data )
261- base .cache .Set (string (append (accountHash [:], storageHash [:]... )), data )
262- } else {
263- rawdb .DeleteStorageSnapshot (batch , accountHash , storageHash )
264- base .cache .Set (string (append (accountHash [:], storageHash [:]... )), nil )
265- }
266- }
267- if batch .ValueSize () > ethdb .IdealBatchSize {
268- if err := batch .Write (); err != nil {
269- log .Crit ("Failed to write storage snapshot" , "err" , err )
270- }
271- batch .Reset ()
272- }
273- }
274- // Update the snapshot block marker and write any remainder data
275- newBase := & diskLayer {
276- root : parent .root ,
277- number : parent .number ,
278- cache : base .cache ,
279- db : base .db ,
280- journal : base .journal ,
281- }
282- rawdb .WriteSnapshotBlock (batch , newBase .number , newBase .root )
283- if err := batch .Write (); err != nil {
284- log .Crit ("Failed to write leftover snapshot" , "err" , err )
285- }
286- dl .parent = newBase
287-
288- return newBase .number , dl .number
289- }
290-
291170// flatten pushes all data from this point downwards, flattening everything into
292171// a single diff at the bottom. Since usually the lowermost diff is the largest,
293172// the flattening bulds up from there in reverse.
0 commit comments