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