bloom.go 5.2 KB

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