github.com/core-coin/go-core/v2@v2.1.9/core/state/snapshot/snapshot.go (about)

     1  // Copyright 2019 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package snapshot implements a journalled, dynamic state dump.
    18  package snapshot
    19  
    20  import (
    21  	"bytes"
    22  	"errors"
    23  	"fmt"
    24  	"sync"
    25  	"sync/atomic"
    26  
    27  	"github.com/core-coin/go-core/v2/xcbdb"
    28  
    29  	"github.com/core-coin/go-core/v2/common"
    30  	"github.com/core-coin/go-core/v2/core/rawdb"
    31  	"github.com/core-coin/go-core/v2/log"
    32  	"github.com/core-coin/go-core/v2/metrics"
    33  	"github.com/core-coin/go-core/v2/rlp"
    34  	"github.com/core-coin/go-core/v2/trie"
    35  )
    36  
    37  var (
    38  	snapshotCleanAccountHitMeter   = metrics.NewRegisteredMeter("state/snapshot/clean/account/hit", nil)
    39  	snapshotCleanAccountMissMeter  = metrics.NewRegisteredMeter("state/snapshot/clean/account/miss", nil)
    40  	snapshotCleanAccountInexMeter  = metrics.NewRegisteredMeter("state/snapshot/clean/account/inex", nil)
    41  	snapshotCleanAccountReadMeter  = metrics.NewRegisteredMeter("state/snapshot/clean/account/read", nil)
    42  	snapshotCleanAccountWriteMeter = metrics.NewRegisteredMeter("state/snapshot/clean/account/write", nil)
    43  
    44  	snapshotCleanStorageHitMeter   = metrics.NewRegisteredMeter("state/snapshot/clean/storage/hit", nil)
    45  	snapshotCleanStorageMissMeter  = metrics.NewRegisteredMeter("state/snapshot/clean/storage/miss", nil)
    46  	snapshotCleanStorageInexMeter  = metrics.NewRegisteredMeter("state/snapshot/clean/storage/inex", nil)
    47  	snapshotCleanStorageReadMeter  = metrics.NewRegisteredMeter("state/snapshot/clean/storage/read", nil)
    48  	snapshotCleanStorageWriteMeter = metrics.NewRegisteredMeter("state/snapshot/clean/storage/write", nil)
    49  
    50  	snapshotDirtyAccountHitMeter   = metrics.NewRegisteredMeter("state/snapshot/dirty/account/hit", nil)
    51  	snapshotDirtyAccountMissMeter  = metrics.NewRegisteredMeter("state/snapshot/dirty/account/miss", nil)
    52  	snapshotDirtyAccountInexMeter  = metrics.NewRegisteredMeter("state/snapshot/dirty/account/inex", nil)
    53  	snapshotDirtyAccountReadMeter  = metrics.NewRegisteredMeter("state/snapshot/dirty/account/read", nil)
    54  	snapshotDirtyAccountWriteMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/account/write", nil)
    55  
    56  	snapshotDirtyStorageHitMeter   = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/hit", nil)
    57  	snapshotDirtyStorageMissMeter  = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/miss", nil)
    58  	snapshotDirtyStorageInexMeter  = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/inex", nil)
    59  	snapshotDirtyStorageReadMeter  = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/read", nil)
    60  	snapshotDirtyStorageWriteMeter = metrics.NewRegisteredMeter("state/snapshot/dirty/storage/write", nil)
    61  
    62  	snapshotDirtyAccountHitDepthHist = metrics.NewRegisteredHistogram("state/snapshot/dirty/account/hit/depth", nil, metrics.NewExpDecaySample(1028, 0.015))
    63  	snapshotDirtyStorageHitDepthHist = metrics.NewRegisteredHistogram("state/snapshot/dirty/storage/hit/depth", nil, metrics.NewExpDecaySample(1028, 0.015))
    64  
    65  	snapshotFlushAccountItemMeter = metrics.NewRegisteredMeter("state/snapshot/flush/account/item", nil)
    66  	snapshotFlushAccountSizeMeter = metrics.NewRegisteredMeter("state/snapshot/flush/account/size", nil)
    67  	snapshotFlushStorageItemMeter = metrics.NewRegisteredMeter("state/snapshot/flush/storage/item", nil)
    68  	snapshotFlushStorageSizeMeter = metrics.NewRegisteredMeter("state/snapshot/flush/storage/size", nil)
    69  
    70  	snapshotBloomIndexTimer = metrics.NewRegisteredResettingTimer("state/snapshot/bloom/index", nil)
    71  	snapshotBloomErrorGauge = metrics.NewRegisteredGaugeFloat64("state/snapshot/bloom/error", nil)
    72  
    73  	snapshotBloomAccountTrueHitMeter  = metrics.NewRegisteredMeter("state/snapshot/bloom/account/truehit", nil)
    74  	snapshotBloomAccountFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/account/falsehit", nil)
    75  	snapshotBloomAccountMissMeter     = metrics.NewRegisteredMeter("state/snapshot/bloom/account/miss", nil)
    76  
    77  	snapshotBloomStorageTrueHitMeter  = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/truehit", nil)
    78  	snapshotBloomStorageFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/falsehit", nil)
    79  	snapshotBloomStorageMissMeter     = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/miss", nil)
    80  
    81  	// ErrSnapshotStale is returned from data accessors if the underlying snapshot
    82  	// layer had been invalidated due to the chain progressing forward far enough
    83  	// to not maintain the layer's original state.
    84  	ErrSnapshotStale = errors.New("snapshot stale")
    85  
    86  	// ErrNotCoveredYet is returned from data accessors if the underlying snapshot
    87  	// is being generated currently and the requested data item is not yet in the
    88  	// range of accounts covered.
    89  	ErrNotCoveredYet = errors.New("not covered yet")
    90  
    91  	// ErrNotConstructed is returned if the callers want to iterate the snapshot
    92  	// while the generation is not finished yet.
    93  	ErrNotConstructed = errors.New("snapshot is not constructed")
    94  
    95  	// errSnapshotCycle is returned if a snapshot is attempted to be inserted
    96  	// that forms a cycle in the snapshot tree.
    97  	errSnapshotCycle = errors.New("snapshot cycle")
    98  )
    99  
   100  // Snapshot represents the functionality supported by a snapshot storage layer.
   101  type Snapshot interface {
   102  	// Root returns the root hash for which this snapshot was made.
   103  	Root() common.Hash
   104  
   105  	// Account directly retrieves the account associated with a particular hash in
   106  	// the snapshot slim data format.
   107  	Account(hash common.Hash) (*Account, error)
   108  
   109  	// AccountRLP directly retrieves the account RLP associated with a particular
   110  	// hash in the snapshot slim data format.
   111  	AccountRLP(hash common.Hash) ([]byte, error)
   112  
   113  	// Storage directly retrieves the storage data associated with a particular hash,
   114  	// within a particular account.
   115  	Storage(accountHash, storageHash common.Hash) ([]byte, error)
   116  }
   117  
   118  // snapshot is the internal version of the snapshot data layer that supports some
   119  // additional methods compared to the public API.
   120  type snapshot interface {
   121  	Snapshot
   122  
   123  	// Parent returns the subsequent layer of a snapshot, or nil if the base was
   124  	// reached.
   125  	//
   126  	// Note, the method is an internal helper to avoid type switching between the
   127  	// disk and diff layers. There is no locking involved.
   128  	Parent() snapshot
   129  
   130  	// Update creates a new layer on top of the existing snapshot diff tree with
   131  	// the specified data items.
   132  	//
   133  	// Note, the maps are retained by the method to avoid copying everything.
   134  	Update(blockRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer
   135  
   136  	// Journal commits an entire diff hierarchy to disk into a single journal entry.
   137  	// This is meant to be used during shutdown to persist the snapshot without
   138  	// flattening everything down (bad for reorgs).
   139  	Journal(buffer *bytes.Buffer) (common.Hash, error)
   140  
   141  	// LegacyJournal is basically identical to Journal. it's the legacy version for
   142  	// flushing legacy journal. Now the only purpose of this function is for testing.
   143  	LegacyJournal(buffer *bytes.Buffer) (common.Hash, error)
   144  
   145  	// Stale return whether this layer has become stale (was flattened across) or
   146  	// if it's still live.
   147  	Stale() bool
   148  
   149  	// AccountIterator creates an account iterator over an arbitrary layer.
   150  	AccountIterator(seek common.Hash) AccountIterator
   151  
   152  	// StorageIterator creates a storage iterator over an arbitrary layer.
   153  	StorageIterator(account common.Hash, seek common.Hash) (StorageIterator, bool)
   154  }
   155  
   156  // SnapshotTree is an Core state snapshot tree. It consists of one persistent
   157  // base layer backed by a key-value store, on top of which arbitrarily many in-
   158  // memory diff layers are topped. The memory diffs can form a tree with branching,
   159  // but the disk layer is singleton and common to all. If a reorg goes deeper than
   160  // the disk layer, everything needs to be deleted.
   161  //
   162  // The goal of a state snapshot is twofold: to allow direct access to account and
   163  // storage data to avoid expensive multi-level trie lookups; and to allow sorted,
   164  // cheap iteration of the account/storage tries for sync aid.
   165  type Tree struct {
   166  	diskdb xcbdb.KeyValueStore      // Persistent database to store the snapshot
   167  	triedb *trie.Database           // In-memory cache to access the trie through
   168  	cache  int                      // Megabytes permitted to use for read caches
   169  	layers map[common.Hash]snapshot // Collection of all known layers
   170  	lock   sync.RWMutex
   171  }
   172  
   173  // New attempts to load an already existing snapshot from a persistent key-value
   174  // store (with a number of memory layers from a journal), ensuring that the head
   175  // of the snapshot matches the expected one.
   176  //
   177  // If the snapshot is missing or the disk layer is broken, the entire is deleted
   178  // and will be reconstructed from scratch based on the tries in the key-value
   179  // store, on a background thread. If the memory layers from the journal is not
   180  // continuous with disk layer or the journal is missing, all diffs will be discarded
   181  // iff it's in "recovery" mode, otherwise rebuild is mandatory.
   182  func New(diskdb xcbdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, async bool, recovery bool) *Tree {
   183  	// Create a new, empty snapshot tree
   184  	snap := &Tree{
   185  		diskdb: diskdb,
   186  		triedb: triedb,
   187  		cache:  cache,
   188  		layers: make(map[common.Hash]snapshot),
   189  	}
   190  	if !async {
   191  		defer snap.waitBuild()
   192  	}
   193  	// Attempt to load a previously persisted snapshot and rebuild one if failed
   194  	head, err := loadSnapshot(diskdb, triedb, cache, root, recovery)
   195  	if err != nil {
   196  		log.Warn("Failed to load snapshot, regenerating", "err", err)
   197  		snap.Rebuild(root)
   198  		return snap
   199  	}
   200  	// Existing snapshot loaded, seed all the layers
   201  	for head != nil {
   202  		snap.layers[head.Root()] = head
   203  		head = head.Parent()
   204  	}
   205  	return snap
   206  }
   207  
   208  // waitBuild blocks until the snapshot finishes rebuilding. This method is meant
   209  // to be used by tests to ensure we're testing what we believe we are.
   210  func (t *Tree) waitBuild() {
   211  	// Find the rebuild termination channel
   212  	var done chan struct{}
   213  
   214  	t.lock.RLock()
   215  	for _, layer := range t.layers {
   216  		if layer, ok := layer.(*diskLayer); ok {
   217  			done = layer.genPending
   218  			break
   219  		}
   220  	}
   221  	t.lock.RUnlock()
   222  
   223  	// Wait until the snapshot is generated
   224  	if done != nil {
   225  		<-done
   226  	}
   227  }
   228  
   229  // Snapshot retrieves a snapshot belonging to the given block root, or nil if no
   230  // snapshot is maintained for that block.
   231  func (t *Tree) Snapshot(blockRoot common.Hash) Snapshot {
   232  	t.lock.RLock()
   233  	defer t.lock.RUnlock()
   234  
   235  	return t.layers[blockRoot]
   236  }
   237  
   238  // Update adds a new snapshot into the tree, if that can be linked to an existing
   239  // old parent. It is disallowed to insert a disk layer (the origin of all).
   240  func (t *Tree) Update(blockRoot common.Hash, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error {
   241  	// Reject noop updates to avoid self-loops in the snapshot tree. This is a
   242  	// special case that can only happen for Clique networks where empty blocks
   243  	// don't modify the state (0 block subsidy).
   244  	//
   245  	// Although we could silently ignore this internally, it should be the caller's
   246  	// responsibility to avoid even attempting to insert such a snapshot.
   247  	if blockRoot == parentRoot {
   248  		return errSnapshotCycle
   249  	}
   250  	// Generate a new snapshot on top of the parent
   251  	parent := t.Snapshot(parentRoot).(snapshot)
   252  	if parent == nil {
   253  		return fmt.Errorf("parent [%#x] snapshot missing", parentRoot)
   254  	}
   255  	snap := parent.Update(blockRoot, destructs, accounts, storage)
   256  
   257  	// Save the new snapshot for later
   258  	t.lock.Lock()
   259  	defer t.lock.Unlock()
   260  
   261  	t.layers[snap.root] = snap
   262  	return nil
   263  }
   264  
   265  // Cap traverses downwards the snapshot tree from a head block hash until the
   266  // number of allowed layers are crossed. All layers beyond the permitted number
   267  // are flattened downwards.
   268  func (t *Tree) Cap(root common.Hash, layers int) error {
   269  	// Retrieve the head snapshot to cap from
   270  	snap := t.Snapshot(root)
   271  	if snap == nil {
   272  		return fmt.Errorf("snapshot [%#x] missing", root)
   273  	}
   274  	diff, ok := snap.(*diffLayer)
   275  	if !ok {
   276  		return fmt.Errorf("snapshot [%#x] is disk layer", root)
   277  	}
   278  	// If the generator is still running, use a more aggressive cap
   279  	diff.origin.lock.RLock()
   280  	if diff.origin.genMarker != nil && layers > 8 {
   281  		layers = 8
   282  	}
   283  	diff.origin.lock.RUnlock()
   284  
   285  	// Run the internal capping and discard all stale layers
   286  	t.lock.Lock()
   287  	defer t.lock.Unlock()
   288  
   289  	// Flattening the bottom-most diff layer requires special casing since there's
   290  	// no child to rewire to the grandparent. In that case we can fake a temporary
   291  	// child for the capping and then remove it.
   292  	var persisted *diskLayer
   293  
   294  	switch layers {
   295  	case 0:
   296  		// If full commit was requested, flatten the diffs and merge onto disk
   297  		diff.lock.RLock()
   298  		base := diffToDisk(diff.flatten().(*diffLayer))
   299  		diff.lock.RUnlock()
   300  
   301  		// Replace the entire snapshot tree with the flat base
   302  		t.layers = map[common.Hash]snapshot{base.root: base}
   303  		return nil
   304  
   305  	case 1:
   306  		// If full flattening was requested, flatten the diffs but only merge if the
   307  		// memory limit was reached
   308  		var (
   309  			bottom *diffLayer
   310  			base   *diskLayer
   311  		)
   312  		diff.lock.RLock()
   313  		bottom = diff.flatten().(*diffLayer)
   314  		if bottom.memory >= aggregatorMemoryLimit {
   315  			base = diffToDisk(bottom)
   316  		}
   317  		diff.lock.RUnlock()
   318  
   319  		// If all diff layers were removed, replace the entire snapshot tree
   320  		if base != nil {
   321  			t.layers = map[common.Hash]snapshot{base.root: base}
   322  			return nil
   323  		}
   324  		// Merge the new aggregated layer into the snapshot tree, clean stales below
   325  		t.layers[bottom.root] = bottom
   326  
   327  	default:
   328  		// Many layers requested to be retained, cap normally
   329  		persisted = t.cap(diff, layers)
   330  	}
   331  	// Remove any layer that is stale or links into a stale layer
   332  	children := make(map[common.Hash][]common.Hash)
   333  	for root, snap := range t.layers {
   334  		if diff, ok := snap.(*diffLayer); ok {
   335  			parent := diff.parent.Root()
   336  			children[parent] = append(children[parent], root)
   337  		}
   338  	}
   339  	var remove func(root common.Hash)
   340  	remove = func(root common.Hash) {
   341  		delete(t.layers, root)
   342  		for _, child := range children[root] {
   343  			remove(child)
   344  		}
   345  		delete(children, root)
   346  	}
   347  	for root, snap := range t.layers {
   348  		if snap.Stale() {
   349  			remove(root)
   350  		}
   351  	}
   352  	// If the disk layer was modified, regenerate all the cumulative blooms
   353  	if persisted != nil {
   354  		var rebloom func(root common.Hash)
   355  		rebloom = func(root common.Hash) {
   356  			if diff, ok := t.layers[root].(*diffLayer); ok {
   357  				diff.rebloom(persisted)
   358  			}
   359  			for _, child := range children[root] {
   360  				rebloom(child)
   361  			}
   362  		}
   363  		rebloom(persisted.root)
   364  	}
   365  	return nil
   366  }
   367  
   368  // cap traverses downwards the diff tree until the number of allowed layers are
   369  // crossed. All diffs beyond the permitted number are flattened downwards. If the
   370  // layer limit is reached, memory cap is also enforced (but not before).
   371  //
   372  // The method returns the new disk layer if diffs were persistend into it.
   373  func (t *Tree) cap(diff *diffLayer, layers int) *diskLayer {
   374  	// Dive until we run out of layers or reach the persistent database
   375  	for ; layers > 2; layers-- {
   376  		// If we still have diff layers below, continue down
   377  		if parent, ok := diff.parent.(*diffLayer); ok {
   378  			diff = parent
   379  		} else {
   380  			// Diff stack too shallow, return without modifications
   381  			return nil
   382  		}
   383  	}
   384  	// We're out of layers, flatten anything below, stopping if it's the disk or if
   385  	// the memory limit is not yet exceeded.
   386  	switch parent := diff.parent.(type) {
   387  	case *diskLayer:
   388  		return nil
   389  
   390  	case *diffLayer:
   391  		// Flatten the parent into the grandparent. The flattening internally obtains a
   392  		// write lock on grandparent.
   393  		flattened := parent.flatten().(*diffLayer)
   394  		t.layers[flattened.root] = flattened
   395  
   396  		diff.lock.Lock()
   397  		defer diff.lock.Unlock()
   398  
   399  		diff.parent = flattened
   400  		if flattened.memory < aggregatorMemoryLimit {
   401  			// Accumulator layer is smaller than the limit, so we can abort, unless
   402  			// there's a snapshot being generated currently. In that case, the trie
   403  			// will move fron underneath the generator so we **must** merge all the
   404  			// partial data down into the snapshot and restart the generation.
   405  			if flattened.parent.(*diskLayer).genAbort == nil {
   406  				return nil
   407  			}
   408  		}
   409  	default:
   410  		panic(fmt.Sprintf("unknown data layer: %T", parent))
   411  	}
   412  	// If the bottom-most layer is larger than our memory cap, persist to disk
   413  	bottom := diff.parent.(*diffLayer)
   414  
   415  	bottom.lock.RLock()
   416  	base := diffToDisk(bottom)
   417  	bottom.lock.RUnlock()
   418  
   419  	t.layers[base.root] = base
   420  	diff.parent = base
   421  	return base
   422  }
   423  
   424  // diffToDisk merges a bottom-most diff into the persistent disk layer underneath
   425  // it. The method will panic if called onto a non-bottom-most diff layer.
   426  //
   427  // The disk layer persistence should be operated in an atomic way. All updates should
   428  // be discarded if the whole transition if not finished.
   429  func diffToDisk(bottom *diffLayer) *diskLayer {
   430  	var (
   431  		base  = bottom.parent.(*diskLayer)
   432  		batch = base.diskdb.NewBatch()
   433  		stats *generatorStats
   434  	)
   435  	// If the disk layer is running a snapshot generator, abort it
   436  	if base.genAbort != nil {
   437  		abort := make(chan *generatorStats)
   438  		base.genAbort <- abort
   439  		stats = <-abort
   440  	}
   441  	// Put the deletion in the batch writer, flush all updates in the final step.
   442  	rawdb.DeleteSnapshotRoot(batch)
   443  
   444  	// Mark the original base as stale as we're going to create a new wrapper
   445  	base.lock.Lock()
   446  	if base.stale {
   447  		panic("parent disk layer is stale") // we've committed into the same base from two children, boo
   448  	}
   449  	base.stale = true
   450  	base.lock.Unlock()
   451  
   452  	// Destroy all the destructed accounts from the database
   453  	for hash := range bottom.destructSet {
   454  		// Skip any account not covered yet by the snapshot
   455  		if base.genMarker != nil && bytes.Compare(hash[:], base.genMarker) > 0 {
   456  			continue
   457  		}
   458  		// Remove all storage slots
   459  		rawdb.DeleteAccountSnapshot(batch, hash)
   460  		base.cache.Set(hash[:], nil)
   461  
   462  		it := rawdb.IterateStorageSnapshots(base.diskdb, hash)
   463  		for it.Next() {
   464  			if key := it.Key(); len(key) == 65 || len(key) == 57 { // TODO(raisty): Yuck, we should move this into the iterator
   465  				batch.Delete(key)
   466  				base.cache.Del(key[1:])
   467  
   468  				snapshotFlushStorageItemMeter.Mark(1)
   469  			}
   470  		}
   471  		it.Release()
   472  	}
   473  	// Push all updated accounts into the database
   474  	for hash, data := range bottom.accountData {
   475  		// Skip any account not covered yet by the snapshot
   476  		if base.genMarker != nil && bytes.Compare(hash[:], base.genMarker) > 0 {
   477  			continue
   478  		}
   479  		// Push the account to disk
   480  		rawdb.WriteAccountSnapshot(batch, hash, data)
   481  		base.cache.Set(hash[:], data)
   482  		snapshotCleanAccountWriteMeter.Mark(int64(len(data)))
   483  
   484  		snapshotFlushAccountItemMeter.Mark(1)
   485  		snapshotFlushAccountSizeMeter.Mark(int64(len(data)))
   486  	}
   487  	// Push all the storage slots into the database
   488  	for accountHash, storage := range bottom.storageData {
   489  		// Skip any account not covered yet by the snapshot
   490  		if base.genMarker != nil && bytes.Compare(accountHash[:], base.genMarker) > 0 {
   491  			continue
   492  		}
   493  		// Generation might be mid-account, track that case too
   494  		midAccount := base.genMarker != nil && bytes.Equal(accountHash[:], base.genMarker[:common.HashLength])
   495  
   496  		for storageHash, data := range storage {
   497  			// Skip any slot not covered yet by the snapshot
   498  			if midAccount && bytes.Compare(storageHash[:], base.genMarker[common.HashLength:]) > 0 {
   499  				continue
   500  			}
   501  			if len(data) > 0 {
   502  				rawdb.WriteStorageSnapshot(batch, accountHash, storageHash, data)
   503  				base.cache.Set(append(accountHash[:], storageHash[:]...), data)
   504  				snapshotCleanStorageWriteMeter.Mark(int64(len(data)))
   505  			} else {
   506  				rawdb.DeleteStorageSnapshot(batch, accountHash, storageHash)
   507  				base.cache.Set(append(accountHash[:], storageHash[:]...), nil)
   508  			}
   509  			snapshotFlushStorageItemMeter.Mark(1)
   510  			snapshotFlushStorageSizeMeter.Mark(int64(len(data)))
   511  		}
   512  	}
   513  	// Update the snapshot block marker and write any remainder data
   514  	rawdb.WriteSnapshotRoot(batch, bottom.root)
   515  
   516  	// Write out the generator progress marker and report
   517  	journalProgress(batch, base.genMarker, stats)
   518  
   519  	// Flush all the updates in the single db operation. Ensure the
   520  	// disk layer transition is atomic.
   521  	if err := batch.Write(); err != nil {
   522  		log.Crit("Failed to write leftover snapshot", "err", err)
   523  	}
   524  	log.Debug("Journalled disk layer", "root", bottom.root, "complete", base.genMarker == nil)
   525  	res := &diskLayer{
   526  		root:       bottom.root,
   527  		cache:      base.cache,
   528  		diskdb:     base.diskdb,
   529  		triedb:     base.triedb,
   530  		genMarker:  base.genMarker,
   531  		genPending: base.genPending,
   532  	}
   533  	// If snapshot generation hasn't finished yet, port over all the starts and
   534  	// continue where the previous round left off.
   535  	//
   536  	// Note, the `base.genAbort` comparison is not used normally, it's checked
   537  	// to allow the tests to play with the marker without triggering this path.
   538  	if base.genMarker != nil && base.genAbort != nil {
   539  		res.genMarker = base.genMarker
   540  		res.genAbort = make(chan chan *generatorStats)
   541  		go res.generate(stats)
   542  	}
   543  	return res
   544  }
   545  
   546  // Journal commits an entire diff hierarchy to disk into a single journal entry.
   547  // This is meant to be used during shutdown to persist the snapshot without
   548  // flattening everything down (bad for reorgs).
   549  //
   550  // The method returns the root hash of the base layer that needs to be persisted
   551  // to disk as a trie too to allow continuing any pending generation op.
   552  func (t *Tree) Journal(root common.Hash) (common.Hash, error) {
   553  	// Retrieve the head snapshot to journal from var snap snapshot
   554  	snap := t.Snapshot(root)
   555  	if snap == nil {
   556  		return common.Hash{}, fmt.Errorf("snapshot [%#x] missing", root)
   557  	}
   558  	// Run the journaling
   559  	t.lock.Lock()
   560  	defer t.lock.Unlock()
   561  
   562  	// Firstly write out the metadata of journal
   563  	journal := new(bytes.Buffer)
   564  	if err := rlp.Encode(journal, journalVersion); err != nil {
   565  		return common.Hash{}, err
   566  	}
   567  	diskroot := t.diskRoot()
   568  	if diskroot == (common.Hash{}) {
   569  		return common.Hash{}, errors.New("invalid disk root")
   570  	}
   571  	// Secondly write out the disk layer root, ensure the
   572  	// diff journal is continuous with disk.
   573  	if err := rlp.Encode(journal, diskroot); err != nil {
   574  		return common.Hash{}, err
   575  	}
   576  	// Finally write out the journal of each layer in reverse order.
   577  	base, err := snap.(snapshot).Journal(journal)
   578  	if err != nil {
   579  		return common.Hash{}, err
   580  	}
   581  	// Store the journal into the database and return
   582  	rawdb.WriteSnapshotJournal(t.diskdb, journal.Bytes())
   583  	return base, nil
   584  }
   585  
   586  // LegacyJournal is basically identical to Journal. it's the legacy
   587  // version for flushing legacy journal. Now the only purpose of this
   588  // function is for testing.
   589  func (t *Tree) LegacyJournal(root common.Hash) (common.Hash, error) {
   590  	// Retrieve the head snapshot to journal from var snap snapshot
   591  	snap := t.Snapshot(root)
   592  	if snap == nil {
   593  		return common.Hash{}, fmt.Errorf("snapshot [%#x] missing", root)
   594  	}
   595  	// Run the journaling
   596  	t.lock.Lock()
   597  	defer t.lock.Unlock()
   598  
   599  	journal := new(bytes.Buffer)
   600  	base, err := snap.(snapshot).LegacyJournal(journal)
   601  	if err != nil {
   602  		return common.Hash{}, err
   603  	}
   604  	// Store the journal into the database and return
   605  	rawdb.WriteSnapshotJournal(t.diskdb, journal.Bytes())
   606  	return base, nil
   607  }
   608  
   609  // Rebuild wipes all available snapshot data from the persistent database and
   610  // discard all caches and diff layers. Afterwards, it starts a new snapshot
   611  // generator with the given root hash.
   612  func (t *Tree) Rebuild(root common.Hash) {
   613  	t.lock.Lock()
   614  	defer t.lock.Unlock()
   615  
   616  	// Firstly delete any recovery flag in the database. Because now we are
   617  	// building a brand new snapshot.
   618  	rawdb.DeleteSnapshotRecoveryNumber(t.diskdb)
   619  
   620  	// Track whether there's a wipe currently running and keep it alive if so
   621  	var wiper chan struct{}
   622  
   623  	// Iterate over and mark all layers stale
   624  	for _, layer := range t.layers {
   625  		switch layer := layer.(type) {
   626  		case *diskLayer:
   627  			// If the base layer is generating, abort it and save
   628  			if layer.genAbort != nil {
   629  				abort := make(chan *generatorStats)
   630  				layer.genAbort <- abort
   631  
   632  				if stats := <-abort; stats != nil {
   633  					wiper = stats.wiping
   634  				}
   635  			}
   636  			// Layer should be inactive now, mark it as stale
   637  			layer.lock.Lock()
   638  			layer.stale = true
   639  			layer.lock.Unlock()
   640  
   641  		case *diffLayer:
   642  			// If the layer is a simple diff, simply mark as stale
   643  			layer.lock.Lock()
   644  			atomic.StoreUint32(&layer.stale, 1)
   645  			layer.lock.Unlock()
   646  
   647  		default:
   648  			panic(fmt.Sprintf("unknown layer type: %T", layer))
   649  		}
   650  	}
   651  	// Start generating a new snapshot from scratch on a backgroung thread. The
   652  	// generator will run a wiper first if there's not one running right now.
   653  	log.Info("Rebuilding state snapshot")
   654  	t.layers = map[common.Hash]snapshot{
   655  		root: generateSnapshot(t.diskdb, t.triedb, t.cache, root, wiper),
   656  	}
   657  }
   658  
   659  // AccountIterator creates a new account iterator for the specified root hash and
   660  // seeks to a starting account hash.
   661  func (t *Tree) AccountIterator(root common.Hash, seek common.Hash) (AccountIterator, error) {
   662  	ok, err := t.generating()
   663  	if err != nil {
   664  		return nil, err
   665  	}
   666  	if ok {
   667  		return nil, ErrNotConstructed
   668  	}
   669  	return newFastAccountIterator(t, root, seek)
   670  }
   671  
   672  // StorageIterator creates a new storage iterator for the specified root hash and
   673  // account. The iterator will be move to the specific start position.
   674  func (t *Tree) StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) {
   675  	ok, err := t.generating()
   676  	if err != nil {
   677  		return nil, err
   678  	}
   679  	if ok {
   680  		return nil, ErrNotConstructed
   681  	}
   682  	return newFastStorageIterator(t, root, account, seek)
   683  }
   684  
   685  // disklayer is an internal helper function to return the disk layer.
   686  // The lock of snapTree is assumed to be held already.
   687  func (t *Tree) disklayer() *diskLayer {
   688  	var snap snapshot
   689  	for _, s := range t.layers {
   690  		snap = s
   691  		break
   692  	}
   693  	if snap == nil {
   694  		return nil
   695  	}
   696  	switch layer := snap.(type) {
   697  	case *diskLayer:
   698  		return layer
   699  	case *diffLayer:
   700  		return layer.origin
   701  	default:
   702  		panic(fmt.Sprintf("%T: undefined layer", snap))
   703  	}
   704  }
   705  
   706  // diskRoot is a internal helper function to return the disk layer root.
   707  // The lock of snapTree is assumed to be held already.
   708  func (t *Tree) diskRoot() common.Hash {
   709  	disklayer := t.disklayer()
   710  	if disklayer == nil {
   711  		return common.Hash{}
   712  	}
   713  	return disklayer.Root()
   714  }
   715  
   716  // generating is an internal helper function which reports whether the snapshot
   717  // is still under the construction.
   718  func (t *Tree) generating() (bool, error) {
   719  	t.lock.Lock()
   720  	defer t.lock.Unlock()
   721  
   722  	layer := t.disklayer()
   723  	if layer == nil {
   724  		return false, errors.New("disk layer is missing")
   725  	}
   726  	layer.lock.RLock()
   727  	defer layer.lock.RUnlock()
   728  	return layer.genMarker != nil, nil
   729  }
   730  
   731  // diskRoot is a external helper function to return the disk layer root.
   732  func (t *Tree) DiskRoot() common.Hash {
   733  	t.lock.Lock()
   734  	defer t.lock.Unlock()
   735  
   736  	return t.diskRoot()
   737  }