diff --git a/eth/protocols/eth/handler.go b/eth/protocols/eth/handler.go index 3d3f6bb90f04..25ddcd93ecfc 100644 --- a/eth/protocols/eth/handler.go +++ b/eth/protocols/eth/handler.go @@ -332,7 +332,7 @@ func handleMessage(backend Backend, peer *Peer) error { break } // Retrieve the requested state entry - if bloom := backend.StateBloom(); bloom != nil && !bloom.Contains(hash) { + if bloom := backend.StateBloom(); bloom != nil && !bloom.Contains(hash[:]) { // Only lookup the trie node if there's chance that we actually have it continue } diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index d7fe1b2814fd..679b328283b0 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -1571,7 +1571,7 @@ func (s *Syncer) processBytecodeResponse(res *bytecodeResponse) { bytes += common.StorageSize(len(code)) rawdb.WriteCode(batch, hash, code) - s.bloom.Add(hash) + s.bloom.Add(hash[:]) } if err := batch.Write(); err != nil { log.Crit("Failed to persist bytecodes", "err", err) @@ -1710,7 +1710,7 @@ func (s *Syncer) processStorageResponse(res *storageResponse) { } // Node is not a boundary, persist to disk batch.Put(it.Key(), it.Value()) - s.bloom.Add(common.BytesToHash(it.Key())) + s.bloom.Add(it.Key()) bytes += common.StorageSize(common.HashLength + len(it.Value())) nodes++ @@ -1867,7 +1867,7 @@ func (s *Syncer) forwardAccountTask(task *accountTask) { } // Node is neither a boundary, not an incomplete account, persist to disk batch.Put(it.Key(), it.Value()) - s.bloom.Add(common.BytesToHash(it.Key())) + s.bloom.Add(it.Key()) bytes += common.StorageSize(common.HashLength + len(it.Value())) nodes++ diff --git a/trie/sync.go b/trie/sync.go index 6354bb86ba2c..05b9f55d3356 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -155,7 +155,7 @@ func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, cal if s.membatch.hasNode(root) { return } - if s.bloom == nil || s.bloom.Contains(root) { + if s.bloom == nil || s.bloom.Contains(root[:]) { // Bloom filter says this might be a duplicate, double check. // If database says yes, then at least the trie node is present // and we hold the assumption that it's NOT legacy contract code. @@ -195,7 +195,7 @@ func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash) { if s.membatch.hasCode(hash) { return } - if s.bloom == nil || s.bloom.Contains(hash) { + if s.bloom == nil || s.bloom.Contains(hash[:]) { // Bloom filter says this might be a duplicate, double check. // If database says yes, the blob is present for sure. // Note we only check the existence with new code scheme, fast @@ -313,11 +313,11 @@ func (s *Sync) Commit(dbw ethdb.Batch) error { // Dump the membatch into a database dbw for key, value := range s.membatch.nodes { rawdb.WriteTrieNode(dbw, key, value) - s.bloom.Add(key) + s.bloom.Add(key[:]) } for key, value := range s.membatch.codes { rawdb.WriteCode(dbw, key, value) - s.bloom.Add(key) + s.bloom.Add(key[:]) } // Drop the membatch data and return s.membatch = newSyncMemBatch() @@ -406,7 +406,7 @@ func (s *Sync) children(req *request, object node) ([]*request, error) { if s.membatch.hasNode(hash) { continue } - if s.bloom == nil || s.bloom.Contains(hash) { + if s.bloom == nil || s.bloom.Contains(node) { // Bloom filter says this might be a duplicate, double check. // If database says yes, then at least the trie node is present // and we hold the assumption that it's NOT legacy contract code. diff --git a/trie/sync_bloom.go b/trie/sync_bloom.go index c93b4e7f08cb..1afcce21dab9 100644 --- a/trie/sync_bloom.go +++ b/trie/sync_bloom.go @@ -157,11 +157,11 @@ func (b *SyncBloom) Close() error { } // Add inserts a new trie node hash into the bloom filter. -func (b *SyncBloom) Add(hash common.Hash) { +func (b *SyncBloom) Add(hash []byte) { if atomic.LoadUint32(&b.closed) == 1 { return } - b.bloom.AddHash(hashToUint64(hash)) + b.bloom.AddHash(binary.BigEndian.Uint64(hash)) bloomAddMeter.Mark(1) } @@ -170,7 +170,7 @@ func (b *SyncBloom) Add(hash common.Hash) { // - true: the bloom maybe contains hash // // While the bloom is being initialized, any query will return true. -func (b *SyncBloom) Contains(hash common.Hash) bool { +func (b *SyncBloom) Contains(hash []byte) bool { bloomTestMeter.Mark(1) if atomic.LoadUint32(&b.inited) == 0 { // We didn't load all the trie nodes from the previous run of Geth yet. As @@ -179,14 +179,9 @@ func (b *SyncBloom) Contains(hash common.Hash) bool { return true } // Bloom initialized, check the real one and report any successful misses - maybe := b.bloom.ContainsHash(hashToUint64(hash)) + maybe := b.bloom.ContainsHash(binary.BigEndian.Uint64(hash)) if !maybe { bloomMissMeter.Mark(1) } return maybe } - -func hashToUint64(h common.Hash) uint64 { - return uint64(h[7]) | uint64(h[6])<<8 | uint64(h[5])<<16 | uint64(h[4])<<24 | - uint64(h[3])<<32 | uint64(h[2])<<40 | uint64(h[1])<<48 | uint64(h[0])<<56 -}