github.com/CommerciumBlockchain/go-commercium@v0.0.0-20220709212705-b46438a77516/core/state/snapshot/generate.go (about)

     1  // Copyright 2019 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package snapshot
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"fmt"
    23  	"math/big"
    24  	"time"
    25  
    26  	"github.com/VictoriaMetrics/fastcache"
    27  	"github.com/CommerciumBlockchain/go-commercium/common"
    28  	"github.com/CommerciumBlockchain/go-commercium/common/math"
    29  	"github.com/CommerciumBlockchain/go-commercium/core/rawdb"
    30  	"github.com/CommerciumBlockchain/go-commercium/crypto"
    31  	"github.com/CommerciumBlockchain/go-commercium/ethdb"
    32  	"github.com/CommerciumBlockchain/go-commercium/log"
    33  	"github.com/CommerciumBlockchain/go-commercium/rlp"
    34  	"github.com/CommerciumBlockchain/go-commercium/trie"
    35  )
    36  
    37  var (
    38  	// emptyRoot is the known root hash of an empty trie.
    39  	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    40  
    41  	// emptyCode is the known hash of the empty EVM bytecode.
    42  	emptyCode = crypto.Keccak256Hash(nil)
    43  )
    44  
    45  // generatorStats is a collection of statistics gathered by the snapshot generator
    46  // for logging purposes.
    47  type generatorStats struct {
    48  	wiping   chan struct{}      // Notification channel if wiping is in progress
    49  	origin   uint64             // Origin prefix where generation started
    50  	start    time.Time          // Timestamp when generation started
    51  	accounts uint64             // Number of accounts indexed
    52  	slots    uint64             // Number of storage slots indexed
    53  	storage  common.StorageSize // Account and storage slot size
    54  }
    55  
    56  // Log creates an contextual log with the given message and the context pulled
    57  // from the internally maintained statistics.
    58  func (gs *generatorStats) Log(msg string, root common.Hash, marker []byte) {
    59  	var ctx []interface{}
    60  	if root != (common.Hash{}) {
    61  		ctx = append(ctx, []interface{}{"root", root}...)
    62  	}
    63  	// Figure out whether we're after or within an account
    64  	switch len(marker) {
    65  	case common.HashLength:
    66  		ctx = append(ctx, []interface{}{"at", common.BytesToHash(marker)}...)
    67  	case 2 * common.HashLength:
    68  		ctx = append(ctx, []interface{}{
    69  			"in", common.BytesToHash(marker[:common.HashLength]),
    70  			"at", common.BytesToHash(marker[common.HashLength:]),
    71  		}...)
    72  	}
    73  	// Add the usual measurements
    74  	ctx = append(ctx, []interface{}{
    75  		"accounts", gs.accounts,
    76  		"slots", gs.slots,
    77  		"storage", gs.storage,
    78  		"elapsed", common.PrettyDuration(time.Since(gs.start)),
    79  	}...)
    80  	// Calculate the estimated indexing time based on current stats
    81  	if len(marker) > 0 {
    82  		if done := binary.BigEndian.Uint64(marker[:8]) - gs.origin; done > 0 {
    83  			left := math.MaxUint64 - binary.BigEndian.Uint64(marker[:8])
    84  
    85  			speed := done/uint64(time.Since(gs.start)/time.Millisecond+1) + 1 // +1s to avoid division by zero
    86  			ctx = append(ctx, []interface{}{
    87  				"eta", common.PrettyDuration(time.Duration(left/speed) * time.Millisecond),
    88  			}...)
    89  		}
    90  	}
    91  	log.Info(msg, ctx...)
    92  }
    93  
    94  // generateSnapshot regenerates a brand new snapshot based on an existing state
    95  // database and head block asynchronously. The snapshot is returned immediately
    96  // and generation is continued in the background until done.
    97  func generateSnapshot(diskdb ethdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, wiper chan struct{}) *diskLayer {
    98  	// Wipe any previously existing snapshot from the database if no wiper is
    99  	// currently in progress.
   100  	if wiper == nil {
   101  		wiper = wipeSnapshot(diskdb, true)
   102  	}
   103  	// Create a new disk layer with an initialized state marker at zero
   104  	rawdb.WriteSnapshotRoot(diskdb, root)
   105  
   106  	base := &diskLayer{
   107  		diskdb:     diskdb,
   108  		triedb:     triedb,
   109  		root:       root,
   110  		cache:      fastcache.New(cache * 1024 * 1024),
   111  		genMarker:  []byte{}, // Initialized but empty!
   112  		genPending: make(chan struct{}),
   113  		genAbort:   make(chan chan *generatorStats),
   114  	}
   115  	go base.generate(&generatorStats{wiping: wiper, start: time.Now()})
   116  	log.Debug("Start snapshot generation", "root", root)
   117  	return base
   118  }
   119  
   120  // journalProgress persists the generator stats into the database to resume later.
   121  func journalProgress(db ethdb.KeyValueWriter, marker []byte, stats *generatorStats) {
   122  	// Write out the generator marker. Note it's a standalone disk layer generator
   123  	// which is not mixed with journal. It's ok if the generator is persisted while
   124  	// journal is not.
   125  	entry := journalGenerator{
   126  		Done:   marker == nil,
   127  		Marker: marker,
   128  	}
   129  	if stats != nil {
   130  		entry.Wiping = (stats.wiping != nil)
   131  		entry.Accounts = stats.accounts
   132  		entry.Slots = stats.slots
   133  		entry.Storage = uint64(stats.storage)
   134  	}
   135  	blob, err := rlp.EncodeToBytes(entry)
   136  	if err != nil {
   137  		panic(err) // Cannot happen, here to catch dev errors
   138  	}
   139  	var logstr string
   140  	switch len(marker) {
   141  	case 0:
   142  		logstr = "done"
   143  	case common.HashLength:
   144  		logstr = fmt.Sprintf("%#x", marker)
   145  	default:
   146  		logstr = fmt.Sprintf("%#x:%#x", marker[:common.HashLength], marker[common.HashLength:])
   147  	}
   148  	log.Debug("Journalled generator progress", "progress", logstr)
   149  	rawdb.WriteSnapshotGenerator(db, blob)
   150  }
   151  
   152  // generate is a background thread that iterates over the state and storage tries,
   153  // constructing the state snapshot. All the arguments are purely for statistics
   154  // gethering and logging, since the method surfs the blocks as they arrive, often
   155  // being restarted.
   156  func (dl *diskLayer) generate(stats *generatorStats) {
   157  	// If a database wipe is in operation, wait until it's done
   158  	if stats.wiping != nil {
   159  		stats.Log("Wiper running, state snapshotting paused", common.Hash{}, dl.genMarker)
   160  		select {
   161  		// If wiper is done, resume normal mode of operation
   162  		case <-stats.wiping:
   163  			stats.wiping = nil
   164  			stats.start = time.Now()
   165  
   166  		// If generator was aborted during wipe, return
   167  		case abort := <-dl.genAbort:
   168  			abort <- stats
   169  			return
   170  		}
   171  	}
   172  	// Create an account and state iterator pointing to the current generator marker
   173  	accTrie, err := trie.NewSecure(dl.root, dl.triedb)
   174  	if err != nil {
   175  		// The account trie is missing (GC), surf the chain until one becomes available
   176  		stats.Log("Trie missing, state snapshotting paused", dl.root, dl.genMarker)
   177  
   178  		abort := <-dl.genAbort
   179  		abort <- stats
   180  		return
   181  	}
   182  	stats.Log("Resuming state snapshot generation", dl.root, dl.genMarker)
   183  
   184  	var accMarker []byte
   185  	if len(dl.genMarker) > 0 { // []byte{} is the start, use nil for that
   186  		accMarker = dl.genMarker[:common.HashLength]
   187  	}
   188  	accIt := trie.NewIterator(accTrie.NodeIterator(accMarker))
   189  	batch := dl.diskdb.NewBatch()
   190  
   191  	// Iterate from the previous marker and continue generating the state snapshot
   192  	logged := time.Now()
   193  	for accIt.Next() {
   194  		// Retrieve the current account and flatten it into the internal format
   195  		accountHash := common.BytesToHash(accIt.Key)
   196  
   197  		var acc struct {
   198  			Nonce    uint64
   199  			Balance  *big.Int
   200  			Root     common.Hash
   201  			CodeHash []byte
   202  		}
   203  		if err := rlp.DecodeBytes(accIt.Value, &acc); err != nil {
   204  			log.Crit("Invalid account encountered during snapshot creation", "err", err)
   205  		}
   206  		data := SlimAccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash)
   207  
   208  		// If the account is not yet in-progress, write it out
   209  		if accMarker == nil || !bytes.Equal(accountHash[:], accMarker) {
   210  			rawdb.WriteAccountSnapshot(batch, accountHash, data)
   211  			stats.storage += common.StorageSize(1 + common.HashLength + len(data))
   212  			stats.accounts++
   213  		}
   214  		// If we've exceeded our batch allowance or termination was requested, flush to disk
   215  		var abort chan *generatorStats
   216  		select {
   217  		case abort = <-dl.genAbort:
   218  		default:
   219  		}
   220  		if batch.ValueSize() > ethdb.IdealBatchSize || abort != nil {
   221  			// Only write and set the marker if we actually did something useful
   222  			if batch.ValueSize() > 0 {
   223  				// Ensure the generator entry is in sync with the data
   224  				marker := accountHash[:]
   225  				journalProgress(batch, marker, stats)
   226  
   227  				batch.Write()
   228  				batch.Reset()
   229  
   230  				dl.lock.Lock()
   231  				dl.genMarker = marker
   232  				dl.lock.Unlock()
   233  			}
   234  			if abort != nil {
   235  				stats.Log("Aborting state snapshot generation", dl.root, accountHash[:])
   236  				abort <- stats
   237  				return
   238  			}
   239  		}
   240  		// If the account is in-progress, continue where we left off (otherwise iterate all)
   241  		if acc.Root != emptyRoot {
   242  			storeTrie, err := trie.NewSecure(acc.Root, dl.triedb)
   243  			if err != nil {
   244  				log.Error("Generator failed to access storage trie", "root", dl.root, "account", accountHash, "stroot", acc.Root, "err", err)
   245  				abort := <-dl.genAbort
   246  				abort <- stats
   247  				return
   248  			}
   249  			var storeMarker []byte
   250  			if accMarker != nil && bytes.Equal(accountHash[:], accMarker) && len(dl.genMarker) > common.HashLength {
   251  				storeMarker = dl.genMarker[common.HashLength:]
   252  			}
   253  			storeIt := trie.NewIterator(storeTrie.NodeIterator(storeMarker))
   254  			for storeIt.Next() {
   255  				rawdb.WriteStorageSnapshot(batch, accountHash, common.BytesToHash(storeIt.Key), storeIt.Value)
   256  				stats.storage += common.StorageSize(1 + 2*common.HashLength + len(storeIt.Value))
   257  				stats.slots++
   258  
   259  				// If we've exceeded our batch allowance or termination was requested, flush to disk
   260  				var abort chan *generatorStats
   261  				select {
   262  				case abort = <-dl.genAbort:
   263  				default:
   264  				}
   265  				if batch.ValueSize() > ethdb.IdealBatchSize || abort != nil {
   266  					// Only write and set the marker if we actually did something useful
   267  					if batch.ValueSize() > 0 {
   268  						// Ensure the generator entry is in sync with the data
   269  						marker := append(accountHash[:], storeIt.Key...)
   270  						journalProgress(batch, marker, stats)
   271  
   272  						batch.Write()
   273  						batch.Reset()
   274  
   275  						dl.lock.Lock()
   276  						dl.genMarker = marker
   277  						dl.lock.Unlock()
   278  					}
   279  					if abort != nil {
   280  						stats.Log("Aborting state snapshot generation", dl.root, append(accountHash[:], storeIt.Key...))
   281  						abort <- stats
   282  						return
   283  					}
   284  				}
   285  			}
   286  			if err := storeIt.Err; err != nil {
   287  				log.Error("Generator failed to iterate storage trie", "accroot", dl.root, "acchash", common.BytesToHash(accIt.Key), "stroot", acc.Root, "err", err)
   288  				abort := <-dl.genAbort
   289  				abort <- stats
   290  				return
   291  			}
   292  		}
   293  		if time.Since(logged) > 8*time.Second {
   294  			stats.Log("Generating state snapshot", dl.root, accIt.Key)
   295  			logged = time.Now()
   296  		}
   297  		// Some account processed, unmark the marker
   298  		accMarker = nil
   299  	}
   300  	if err := accIt.Err; err != nil {
   301  		log.Error("Generator failed to iterate account trie", "root", dl.root, "err", err)
   302  		abort := <-dl.genAbort
   303  		abort <- stats
   304  		return
   305  	}
   306  	// Snapshot fully generated, set the marker to nil
   307  	if batch.ValueSize() > 0 {
   308  		// Ensure the generator entry is in sync with the data
   309  		journalProgress(batch, nil, stats)
   310  
   311  		batch.Write()
   312  	}
   313  	log.Info("Generated state snapshot", "accounts", stats.accounts, "slots", stats.slots,
   314  		"storage", stats.storage, "elapsed", common.PrettyDuration(time.Since(stats.start)))
   315  
   316  	dl.lock.Lock()
   317  	dl.genMarker = nil
   318  	close(dl.genPending)
   319  	dl.lock.Unlock()
   320  
   321  	// Someone will be looking for us, wait it out
   322  	abort := <-dl.genAbort
   323  	abort <- nil
   324  }