github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/core/state/pruner/bloom.go (about)

     1  // Copyright 2020 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 pruner
    18  
    19  import (
    20  	"encoding/binary"
    21  	"errors"
    22  	"os"
    23  
    24  	bloomfilter "github.com/holiman/bloomfilter/v2"
    25  
    26  	"github.com/scroll-tech/go-ethereum/common"
    27  	"github.com/scroll-tech/go-ethereum/core/rawdb"
    28  	"github.com/scroll-tech/go-ethereum/log"
    29  )
    30  
    31  // stateBloomHasher is a wrapper around a byte blob to satisfy the interface API
    32  // requirements of the bloom library used. It's used to convert a trie hash or
    33  // contract code hash into a 64 bit mini hash.
    34  type stateBloomHasher []byte
    35  
    36  func (f stateBloomHasher) Write(p []byte) (n int, err error) { panic("not implemented") }
    37  func (f stateBloomHasher) Sum(b []byte) []byte               { panic("not implemented") }
    38  func (f stateBloomHasher) Reset()                            { panic("not implemented") }
    39  func (f stateBloomHasher) BlockSize() int                    { panic("not implemented") }
    40  func (f stateBloomHasher) Size() int                         { return 8 }
    41  func (f stateBloomHasher) Sum64() uint64                     { return binary.BigEndian.Uint64(f) }
    42  
    43  // stateBloom is a bloom filter used during the state convesion(snapshot->state).
    44  // The keys of all generated entries will be recorded here so that in the pruning
    45  // stage the entries belong to the specific version can be avoided for deletion.
    46  //
    47  // The false-positive is allowed here. The "false-positive" entries means they
    48  // actually don't belong to the specific version but they are not deleted in the
    49  // pruning. The downside of the false-positive allowance is we may leave some "dangling"
    50  // nodes in the disk. But in practice the it's very unlike the dangling node is
    51  // state root. So in theory this pruned state shouldn't be visited anymore. Another
    52  // potential issue is for fast sync. If we do another fast sync upon the pruned
    53  // database, it's problematic which will stop the expansion during the syncing.
    54  // TODO address it @rjl493456442 @holiman @karalabe.
    55  //
    56  // After the entire state is generated, the bloom filter should be persisted into
    57  // the disk. It indicates the whole generation procedure is finished.
    58  type stateBloom struct {
    59  	bloom *bloomfilter.Filter
    60  }
    61  
    62  // newStateBloomWithSize creates a brand new state bloom for state generation.
    63  // The bloom filter will be created by the passing bloom filter size. According
    64  // to the https://hur.st/bloomfilter/?n=600000000&p=&m=2048MB&k=4, the parameters
    65  // are picked so that the false-positive rate for mainnet is low enough.
    66  func newStateBloomWithSize(size uint64) (*stateBloom, error) {
    67  	bloom, err := bloomfilter.New(size*1024*1024*8, 4)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	log.Info("Initialized state bloom", "size", common.StorageSize(float64(bloom.M()/8)))
    72  	return &stateBloom{bloom: bloom}, nil
    73  }
    74  
    75  // NewStateBloomFromDisk loads the state bloom from the given file.
    76  // In this case the assumption is held the bloom filter is complete.
    77  func NewStateBloomFromDisk(filename string) (*stateBloom, error) {
    78  	bloom, _, err := bloomfilter.ReadFile(filename)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return &stateBloom{bloom: bloom}, nil
    83  }
    84  
    85  // Commit flushes the bloom filter content into the disk and marks the bloom
    86  // as complete.
    87  func (bloom *stateBloom) Commit(filename, tempname string) error {
    88  	// Write the bloom out into a temporary file
    89  	_, err := bloom.bloom.WriteFile(tempname)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	// Ensure the file is synced to disk
    94  	f, err := os.OpenFile(tempname, os.O_RDWR, 0666)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	if err := f.Sync(); err != nil {
    99  		f.Close()
   100  		return err
   101  	}
   102  	f.Close()
   103  
   104  	// Move the teporary file into it's final location
   105  	return os.Rename(tempname, filename)
   106  }
   107  
   108  // Put implements the KeyValueWriter interface. But here only the key is needed.
   109  func (bloom *stateBloom) Put(key []byte, value []byte) error {
   110  	// If the key length is not 32bytes, ensure it's contract code
   111  	// entry with new scheme.
   112  	if len(key) != common.HashLength {
   113  		isCode, codeKey := rawdb.IsCodeKey(key)
   114  		if !isCode {
   115  			return errors.New("invalid entry")
   116  		}
   117  		bloom.bloom.Add(stateBloomHasher(codeKey))
   118  		return nil
   119  	}
   120  	bloom.bloom.Add(stateBloomHasher(key))
   121  	return nil
   122  }
   123  
   124  // Delete removes the key from the key-value data store.
   125  func (bloom *stateBloom) Delete(key []byte) error { panic("not supported") }
   126  
   127  // Contain is the wrapper of the underlying contains function which
   128  // reports whether the key is contained.
   129  // - If it says yes, the key may be contained
   130  // - If it says no, the key is definitely not contained.
   131  func (bloom *stateBloom) Contain(key []byte) (bool, error) {
   132  	return bloom.bloom.Contains(stateBloomHasher(key)), nil
   133  }