github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"math/big"
    25  	"time"
    26  
    27  	"github.com/DxChainNetwork/dxc/common"
    28  	"github.com/DxChainNetwork/dxc/common/hexutil"
    29  	"github.com/DxChainNetwork/dxc/common/math"
    30  	"github.com/DxChainNetwork/dxc/core/rawdb"
    31  	"github.com/DxChainNetwork/dxc/crypto"
    32  	"github.com/DxChainNetwork/dxc/ethdb"
    33  	"github.com/DxChainNetwork/dxc/ethdb/memorydb"
    34  	"github.com/DxChainNetwork/dxc/log"
    35  	"github.com/DxChainNetwork/dxc/metrics"
    36  	"github.com/DxChainNetwork/dxc/rlp"
    37  	"github.com/DxChainNetwork/dxc/trie"
    38  	"github.com/VictoriaMetrics/fastcache"
    39  )
    40  
    41  var (
    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 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 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
    66  var (
    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.
    98  type 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.
   108  func (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.
   147  func 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.
   174  func 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.
   208  type 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.
   218  func (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.
   224  func (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.
   234  func (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.
   251  func (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)
   379  type 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.
   384  func (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.
   539  func (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  			// Flush out the batch anyway no matter it's empty or not.
   564  			// It's possible that all the states are recovered and the
   565  			// generation indeed makes progress.
   566  			journalProgress(batch, currentLocation, stats)
   567  
   568  			if err := batch.Write(); err != nil {
   569  				return err
   570  			}
   571  			batch.Reset()
   572  
   573  			dl.lock.Lock()
   574  			dl.genMarker = currentLocation
   575  			dl.lock.Unlock()
   576  
   577  			if abort != nil {
   578  				stats.Log("Aborting state snapshot generation", dl.root, currentLocation)
   579  				return errors.New("aborted")
   580  			}
   581  		}
   582  		if time.Since(logged) > 8*time.Second {
   583  			stats.Log("Generating state snapshot", dl.root, currentLocation)
   584  			logged = time.Now()
   585  		}
   586  		return nil
   587  	}
   588  
   589  	onAccount := func(key []byte, val []byte, write bool, delete bool) error {
   590  		var (
   591  			start       = time.Now()
   592  			accountHash = common.BytesToHash(key)
   593  		)
   594  		if delete {
   595  			rawdb.DeleteAccountSnapshot(batch, accountHash)
   596  			snapWipedAccountMeter.Mark(1)
   597  
   598  			// Ensure that any previous snapshot storage values are cleared
   599  			prefix := append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...)
   600  			keyLen := len(rawdb.SnapshotStoragePrefix) + 2*common.HashLength
   601  			if err := wipeKeyRange(dl.diskdb, "storage", prefix, nil, nil, keyLen, snapWipedStorageMeter, false); err != nil {
   602  				return err
   603  			}
   604  			snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
   605  			return nil
   606  		}
   607  		// Retrieve the current account and flatten it into the internal format
   608  		var acc struct {
   609  			Nonce    uint64
   610  			Balance  *big.Int
   611  			Root     common.Hash
   612  			CodeHash []byte
   613  		}
   614  		if err := rlp.DecodeBytes(val, &acc); err != nil {
   615  			log.Crit("Invalid account encountered during snapshot creation", "err", err)
   616  		}
   617  		// If the account is not yet in-progress, write it out
   618  		if accMarker == nil || !bytes.Equal(accountHash[:], accMarker) {
   619  			dataLen := len(val) // Approximate size, saves us a round of RLP-encoding
   620  			if !write {
   621  				if bytes.Equal(acc.CodeHash, emptyCode[:]) {
   622  					dataLen -= 32
   623  				}
   624  				if acc.Root == emptyRoot {
   625  					dataLen -= 32
   626  				}
   627  				snapRecoveredAccountMeter.Mark(1)
   628  			} else {
   629  				data := SlimAccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash)
   630  				dataLen = len(data)
   631  				rawdb.WriteAccountSnapshot(batch, accountHash, data)
   632  				snapGeneratedAccountMeter.Mark(1)
   633  			}
   634  			stats.storage += common.StorageSize(1 + common.HashLength + dataLen)
   635  			stats.accounts++
   636  		}
   637  		// If we've exceeded our batch allowance or termination was requested, flush to disk
   638  		if err := checkAndFlush(accountHash[:]); err != nil {
   639  			return err
   640  		}
   641  		// If the iterated account is the contract, create a further loop to
   642  		// verify or regenerate the contract storage.
   643  		if acc.Root == emptyRoot {
   644  			// If the root is empty, we still need to ensure that any previous snapshot
   645  			// storage values are cleared
   646  			// TODO: investigate if this can be avoided, this will be very costly since it
   647  			// affects every single EOA account
   648  			//  - Perhaps we can avoid if where codeHash is emptyCode
   649  			prefix := append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...)
   650  			keyLen := len(rawdb.SnapshotStoragePrefix) + 2*common.HashLength
   651  			if err := wipeKeyRange(dl.diskdb, "storage", prefix, nil, nil, keyLen, snapWipedStorageMeter, false); err != nil {
   652  				return err
   653  			}
   654  			snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
   655  		} else {
   656  			snapAccountWriteCounter.Inc(time.Since(start).Nanoseconds())
   657  
   658  			var storeMarker []byte
   659  			if accMarker != nil && bytes.Equal(accountHash[:], accMarker) && len(dl.genMarker) > common.HashLength {
   660  				storeMarker = dl.genMarker[common.HashLength:]
   661  			}
   662  			onStorage := func(key []byte, val []byte, write bool, delete bool) error {
   663  				defer func(start time.Time) {
   664  					snapStorageWriteCounter.Inc(time.Since(start).Nanoseconds())
   665  				}(time.Now())
   666  
   667  				if delete {
   668  					rawdb.DeleteStorageSnapshot(batch, accountHash, common.BytesToHash(key))
   669  					snapWipedStorageMeter.Mark(1)
   670  					return nil
   671  				}
   672  				if write {
   673  					rawdb.WriteStorageSnapshot(batch, accountHash, common.BytesToHash(key), val)
   674  					snapGeneratedStorageMeter.Mark(1)
   675  				} else {
   676  					snapRecoveredStorageMeter.Mark(1)
   677  				}
   678  				stats.storage += common.StorageSize(1 + 2*common.HashLength + len(val))
   679  				stats.slots++
   680  
   681  				// If we've exceeded our batch allowance or termination was requested, flush to disk
   682  				if err := checkAndFlush(append(accountHash[:], key...)); err != nil {
   683  					return err
   684  				}
   685  				return nil
   686  			}
   687  			var storeOrigin = common.CopyBytes(storeMarker)
   688  			for {
   689  				exhausted, last, err := dl.generateRange(acc.Root, append(rawdb.SnapshotStoragePrefix, accountHash.Bytes()...), "storage", storeOrigin, storageCheckRange, stats, onStorage, nil)
   690  				if err != nil {
   691  					return err
   692  				}
   693  				if exhausted {
   694  					break
   695  				}
   696  				if storeOrigin = increaseKey(last); storeOrigin == nil {
   697  					break // special case, the last is 0xffffffff...fff
   698  				}
   699  			}
   700  		}
   701  		// Some account processed, unmark the marker
   702  		accMarker = nil
   703  		return nil
   704  	}
   705  
   706  	// Global loop for regerating the entire state trie + all layered storage tries.
   707  	for {
   708  		exhausted, last, err := dl.generateRange(dl.root, rawdb.SnapshotAccountPrefix, "account", accOrigin, accountRange, stats, onAccount, FullAccountRLP)
   709  		// The procedure it aborted, either by external signal or internal error
   710  		if err != nil {
   711  			if abort == nil { // aborted by internal error, wait the signal
   712  				abort = <-dl.genAbort
   713  			}
   714  			abort <- stats
   715  			return
   716  		}
   717  		// Abort the procedure if the entire snapshot is generated
   718  		if exhausted {
   719  			break
   720  		}
   721  		if accOrigin = increaseKey(last); accOrigin == nil {
   722  			break // special case, the last is 0xffffffff...fff
   723  		}
   724  		accountRange = accountCheckRange
   725  	}
   726  	// Snapshot fully generated, set the marker to nil.
   727  	// Note even there is nothing to commit, persist the
   728  	// generator anyway to mark the snapshot is complete.
   729  	journalProgress(batch, nil, stats)
   730  	if err := batch.Write(); err != nil {
   731  		log.Error("Failed to flush batch", "err", err)
   732  
   733  		abort = <-dl.genAbort
   734  		abort <- stats
   735  		return
   736  	}
   737  	batch.Reset()
   738  
   739  	log.Info("Generated state snapshot", "accounts", stats.accounts, "slots", stats.slots,
   740  		"storage", stats.storage, "elapsed", common.PrettyDuration(time.Since(stats.start)))
   741  
   742  	dl.lock.Lock()
   743  	dl.genMarker = nil
   744  	close(dl.genPending)
   745  	dl.lock.Unlock()
   746  
   747  	// Someone will be looking for us, wait it out
   748  	abort = <-dl.genAbort
   749  	abort <- nil
   750  }
   751  
   752  // increaseKey increase the input key by one bit. Return nil if the entire
   753  // addition operation overflows,
   754  func increaseKey(key []byte) []byte {
   755  	for i := len(key) - 1; i >= 0; i-- {
   756  		key[i]++
   757  		if key[i] != 0x0 {
   758  			return key
   759  		}
   760  	}
   761  	return nil
   762  }