@@ -148,11 +148,11 @@ type snapshot interface {
148148 StorageIterator (account common.Hash , seek common.Hash ) (StorageIterator , bool )
149149}
150150
151- // SnapshotTree is an Ethereum state snapshot tree. It consists of one persistent
152- // base layer backed by a key-value store, on top of which arbitrarily many in-
153- // memory diff layers are topped. The memory diffs can form a tree with branching,
154- // but the disk layer is singleton and common to all. If a reorg goes deeper than
155- // the disk layer, everything needs to be deleted.
151+ // Tree is an Ethereum state snapshot tree. It consists of one persistent base
152+ // layer backed by a key-value store, on top of which arbitrarily many in-memory
153+ // diff layers are topped. The memory diffs can form a tree with branching, but
154+ // the disk layer is singleton and common to all. If a reorg goes deeper than the
155+ // disk layer, everything needs to be deleted.
156156//
157157// The goal of a state snapshot is twofold: to allow direct access to account and
158158// storage data to avoid expensive multi-level trie lookups; and to allow sorted,
@@ -186,7 +186,11 @@ func New(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root comm
186186 defer snap .waitBuild ()
187187 }
188188 // Attempt to load a previously persisted snapshot and rebuild one if failed
189- head , err := loadSnapshot (diskdb , triedb , cache , root , recovery )
189+ head , disabled , err := loadSnapshot (diskdb , triedb , cache , root , recovery )
190+ if disabled {
191+ log .Warn ("Snapshot maintenance disabled (syncing)" )
192+ return snap , nil
193+ }
190194 if err != nil {
191195 if rebuild {
192196 log .Warn ("Failed to load snapshot, regenerating" , "err" , err )
@@ -224,6 +228,55 @@ func (t *Tree) waitBuild() {
224228 }
225229}
226230
231+ // Disable interrupts any pending snapshot generator, deletes all the snapshot
232+ // layers in memory and marks snapshots disabled globally. In order to resume
233+ // the snapshot functionality, the caller must invoke Rebuild.
234+ func (t * Tree ) Disable () {
235+ // Interrupt any live snapshot layers
236+ t .lock .Lock ()
237+ defer t .lock .Unlock ()
238+
239+ for _ , layer := range t .layers {
240+ switch layer := layer .(type ) {
241+ case * diskLayer :
242+ // If the base layer is generating, abort it
243+ if layer .genAbort != nil {
244+ abort := make (chan * generatorStats )
245+ layer .genAbort <- abort
246+ <- abort
247+ }
248+ // Layer should be inactive now, mark it as stale
249+ layer .lock .Lock ()
250+ layer .stale = true
251+ layer .lock .Unlock ()
252+
253+ case * diffLayer :
254+ // If the layer is a simple diff, simply mark as stale
255+ layer .lock .Lock ()
256+ atomic .StoreUint32 (& layer .stale , 1 )
257+ layer .lock .Unlock ()
258+
259+ default :
260+ panic (fmt .Sprintf ("unknown layer type: %T" , layer ))
261+ }
262+ }
263+ t .layers = map [common.Hash ]snapshot {}
264+
265+ // Delete all snapshot liveness information from the database
266+ batch := t .diskdb .NewBatch ()
267+
268+ rawdb .WriteSnapshotDisabled (batch )
269+ rawdb .DeleteSnapshotRoot (batch )
270+ rawdb .DeleteSnapshotJournal (batch )
271+ rawdb .DeleteSnapshotGenerator (batch )
272+ rawdb .DeleteSnapshotRecoveryNumber (batch )
273+ // Note, we don't delete the sync progress
274+
275+ if err := batch .Write (); err != nil {
276+ log .Crit ("Failed to disable snapshots" , "err" , err )
277+ }
278+ }
279+
227280// Snapshot retrieves a snapshot belonging to the given block root, or nil if no
228281// snapshot is maintained for that block.
229282func (t * Tree ) Snapshot (blockRoot common.Hash ) Snapshot {
@@ -626,8 +679,9 @@ func (t *Tree) Rebuild(root common.Hash) {
626679 defer t .lock .Unlock ()
627680
628681 // Firstly delete any recovery flag in the database. Because now we are
629- // building a brand new snapshot.
682+ // building a brand new snapshot. Also reenable the snapshot feature.
630683 rawdb .DeleteSnapshotRecoveryNumber (t .diskdb )
684+ rawdb .DeleteSnapshotDisabled (t .diskdb )
631685
632686 // Iterate over and mark all layers stale
633687 for _ , layer := range t .layers {
0 commit comments