1// Copyright 2019 The go-ethereum Authors
2// This file is part of the go-ethereum library.
3//
4// The go-ethereum library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// The go-ethereum library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public License
15// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16
17package snapshot
18
19import (
20	"bytes"
21	"encoding/binary"
22	"errors"
23	"fmt"
24	"math/big"
25	"time"
26
27	"github.com/VictoriaMetrics/fastcache"
28	"github.com/ethereum/go-ethereum/common"
29	"github.com/ethereum/go-ethereum/common/hexutil"
30	"github.com/ethereum/go-ethereum/common/math"
31	"github.com/ethereum/go-ethereum/core/rawdb"
32	"github.com/ethereum/go-ethereum/crypto"
33	"github.com/ethereum/go-ethereum/ethdb"
34	"github.com/ethereum/go-ethereum/ethdb/memorydb"
35	"github.com/ethereum/go-ethereum/log"
36	"github.com/ethereum/go-ethereum/metrics"
37	"github.com/ethereum/go-ethereum/rlp"
38	"github.com/ethereum/go-ethereum/trie"
39)
40
41var (
42	// emptyRoot is the known root hash of an empty trie.
43	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
44
45	// emptyCode is the known hash of the empty EVM bytecode.
46	emptyCode = crypto.Keccak256Hash(nil)
47
48	// accountCheckRange is the upper limit of the number of accounts involved in
49	// each range check. This is a value estimated based on experience. If this
50	// value is too large, the failure rate of range prove will increase. Otherwise
51	// the value is too small, the efficiency of the state recovery will decrease.
52	accountCheckRange = 128
53
54	// storageCheckRange is the upper limit of the number of storage slots involved
55	// in each range check. This is a value estimated based on experience. If this
56	// value is too large, the failure rate of range prove will increase. Otherwise
57	// the value is too small, the efficiency of the state recovery will decrease.
58	storageCheckRange = 1024
59
60	// errMissingTrie is returned if the target trie is missing while the generation
61	// is running. In this case the generation is aborted and wait the new signal.
62	errMissingTrie = errors.New("missing trie")
63)
64
65// Metrics in generation
66var (
67	snapGeneratedAccountMeter     = metrics.NewRegisteredMeter("state/snapshot/generation/account/generated", nil)
68	snapRecoveredAccountMeter     = metrics.NewRegisteredMeter("state/snapshot/generation/account/recovered", nil)
69	snapWipedAccountMeter         = metrics.NewRegisteredMeter("state/snapshot/generation/account/wiped", nil)
70	snapMissallAccountMeter       = metrics.NewRegisteredMeter("state/snapshot/generation/account/missall", nil)
71	snapGeneratedStorageMeter     = metrics.NewRegisteredMeter("state/snapshot/generation/storage/generated", nil)
72	snapRecoveredStorageMeter     = metrics.NewRegisteredMeter("state/snapshot/generation/storage/recovered", nil)
73	snapWipedStorageMeter         = metrics.NewRegisteredMeter("state/snapshot/generation/storage/wiped", nil)
74	snapMissallStorageMeter       = metrics.NewRegisteredMeter("state/snapshot/generation/storage/missall", nil)
75	snapSuccessfulRangeProofMeter = metrics.NewRegisteredMeter("state/snapshot/generation/proof/success", nil)
76	snapFailedRangeProofMeter     = metrics.NewRegisteredMeter("state/snapshot/generation/proof/failure", nil)
77
78	// snapAccountProveCounter measures time spent on the account proving
79	snapAccountProveCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/prove", nil)
80	// snapAccountTrieReadCounter measures time spent on the account trie iteration
81	snapAccountTrieReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/trieread", nil)
82	// snapAccountSnapReadCounter measues time spent on the snapshot account iteration
83	snapAccountSnapReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/snapread", nil)
84	// snapAccountWriteCounter measures time spent on writing/updating/deleting accounts
85	snapAccountWriteCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/account/write", nil)
86	// snapStorageProveCounter measures time spent on storage proving
87	snapStorageProveCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/prove", nil)
88	// snapStorageTrieReadCounter measures time spent on the storage trie iteration
89	snapStorageTrieReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/trieread", nil)
90	// snapStorageSnapReadCounter measures time spent on the snapshot storage iteration
91	snapStorageSnapReadCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/snapread", nil)
92	// snapStorageWriteCounter measures time spent on writing/updating/deleting storages
93	snapStorageWriteCounter = metrics.NewRegisteredCounter("state/snapshot/generation/duration/storage/write", nil)
94)
95
96// generatorStats is a collection of statistics gathered by the snapshot generator
97// for logging purposes.
98type generatorStats struct {
99	origin   uint64             // Origin prefix where generation started
100	start    time.Time          // Timestamp when generation started
101	accounts uint64             // Number of accounts indexed(generated or recovered)
102	slots    uint64             // Number of storage slots indexed(generated or recovered)
103	storage  common.StorageSize // Total account and storage slot size(generation or recovery)
104}
105
106// Log creates an contextual log with the given message and the context pulled
107// from the internally maintained statistics.
108func (gs *generatorStats) Log(msg string, root common.Hash, marker []byte) {
109	var ctx []interface{}
110	if root != (common.Hash{}) {
111		ctx = append(ctx, []interface{}{"root", root}...)
112	}
113	// Figure out whether we're after or within an account
114	switch len(marker) {
115	case common.HashLength:
116		ctx = append(ctx, []interface{}{"at", common.BytesToHash(marker)}...)
117	case 2 * common.HashLength:
118		ctx = append(ctx, []interface{}{
119			"in", common.BytesToHash(marker[:common.HashLength]),
120			"at", common.BytesToHash(marker[common.HashLength:]),
121		}...)
122	}
123	// Add the usual measurements
124	ctx = append(ctx, []interface{}{
125		"accounts", gs.accounts,
126		"slots", gs.slots,
127		"storage", gs.storage,
128		"elapsed", common.PrettyDuration(time.Since(gs.start)),
129	}...)
130	// Calculate the estimated indexing time based on current stats
131	if len(marker) > 0 {
132		if done := binary.BigEndian.Uint64(marker[:8]) - gs.origin; done > 0 {
133			left := math.MaxUint64 - binary.BigEndian.Uint64(marker[:8])
134
135			speed := done/uint64(time.Since(gs.start)/time.Millisecond+1) + 1 // +1s to avoid division by zero
136			ctx = append(ctx, []interface{}{
137				"eta", common.PrettyDuration(time.Duration(left/speed) * time.Millisecond),
138			}...)
139		}
140	}
141	log.Info(msg, ctx...)
142}
143
144// generateSnapshot regenerates a brand new snapshot based on an existing state
145// database and head block asynchronously. The snapshot is returned immediately
146// and generation is continued in the background until done.
147func generateSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash) *diskLayer {
148	// Create a new disk layer with an initialized state marker at zero
149	var (
150		stats     = &generatorStats{start: time.Now()}
151		batch     = diskdb.NewBatch()
152		genMarker = []byte{} // Initialized but empty!
153	)
154	rawdb.WriteSnapshotRoot(batch, root)
155	journalProgress(batch, genMarker, stats)
156	if err := batch.Write(); err != nil {
157		log.Crit("Failed to write initialized state marker", "err", err)
158	}
159	base := &diskLayer{
160		diskdb:     diskdb,
161		triedb:     triedb,
162		root:       root,
163		cache:      fastcache.New(cache * 1024 * 1024),
164		genMarker:  genMarker,
165		genPending: make(chan struct{}),
166		genAbort:   make(chan chan *generatorStats),
167	}
168	go base.generate(stats)
169	log.Debug("Start snapshot generation", "root", root)
170	return base
171}
172
173// journalProgress persists the generator stats into the database to resume later.
174func journalProgress(db ethdb.KeyValueWriter, marker []byte, stats *generatorStats) {
175	// Write out the generator marker. Note it's a standalone disk layer generator
176	// which is not mixed with journal. It's ok if the generator is persisted while
177	// journal is not.
178	entry := journalGenerator{
179		Done:   marker == nil,
180		Marker: marker,
181	}
182	if stats != nil {
183		entry.Accounts = stats.accounts
184		entry.Slots = stats.slots
185		entry.Storage = uint64(stats.storage)
186	}
187	blob, err := rlp.EncodeToBytes(entry)
188	if err != nil {
189		panic(err) // Cannot happen, here to catch dev errors
190	}
191	var logstr string
192	switch {
193	case marker == nil:
194		logstr = "done"
195	case bytes.Equal(marker, []byte{}):
196		logstr = "empty"
197	case len(marker) == common.HashLength:
198		logstr = fmt.Sprintf("%#x", marker)
199	default:
200		logstr = fmt.Sprintf("%#x:%#x", marker[:common.HashLength], marker[common.HashLength:])
201	}
202	log.Debug("Journalled generator progress", "progress", logstr)
203	rawdb.WriteSnapshotGenerator(db, blob)
204}
205
206// proofResult contains the output of range proving which can be used
207// for further processing regardless if it is successful or not.
208type proofResult struct {
209	keys     [][]byte   // The key set of all elements being iterated, even proving is failed
210	vals     [][]byte   // The val set of all elements being iterated, even proving is failed
211	diskMore bool       // Set when the database has extra snapshot states since last iteration
212	trieMore bool       // Set when the trie has extra snapshot states(only meaningful for successful proving)
213	proofErr error      // Indicator whether the given state range is valid or not
214	tr       *trie.Trie // The trie, in case the trie was resolved by the prover (may be nil)
215}
216
217// valid returns the indicator that range proof is successful or not.
218func (result *proofResult) valid() bool {
219	return result.proofErr == nil
220}
221
222// last returns the last verified element key regardless of whether the range proof is
223// successful or not. Nil is returned if nothing involved in the proving.
224func (result *proofResult) last() []byte {
225	var last []byte
226	if len(result.keys) > 0 {
227		last = result.keys[len(result.keys)-1]
228	}
229	return last
230}
231
232// forEach iterates all the visited elements and applies the given callback on them.
233// The iteration is aborted if the callback returns non-nil error.
234func (result *proofResult) forEach(callback func(key []byte, val []byte) error) error {
235	for i := 0; i < len(result.keys); i++ {
236		key, val := result.keys[i], result.vals[i]
237		if err := callback(key, val); err != nil {
238			return err
239		}
240	}
241	return nil
242}
243
244// proveRange proves the snapshot segment with particular prefix is "valid".
245// The iteration start point will be assigned if the iterator is restored from
246// the last interruption. Max will be assigned in order to limit the maximum
247// amount of data involved in each iteration.
248//
249// The proof result will be returned if the range proving is finished, otherwise
250// the error will be returned to abort the entire procedure.
251func (dl *diskLayer) proveRange(stats *generatorStats, root common.Hash, prefix []byte, kind string, origin []byte, max int, valueConvertFn func([]byte) ([]byte, error)) (*proofResult, error) {
252	var (
253		keys     [][]byte
254		vals     [][]byte
255		proof    = rawdb.NewMemoryDatabase()
256		diskMore = false
257	)
258	iter := dl.diskdb.NewIterator(prefix, origin)
259	defer iter.Release()
260
261	var start = time.Now()
262	for iter.Next() {
263		key := iter.Key()
264		if len(key) != len(prefix)+common.HashLength {
265			continue
266		}
267		if len(keys) == max {
268			// Break if we've reached the max size, and signal that we're not
269			// done yet.
270			diskMore = true
271			break
272		}
273		keys = append(keys, common.CopyBytes(key[len(prefix):]))
274
275		if valueConvertFn == nil {
276			vals = append(vals, common.CopyBytes(iter.Value()))
277		} else {
278			val, err := valueConvertFn(iter.Value())
279			if err != nil {
280				// Special case, the state data is corrupted (invalid slim-format account),
281				// don't abort the entire procedure directly. Instead, let the fallback
282				// generation to heal the invalid data.
283				//
284				// Here append the original value to ensure that the number of key and
285				// value are the same.
286				vals = append(vals, common.CopyBytes(iter.Value()))
287				log.Error("Failed to convert account state data", "err", err)
288			} else {
289				vals = append(vals, val)
290			}
291		}
292	}
293	// Update metrics for database iteration and merkle proving
294	if kind == "storage" {
295		snapStorageSnapReadCounter.Inc(time.Since(start).Nanoseconds())
296	} else {
297		snapAccountSnapReadCounter.Inc(time.Since(start).Nanoseconds())
298	}
299	defer func(start time.Time) {
300		if kind == "storage" {
301			snapStorageProveCounter.Inc(time.Since(start).Nanoseconds())
302		} else {
303			snapAccountProveCounter.Inc(time.Since(start).Nanoseconds())
304		}
305	}(time.Now())
306
307	// The snap state is exhausted, pass the entire key/val set for verification
308	if origin == nil && !diskMore {
309		stackTr := trie.NewStackTrie(nil)
310		for i, key := range keys {
311			stackTr.TryUpdate(key, vals[i])
312		}
313		if gotRoot := stackTr.Hash(); gotRoot != root {
314			return &proofResult{
315				keys:     keys,
316				vals:     vals,
317				proofErr: fmt.Errorf("wrong root: have %#x want %#x", gotRoot, root),
318			}, nil
319		}
320		return &proofResult{keys: keys, vals: vals}, nil
321	}
322	// Snap state is chunked, generate edge proofs for verification.
323	tr, err := trie.New(root, dl.triedb)
324	if err != nil {
325		stats.Log("Trie missing, state snapshotting paused", dl.root, dl.genMarker)
326		return nil, errMissingTrie
327	}
328	// Firstly find out the key of last iterated element.
329	var last []byte
330	if len(keys) > 0 {
331		last = keys[len(keys)-1]
332	}
333	// Generate the Merkle proofs for the first and last element
334	if origin == nil {
335		origin = common.Hash{}.Bytes()
336	}
337	if err := tr.Prove(origin, 0, proof); err != nil {
338		log.Debug("Failed to prove range", "kind", kind, "origin", origin, "err", err)
339		return &proofResult{
340			keys:     keys,
341			vals:     vals,
342			diskMore: diskMore,
343			proofErr: err,
344			tr:       tr,
345		}, nil
346	}
347	if last != nil {
348		if err := tr.Prove(last, 0, proof); err != nil {
349			log.Debug("Failed to prove range", "kind", kind, "last", last, "err", err)
350			return &proofResult{
351				keys:     keys,
352				vals:     vals,
353				diskMore: diskMore,
354				proofErr: err,
355				tr:       tr,
356			}, nil
357		}
358	}
359	// Verify the snapshot segment with range prover, ensure that all flat states
360	// in this range correspond to merkle trie.
361	cont, err := trie.VerifyRangeProof(root, origin, last, keys, vals, proof)
362	return &proofResult{
363			keys:     keys,
364			vals:     vals,
365			diskMore: diskMore,
366			trieMore: cont,
367			proofErr: err,
368			tr:       tr},
369		nil
370}
371
372// onStateCallback is a function that is called by generateRange, when processing a range of
373// accounts or storage slots. For each element, the callback is invoked.
374// If 'delete' is true, then this element (and potential slots) needs to be deleted from the snapshot.
375// If 'write' is true, then this element needs to be updated with the 'val'.
376// If 'write' is false, then this element is already correct, and needs no update. However,
377// for accounts, the storage trie of the account needs to be checked.
378// The 'val' is the canonical encoding of the value (not the slim format for accounts)
379type onStateCallback func(key []byte, val []byte, write bool, delete bool) error
380
381// generateRange generates the state segment with particular prefix. Generation can
382// either verify the correctness of existing state through rangeproof and skip
383// generation, or iterate trie to regenerate state on demand.
384func (dl *diskLayer) generateRange(root common.Hash, prefix []byte, kind string, origin []byte, max int, stats *generatorStats, onState onStateCallback, valueConvertFn func([]byte) ([]byte, error)) (bool, []byte, error) {
385	// Use range prover to check the validity of the flat state in the range
386	result, err := dl.proveRange(stats, root, prefix, kind, origin, max, valueConvertFn)
387	if err != nil {
388		return false, nil, err
389	}
390	last := result.last()
391
392	// Construct contextual logger
393	logCtx := []interface{}{"kind", kind, "prefix", hexutil.Encode(prefix)}
394	if len(origin) > 0 {
395		logCtx = append(logCtx, "origin", hexutil.Encode(origin))
396	}
397	logger := log.New(logCtx...)
398
399	// The range prover says the range is correct, skip trie iteration
400	if result.valid() {
401		snapSuccessfulRangeProofMeter.Mark(1)
402		logger.Trace("Proved state range", "last", hexutil.Encode(last))
403
404		// The verification is passed, process each state with the given
405		// callback function. If this state represents a contract, the
406		// corresponding storage check will be performed in the callback
407		if err := result.forEach(func(key []byte, val []byte) error { return onState(key, val, false, false) }); err != nil {
408			return false, nil, err
409		}
410		// Only abort the iteration when both database and trie are exhausted
411		return !result.diskMore && !result.trieMore, last, nil
412	}
413	logger.Trace("Detected outdated state range", "last", hexutil.Encode(last), "err", result.proofErr)
414	snapFailedRangeProofMeter.Mark(1)
415
416	// Special case, the entire trie is missing. In the original trie scheme,
417	// all the duplicated subtries will be filter out(only one copy of data
418	// will be stored). While in the snapshot model, all the storage tries
419	// belong to different contracts will be kept even they are duplicated.
420	// Track it to a certain extent remove the noise data used for statistics.
421	if origin == nil && last == nil {
422		meter := snapMissallAccountMeter
423		if kind == "storage" {
424			meter = snapMissallStorageMeter
425		}
426		meter.Mark(1)
427	}
428
429	// We use the snap data to build up a cache which can be used by the
430	// main account trie as a primary lookup when resolving hashes
431	var snapNodeCache ethdb.KeyValueStore
432	if len(result.keys) > 0 {
433		snapNodeCache = memorydb.New()
434		snapTrieDb := trie.NewDatabase(snapNodeCache)
435		snapTrie, _ := trie.New(common.Hash{}, snapTrieDb)
436		for i, key := range result.keys {
437			snapTrie.Update(key, result.vals[i])
438		}
439		root, _, _ := snapTrie.Commit(nil)
440		snapTrieDb.Commit(root, false, nil)
441	}
442	tr := result.tr
443	if tr == nil {
444		tr, err = trie.New(root, dl.triedb)
445		if err != nil {
446			stats.Log("Trie missing, state snapshotting paused", dl.root, dl.genMarker)
447			return false, nil, errMissingTrie
448		}
449	}
450
451	var (
452		trieMore       bool
453		nodeIt         = tr.NodeIterator(origin)
454		iter           = trie.NewIterator(nodeIt)
455		kvkeys, kvvals = result.keys, result.vals
456
457		// counters
458		count     = 0 // number of states delivered by iterator
459		created   = 0 // states created from the trie
460		updated   = 0 // states updated from the trie
461		deleted   = 0 // states not in trie, but were in snapshot
462		untouched = 0 // states already correct
463
464		// timers
465		start    = time.Now()
466		internal time.Duration
467	)
468	nodeIt.AddResolver(snapNodeCache)
469	for iter.Next() {
470		if last != nil && bytes.Compare(iter.Key, last) > 0 {
471			trieMore = true
472			break
473		}
474		count++
475		write := true
476		created++
477		for len(kvkeys) > 0 {
478			if cmp := bytes.Compare(kvkeys[0], iter.Key); cmp < 0 {
479				// delete the key
480				istart := time.Now()
481				if err := onState(kvkeys[0], nil, false, true); err != nil {
482					return false, nil, err
483				}
484				kvkeys = kvkeys[1:]
485				kvvals = kvvals[1:]
486				deleted++
487				internal += time.Since(istart)
488				continue
489			} else if cmp == 0 {
490				// the snapshot key can be overwritten
491				created--
492				if write = !bytes.Equal(kvvals[0], iter.Value); write {
493					updated++
494				} else {
495					untouched++
496				}
497				kvkeys = kvkeys[1:]
498				kvvals = kvvals[1:]
499			}
500			break
501		}
502		istart := time.Now()
503		if err := onState(iter.Key, iter.Value, write, false); err != nil {
504			return false, nil, err
505		}
506		internal += time.Since(istart)
507	}
508	if iter.Err != nil {
509		return false, nil, iter.Err
510	}
511	// Delete all stale snapshot states remaining
512	istart := time.Now()
513	for _, key := range kvkeys {
514		if err := onState(key, nil, false, true); err != nil {
515			return false, nil, err
516		}
517		deleted += 1
518	}
519	internal += time.Since(istart)
520
521	// Update metrics for counting trie iteration
522	if kind == "storage" {
523		snapStorageTrieReadCounter.Inc((time.Since(start) - internal).Nanoseconds())
524	} else {
525		snapAccountTrieReadCounter.Inc((time.Since(start) - internal).Nanoseconds())
526	}
527	logger.Debug("Regenerated state range", "root", root, "last", hexutil.Encode(last),
528		"count", count, "created", created, "updated", updated, "untouched", untouched, "deleted", deleted)
529
530	// If there are either more trie items, or there are more snap items
531	// (in the next segment), then we need to keep working
532	return !trieMore && !result.diskMore, last, nil
533}
534
535// generate is a background thread that iterates over the state and storage tries,
536// constructing the state snapshot. All the arguments are purely for statistics
537// gathering and logging, since the method surfs the blocks as they arrive, often
538// being restarted.
539func (dl *diskLayer) generate(stats *generatorStats) {
540	var (
541		accMarker    []byte
542		accountRange = accountCheckRange
543	)
544	if len(dl.genMarker) > 0 { // []byte{} is the start, use nil for that
545		// Always reset the initial account range as 1
546		// whenever recover from the interruption.
547		accMarker, accountRange = dl.genMarker[:common.HashLength], 1
548	}
549	var (
550		batch     = dl.diskdb.NewBatch()
551		logged    = time.Now()
552		accOrigin = common.CopyBytes(accMarker)
553		abort     chan *generatorStats
554	)
555	stats.Log("Resuming state snapshot generation", dl.root, dl.genMarker)
556
557	checkAndFlush := func(currentLocation []byte) error {
558		select {
559		case abort = <-dl.genAbort:
560		default:
561		}
562		if batch.ValueSize() > ethdb.IdealBatchSize || abort != nil {
563			if bytes.Compare(currentLocation, dl.genMarker) < 0 {
564				log.Error("Snapshot generator went backwards",
565					"currentLocation", fmt.Sprintf("%x", currentLocation),
566					"genMarker", fmt.Sprintf("%x", dl.genMarker))
567			}
568
569			// Flush out the batch anyway no matter it's empty or not.
570			// It's possible that all the states are recovered and the
571			// generation indeed makes progress.
572			journalProgress(batch, currentLocation, stats)
573
574			if err := batch.Write(); err != nil {
575				return err
576			}
577			batch.Reset()
578
579			dl.lock.Lock()
580			dl.genMarker = currentLocation
581			dl.lock.Unlock()
582
583			if abort != nil {
584				stats.Log("Aborting state snapshot generation", dl.root, currentLocation)
585				return errors.New("aborted")
586			}
587		}
588		if time.Since(logged) > 8*time.Second {
589			stats.Log("Generating state snapshot", dl.root, currentLocation)
590			logged = time.Now()
591		}
592		return nil
593	}
594
595	onAccount := func(key []byte, val []byte, write bool, delete bool) error {
596		var (
597			start       = time.Now()
598			accountHash = common.BytesToHash(key)
599		)
600		if delete {
601			rawdb.DeleteAccountSnapshot(batch, accountHash)
602			snapWipedAccountMeter.Mark(1)
603
604			// Ensure that any previous snapshot storage values are cleared
605			prefix := append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...)
606			keyLen := len(rawdb.SnapshotStoragePrefix) + 2*common.HashLength
607			if err := wipeKeyRange(dl.diskdb, "storage", prefix, nil, nil, keyLen, snapWipedStorageMeter, false); err != nil {
608				return err
609			}
610			snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
611			return nil
612		}
613		// Retrieve the current account and flatten it into the internal format
614		var acc struct {
615			Nonce    uint64
616			Balance  *big.Int
617			Root     common.Hash
618			CodeHash []byte
619		}
620		if err := rlp.DecodeBytes(val, &acc); err != nil {
621			log.Crit("Invalid account encountered during snapshot creation", "err", err)
622		}
623		// If the account is not yet in-progress, write it out
624		if accMarker == nil || !bytes.Equal(accountHash[:], accMarker) {
625			dataLen := len(val) // Approximate size, saves us a round of RLP-encoding
626			if !write {
627				if bytes.Equal(acc.CodeHash, emptyCode[:]) {
628					dataLen -= 32
629				}
630				if acc.Root == emptyRoot {
631					dataLen -= 32
632				}
633				snapRecoveredAccountMeter.Mark(1)
634			} else {
635				data := SlimAccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash)
636				dataLen = len(data)
637				rawdb.WriteAccountSnapshot(batch, accountHash, data)
638				snapGeneratedAccountMeter.Mark(1)
639			}
640			stats.storage += common.StorageSize(1 + common.HashLength + dataLen)
641			stats.accounts++
642		}
643		marker := accountHash[:]
644		// If the snap generation goes here after interrupted, genMarker may go backward
645		// when last genMarker is consisted of accountHash and storageHash
646		if accMarker != nil && bytes.Equal(marker, accMarker) && len(dl.genMarker) > common.HashLength {
647			marker = dl.genMarker[:]
648		}
649		// If we've exceeded our batch allowance or termination was requested, flush to disk
650		if err := checkAndFlush(marker); err != nil {
651			return err
652		}
653		// If the iterated account is the contract, create a further loop to
654		// verify or regenerate the contract storage.
655		if acc.Root == emptyRoot {
656			// If the root is empty, we still need to ensure that any previous snapshot
657			// storage values are cleared
658			// TODO: investigate if this can be avoided, this will be very costly since it
659			// affects every single EOA account
660			//  - Perhaps we can avoid if where codeHash is emptyCode
661			prefix := append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...)
662			keyLen := len(rawdb.SnapshotStoragePrefix) + 2*common.HashLength
663			if err := wipeKeyRange(dl.diskdb, "storage", prefix, nil, nil, keyLen, snapWipedStorageMeter, false); err != nil {
664				return err
665			}
666			snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
667		} else {
668			snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
669
670			var storeMarker []byte
671			if accMarker != nil && bytes.Equal(accountHash[:], accMarker) && len(dl.genMarker) > common.HashLength {
672				storeMarker = dl.genMarker[common.HashLength:]
673			}
674			onStorage := func(key []byte, val []byte, write bool, delete bool) error {
675				defer func(start time.Time) {
676					snapStorageWriteCounter.Inc(time.Since(start).Nanoseconds())
677				}(time.Now())
678
679				if delete {
680					rawdb.DeleteStorageSnapshot(batch, accountHash, common.BytesToHash(key))
681					snapWipedStorageMeter.Mark(1)
682					return nil
683				}
684				if write {
685					rawdb.WriteStorageSnapshot(batch, accountHash, common.BytesToHash(key), val)
686					snapGeneratedStorageMeter.Mark(1)
687				} else {
688					snapRecoveredStorageMeter.Mark(1)
689				}
690				stats.storage += common.StorageSize(1 + 2*common.HashLength + len(val))
691				stats.slots++
692
693				// If we've exceeded our batch allowance or termination was requested, flush to disk
694				if err := checkAndFlush(append(accountHash[:], key...)); err != nil {
695					return err
696				}
697				return nil
698			}
699			var storeOrigin = common.CopyBytes(storeMarker)
700			for {
701				exhausted, last, err := dl.generateRange(acc.Root, append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...), "storage", storeOrigin, storageCheckRange, stats, onStorage, nil)
702				if err != nil {
703					return err
704				}
705				if exhausted {
706					break
707				}
708				if storeOrigin = increaseKey(last); storeOrigin == nil {
709					break // special case, the last is 0xffffffff...fff
710				}
711			}
712		}
713		// Some account processed, unmark the marker
714		accMarker = nil
715		return nil
716	}
717
718	// Global loop for regerating the entire state trie + all layered storage tries.
719	for {
720		exhausted, last, err := dl.generateRange(dl.root, rawdb.SnapshotAccountPrefix, "account", accOrigin, accountRange, stats, onAccount, FullAccountRLP)
721		// The procedure it aborted, either by external signal or internal error
722		if err != nil {
723			if abort == nil { // aborted by internal error, wait the signal
724				abort = <-dl.genAbort
725			}
726			abort <- stats
727			return
728		}
729		// Abort the procedure if the entire snapshot is generated
730		if exhausted {
731			break
732		}
733		if accOrigin = increaseKey(last); accOrigin == nil {
734			break // special case, the last is 0xffffffff...fff
735		}
736		accountRange = accountCheckRange
737	}
738	// Snapshot fully generated, set the marker to nil.
739	// Note even there is nothing to commit, persist the
740	// generator anyway to mark the snapshot is complete.
741	journalProgress(batch, nil, stats)
742	if err := batch.Write(); err != nil {
743		log.Error("Failed to flush batch", "err", err)
744
745		abort = <-dl.genAbort
746		abort <- stats
747		return
748	}
749	batch.Reset()
750
751	log.Info("Generated state snapshot", "accounts", stats.accounts, "slots", stats.slots,
752		"storage", stats.storage, "elapsed", common.PrettyDuration(time.Since(stats.start)))
753
754	dl.lock.Lock()
755	dl.genMarker = nil
756	close(dl.genPending)
757	dl.lock.Unlock()
758
759	// Someone will be looking for us, wait it out
760	abort = <-dl.genAbort
761	abort <- nil
762}
763
764// increaseKey increase the input key by one bit. Return nil if the entire
765// addition operation overflows,
766func increaseKey(key []byte) []byte {
767	for i := len(key) - 1; i >= 0; i-- {
768		key[i]++
769		if key[i] != 0x0 {
770			return key
771		}
772	}
773	return nil
774}
775