github.com/phillinzzz/newBsc@v1.1.6/core/state/snapshot/snapshot.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 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/phillinzzz/newBsc/common"
    28  	"github.com/phillinzzz/newBsc/core/rawdb"
    29  	"github.com/phillinzzz/newBsc/crypto"
    30  	"github.com/phillinzzz/newBsc/ethdb"
    31  	"github.com/phillinzzz/newBsc/log"
    32  	"github.com/phillinzzz/newBsc/metrics"
    33  	"github.com/phillinzzz/newBsc/rlp"
    34  	"github.com/phillinzzz/newBsc/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  
    64  	snapshotFlushAccountItemMeter = metrics.NewRegisteredMeter("state/snapshot/flush/account/item", nil)
    65  	snapshotFlushAccountSizeMeter = metrics.NewRegisteredMeter("state/snapshot/flush/account/size", nil)
    66  	snapshotFlushStorageItemMeter = metrics.NewRegisteredMeter("state/snapshot/flush/storage/item", nil)
    67  	snapshotFlushStorageSizeMeter = metrics.NewRegisteredMeter("state/snapshot/flush/storage/size", nil)
    68  
    69  	snapshotBloomIndexTimer = metrics.NewRegisteredResettingTimer("state/snapshot/bloom/index", nil)
    70  	snapshotBloomErrorGauge = metrics.NewRegisteredGaugeFloat64("state/snapshot/bloom/error", nil)
    71  
    72  	snapshotBloomAccountTrueHitMeter  = metrics.NewRegisteredMeter("state/snapshot/bloom/account/truehit", nil)
    73  	snapshotBloomAccountFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/account/falsehit", nil)
    74  	snapshotBloomAccountMissMeter     = metrics.NewRegisteredMeter("state/snapshot/bloom/account/miss", nil)
    75  
    76  	snapshotBloomStorageTrueHitMeter  = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/truehit", nil)
    77  	snapshotBloomStorageFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/falsehit", nil)
    78  	snapshotBloomStorageMissMeter     = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/miss", nil)
    79  
    80  	// ErrSnapshotStale is returned from data accessors if the underlying snapshot
    81  	// layer had been invalidated due to the chain progressing forward far enough
    82  	// to not maintain the layer's original state.
    83  	ErrSnapshotStale = errors.New("snapshot stale")
    84  
    85  	// ErrNotCoveredYet is returned from data accessors if the underlying snapshot
    86  	// is being generated currently and the requested data item is not yet in the
    87  	// range of accounts covered.
    88  	ErrNotCoveredYet = errors.New("not covered yet")
    89  
    90  	// ErrNotConstructed is returned if the callers want to iterate the snapshot
    91  	// while the generation is not finished yet.
    92  	ErrNotConstructed = errors.New("snapshot is not constructed")
    93  
    94  	// errSnapshotCycle is returned if a snapshot is attempted to be inserted
    95  	// that forms a cycle in the snapshot tree.
    96  	errSnapshotCycle = errors.New("snapshot cycle")
    97  )
    98  
    99  // Snapshot represents the functionality supported by a snapshot storage layer.
   100  type Snapshot interface {
   101  	// Root returns the root hash for which this snapshot was made.
   102  	Root() common.Hash
   103  
   104  	// Account directly retrieves the account associated with a particular hash in
   105  	// the snapshot slim data format.
   106  	Account(hash common.Hash) (*Account, error)
   107  
   108  	// AccountRLP directly retrieves the account RLP associated with a particular
   109  	// hash in the snapshot slim data format.
   110  	AccountRLP(hash common.Hash) ([]byte, error)
   111  
   112  	// Storage directly retrieves the storage data associated with a particular hash,
   113  	// within a particular account.
   114  	Storage(accountHash, storageHash common.Hash) ([]byte, error)
   115  }
   116  
   117  // snapshot is the internal version of the snapshot data layer that supports some
   118  // additional methods compared to the public API.
   119  type snapshot interface {
   120  	Snapshot
   121  
   122  	// Parent returns the subsequent layer of a snapshot, or nil if the base was
   123  	// reached.
   124  	//
   125  	// Note, the method is an internal helper to avoid type switching between the
   126  	// disk and diff layers. There is no locking involved.
   127  	Parent() snapshot
   128  
   129  	// Update creates a new layer on top of the existing snapshot diff tree with
   130  	// the specified data items.
   131  	//
   132  	// Note, the maps are retained by the method to avoid copying everything.
   133  	Update(blockRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer
   134  
   135  	// Journal commits an entire diff hierarchy to disk into a single journal entry.
   136  	// This is meant to be used during shutdown to persist the snapshot without
   137  	// flattening everything down (bad for reorgs).
   138  	Journal(buffer *bytes.Buffer) (common.Hash, error)
   139  
   140  	// Stale return whether this layer has become stale (was flattened across) or
   141  	// if it's still live.
   142  	Stale() bool
   143  
   144  	// AccountIterator creates an account iterator over an arbitrary layer.
   145  	AccountIterator(seek common.Hash) AccountIterator
   146  
   147  	// StorageIterator creates a storage iterator over an arbitrary layer.
   148  	StorageIterator(account common.Hash, seek common.Hash) (StorageIterator, bool)
   149  }
   150  
   151  // Tree is an Ethereum state snapshot tree. It consists of one persistent base
   152  // layer backed by a key-value store, on top of which arbitrarily many in-memory
   153  // diff layers are topped. The memory diffs can form a tree with branching, but
   154  // the disk layer is singleton and common to all. If a reorg goes deeper than the
   155  // disk layer, everything needs to be deleted.
   156  //
   157  // The goal of a state snapshot is twofold: to allow direct access to account and
   158  // storage data to avoid expensive multi-level trie lookups; and to allow sorted,
   159  // cheap iteration of the account/storage tries for sync aid.
   160  type Tree struct {
   161  	diskdb   ethdb.KeyValueStore      // Persistent database to store the snapshot
   162  	triedb   *trie.Database           // In-memory cache to access the trie through
   163  	cache    int                      // Megabytes permitted to use for read caches
   164  	layers   map[common.Hash]snapshot // Collection of all known layers
   165  	lock     sync.RWMutex
   166  	capLimit int
   167  }
   168  
   169  // New attempts to load an already existing snapshot from a persistent key-value
   170  // store (with a number of memory layers from a journal), ensuring that the head
   171  // of the snapshot matches the expected one.
   172  //
   173  // If the snapshot is missing or the disk layer is broken, the entire is deleted
   174  // and will be reconstructed from scratch based on the tries in the key-value
   175  // store, on a background thread. If the memory layers from the journal is not
   176  // continuous with disk layer or the journal is missing, all diffs will be discarded
   177  // iff it's in "recovery" mode, otherwise rebuild is mandatory.
   178  func New(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache, cap int, root common.Hash, async bool, rebuild bool, recovery bool) (*Tree, error) {
   179  	// Create a new, empty snapshot tree
   180  	snap := &Tree{
   181  		diskdb:   diskdb,
   182  		triedb:   triedb,
   183  		cache:    cache,
   184  		capLimit: cap,
   185  		layers:   make(map[common.Hash]snapshot),
   186  	}
   187  	if !async {
   188  		defer snap.waitBuild()
   189  	}
   190  	// Attempt to load a previously persisted snapshot and rebuild one if failed
   191  	head, disabled, err := loadSnapshot(diskdb, triedb, cache, root, recovery)
   192  	if disabled {
   193  		log.Warn("Snapshot maintenance disabled (syncing)")
   194  		return snap, nil
   195  	}
   196  	if err != nil {
   197  		if rebuild {
   198  			log.Warn("Failed to load snapshot, regenerating", "err", err)
   199  			snap.Rebuild(root)
   200  			return snap, nil
   201  		}
   202  		return nil, err // Bail out the error, don't rebuild automatically.
   203  	}
   204  	// Existing snapshot loaded, seed all the layers
   205  	for head != nil {
   206  		snap.layers[head.Root()] = head
   207  		head = head.Parent()
   208  	}
   209  	log.Info("Snapshot loaded", "diskRoot", snap.diskRoot(), "root", root)
   210  	return snap, nil
   211  }
   212  
   213  // waitBuild blocks until the snapshot finishes rebuilding. This method is meant
   214  // to be used by tests to ensure we're testing what we believe we are.
   215  func (t *Tree) waitBuild() {
   216  	// Find the rebuild termination channel
   217  	var done chan struct{}
   218  
   219  	t.lock.RLock()
   220  	for _, layer := range t.layers {
   221  		if layer, ok := layer.(*diskLayer); ok {
   222  			done = layer.genPending
   223  			break
   224  		}
   225  	}
   226  	t.lock.RUnlock()
   227  
   228  	// Wait until the snapshot is generated
   229  	if done != nil {
   230  		<-done
   231  	}
   232  }
   233  
   234  // Disable interrupts any pending snapshot generator, deletes all the snapshot
   235  // layers in memory and marks snapshots disabled globally. In order to resume
   236  // the snapshot functionality, the caller must invoke Rebuild.
   237  func (t *Tree) Disable() {
   238  	// Interrupt any live snapshot layers
   239  	t.lock.Lock()
   240  	defer t.lock.Unlock()
   241  
   242  	for _, layer := range t.layers {
   243  		switch layer := layer.(type) {
   244  		case *diskLayer:
   245  			// If the base layer is generating, abort it
   246  			if layer.genAbort != nil {
   247  				abort := make(chan *generatorStats)
   248  				layer.genAbort <- abort
   249  				<-abort
   250  			}
   251  			// Layer should be inactive now, mark it as stale
   252  			layer.lock.Lock()
   253  			layer.stale = true
   254  			layer.lock.Unlock()
   255  
   256  		case *diffLayer:
   257  			// If the layer is a simple diff, simply mark as stale
   258  			layer.lock.Lock()
   259  			atomic.StoreUint32(&layer.stale, 1)
   260  			layer.lock.Unlock()
   261  
   262  		default:
   263  			panic(fmt.Sprintf("unknown layer type: %T", layer))
   264  		}
   265  	}
   266  	t.layers = map[common.Hash]snapshot{}
   267  
   268  	// Delete all snapshot liveness information from the database
   269  	batch := t.diskdb.NewBatch()
   270  
   271  	rawdb.WriteSnapshotDisabled(batch)
   272  	rawdb.DeleteSnapshotRoot(batch)
   273  	rawdb.DeleteSnapshotJournal(batch)
   274  	rawdb.DeleteSnapshotGenerator(batch)
   275  	rawdb.DeleteSnapshotRecoveryNumber(batch)
   276  	// Note, we don't delete the sync progress
   277  
   278  	if err := batch.Write(); err != nil {
   279  		log.Crit("Failed to disable snapshots", "err", err)
   280  	}
   281  }
   282  
   283  // Snapshot retrieves a snapshot belonging to the given block root, or nil if no
   284  // snapshot is maintained for that block.
   285  func (t *Tree) Snapshot(blockRoot common.Hash) Snapshot {
   286  	t.lock.RLock()
   287  	defer t.lock.RUnlock()
   288  
   289  	return t.layers[blockRoot]
   290  }
   291  
   292  // Snapshots returns all visited layers from the topmost layer with specific
   293  // root and traverses downward. The layer amount is limited by the given number.
   294  // If nodisk is set, then disk layer is excluded.
   295  func (t *Tree) Snapshots(root common.Hash, limits int, nodisk bool) []Snapshot {
   296  	t.lock.RLock()
   297  	defer t.lock.RUnlock()
   298  
   299  	if limits == 0 {
   300  		return nil
   301  	}
   302  	layer := t.layers[root]
   303  	if layer == nil {
   304  		return nil
   305  	}
   306  	var ret []Snapshot
   307  	for {
   308  		if _, isdisk := layer.(*diskLayer); isdisk && nodisk {
   309  			break
   310  		}
   311  		ret = append(ret, layer)
   312  		limits -= 1
   313  		if limits == 0 {
   314  			break
   315  		}
   316  		parent := layer.Parent()
   317  		if parent == nil {
   318  			break
   319  		}
   320  		layer = parent
   321  	}
   322  	return ret
   323  }
   324  
   325  func (t *Tree) Update(blockRoot common.Hash, parentRoot common.Hash, destructs map[common.Address]struct{}, accounts map[common.Address][]byte, storage map[common.Address]map[string][]byte) error {
   326  	hashDestructs, hashAccounts, hashStorage := transformSnapData(destructs, accounts, storage)
   327  	return t.update(blockRoot, parentRoot, hashDestructs, hashAccounts, hashStorage)
   328  }
   329  
   330  // Update adds a new snapshot into the tree, if that can be linked to an existing
   331  // old parent. It is disallowed to insert a disk layer (the origin of all).
   332  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 {
   333  	// Reject noop updates to avoid self-loops in the snapshot tree. This is a
   334  	// special case that can only happen for Clique networks where empty blocks
   335  	// don't modify the state (0 block subsidy).
   336  	//
   337  	// Although we could silently ignore this internally, it should be the caller's
   338  	// responsibility to avoid even attempting to insert such a snapshot.
   339  	if blockRoot == parentRoot {
   340  		return errSnapshotCycle
   341  	}
   342  	// Generate a new snapshot on top of the parent
   343  	parent := t.Snapshot(parentRoot)
   344  	if parent == nil {
   345  		return fmt.Errorf("parent [%#x] snapshot missing", parentRoot)
   346  	}
   347  	snap := parent.(snapshot).Update(blockRoot, destructs, accounts, storage)
   348  
   349  	// Save the new snapshot for later
   350  	t.lock.Lock()
   351  	defer t.lock.Unlock()
   352  
   353  	t.layers[snap.root] = snap
   354  	log.Debug("Snapshot updated", "blockRoot", blockRoot)
   355  	return nil
   356  }
   357  
   358  func (t *Tree) CapLimit() int {
   359  	return t.capLimit
   360  }
   361  
   362  // Cap traverses downwards the snapshot tree from a head block hash until the
   363  // number of allowed layers are crossed. All layers beyond the permitted number
   364  // are flattened downwards.
   365  //
   366  // Note, the final diff layer count in general will be one more than the amount
   367  // requested. This happens because the bottom-most diff layer is the accumulator
   368  // which may or may not overflow and cascade to disk. Since this last layer's
   369  // survival is only known *after* capping, we need to omit it from the count if
   370  // we want to ensure that *at least* the requested number of diff layers remain.
   371  func (t *Tree) Cap(root common.Hash, layers int) error {
   372  	// Retrieve the head snapshot to cap from
   373  	snap := t.Snapshot(root)
   374  	if snap == nil {
   375  		return fmt.Errorf("snapshot [%#x] missing", root)
   376  	}
   377  	diff, ok := snap.(*diffLayer)
   378  	if !ok {
   379  		return fmt.Errorf("snapshot [%#x] is disk layer", root)
   380  	}
   381  	// If the generator is still running, use a more aggressive cap
   382  	diff.origin.lock.RLock()
   383  	if diff.origin.genMarker != nil && layers > 8 {
   384  		layers = 8
   385  	}
   386  	diff.origin.lock.RUnlock()
   387  
   388  	// Run the internal capping and discard all stale layers
   389  	t.lock.Lock()
   390  	defer t.lock.Unlock()
   391  
   392  	// Flattening the bottom-most diff layer requires special casing since there's
   393  	// no child to rewire to the grandparent. In that case we can fake a temporary
   394  	// child for the capping and then remove it.
   395  	if layers == 0 {
   396  		// If full commit was requested, flatten the diffs and merge onto disk
   397  		diff.lock.RLock()
   398  		base := diffToDisk(diff.flatten().(*diffLayer))
   399  		diff.lock.RUnlock()
   400  
   401  		// Replace the entire snapshot tree with the flat base
   402  		t.layers = map[common.Hash]snapshot{base.root: base}
   403  		return nil
   404  	}
   405  	persisted := t.cap(diff, layers)
   406  
   407  	// Remove any layer that is stale or links into a stale layer
   408  	children := make(map[common.Hash][]common.Hash)
   409  	for root, snap := range t.layers {
   410  		if diff, ok := snap.(*diffLayer); ok {
   411  			parent := diff.parent.Root()
   412  			children[parent] = append(children[parent], root)
   413  		}
   414  	}
   415  	var remove func(root common.Hash)
   416  	remove = func(root common.Hash) {
   417  		delete(t.layers, root)
   418  		for _, child := range children[root] {
   419  			remove(child)
   420  		}
   421  		delete(children, root)
   422  	}
   423  	for root, snap := range t.layers {
   424  		if snap.Stale() {
   425  			remove(root)
   426  		}
   427  	}
   428  	// If the disk layer was modified, regenerate all the cumulative blooms
   429  	if persisted != nil {
   430  		var rebloom func(root common.Hash)
   431  		rebloom = func(root common.Hash) {
   432  			if diff, ok := t.layers[root].(*diffLayer); ok {
   433  				diff.rebloom(persisted)
   434  			}
   435  			for _, child := range children[root] {
   436  				rebloom(child)
   437  			}
   438  		}
   439  		rebloom(persisted.root)
   440  	}
   441  	log.Debug("Snapshot capped", "root", root)
   442  	return nil
   443  }
   444  
   445  // cap traverses downwards the diff tree until the number of allowed layers are
   446  // crossed. All diffs beyond the permitted number are flattened downwards. If the
   447  // layer limit is reached, memory cap is also enforced (but not before).
   448  //
   449  // The method returns the new disk layer if diffs were persisted into it.
   450  //
   451  // Note, the final diff layer count in general will be one more than the amount
   452  // requested. This happens because the bottom-most diff layer is the accumulator
   453  // which may or may not overflow and cascade to disk. Since this last layer's
   454  // survival is only known *after* capping, we need to omit it from the count if
   455  // we want to ensure that *at least* the requested number of diff layers remain.
   456  func (t *Tree) cap(diff *diffLayer, layers int) *diskLayer {
   457  	// Dive until we run out of layers or reach the persistent database
   458  	for i := 0; i < layers-1; i++ {
   459  		// If we still have diff layers below, continue down
   460  		if parent, ok := diff.parent.(*diffLayer); ok {
   461  			diff = parent
   462  		} else {
   463  			// Diff stack too shallow, return without modifications
   464  			return nil
   465  		}
   466  	}
   467  	// We're out of layers, flatten anything below, stopping if it's the disk or if
   468  	// the memory limit is not yet exceeded.
   469  	switch parent := diff.parent.(type) {
   470  	case *diskLayer:
   471  		return nil
   472  
   473  	case *diffLayer:
   474  		// Flatten the parent into the grandparent. The flattening internally obtains a
   475  		// write lock on grandparent.
   476  		flattened := parent.flatten().(*diffLayer)
   477  		t.layers[flattened.root] = flattened
   478  
   479  		diff.lock.Lock()
   480  		defer diff.lock.Unlock()
   481  
   482  		diff.parent = flattened
   483  		if flattened.memory < aggregatorMemoryLimit {
   484  			// Accumulator layer is smaller than the limit, so we can abort, unless
   485  			// there's a snapshot being generated currently. In that case, the trie
   486  			// will move fron underneath the generator so we **must** merge all the
   487  			// partial data down into the snapshot and restart the generation.
   488  			if flattened.parent.(*diskLayer).genAbort == nil {
   489  				return nil
   490  			}
   491  		}
   492  	default:
   493  		panic(fmt.Sprintf("unknown data layer: %T", parent))
   494  	}
   495  	// If the bottom-most layer is larger than our memory cap, persist to disk
   496  	bottom := diff.parent.(*diffLayer)
   497  
   498  	bottom.lock.RLock()
   499  	base := diffToDisk(bottom)
   500  	bottom.lock.RUnlock()
   501  
   502  	t.layers[base.root] = base
   503  	diff.parent = base
   504  	return base
   505  }
   506  
   507  // diffToDisk merges a bottom-most diff into the persistent disk layer underneath
   508  // it. The method will panic if called onto a non-bottom-most diff layer.
   509  //
   510  // The disk layer persistence should be operated in an atomic way. All updates should
   511  // be discarded if the whole transition if not finished.
   512  func diffToDisk(bottom *diffLayer) *diskLayer {
   513  	var (
   514  		base  = bottom.parent.(*diskLayer)
   515  		batch = base.diskdb.NewBatch()
   516  		stats *generatorStats
   517  	)
   518  	// If the disk layer is running a snapshot generator, abort it
   519  	if base.genAbort != nil {
   520  		abort := make(chan *generatorStats)
   521  		base.genAbort <- abort
   522  		stats = <-abort
   523  	}
   524  	// Put the deletion in the batch writer, flush all updates in the final step.
   525  	rawdb.DeleteSnapshotRoot(batch)
   526  
   527  	// Mark the original base as stale as we're going to create a new wrapper
   528  	base.lock.Lock()
   529  	if base.stale {
   530  		panic("parent disk layer is stale") // we've committed into the same base from two children, boo
   531  	}
   532  	base.stale = true
   533  	base.lock.Unlock()
   534  
   535  	// Destroy all the destructed accounts from the database
   536  	for hash := range bottom.destructSet {
   537  		// Skip any account not covered yet by the snapshot
   538  		if base.genMarker != nil && bytes.Compare(hash[:], base.genMarker) > 0 {
   539  			continue
   540  		}
   541  		// Remove all storage slots
   542  		rawdb.DeleteAccountSnapshot(batch, hash)
   543  		base.cache.Set(hash[:], nil)
   544  
   545  		it := rawdb.IterateStorageSnapshots(base.diskdb, hash)
   546  		for it.Next() {
   547  			if key := it.Key(); len(key) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator
   548  				batch.Delete(key)
   549  				base.cache.Del(key[1:])
   550  				snapshotFlushStorageItemMeter.Mark(1)
   551  
   552  				// Ensure we don't delete too much data blindly (contract can be
   553  				// huge). It's ok to flush, the root will go missing in case of a
   554  				// crash and we'll detect and regenerate the snapshot.
   555  				if batch.ValueSize() > ethdb.IdealBatchSize {
   556  					if err := batch.Write(); err != nil {
   557  						log.Crit("Failed to write storage deletions", "err", err)
   558  					}
   559  					batch.Reset()
   560  				}
   561  			}
   562  		}
   563  		it.Release()
   564  	}
   565  	// Push all updated accounts into the database
   566  	for hash, data := range bottom.accountData {
   567  		// Skip any account not covered yet by the snapshot
   568  		if base.genMarker != nil && bytes.Compare(hash[:], base.genMarker) > 0 {
   569  			continue
   570  		}
   571  		// Push the account to disk
   572  		rawdb.WriteAccountSnapshot(batch, hash, data)
   573  		base.cache.Set(hash[:], data)
   574  		snapshotCleanAccountWriteMeter.Mark(int64(len(data)))
   575  
   576  		snapshotFlushAccountItemMeter.Mark(1)
   577  		snapshotFlushAccountSizeMeter.Mark(int64(len(data)))
   578  
   579  		// Ensure we don't write too much data blindly. It's ok to flush, the
   580  		// root will go missing in case of a crash and we'll detect and regen
   581  		// the snapshot.
   582  		if batch.ValueSize() > ethdb.IdealBatchSize {
   583  			if err := batch.Write(); err != nil {
   584  				log.Crit("Failed to write storage deletions", "err", err)
   585  			}
   586  			batch.Reset()
   587  		}
   588  	}
   589  	// Push all the storage slots into the database
   590  	for accountHash, storage := range bottom.storageData {
   591  		// Skip any account not covered yet by the snapshot
   592  		if base.genMarker != nil && bytes.Compare(accountHash[:], base.genMarker) > 0 {
   593  			continue
   594  		}
   595  		// Generation might be mid-account, track that case too
   596  		midAccount := base.genMarker != nil && bytes.Equal(accountHash[:], base.genMarker[:common.HashLength])
   597  
   598  		for storageHash, data := range storage {
   599  			// Skip any slot not covered yet by the snapshot
   600  			if midAccount && bytes.Compare(storageHash[:], base.genMarker[common.HashLength:]) > 0 {
   601  				continue
   602  			}
   603  			if len(data) > 0 {
   604  				rawdb.WriteStorageSnapshot(batch, accountHash, storageHash, data)
   605  				base.cache.Set(append(accountHash[:], storageHash[:]...), data)
   606  				snapshotCleanStorageWriteMeter.Mark(int64(len(data)))
   607  			} else {
   608  				rawdb.DeleteStorageSnapshot(batch, accountHash, storageHash)
   609  				base.cache.Set(append(accountHash[:], storageHash[:]...), nil)
   610  			}
   611  			snapshotFlushStorageItemMeter.Mark(1)
   612  			snapshotFlushStorageSizeMeter.Mark(int64(len(data)))
   613  		}
   614  	}
   615  	// Update the snapshot block marker and write any remainder data
   616  	rawdb.WriteSnapshotRoot(batch, bottom.root)
   617  
   618  	// Write out the generator progress marker and report
   619  	journalProgress(batch, base.genMarker, stats)
   620  
   621  	// Flush all the updates in the single db operation. Ensure the
   622  	// disk layer transition is atomic.
   623  	if err := batch.Write(); err != nil {
   624  		log.Crit("Failed to write leftover snapshot", "err", err)
   625  	}
   626  	log.Debug("Journalled disk layer", "root", bottom.root, "complete", base.genMarker == nil)
   627  	res := &diskLayer{
   628  		root:       bottom.root,
   629  		cache:      base.cache,
   630  		diskdb:     base.diskdb,
   631  		triedb:     base.triedb,
   632  		genMarker:  base.genMarker,
   633  		genPending: base.genPending,
   634  	}
   635  	// If snapshot generation hasn't finished yet, port over all the starts and
   636  	// continue where the previous round left off.
   637  	//
   638  	// Note, the `base.genAbort` comparison is not used normally, it's checked
   639  	// to allow the tests to play with the marker without triggering this path.
   640  	if base.genMarker != nil && base.genAbort != nil {
   641  		res.genMarker = base.genMarker
   642  		res.genAbort = make(chan chan *generatorStats)
   643  		go res.generate(stats)
   644  	}
   645  	return res
   646  }
   647  
   648  // Journal commits an entire diff hierarchy to disk into a single journal entry.
   649  // This is meant to be used during shutdown to persist the snapshot without
   650  // flattening everything down (bad for reorgs).
   651  //
   652  // The method returns the root hash of the base layer that needs to be persisted
   653  // to disk as a trie too to allow continuing any pending generation op.
   654  func (t *Tree) Journal(root common.Hash) (common.Hash, error) {
   655  	// Retrieve the head snapshot to journal from var snap snapshot
   656  	snap := t.Snapshot(root)
   657  	if snap == nil {
   658  		return common.Hash{}, fmt.Errorf("snapshot [%#x] missing", root)
   659  	}
   660  	// Run the journaling
   661  	t.lock.Lock()
   662  	defer t.lock.Unlock()
   663  
   664  	// Firstly write out the metadata of journal
   665  	journal := new(bytes.Buffer)
   666  	if err := rlp.Encode(journal, journalVersion); err != nil {
   667  		return common.Hash{}, err
   668  	}
   669  	diskroot := t.diskRoot()
   670  	if diskroot == (common.Hash{}) {
   671  		return common.Hash{}, errors.New("invalid disk root")
   672  	}
   673  	// Secondly write out the disk layer root, ensure the
   674  	// diff journal is continuous with disk.
   675  	if err := rlp.Encode(journal, diskroot); err != nil {
   676  		return common.Hash{}, err
   677  	}
   678  	// Finally write out the journal of each layer in reverse order.
   679  	base, err := snap.(snapshot).Journal(journal)
   680  	if err != nil {
   681  		return common.Hash{}, err
   682  	}
   683  	// Store the journal into the database and return
   684  	rawdb.WriteSnapshotJournal(t.diskdb, journal.Bytes())
   685  	return base, nil
   686  }
   687  
   688  // Rebuild wipes all available snapshot data from the persistent database and
   689  // discard all caches and diff layers. Afterwards, it starts a new snapshot
   690  // generator with the given root hash.
   691  func (t *Tree) Rebuild(root common.Hash) {
   692  	t.lock.Lock()
   693  	defer t.lock.Unlock()
   694  
   695  	// Firstly delete any recovery flag in the database. Because now we are
   696  	// building a brand new snapshot. Also reenable the snapshot feature.
   697  	rawdb.DeleteSnapshotRecoveryNumber(t.diskdb)
   698  	rawdb.DeleteSnapshotDisabled(t.diskdb)
   699  
   700  	// Iterate over and mark all layers stale
   701  	for _, layer := range t.layers {
   702  		switch layer := layer.(type) {
   703  		case *diskLayer:
   704  			// If the base layer is generating, abort it and save
   705  			if layer.genAbort != nil {
   706  				abort := make(chan *generatorStats)
   707  				layer.genAbort <- abort
   708  				<-abort
   709  			}
   710  			// Layer should be inactive now, mark it as stale
   711  			layer.lock.Lock()
   712  			layer.stale = true
   713  			layer.lock.Unlock()
   714  
   715  		case *diffLayer:
   716  			// If the layer is a simple diff, simply mark as stale
   717  			layer.lock.Lock()
   718  			atomic.StoreUint32(&layer.stale, 1)
   719  			layer.lock.Unlock()
   720  
   721  		default:
   722  			panic(fmt.Sprintf("unknown layer type: %T", layer))
   723  		}
   724  	}
   725  	// Start generating a new snapshot from scratch on a background thread. The
   726  	// generator will run a wiper first if there's not one running right now.
   727  	log.Info("Rebuilding state snapshot")
   728  	t.layers = map[common.Hash]snapshot{
   729  		root: generateSnapshot(t.diskdb, t.triedb, t.cache, root),
   730  	}
   731  }
   732  
   733  // AccountIterator creates a new account iterator for the specified root hash and
   734  // seeks to a starting account hash.
   735  func (t *Tree) AccountIterator(root common.Hash, seek common.Hash) (AccountIterator, error) {
   736  	ok, err := t.generating()
   737  	if err != nil {
   738  		return nil, err
   739  	}
   740  	if ok {
   741  		return nil, ErrNotConstructed
   742  	}
   743  	return newFastAccountIterator(t, root, seek)
   744  }
   745  
   746  // StorageIterator creates a new storage iterator for the specified root hash and
   747  // account. The iterator will be move to the specific start position.
   748  func (t *Tree) StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) {
   749  	ok, err := t.generating()
   750  	if err != nil {
   751  		return nil, err
   752  	}
   753  	if ok {
   754  		return nil, ErrNotConstructed
   755  	}
   756  	return newFastStorageIterator(t, root, account, seek)
   757  }
   758  
   759  // Verify iterates the whole state(all the accounts as well as the corresponding storages)
   760  // with the specific root and compares the re-computed hash with the original one.
   761  func (t *Tree) Verify(root common.Hash) error {
   762  	acctIt, err := t.AccountIterator(root, common.Hash{})
   763  	if err != nil {
   764  		return err
   765  	}
   766  	defer acctIt.Release()
   767  
   768  	got, err := generateTrieRoot(nil, acctIt, common.Hash{}, stackTrieGenerate, func(db ethdb.KeyValueWriter, accountHash, codeHash common.Hash, stat *generateStats) (common.Hash, error) {
   769  		storageIt, err := t.StorageIterator(root, accountHash, common.Hash{})
   770  		if err != nil {
   771  			return common.Hash{}, err
   772  		}
   773  		defer storageIt.Release()
   774  
   775  		hash, err := generateTrieRoot(nil, storageIt, accountHash, stackTrieGenerate, nil, stat, false)
   776  		if err != nil {
   777  			return common.Hash{}, err
   778  		}
   779  		return hash, nil
   780  	}, newGenerateStats(), true)
   781  
   782  	if err != nil {
   783  		return err
   784  	}
   785  	if got != root {
   786  		return fmt.Errorf("state root hash mismatch: got %x, want %x", got, root)
   787  	}
   788  	return nil
   789  }
   790  
   791  // disklayer is an internal helper function to return the disk layer.
   792  // The lock of snapTree is assumed to be held already.
   793  func (t *Tree) disklayer() *diskLayer {
   794  	var snap snapshot
   795  	for _, s := range t.layers {
   796  		snap = s
   797  		break
   798  	}
   799  	if snap == nil {
   800  		return nil
   801  	}
   802  	switch layer := snap.(type) {
   803  	case *diskLayer:
   804  		return layer
   805  	case *diffLayer:
   806  		return layer.origin
   807  	default:
   808  		panic(fmt.Sprintf("%T: undefined layer", snap))
   809  	}
   810  }
   811  
   812  // diskRoot is a internal helper function to return the disk layer root.
   813  // The lock of snapTree is assumed to be held already.
   814  func (t *Tree) diskRoot() common.Hash {
   815  	disklayer := t.disklayer()
   816  	if disklayer == nil {
   817  		return common.Hash{}
   818  	}
   819  	return disklayer.Root()
   820  }
   821  
   822  // generating is an internal helper function which reports whether the snapshot
   823  // is still under the construction.
   824  func (t *Tree) generating() (bool, error) {
   825  	t.lock.Lock()
   826  	defer t.lock.Unlock()
   827  
   828  	layer := t.disklayer()
   829  	if layer == nil {
   830  		return false, errors.New("disk layer is missing")
   831  	}
   832  	layer.lock.RLock()
   833  	defer layer.lock.RUnlock()
   834  	return layer.genMarker != nil, nil
   835  }
   836  
   837  // diskRoot is a external helper function to return the disk layer root.
   838  func (t *Tree) DiskRoot() common.Hash {
   839  	t.lock.Lock()
   840  	defer t.lock.Unlock()
   841  
   842  	return t.diskRoot()
   843  }
   844  
   845  // TODO we can further improve it when the set is very large
   846  func transformSnapData(destructs map[common.Address]struct{}, accounts map[common.Address][]byte,
   847  	storage map[common.Address]map[string][]byte) (map[common.Hash]struct{}, map[common.Hash][]byte,
   848  	map[common.Hash]map[common.Hash][]byte) {
   849  	hasher := crypto.NewKeccakState()
   850  	hashDestructs := make(map[common.Hash]struct{}, len(destructs))
   851  	hashAccounts := make(map[common.Hash][]byte, len(accounts))
   852  	hashStorages := make(map[common.Hash]map[common.Hash][]byte, len(storage))
   853  	for addr := range destructs {
   854  		hashDestructs[crypto.Keccak256Hash(addr[:])] = struct{}{}
   855  	}
   856  	for addr, account := range accounts {
   857  		hashAccounts[crypto.Keccak256Hash(addr[:])] = account
   858  	}
   859  	for addr, accountStore := range storage {
   860  		hashStorage := make(map[common.Hash][]byte, len(accountStore))
   861  		for k, v := range accountStore {
   862  			hashStorage[crypto.HashData(hasher, []byte(k))] = v
   863  		}
   864  		hashStorages[crypto.Keccak256Hash(addr[:])] = hashStorage
   865  	}
   866  	return hashDestructs, hashAccounts, hashStorages
   867  }