github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/core/state/snapshot/generate.go (about)

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