|
17 | 17 | package pruner |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bufio" |
20 | 21 | "encoding/binary" |
21 | 22 | "errors" |
| 23 | + "fmt" |
| 24 | + "io" |
22 | 25 | "os" |
23 | 26 |
|
24 | 27 | "github.com/ethereum/go-ethereum/common" |
25 | 28 | "github.com/ethereum/go-ethereum/core/rawdb" |
26 | 29 | "github.com/ethereum/go-ethereum/log" |
| 30 | + "github.com/ethereum/go-ethereum/rlp" |
27 | 31 | bloomfilter "github.com/holiman/bloomfilter/v2" |
28 | 32 | ) |
29 | 33 |
|
@@ -73,27 +77,54 @@ func newStateBloomWithSize(size uint64) (*stateBloom, error) { |
73 | 77 |
|
74 | 78 | // NewStateBloomFromDisk loads the state bloom from the given file. |
75 | 79 | // 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) |
| 80 | +func NewStateBloomFromDisk(filename string) (*stateBloom, []common.Hash, error) { |
| 81 | + f, err := os.Open(filename) |
78 | 82 | if err != nil { |
79 | | - return nil, err |
| 83 | + return nil, nil, err |
80 | 84 | } |
81 | | - return &stateBloom{bloom: bloom}, nil |
| 85 | + defer f.Close() |
| 86 | + r := bufio.NewReader(f) |
| 87 | + version := []byte{0} |
| 88 | + _, err = io.ReadFull(r, version) |
| 89 | + if err != nil { |
| 90 | + return nil, nil, err |
| 91 | + } |
| 92 | + if version[0] != 0 { |
| 93 | + return nil, nil, fmt.Errorf("unknown state bloom filter version %v", version[0]) |
| 94 | + } |
| 95 | + var roots []common.Hash |
| 96 | + err = rlp.Decode(r, &roots) |
| 97 | + if err != nil { |
| 98 | + return nil, nil, err |
| 99 | + } |
| 100 | + bloom, _, err := bloomfilter.ReadFrom(r) |
| 101 | + if err != nil { |
| 102 | + return nil, nil, err |
| 103 | + } |
| 104 | + return &stateBloom{bloom: bloom}, roots, nil |
82 | 105 | } |
83 | 106 |
|
84 | 107 | // Commit flushes the bloom filter content into the disk and marks the bloom |
85 | 108 | // 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) |
| 109 | +func (bloom *stateBloom) Commit(filename, tempname string, roots []common.Hash) error { |
| 110 | + f, err := os.OpenFile(tempname, os.O_RDWR|os.O_CREATE, 0666) |
89 | 111 | if err != nil { |
90 | 112 | return err |
91 | 113 | } |
92 | | - // Ensure the file is synced to disk |
93 | | - f, err := os.OpenFile(tempname, os.O_RDWR, 0666) |
| 114 | + _, err = f.Write([]byte{0}) // version |
94 | 115 | if err != nil { |
95 | 116 | return err |
96 | 117 | } |
| 118 | + err = rlp.Encode(f, roots) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + // Write the bloom out into a temporary file |
| 123 | + _, err = bloom.bloom.WriteTo(f) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + // Ensure the file is synced to disk |
97 | 128 | if err := f.Sync(); err != nil { |
98 | 129 | f.Close() |
99 | 130 | return err |
@@ -130,3 +161,11 @@ func (bloom *stateBloom) Delete(key []byte) error { panic("not supported") } |
130 | 161 | func (bloom *stateBloom) Contain(key []byte) (bool, error) { |
131 | 162 | return bloom.bloom.Contains(stateBloomHasher(key)), nil |
132 | 163 | } |
| 164 | + |
| 165 | +func (bloom *stateBloom) FalsePosititveProbability() float64 { |
| 166 | + return bloom.bloom.FalsePosititveProbability() |
| 167 | +} |
| 168 | + |
| 169 | +func (bloom *stateBloom) Size() uint64 { |
| 170 | + return bloom.bloom.M() |
| 171 | +} |
0 commit comments