github.com/theQRL/go-zond@v0.1.1/core/state/snapshot/disklayer.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package snapshot
    18  
    19  import (
    20  	"bytes"
    21  	"sync"
    22  
    23  	"github.com/VictoriaMetrics/fastcache"
    24  	"github.com/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/core/rawdb"
    26  	"github.com/theQRL/go-zond/core/types"
    27  	"github.com/theQRL/go-zond/rlp"
    28  	"github.com/theQRL/go-zond/trie"
    29  	"github.com/theQRL/go-zond/zonddb"
    30  )
    31  
    32  // diskLayer is a low level persistent snapshot built on top of a key-value store.
    33  type diskLayer struct {
    34  	diskdb zonddb.KeyValueStore // Key-value store containing the base snapshot
    35  	triedb *trie.Database      // Trie node cache for reconstruction purposes
    36  	cache  *fastcache.Cache    // Cache to avoid hitting the disk for direct access
    37  
    38  	root  common.Hash // Root hash of the base snapshot
    39  	stale bool        // Signals that the layer became stale (state progressed)
    40  
    41  	genMarker  []byte                    // Marker for the state that's indexed during initial layer generation
    42  	genPending chan struct{}             // Notification channel when generation is done (test synchronicity)
    43  	genAbort   chan chan *generatorStats // Notification channel to abort generating the snapshot in this layer
    44  
    45  	lock sync.RWMutex
    46  }
    47  
    48  // Root returns  root hash for which this snapshot was made.
    49  func (dl *diskLayer) Root() common.Hash {
    50  	return dl.root
    51  }
    52  
    53  // Parent always returns nil as there's no layer below the disk.
    54  func (dl *diskLayer) Parent() snapshot {
    55  	return nil
    56  }
    57  
    58  // Stale return whether this layer has become stale (was flattened across) or if
    59  // it's still live.
    60  func (dl *diskLayer) Stale() bool {
    61  	dl.lock.RLock()
    62  	defer dl.lock.RUnlock()
    63  
    64  	return dl.stale
    65  }
    66  
    67  // Account directly retrieves the account associated with a particular hash in
    68  // the snapshot slim data format.
    69  func (dl *diskLayer) Account(hash common.Hash) (*types.SlimAccount, error) {
    70  	data, err := dl.AccountRLP(hash)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	if len(data) == 0 { // can be both nil and []byte{}
    75  		return nil, nil
    76  	}
    77  	account := new(types.SlimAccount)
    78  	if err := rlp.DecodeBytes(data, account); err != nil {
    79  		panic(err)
    80  	}
    81  	return account, nil
    82  }
    83  
    84  // AccountRLP directly retrieves the account RLP associated with a particular
    85  // hash in the snapshot slim data format.
    86  func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
    87  	dl.lock.RLock()
    88  	defer dl.lock.RUnlock()
    89  
    90  	// If the layer was flattened into, consider it invalid (any live reference to
    91  	// the original should be marked as unusable).
    92  	if dl.stale {
    93  		return nil, ErrSnapshotStale
    94  	}
    95  	// If the layer is being generated, ensure the requested hash has already been
    96  	// covered by the generator.
    97  	if dl.genMarker != nil && bytes.Compare(hash[:], dl.genMarker) > 0 {
    98  		return nil, ErrNotCoveredYet
    99  	}
   100  	// If we're in the disk layer, all diff layers missed
   101  	snapshotDirtyAccountMissMeter.Mark(1)
   102  
   103  	// Try to retrieve the account from the memory cache
   104  	if blob, found := dl.cache.HasGet(nil, hash[:]); found {
   105  		snapshotCleanAccountHitMeter.Mark(1)
   106  		snapshotCleanAccountReadMeter.Mark(int64(len(blob)))
   107  		return blob, nil
   108  	}
   109  	// Cache doesn't contain account, pull from disk and cache for later
   110  	blob := rawdb.ReadAccountSnapshot(dl.diskdb, hash)
   111  	dl.cache.Set(hash[:], blob)
   112  
   113  	snapshotCleanAccountMissMeter.Mark(1)
   114  	if n := len(blob); n > 0 {
   115  		snapshotCleanAccountWriteMeter.Mark(int64(n))
   116  	} else {
   117  		snapshotCleanAccountInexMeter.Mark(1)
   118  	}
   119  	return blob, nil
   120  }
   121  
   122  // Storage directly retrieves the storage data associated with a particular hash,
   123  // within a particular account.
   124  func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
   125  	dl.lock.RLock()
   126  	defer dl.lock.RUnlock()
   127  
   128  	// If the layer was flattened into, consider it invalid (any live reference to
   129  	// the original should be marked as unusable).
   130  	if dl.stale {
   131  		return nil, ErrSnapshotStale
   132  	}
   133  	key := append(accountHash[:], storageHash[:]...)
   134  
   135  	// If the layer is being generated, ensure the requested hash has already been
   136  	// covered by the generator.
   137  	if dl.genMarker != nil && bytes.Compare(key, dl.genMarker) > 0 {
   138  		return nil, ErrNotCoveredYet
   139  	}
   140  	// If we're in the disk layer, all diff layers missed
   141  	snapshotDirtyStorageMissMeter.Mark(1)
   142  
   143  	// Try to retrieve the storage slot from the memory cache
   144  	if blob, found := dl.cache.HasGet(nil, key); found {
   145  		snapshotCleanStorageHitMeter.Mark(1)
   146  		snapshotCleanStorageReadMeter.Mark(int64(len(blob)))
   147  		return blob, nil
   148  	}
   149  	// Cache doesn't contain storage slot, pull from disk and cache for later
   150  	blob := rawdb.ReadStorageSnapshot(dl.diskdb, accountHash, storageHash)
   151  	dl.cache.Set(key, blob)
   152  
   153  	snapshotCleanStorageMissMeter.Mark(1)
   154  	if n := len(blob); n > 0 {
   155  		snapshotCleanStorageWriteMeter.Mark(int64(n))
   156  	} else {
   157  		snapshotCleanStorageInexMeter.Mark(1)
   158  	}
   159  	return blob, nil
   160  }
   161  
   162  // Update creates a new layer on top of the existing snapshot diff tree with
   163  // the specified data items. Note, the maps are retained by the method to avoid
   164  // copying everything.
   165  func (dl *diskLayer) Update(blockHash common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
   166  	return newDiffLayer(dl, blockHash, destructs, accounts, storage)
   167  }