From 876f3573647f9a62d94073b342bc678f380f0304 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 22 Mar 2019 16:13:28 +0100 Subject: [PATCH] trie: disable fnv64a hashing of hashes for bigcache (#19314) * trie: disable fnv64a hashing of hashes for bigcache * trie/database: add very important period --- trie/database.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/trie/database.go b/trie/database.go index c853dfe51..6df1a7f79 100644 --- a/trie/database.go +++ b/trie/database.go @@ -17,6 +17,7 @@ package trie import ( + "encoding/binary" "fmt" "io" "sync" @@ -257,6 +258,19 @@ func expandNode(hash hashNode, n node) node { } } +// trienodeHasher is a struct to be used with BigCache, which uses a Hasher to +// determine which shard to place an entry into. It's not a cryptographic hash, +// just to provide a bit of anti-collision (default is FNV64a). +// +// Since trie keys are already hashes, we can just use the key directly to +// map shard id. +type trienodeHasher struct{} + +// Sum64 implements the bigcache.Hasher interface. +func (t trienodeHasher) Sum64(key string) uint64 { + return binary.BigEndian.Uint64([]byte(key)) +} + // NewDatabase creates a new trie database to store ephemeral trie content before // its written out to disk or garbage collected. No read cache is created, so all // data retrievals will hit the underlying disk database. @@ -276,6 +290,7 @@ func NewDatabaseWithCache(diskdb ethdb.KeyValueStore, cache int) *Database { MaxEntriesInWindow: cache * 1024, MaxEntrySize: 512, HardMaxCacheSize: cache, + Hasher: trienodeHasher{}, }) } return &Database{