github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/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/DxChainNetwork/dxc/common" 25 "github.com/DxChainNetwork/dxc/core/rawdb" 26 "github.com/DxChainNetwork/dxc/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 // TODO address it @rjl493456442 @holiman @karalabe. 54 // 55 // After the entire state is generated, the bloom filter should be persisted into 56 // the disk. It indicates the whole generation procedure is finished. 57 type stateBloom struct { 58 bloom *bloomfilter.Filter 59 } 60 61 // newStateBloomWithSize creates a brand new state bloom for state generation. 62 // The bloom filter will be created by the passing bloom filter size. According 63 // to the https://hur.st/bloomfilter/?n=600000000&p=&m=2048MB&k=4, the parameters 64 // are picked so that the false-positive rate for mainnet is low enough. 65 func newStateBloomWithSize(size uint64) (*stateBloom, error) { 66 bloom, err := bloomfilter.New(size*1024*1024*8, 4) 67 if err != nil { 68 return nil, err 69 } 70 log.Info("Initialized state bloom", "size", common.StorageSize(float64(bloom.M()/8))) 71 return &stateBloom{bloom: bloom}, nil 72 } 73 74 // NewStateBloomFromDisk loads the state bloom from the given file. 75 // In this case the assumption is held the bloom filter is complete. 76 func NewStateBloomFromDisk(filename string) (*stateBloom, error) { 77 bloom, _, err := bloomfilter.ReadFile(filename) 78 if err != nil { 79 return nil, err 80 } 81 return &stateBloom{bloom: bloom}, nil 82 } 83 84 // Commit flushes the bloom filter content into the disk and marks the bloom 85 // as complete. 86 func (bloom *stateBloom) Commit(filename, tempname string) error { 87 // Write the bloom out into a temporary file 88 _, err := bloom.bloom.WriteFile(tempname) 89 if err != nil { 90 return err 91 } 92 // Ensure the file is synced to disk 93 f, err := os.OpenFile(tempname, os.O_RDWR, 0666) 94 if err != nil { 95 return err 96 } 97 if err := f.Sync(); err != nil { 98 f.Close() 99 return err 100 } 101 f.Close() 102 103 // Move the teporary file into it's final location 104 return os.Rename(tempname, filename) 105 } 106 107 // Put implements the KeyValueWriter interface. But here only the key is needed. 108 func (bloom *stateBloom) Put(key []byte, value []byte) error { 109 // If the key length is not 32bytes, ensure it's contract code 110 // entry with new scheme. 111 if len(key) != common.HashLength { 112 isCode, codeKey := rawdb.IsCodeKey(key) 113 if !isCode { 114 return errors.New("invalid entry") 115 } 116 bloom.bloom.Add(stateBloomHasher(codeKey)) 117 return nil 118 } 119 bloom.bloom.Add(stateBloomHasher(key)) 120 return nil 121 } 122 123 // Delete removes the key from the key-value data store. 124 func (bloom *stateBloom) Delete(key []byte) error { panic("not supported") } 125 126 // Contain is the wrapper of the underlying contains function which 127 // reports whether the key is contained. 128 // - If it says yes, the key may be contained 129 // - If it says no, the key is definitely not contained. 130 func (bloom *stateBloom) Contain(key []byte) (bool, error) { 131 return bloom.bloom.Contains(stateBloomHasher(key)), nil 132 }