github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state/snapshot/utils.go (about)

     1  // (c) 2022, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2022 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package snapshot
    28  
    29  import (
    30  	"bytes"
    31  	"fmt"
    32  	"time"
    33  
    34  	"github.com/MetalBlockchain/subnet-evm/core/rawdb"
    35  	"github.com/MetalBlockchain/subnet-evm/ethdb"
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/log"
    38  )
    39  
    40  // CheckDanglingStorage iterates the snap storage data, and verifies that all
    41  // storage also has corresponding account data.
    42  func CheckDanglingStorage(chaindb ethdb.KeyValueStore) error {
    43  	if err := checkDanglingDiskStorage(chaindb); err != nil {
    44  		log.Error("Database check error", "err", err)
    45  		return err
    46  	}
    47  	return nil
    48  }
    49  
    50  // checkDanglingDiskStorage checks if there is any 'dangling' storage data in the
    51  // disk-backed snapshot layer.
    52  func checkDanglingDiskStorage(chaindb ethdb.KeyValueStore) error {
    53  	var (
    54  		lastReport = time.Now()
    55  		start      = time.Now()
    56  		lastKey    []byte
    57  		it         = rawdb.NewKeyLengthIterator(chaindb.NewIterator(rawdb.SnapshotStoragePrefix, nil), 1+2*common.HashLength)
    58  	)
    59  	log.Info("Checking dangling snapshot disk storage")
    60  
    61  	defer it.Release()
    62  	for it.Next() {
    63  		k := it.Key()
    64  		accKey := k[1:33]
    65  		if bytes.Equal(accKey, lastKey) {
    66  			// No need to look up for every slot
    67  			continue
    68  		}
    69  		lastKey = common.CopyBytes(accKey)
    70  		if time.Since(lastReport) > time.Second*8 {
    71  			log.Info("Iterating snap storage", "at", fmt.Sprintf("%#x", accKey), "elapsed", common.PrettyDuration(time.Since(start)))
    72  			lastReport = time.Now()
    73  		}
    74  		if data := rawdb.ReadAccountSnapshot(chaindb, common.BytesToHash(accKey)); len(data) == 0 {
    75  			log.Warn("Dangling storage - missing account", "account", fmt.Sprintf("%#x", accKey), "storagekey", fmt.Sprintf("%#x", k))
    76  			return fmt.Errorf("dangling snapshot storage account %#x", accKey)
    77  		}
    78  	}
    79  	log.Info("Verified the snapshot disk storage", "time", common.PrettyDuration(time.Since(start)), "err", it.Error())
    80  	return nil
    81  }