core/state/snapshot: update bloom filter, remove hashing structs

This commit is contained in:
Martin Holst Swende 2019-12-11 10:40:29 +01:00
parent 95ce96c89e
commit dc5e16e846
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 22 additions and 48 deletions

View file

@ -116,49 +116,23 @@ type diffLayer struct {
lock sync.RWMutex
}
// destructBloomHasher is a wrapper around a common.Hash to satisfy the interface
// API requirements of the bloom library used. It's used to convert a destruct
// event into a 64 bit mini hash.
type destructBloomHasher common.Hash
// accountBloomHash is s used to convert an account
// hash into a 64 bit mini hash.
func accountBloomHash(h common.Hash) uint64 {
return binary.BigEndian.Uint64(h[bloomHasherOffset : bloomHasherOffset+8])
}
func (h destructBloomHasher) Write(p []byte) (n int, err error) { panic("not implemented") }
func (h destructBloomHasher) Sum(b []byte) []byte { panic("not implemented") }
func (h destructBloomHasher) Reset() { panic("not implemented") }
func (h destructBloomHasher) BlockSize() int { panic("not implemented") }
func (h destructBloomHasher) Size() int { return 8 }
func (h destructBloomHasher) Sum64() uint64 {
// storageBloomHash is used to convert an account + storage hash into a 64 bit mini hash.
func storageBloomHash(accountHash, storageHash common.Hash) uint64 {
return binary.BigEndian.Uint64(accountHash[bloomHasherOffset:bloomHasherOffset+8]) ^
binary.BigEndian.Uint64(storageHash[bloomHasherOffset:bloomHasherOffset+8])
}
// destructBloomHash is used to convert a destruct event into a 64 bit mini hash.
func destructBloomHash(accountHash common.Hash) uint64 {
return binary.BigEndian.Uint64(h[bloomDestructHasherOffset : bloomDestructHasherOffset+8])
}
// accountBloomHasher is a wrapper around a common.Hash to satisfy the interface
// API requirements of the bloom library used. It's used to convert an account
// hash into a 64 bit mini hash.
type accountBloomHasher common.Hash
func (h accountBloomHasher) Write(p []byte) (n int, err error) { panic("not implemented") }
func (h accountBloomHasher) Sum(b []byte) []byte { panic("not implemented") }
func (h accountBloomHasher) Reset() { panic("not implemented") }
func (h accountBloomHasher) BlockSize() int { panic("not implemented") }
func (h accountBloomHasher) Size() int { return 8 }
func (h accountBloomHasher) Sum64() uint64 {
return binary.BigEndian.Uint64(h[bloomAccountHasherOffset : bloomAccountHasherOffset+8])
}
// storageBloomHasher is a wrapper around a [2]common.Hash to satisfy the interface
// API requirements of the bloom library used. It's used to convert an account
// hash into a 64 bit mini hash.
type storageBloomHasher [2]common.Hash
func (h storageBloomHasher) Write(p []byte) (n int, err error) { panic("not implemented") }
func (h storageBloomHasher) Sum(b []byte) []byte { panic("not implemented") }
func (h storageBloomHasher) Reset() { panic("not implemented") }
func (h storageBloomHasher) BlockSize() int { panic("not implemented") }
func (h storageBloomHasher) Size() int { return 8 }
func (h storageBloomHasher) Sum64() uint64 {
return binary.BigEndian.Uint64(h[0][bloomStorageHasherOffset:bloomStorageHasherOffset+8]) ^
binary.BigEndian.Uint64(h[1][bloomStorageHasherOffset:bloomStorageHasherOffset+8])
}
// newDiffLayer creates a new diff on top of an existing snapshot, whether that's a low
// level persistent database or a hierarchical diff already.
func newDiffLayer(parent snapshot, root common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
@ -204,7 +178,7 @@ func (dl *diffLayer) initBloom() {
if data == nil {
panic(fmt.Sprintf("account %#x nil", hash))
}
dl.diffed.Add(accountBloomHasher(hash))
dl.diffed.AddHash(accountBloomHash(hash))
dataSize += uint64(len(data))
nHashes++
}
@ -218,7 +192,7 @@ func (dl *diffLayer) initBloom() {
panic(fmt.Sprintf("storage %#x nil", accountHash))
}
for storageHash, data := range slots {
dl.diffed.Add(storageBloomHasher{accountHash, storageHash})
dl.diffed.AddHash(storageBloomHash(accountHash, storageHash))
dataSize += uint64(len(data))
nHashes++
}
@ -304,9 +278,9 @@ func (dl *diffLayer) AccountRLP(hash common.Hash) ([]byte, error) {
// Check the bloom filter first whether there's even a point in reaching into
// all the maps in all the layers below
dl.lock.RLock()
hit := dl.cumulative.Contains(accountBloomHasher(hash))
hit := dl.cumulative.ContainsHash(accountBloomHash(hash))
if !hit {
hit = dl.cumulative.Contains(destructBloomHasher(hash))
hit = dl.cumulative.ContainsHash(destructBloomHash(hash))
}
dl.lock.RUnlock()
@ -364,9 +338,9 @@ func (dl *diffLayer) Storage(accountHash, storageHash common.Hash) ([]byte, erro
// Check the bloom filter first whether there's even a point in reaching into
// all the maps in all the layers below
dl.lock.RLock()
hit := dl.cumulative.Contains(storageBloomHasher{accountHash, storageHash})
hit := dl.cumulative.ContainsHash(storageBloomHash(accountHash, storageHash))
if !hit {
hit = dl.cumulative.Contains(destructBloomHasher(accountHash))
hit = dl.cumulative.Contains(destructBloomHash(accountHash))
}
dl.lock.RUnlock()

2
go.mod
View file

@ -33,7 +33,7 @@ require (
github.com/gorilla/websocket v1.4.1-0.20190629185528-ae1634f6a989
github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad
github.com/holiman/bloomfilter v0.0.0-20191206152701-e4d21e6716c9
github.com/holiman/bloomfilter v0.0.0-20191211091059-3e24bf2f1d8e
github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3
github.com/influxdata/influxdb v1.2.3-0.20180221223340-01288bdb0883
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458

4
go.sum
View file

@ -99,8 +99,8 @@ github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277 h1:E0whKx
github.com/graph-gophers/graphql-go v0.0.0-20191115155744-f33e81362277/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0PvIGS9TTtxg4R+JxuPGav82J8rA+GFnY7po=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/holiman/bloomfilter v0.0.0-20191206152701-e4d21e6716c9 h1:8FJHmfDjj0LRfpyJYRC8gQl4NO9Pl3TOOkkImi59bOk=
github.com/holiman/bloomfilter v0.0.0-20191206152701-e4d21e6716c9/go.mod h1:+E92jn6hSglI3YfM9VLe+w2tHKtSL9l83iBiZS7/4Lw=
github.com/holiman/bloomfilter v0.0.0-20191211091059-3e24bf2f1d8e h1:+9n746lrjamJzr9i9c0O6DyMQ7bqvq9EK3WR7OnaKss=
github.com/holiman/bloomfilter v0.0.0-20191211091059-3e24bf2f1d8e/go.mod h1:+E92jn6hSglI3YfM9VLe+w2tHKtSL9l83iBiZS7/4Lw=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3 h1:DqD8eigqlUm0+znmx7zhL0xvTW3+e1jCekJMfBUADWI=