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