github.com/dim4egster/coreth@v0.10.2/core/state/pruner/bloom.go (about)

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