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